模型上下文协议服务器

一个包含天气服务、谷歌表格自动化、个人财务管理和知识库检索的AI工具集成平台,可与Claude、GPT-4和Gemini等LLM无缝协作。

  • by Marufakamallabonno
  • Python
  • 本地部署
  • 更新于
在 GitHub 查看 Marufakamallabonno/mcp-poc

使用说明

🧠 模型上下文协议(MCP)——本地与部署集成

一个全面的合集 模型上下文协议(MCP) 用……构建的服务器 FastMCP,展示如何创建、集成和部署能够与大型语言模型(LLM)如(此处可具体指明模型名称,但原文未给出)无缝协作的AI驱动工具 克劳德, GPT-4,以及 双子座。

🚀 功能

这个项目包括 4台已准备好投入生产的MCP服务器:

  1. 🌦️ 天气服务器 - 来自国家气象局的实时天气数据和警报
  2. 📊 Google 电子表格服务器 - 完整的电子表格自动化与管理
  3. 💰 支出追踪器 - 基于SQLite的个人财务管理
  4. 📚 知识库(RAG) - 简单的检索增强生成系统

📋 先决条件

  • Python 3.13及以上版本 (必填)
  • 紫外线 - 快速Python包安装器(推荐)
  • Git(一种分布式版本控制系统) - 用于版本控制
  • OpenAI API密钥 或者 Groq API密钥 (为客户)

对于Google Sheets服务器:

  • 启用Sheets API的Google云项目
  • 服务帐户凭据JSON文件

🛠️ 安装

1. 克隆仓库

git clone https://github.com/Marufakamallabonno/mcp-poc.git
cd MCP-CRASH-Course

2. 安装 uv(如果尚未安装)

Windows(PowerShell):

irm https://astral.sh/uv/install.ps1 | iex

macOS/Linux:

curl -LsSf https://astral.sh/uv/install.sh | sh

3. 同步依赖项

uv sync

这将安装所有必需的软件包 pyproject.toml.

⚙️ 配置

1. MCP服务器配置

主配置文件位于 server/mcpconfig.json 如果你想在本地使用它,请根据需要更新路径:

{
  "mcpServers": {
    "weather": {
      "command": "uv",
      "args": ["run", "--with", "mcp[cli]", "mcp", "run", "path/to/weather.py"]
    },
    "google_sheets": {
      "command": "uv",
      "args": ["run", "--with", "google-api-python-client", "--with", "google-auth", "--with", "google-auth-oauthlib", "--with", "mcp[cli]", "mcp", "run", "path/to/google_sheet.py"],
      "env": {
        "SERVICE_ACCOUNT_PATH": "path/to/your/service-account.json",
        "SPREADSHEET_ID": "your-spreadsheet-id"
      }
    },
    "expense_tracker": {
      "command": "uv",
      "args": ["run", "--with", "mcp[cli]", "mcp", "run", "path/to/expense_tracker.py"]
    },
    "rag": {
      "command": "uv",
      "args": ["run", "--with", "mcp[cli]", "mcp", "run", "path/to/rag.py"]
    }
  }
}

2. 环境变量

创建一个 .env 项目根目录中的文件:

OPENAI_API_KEY=your_openai_api_key_here
# OR
GROQ_API_KEY=your_groq_api_key_here

3. Google Sheets 设置(可选)

如果使用Google Sheets服务器:

  1. 首选 Google Cloud 控制台
  2. 创建一个新项目或选择现有项目
  3. 启用 Google Sheets API 并且 谷歌云端硬盘API
  4. 创建一个 服务账户
  5. 下载凭证JSON文件
  6. 把它放在 server/keys/ 目录
  7. 更新路径中的 mcpconfig.json

4. 知识库设置(可选)

编辑 server/data/kb.json 添加您的问答对:

[
  {
    "question": "What is MCP?",
    "answer": "Model Context Protocol is a standard for AI tools..."
  }
]

🎯 使用方法

选项1:与大型语言模型(LLM)交互的客户端

运行连接所有MCP服务器的交互式聊天客户端:

cd MCP-CRASH-Course
uv run server/client.py

这将启动一个交互式聊天会话,在其中您可以:

  • 询问任何地点的天气情况
  • 管理Google表格
  • 追踪费用
  • 查询知识库

示例对话:

You: What's the weather in San Francisco?
You: Create a new spreadsheet called "Budget 2025"
You: Add an expense of $50 for groceries today
You: What is MCP?

选项2:测试单个服务器

独立测试每个MCP服务器:

# Weather Server
uv run --with mcp[cli] mcp dev server/weather.py

# Google Sheets Server
uv run --with google-api-python-client --with google-auth --with google-auth-oauthlib --with mcp[cli] mcp dev server/google_sheet.py

# Expense Tracker
uv run --with mcp[cli] mcp dev server/expense_tracker.py

# Knowledge Base
uv run --with mcp[cli] mcp dev server/rag.py

选项3:与Claude桌面版配合使用

  1. 复制 server/mcpconfig.json 到Claude的配置目录:

    • Windows: %APPDATA%\Claude\claude_desktop_config.json
    • macOS(苹果电脑操作系统): ~/Library/Application Support/Claude/claude_desktop_config.json
    • Linux: ~/.config/Claude/claude_desktop_config.json
  2. 重启Claude桌面版

  3. MCP 工具将出现在 Claude 的工具菜单中

📦 可用的MCP服务器

1. 天气服务器(weather.py)

工具:

  • get_alerts - 获取美国某州的天气警报
  • get_forecast - 获取坐标位置的天气预报
  • 额外的天气相关工具

示例:

# Get weather alerts for California
get_alerts(state="CA")

2. Google 电子表格服务器(google_sheet.py)

工具:

  • get_sheet_data - 从工作表中读取数据
  • update_cells - 更新单元格值
  • batch_update_cells - 一次性更新多个范围
  • create_spreadsheet - 创建新的电子表格
  • create_sheet - 添加新的工作表标签
  • list_sheets - 列出电子表格中的所有工作表
  • list_spreadsheets - 列出云端硬盘中的所有电子表格
  • copy_sheet - 在电子表格之间复制工作表
  • rename_sheet - 重命名工作表标签
  • add_rows / add_columns - 添加尺寸
  • share_spreadsheet - 与用户分享
  • get_sheet_formulas - 提取公式
  • get_multiple_sheet_data - 批量读取操作
  • get_multiple_spreadsheet_summary - 多工作表汇总

资源:

  • spreadsheet://{spreadsheet_id}/info - 获取电子表格的元数据

3. 支出追踪器(expense_tracker.py)

工具:

  • add_expense - 添加一项新费用
  • get_expenses - 按日期范围检索费用
  • update_expense - 修改现有费用
  • delete_expense - 删除一项费用
  • get_summary - 按类别获取支出汇总
  • export_expenses - 导出为CSV/JSON格式

数据库: 使用 SQLite(expense.db) 用于持久存储

4. 知识库RAG(检索增强生成)rag.py)

工具:

  • get_knowledge_base - 提取所有问答对
  • search_knowledge - 搜索特定主题
  • add_knowledge - 添加新的问答对

数据: 储存在 server/data/kb.json

🔧 故障排除

JSON解析错误

问题: SyntaxError: Unexpected token... is not valid JSON

解决方案: 当MCP服务器使用时会发生这种情况 print() 声明。使用 sys.stderr.write() 相反:

import sys
# Instead of: print("message")
sys.stderr.write("message\n")

文件锁定错误

问题: The process cannot access the file because it is being used by another process

解决方案:

  1. 关闭Claude Desktop或任何MCP客户端
  2. 终止正在运行的Python进程
  3. 删除 .venv 并且运行 uv sync 再次

Google Sheets 认证

问题: 权限被拒绝或配额已超出

解决方案:

  1. 验证服务账户的JSON是否有效
  2. 将您的电子表格与服务帐户电子邮件共享
  3. 在云端控制台中启用Google Sheets API
  4. 检查驱动器存储配额

模块未找到

问题: ModuleNotFoundError: No module named 'X'

解决方案:

uv sync  # Reinstall all dependencies

🎓 学习资源

🤝 贡献

欢迎投稿!请随时:

  • 添加新的MCP服务器
  • 改进现有工具
  • 修复漏洞
  • 更新文档

📝 许可证

这个项目是开源的,并且遵循以下许可协议提供 麻省理工学院许可证。

🙏 致谢

📧 联系方式

如需提问或寻求支持,请在GitHub上提交一个议题。


快乐的MCP大楼! 🚀(火箭发射或快速前进的符号,无具体文字对应,可意译为“火箭升空”或“飞速前进”等,根据上下文具体使用)