本地LLM集成工具

该项目展示了如何将本地LLM(Ollama)与Model Context Protocol(MCP)服务器集成,以增强AI功能,包括工具调用和实时数据处理。

  • by Sanjaykrishnamurthy
  • Python
  • 本地部署
  • 更新于
在 GitHub 查看 sanjaykrishnamurthy/mcp-ollama-integration

使用说明

MCP Ollama集成演示

将本地LLM(Ollama)与模型上下文协议(MCP)服务器集成以增强AI功能的实际演示。

🚀 概述

本项目演示了如何:

  • 使用FastMCP构建MCP服务器
  • 创建具有配置驱动架构的MCP客户端
  • 将本地LLM(Ollama)与MCP工具集成
  • 使AI助手能够访问外部工具和服务

📁 项目结构

├── server.py                  # MCP weather server using FastMCP
├── config.json               # MCP server configuration
├── client.py                 # Basic MCP client
├── client_config.py          # Configuration-based MCP client
├── simple_ollama_mcp.py      # ✨ Ollama + MCP integration demo
├── ollama_mcp_client.py      # Advanced Ollama + MCP integration
└── test_ollama_connection.py # Ollama connection tester

🛠️ 设置

先决条件

  • Python 3.10+
  • 没有 安装有模型(例如。, gpt-oss:20b)
  • MCP Python SDK

安装

  1. 克隆存储库:

    git clone https://github.com/Sanjaykrishnamurthy/mcp-ollama-integration.git
    cd mcp-ollama-integration
    
  2. 安装依赖项:

    pip install "mcp[cli]" openai
    
  3. 启动Ollama服务器:

    ollama serve
    
  4. 拉取模型(如果尚未可用):

    ollama pull gpt-oss:20b
    

🎯 使用示例

1.基本MCP客户端

python client.py

测试基本的MCP服务器连接和工具调用。

2.基于配置的客户端

python client_config.py

演示具有多服务器支持的配置驱动MCP客户端。

3.Ollama+MCP集成⭐

python simple_ollama_mcp.py

展示了本地LLM如何使用MCP工具用实时数据回答问题。

交互示例:

👤 User: What's the weather in Paris?
🎯 Detected location: Paris
🌤️ Getting weather data from MCP server...
🔧 MCP Result: The weather in Paris is sunny ☀️
🤖 Ollama Response: Paris is sunny today ☀️—perfect for a walk around the city!

🔧 运作原理

MCP服务器(server.py)

from mcp.server.fastmcp import FastMCP

mcp = FastMCP("weather-server")

@mcp.tool()
async def get_weather(location: str = "London") -> str:
    """Get weather information for a location."""
    return f"The weather in {location} is sunny ☀️"

if __name__ == "__main__":
    mcp.run(transport="stdio")

Olama集成(simple_ollama_mcp.py)

  1. 问题分析 -Ollama确定是否需要工具
  2. 位置提取 -Ollama从问题中提取城市名称
  3. 工具执行 -MCP客户端呼叫天气服务器
  4. 响应格式 -Ollama创造自然语言反应

🌟 主要特点

✅ FastMCP服务器 -现代、类型安全的MCP服务器实现
✅ 标准运输 -简单的基于流程的沟通
✅ 配置驱动 -基于JSON的服务器管理
✅ 本地LLM集成 -适用于Olama车型
✅ 刀具检测 -基于用户意图的智能工具使用
✅ 错误处理 -优雅的失败和恢复

🔄 建筑

User Question
     ⬇️
Ollama LLM (Intent Detection)
     ⬇️
MCP Client (Tool Orchestration)
     ⬇️
MCP Server (Tool Execution)
     ⬇️
Ollama LLM (Response Formatting)
     ⬇️
Natural Language Response

📝 配置

编辑 config.json 要添加更多MCP服务器:

{
  "mcpServers": {
    "weather-server": {
      "command": "python",
      "args": ["server.py"],
      "env": {},
      "description": "Weather information server"
    },
    "your-server": {
      "command": "python", 
      "args": ["your_server.py"],
      "env": {"API_KEY": "your-key"},
      "description": "Your custom server"
    }
  }
}

🚀 扩展项目

添加新工具

@mcp.tool()
async def your_tool(param: str) -> str:
    """Your tool description."""
    return f"Result for {param}"

支持更多型号

替换 gpt-oss:20b 与Olama兼容的任何型号:

  • llama2:7b
  • llama2:70b
  • codellama:7b
  • mistral:7b

🐛 故障排除

常见问题

  1. Ollama连接失败

    # Make sure Ollama is running
    ollama serve
    
    # Test connection
    python test_ollama_connection.py
    
  2. 未找到型号

    # List available models
    ollama list
    
    # Pull missing model
    ollama pull gpt-oss:20b
    
  3. MCP服务器问题

    # Test basic MCP functionality
    python client.py
    

📚 了解更多

🤝 贡献

欢迎投稿!请随时提交pull请求或打开bug和功能请求的问题。

📄 许可证

此项目根据MIT许可证获得许可-请参阅 许可证 文件以获取详细信息。

🙏 致谢