Token导航 LogoToken导航TokenDH.com
MCP Ollama Integration logo
AI代理stdio官方级别未说明来源级核验

MCP Ollama Integration

MCP Server

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

工具数

1

提示词数

0

GitHub Stars

0

资源数

0
本地LLMPythonAI代理

安装说明

本站只整理中文说明和来源信息,不托管安装包,也不代用户安装。

作者 / 组织

Sanjaykrishnamurthy

提供方

Sanjaykrishnamurthy

最后核验

2026/5/17 20:22

快速接入

先看主来源和安装命令,再打开仓库或文档;下面只保留这个条目的关键接入事实。

命令预览

pip install "mcp[cli]" openai

详细介绍

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
  1. 安装依赖项:
   pip install "mcp[cli]" openai
  1. 启动Ollama服务器:
   ollama serve
  1. 拉取模型(如果尚未可用):
   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
  1. 未找到型号
   # List available models
   ollama list

   # Pull missing model
   ollama pull gpt-oss:20b
  1. MCP服务器问题
   # Test basic MCP functionality
   python client.py

📚 了解更多

🤝 贡献

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

📄 许可证

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

🙏 致谢

目录标签

目录标签

本地LLMPythonAI代理本地部署MCP集成AI工具调用实时数据处理配置驱动

接入字段

传输方式(transport,传输协议)

stdio

鉴权方式(authType,认证方式)

none

工具数量(toolCount,工具数)

1

资源数量(resourceCount,资源数)

0

提示词数量(promptCount,提示词数)

0

权限和风险

stdionone部署方式未说明

接入前请确认传输方式、认证方式和部署位置,并根据实际工具能力限制访问范围。

安装前确认

不要直接授予不必要的文件、网络或账号权限;先核对安装命令和配置内容。

来源信息

继续浏览同类 MCP