🦙 ollama-mcpo适配器
将MCPO、MCP暴露给OpenAPI代理服务器,作为Ollama兼容功能的工具 使用简单的Python适配器和可选的运行时服务。
______________________________________________________________________
✨ 特性
______________________________________________________________________
🚀 快速启动
1.安装
pip install ollama-mcpo-adapter与现有MCPO实例一起使用
假设MCPO的运行方式如下:
uvx mcpo --port 5090 --config /path/to/mcp_config.json您可以使用适配器获得Ollama ToolCall格式的所有可用函数:
from ollama_mcpo_adapter import OllamaMCPOAdapter
adapter = OllamaMCPOAdapter(host="localhost", port=5090, config_path="/path/to/mcp_config.json")
# Gets tool descriptions from MCPO FastAPI /docs
tools = adapter.list_tools_ollama()您可以省略配置路径。但是,使用提供的配置发现MCP服务器名称更可靠。 否则,将从自动生成的OpenAPI文档中读取服务器名称 MCPO 未来可能会发生变化。
______________________________________________________________________
使用本地MCPO服务
您可以使用此扩展启动MCPO服务:
from ollama_mcpo_adapter import MCPOService
# Provide your mcp config as JSON file or dictionary
mcp_config = {
"mcpServers": {
"time": {"command": "uvx", "args": ["mcp-server-time", "--local-timezone=Europe/Berlin"]}
}
}
mcpo = MCPOService("127.0.0.1", 4090, config=mcp_config,
# -OR- from an existing mcp_config file
config_path="path/to/mcp_config.json")
# MCPOSService class handles MCPO server start-up and shutdown and in a subprocess
mcpo.start(wait=True)
...
mcpo.stop()然后使用适配器获取所有可用工具:
from ollama_mcpo_adapter import OllamaMCPOAdapter
adapter = OllamaMCPOAdapter("127.0.0.1", 4090)
tools = adapter.list_tools_ollama()把这个发给Ollama:
from ollama import Client
# Send a prompt to Ollama using discovered tools
client = Client(host="http://127.0.0.1:11434")
response = client.chat(
model="qwen2.5-coder:14b-instruct-q4_K_M",
messages=[{"role": "user", "content": "Write a file..."}],
tools=tools,
)最后调用工具:
# Handle any tool calls
if response.message.tool_calls:
adapter.call_tools_from_response(response.message.tool_calls)______________________________________________________________________
环境
- MS Windows npx路径,您可以在配置解析器中用npx的路径覆盖npx
例子: WIN_NODEJS_NPX_PATH=C:\Program Files\nodejs\npx.cmd
______________________________________________________________________
🧪 运行测试
pytest______________________________________________________________________
📂 项目结构
ollama_mcpo_adapter/
├── adapter.py # Tool discovery + Ollama integration
├── service.py # Optional: launch MCPO programmatically
├── service_runner.py # MCPO subprocess control
├── config_parser.py # MCP config parsing helpers
├── dispatcher.py # Dispatch tool calls______________________________________________________________________
📜 许可证
MIT。看 许可证.
