多服务器MCP实现(Github MCP、MS Learn MCP、文件系统MCP、Azure OpenAI)
用于GitHub、Microsoft Learn和Azure OpenAI文件系统的MCP服务器-多服务器模型上下文协议实现
A. 生产准备就绪 同时连接到多个MCP服务器的MCP客户端:
- 🐙 GitHub服务器:搜索仓库,获取仓库信息,追踪仓库趋势
- 📚 Microsoft学习服务器:搜索文档、Azure服务信息
- 💾 文件系统服务器:自动组织保存/读取文件
建筑
┌─────────────────┐
│ Azure OpenAI │
└────────┬────────┘
│
┌────────▼────────┐
│ MCP Client │
│ (client.py) │
└─┬─────┬────┬────┘
│ │ │
┌─────────────┘ │ └─────────────┐
│ │ │
┌───────▼────────┐ ┌───────▼────────┐ ┌─────▼──────────┐
│ GitHub Server │ │ MSLearn Server │ │ FileSystem │
│ (stdio) │ │ (stdio) │ │ Server (stdio) │
└────────────────┘ └────────────────┘ └────────────────┘特性
1.GitHub集成
- search_github_repos:按查询搜索存储库
- get_repo_info:获取详细的回购信息
- get_trending_repos:按语言查找趋势存储库
2.微软学习集成
- search_mslearn:搜索MS学习文档
- get_azure-services:按类别获取Azure服务
3.文件系统集成
- 保存文件:自动组织保存到
data/_/ - read_file:从项目中读取任何文件
- list_saved_files:列出所有已保存的文件
设置
- 安装依赖项 (如果还没有):
pip install mcp openai python-dotenv httpx- 配置
.env(以项目根为单位):
AZURE_OPENAI_ENDPOINT=https://your-endpoint.openai.azure.com/
AZURE_OPENAI_KEY=your-key
AZURE_OPENAI_DEPLOYMENT=gpt-4o-mini
GITHUB_TOKEN=your-github-token # Optional, for higher rate limits- 运行客户端:
python client.py使用示例
示例1:搜索GitHub并保存
You: Search GitHub for Python machine learning libraries and save the top 3 to a file
[AI will]:
1. Call search_github_repos with query "Python machine learning"
2. Format the results
3. Call save_file to save in data/github_search_20240115_143022/ml_libraries.md示例2:微软学习搜索
You: What are Azure AI services?
[AI will]:
1. Call get_azure_services with category "ai"
2. Present the list of Azure AI services示例3:组合工作流
You: Find trending JavaScript repos, then search MS Learn for Node.js tutorials, and save everything to a file
[AI will]:
1. Call get_trending_repos with language "JavaScript"
2. Call search_mslearn with query "Node.js tutorials"
3. Combine results and call save_file to save in organized folder示例4:代码保存
You: Show me a Python async example and save it as async_example.py
[AI will]:
1. Generate Python async code
2. Call save_file with filename "async_example.py"
3. File saved to data/python_async_20240115_143530/async_example.py运作原理
1.客户端启动
- 生成3个MCP服务器作为子进程
- 每台服务器通过stdio(JSON-RPC 2.0)进行通信
- 客户端与所有服务器建立MCP会话
- 从所有服务器收集所有工具(共9个工具)
2.用户查询处理
- 用户类型自然语言查询
- 客户端向Azure OpenAI发送所有9个可用工具
- OpenAI决定调用哪个工具以及使用什么参数
3.工具执行
- 客户端标识哪个服务器拥有所请求的工具
- 通过MCP协议调用工具(通过stdio的JSON-RPC)
- 服务器执行并返回结果
- 客户端将结果发送回OpenAI进行最终响应
4.文件组织
保存文件时,文件系统服务器会自动创建:
data/
├── github_search_20240115_143022/
│ ├── ml_libraries.md
│ └── results.json
├── azure_services_20240115_144530/
│ └── ai_services.txt
└── python_code_20240115_145012/
└── async_example.pyMCP协议合规性
✅ 正确的运输 (JSON-RPC 2.0)\ ✅ 多个服务器连接 (3场同时进行的会议)\ ✅ 工具发现 (列出来自每个服务器的工具)\ ✅ 工具执行 (通过适当的协议调用tool)\ ✅ OpenAI函数调用 (本机工具参数)\ ✅ 自动刀具布线 (客户端为每个工具找到正确的服务器)
延伸
添加新服务器
- 创建
server_newservice.py使用MCP服务器 - 添加到
serversdict inclient.py - 客户端自动发现并使用新工具
添加新工具
只需添加到任何服务器 list_tools() -客户会自动取走它们!
备注
- GitHub代币:可选,但建议用于更高的API费率限制
- 文件路径:所有这些都与客户端运行的项目根相关
- 错误处理:API故障的优雅后退
- 并发工具:AI可以按顺序调用多个工具
这是一个 实际MCP实施 展示了多服务器编排的强大功能!
