返回市场
奥拉玛快速MCP封装器

奥拉玛快速MCP封装器

作者:andreamoro6 星标更新:2025-09-03

项目介绍

Ollama-FastMCP Wrapper

一个代理服务,连接OllamaFastMCP,从而允许模型在本地使用,并直接通过MCP服务器及其工具运行。


✨ 特性

  • 运行时连接/断开多个MCP服务器(使用FastMCP)。
  • 将FastMCP工具暴露为可调用函数给Ollama模型。
  • 使用具有工具增强推理能力的Ollama本地运行。
  • 历史对话可以持久保存到磁盘。
  • 自动总结与模型的历史对话。
  • 运行方式:
    • API服务器(通过FastAPI + Uvicorn)
    • 交互式CLI

:question: 什么是MCP?

Model Context Protocol (MCP) 是一种协议,它允许您构建服务器以安全、标准化的方式向LLM应用程序提供数据和功能。在此包装器中,MCP的使用仅限于工具部分。

⚡ 安装

  1. 克隆此仓库并安装依赖项:

    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>

  1. 确保您有:
  • 正在运行的 Ollama客户端
    ollama serve
    
  • 使用工具功能的MCP服务器
    • 没有MCP服务器,您可以将此包装器作为对话接口使用
    • 仓库中有一个简单的数学MCP服务器。

⛵ 使用方法

运行包装器:

python ollama_wrapper.py

您将被询问要启动哪种模式:

  • API模式 → 在 http://127.0.0.1:8000 启动REST API
  • CLI模式 → 启动基于终端的聊天循环

🖥️ 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"
    }'
$ > ...

💬 CLI模式

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",
    }
}
  • STDIO传输 → 本地启动服务器
  • HTTP传输 → 连接到远程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

:bulb: 路线图 / 待办事项

  • 实现CLI模式中的FastMCP支持
  • 添加API主机配置的参数解析器
  • 日志支持

发布历史

  • 2025-09-02 - v. 0.2.0 - 添加外部MCP配置文件支持和参数解析器
  • 2025-08-19 - v. 0.1.0 - 初始发布

许可证

MIT许可证 © 2025 Andrea MORO