一个代理服务,连接Ollama与FastMCP,从而允许模型在本地使用,并直接通过MCP服务器及其工具运行。
Model Context Protocol (MCP) 是一种协议,它允许您构建服务器以安全、标准化的方式向LLM应用程序提供数据和功能。在此包装器中,MCP的使用仅限于工具部分。
克隆此仓库并安装依赖项:
git clone https://github.com/andreamoro/ollama-fastmcp-wrapper.git
根据您使用的包管理器安装依赖项。如果是uv:
cd ollama-fastmcp-wrapper
uv sync
否则使用传统的(过时的)pip:
cd ollama-fastmcp-wrapper
pip install -r requirements.txt
Python依赖项包括:<br> - fastapi<br> - fastmcp<br> - uvicorn<br> - ollama<br>
ollama serve
运行包装器:
python ollama_wrapper.py
您将被询问要启动哪种模式:
http://127.0.0.1:8000 启动REST API启动API:
python ollama_wrapper.py
# 选择 "api"
端点:
GET /servers → 列出可用的FastMCP服务器POST /connect/{server_name} → 连接FastMCP服务器POST /disconnect/{server_name} → 断开服务器连接GET /list_tools?server_name={server_name} → 列出服务器的工具POST /chat → 发送聊天请求POST /save_history/{file_name} → 将对话历史保存到磁盘POST /overwrite_history/{file_name} → 用当前对话覆盖现有的对话文件示例:
$ > curl -X POST http://0.0.0.0:8000/connect/math
$ > {"status_code":200,"detail":"math server successfully connected."}
$ > curl http://127.0.0.1:8000/chat -H "Content-Type: application/json" -d '{
"message": "将5和10相加,然后将结果乘以20。",
"model": "llama3.2:3b",
"mcp_server": "math"
}'
$ > ...
python ollama_wrapper.py
# 选择 "cli"
然后键入消息:
您:你好!
机器人:你好
使用 /exit 或 /quit 退出。
FastMCP服务器配置在 ollama_wrapper.py 中的 FASTMCP_SERVERS 内。示例如下:
FASTMCP_SERVERS = {
"math": {
"name": "math_server",
"command": "uv",
"args": ["run", "--with", "fastmcp", "math_server.py"],
"host": "http://localhost:5000/mcp",
}
}
flowchart TD
O[Ollama] --> W[Ollama-FastMCP Wrapper]
W -->|API模式| A[API]
W -->|CLI模式| C[CLI]
A --> M{ MCP服务器可用?}
C --> Z
M -->|是| F[FastMCP工具]
F --> Z[聊天]
M -->|否| Z
MIT许可证 © 2025 Andrea MORO