迪蒙斯马特。本地OllamaMCP服务器
  
一个模型上下文协议(MCP)服务器,提供查询本地Ollama实例的工具。与官方合作建造 模型上下文协议 Anthropic+Microsoft的SDK,它使较大的模型(如Claude、GPT-4)能够在Ollama上运行的较小的本地模型(如Llama 3、Mistral等)上测试提示。
特性
- query_ollama -向当地Ollama模特发送提示并获得回复
- query_ollama_with_files -在工作区根目录下的一个或多个文件上测试提示模板(支持通配符)
- list_ollama_connections -列出所有已配置的Ollama服务器连接
- list_ollama_models -检查每个Ollama服务器上有哪些型号
- 当请求的模型缺失时,交互式启发回退(MCP 0.4.1预览)
- 完全符合MCP规范,符合正确的JSON-RPC 2.0框架
- 通过身份验证支持多个Ollama服务器连接
- 从方法签名自动生成工具模式
- 自签名证书的SSL证书验证控制
- 长时间运行的模型推理请求的默认超时时间为1小时
先决条件
- .NET 8.0 SDK
- 奥拉玛 在本地运行(默认值:
http://localhost:11434)
安装
作为一个。NET工具
全局安装该工具:
dotnet tool install --global DimonSmart.LocalOllamaMCPServer要更新到最新版本:
dotnet tool update --global DimonSmart.LocalOllamaMCPServer在VS Code(MCP)中一键安装
安装后。NET工具,您只需单击一下即可将此服务器添加到VS Code或VS Code Insiders:
 
VS Code将向您显示MCP配置,并允许您选择是否将其添加到您的 用户 或 工作区 设置。
如果你更喜欢原始网址,你可以使用:
VS代码:
vscode:mcp/install?%7B%22name%22%3A%20%22DimonSmart%20Local%20Ollama%20MCP%22%2C%20%22type%22%3A%20%22stdio%22%2C%20%22command%22%3A%20%22DimonSmart.LocalOllamaMCPServer%22%7DVS代码内幕人士:
vscode-insiders:mcp/install?%7B%22name%22%3A%20%22DimonSmart%20Local%20Ollama%20MCP%22%2C%20%22type%22%3A%20%22stdio%22%2C%20%22command%22%3A%20%22DimonSmart.LocalOllamaMCPServer%22%7D底层JSON配置是:
{
"name": "DimonSmart Local Ollama MCP",
"type": "stdio",
"command": "DimonSmart.LocalOllamaMCPServer"
}您还可以通过以下方式将此JSON粘贴到VS代码中 MCP:添加服务器 命令选项板中的命令。
来源
- 克隆存储库。
- 构建项目:
dotnet build用法
运行服务器
您可以直接运行服务器:
dotnet run --project src/DimonSmart.LocalOllamaMCPServer/DimonSmart.LocalOllamaMCPServer.csproj或者,如果作为工具安装:
DimonSmart.LocalOllamaMCPServer要检查版本和配置文件位置:
DimonSmart.LocalOllamaMCPServer --version服务器使用MCP协议通过标准输入/输出(stdio)进行通信。它旨在供MCP客户端使用,例如:
可用工具
query_ollama
向当地Ollama模特发送提示并接收回复。
参数:
model_name(字符串,必填)-Ollama模型的名称(例如。,llama3,mistral,phi4)prompt(string,必填)-要发送到模型的提示文本options(对象,可选)-模型参数,如temperature,top_p等等。connection_name(字符串,可选)-Olama服务器连接的名称。如果省略,则使用默认值。
请求示例(JSON-RPC):
{
"jsonrpc": "2.0",
"method": "tools/call",
"params": {
"name": "query_ollama",
"arguments": {
"model_name": "llama3",
"prompt": "Why is the sky blue?",
"connection_name": "remote-gpu",
"options": {
"temperature": 0.7
}
}
},
"id": 1
}模型缺失时的引用示例
如果你打电话 query_ollama 对于所选连接上不存在的模型,服务器现在会发出MCP elicitation/create 请求用户选择可用型号之一。这使对话保持在同一个工具调用中,而不是立即失败。
- 客户端发送无效的工具调用:
{
"jsonrpc": "2.0",
"method": "tools/call",
"params": {
"name": "query_ollama",
"arguments": {
"model_name": "llama3-invalid",
"prompt": "hello",
"connection_name": "local"
}
},
"id": 42
}- 服务器查找真实模型(例如
llama3和mistral)并提示客户端:
{
"jsonrpc": "2.0",
"method": "elicitation/create",
"params": {
"message": "The model 'llama3-invalid' was not found on 'local'. Please choose another model to continue.",
"requested_schema": {
"required": ["modelName"],
"properties": {
"modelName": {
"title": "Select an Ollama model",
"description": "Pick a model available on 'local'.",
"enum": ["llama3", "mistral"],
"default": "llama3"
}
}
}
},
"id": "elicitation-42"
}- 用户选择模型后,客户端回复:
{
"jsonrpc": "2.0",
"method": "elicitation/response",
"params": {
"request_id": "elicitation-42",
"accepted": true,
"content": {
"modelName": "mistral"
}
}
}- 服务器使用所选模型重新运行原始工具调用(
mistral)并返回最终结果tools/call结果。如果用户取消或客户端不支持启发,则工具调用将退出并显示描述性错误消息。
query_ollama_with_files
针对MCP主机通告的工作区根目录下的一个或多个文件(通过 roots/list 请求)。服务器在运行时请求这些根,并拒绝读取它们之外的任何内容。支持通配符批量处理多个文件。
提示模板中的占位符:
{{data}}-替换为文件内容,除非send_data_as_user_message=true
参数:
model_name(字符串,必填)-目标模型prompt_template(字符串,必填)-包含上述占位符的模板file_path(string,可选)-要应用的文件掩码。使用以下模式*.*对于所有文件,*.cs对于C#文件,或单个文件的特定名称send_data_as_user_message(bool,可选)-将文件内容作为单独的用户样式消息附加,而不是内联替换max_files(int,可选)-使用通配符时限制处理的文件数量(0=无限制)connection_name(字符串,可选)-Olama服务器连接(如果省略,则默认)
示例请求(处理单个markdown文件):
{
"jsonrpc": "2.0",
"method": "tools/call",
"params": {
"name": "query_ollama_with_files",
"arguments": {
"model_name": "llama3",
"prompt_template": "Summarize {{file_name}} in 3 bullet points. Content: {{data}}",
"file_path": "docs/example.md"
}
},
"id": 99
}示例请求(处理根目录下的所有markdown文件):
{
"jsonrpc": "2.0",
"method": "tools/call",
"params": {
"name": "query_ollama_with_files",
"arguments": {
"model_name": "mistral",
"prompt_template": "Classify the tone of {{file_path}}. Respond with 'positive', 'neutral', or 'negative'.",
"file_path": "*.md",
"max_files": 5
}
},
"id": 100
}如果连接的主机不支持 roots/list 或者返回一个空列表,该工具会失败并返回错误,而不是触摸文件系统。
list_ollama_connections
列出所有已配置的Ollama服务器连接。为了安全起见,密码被屏蔽了。
参数: 无
请求示例:
{
"jsonrpc": "2.0",
"method": "tools/call",
"params": {
"name": "list_ollama_connections",
"arguments": {}
},
"id": 2
}list_ollama_models
列出所选Ollama服务器报告的所有型号 /api/tags这在决定在启发过程中选择哪种模型时特别方便。
参数:
connection_name(字符串,可选)-Olama服务器连接的名称。如果省略,则使用默认值。
请求示例:
{
"jsonrpc": "2.0",
"method": "tools/call",
"params": {
"name": "list_ollama_models",
"arguments": {
"connection_name": "remote-gpu"
}
},
"id": 3
}配置
服务器通过配置支持多个Ollama实例。您可以使用以下任一方式配置连接 appsettings.json 或环境变量。如果没有提供配置,服务器将自动使用本地Ollama服务器的默认设置 http://localhost:11434.
通过应用程序配置
创建或编辑 appsettings.json 在工具的安装目录中。这 DefaultServerName 指定何时使用哪个服务器 connection_name 在工具调用中不提供。
{
"Ollama": {
"DefaultServerName": "local",
"Servers": [
{
"Name": "local",
"BaseUrl": "http://localhost:11434"
},
{
"Name": "remote-gpu",
"BaseUrl": "https://my-gpu-server.com:11434",
"User": "admin",
"Password": "secret-password",
"IgnoreSsl": true
}
]
}
}服务器配置属性:
Name-服务器连接的唯一标识符BaseUrl-Ollama服务器URLUser(可选)-基本身份验证的用户名Password(可选)-基本身份验证密码IgnoreSsl(可选)-设置为true接受自签名SSL证书
用于快速测试的工作区根
MCP主机(例如,VS Code MCP扩展)负责通过以下方式通告文件系统边界 roots/list。此服务器在运行时请求根列表,并将所有文件操作限制在该集合内。如果主机不暴露任何根,则使用以下工具 query_ollama_with_files 返回错误,而不是读取任意路径。
相对的 file_path 根据报告的根解析值。工具,如 query_ollama_with_files 如果主机没有公开任何根,则返回错误。
通过环境变量进行配置
您还可以按照标准使用环境变量配置服务器。NET配置命名约定:
Ollama__DefaultServerName=remote-gpu
Ollama__Servers__0__Name=local
Ollama__Servers__0__BaseUrl=http://localhost:11434
Ollama__Servers__1__Name=remote-gpu
Ollama__Servers__1__BaseUrl=https://my-gpu-server.com:11434
Ollama__Servers__1__User=admin
Ollama__Servers__1__Password=secret
Ollama__Servers__1__IgnoreSsl=true发展
从源头构建
- 克隆存储库:
git clone https://github.com/DimonSmart/LocalOllamaMCPServer.git
cd LocalOllamaMCPServer- 构建项目:
dotnet build- 在本地运行:
dotnet run --project src/DimonSmart.LocalOllamaMCPServer/DimonSmart.LocalOllamaMCPServer.csproj运行测试
该项目使用 EasyVCR 记录和回放HTTP交互以进行可靠测试。
运行所有测试:
dotnet test录制新磁带:
- 确保Olama在本地运行
- 拉动测试模型:
ollama pull phi4:latest - 删除中的现有钞箱
tests/DimonSmart.LocalOllamaMCPServer.Tests/cassettes/ - 运行测试以生成新钞箱
CI/CD
该项目包括GitHub Actions工作流,这些工作流:
- 对每次推送进行构建和测试
main - 在版本标签上将NuGet包发布到NuGet.org(例如。,
v2.0.0) - 使用工件创建GitHub版本
技术栈
- .NET 8.0 -目标框架
- 模型上下文协议SDK -MCP正式实施
- 微软。扩展。托管 -应用程序生命周期管理
- 微软。扩展。超文本传输协议 -HTTP客户端工厂
- EasyVCR -用于测试的HTTP记录
相关项目
许可证
麻省理工学院
