MCP服务器示例

为ChatGPT和其他AI助手提供的最小化、单文件的MCP(模型上下文协议)服务器实现。

  • by aimechanic
  • Python
  • 本地部署
  • 更新于
在 GitHub 查看 aimechanic/mcp_stress_test

使用说明

简单的MCP服务器示例

用于ChatGPT和其他AI助手的最小单文件MCP(模型上下文协议)服务器实现。

文件

  • simple_mcp_server.py -具有3个工具(echo、get_time、compute)的基本工作MCP服务器
  • mcp_stress_test.py -配备12个工具的综合压力测试服务器,用于测试MCP限制
  • mcp_server_with_redis_sse.py -SSE(服务器发送事件)和Redis流集成示例
  • simple_mcp_sse_test.py -简单的SSE实现,用于实时更新

快速开始

# Install dependencies
pip install -r requirements.txt

# Run the basic server
python3 simple_mcp_server.py

# Or run the stress test server
python3 mcp_stress_test.py

连接到ChatGPT

  1. 暴露您的本地服务器(使用ngrok或类似工具):

    ngrok http 8080
    
  2. 在ChatGPT中:

    • 前往设置→ 连接器
    • 点击“新建连接器”
    • 输入您的ngrok URL+ /mcp: https://your-url.ngrok-free.app/mcp
    • 选择“无身份验证”
    • 点击“创建”
  3. 在聊天中使用连接器:

    • 点击消息框中的“+”
    • 选择“使用连接器”
    • 选择您的连接器
    • 要求ChatGPT使用这些工具

手动测试

# Test the /mcp endpoint
curl -X POST http://localhost:8080/mcp \
  -H "Content-Type: application/json" \
  -d '{"method":"tools/list","id":"1"}'

# Call a tool
curl -X POST http://localhost:8080/mcp \
  -H "Content-Type: application/json" \
  -d '{"method":"tools/call","params":{"name":"get_time","arguments":{}},"id":"2"}'

压力测试工具

这 mcp_stress_test.py 包括以下测试:

  • 响应大小:微小→ 巨大(高达500KB+)
  • 数据结构:深嵌套、宽对象、复杂类型
  • 处理:数组、字符串、元数据
  • 边缘案例:unicode、特殊字符、错误

建议的测试进度:

  1. test_small_response -基线
  2. test_complex_types -unicode,特殊字符
  3. test_medium_response (段落=20)-中等大小
  4. test_wide_structure (项目=200)-多个按键
  5. test_deep_nested_structure (深度=20)-嵌套限制
  6. test_array_processing (大小=5000)-数组处理
  7. test_context_window (context_size=“large”)-上下文限制
  8. test_large_response (size_kb=200)-大有效载荷

MCP协议端点

所有服务器都实现:

  • POST /mcp -JSON-RPC 2.0主端点
  • GET /mcp/manifest.json -工具发现
  • GET /health -健康检查
  • GET / -服务器信息

支持的方法:

  • initialize -协议握手
  • tools/list -列出可用工具
  • tools/call -执行工具
  • ping -活下去

特性

✅ 单文件实现-易于理解和修改 ✅ 除了FastAPI/uvicorn之外没有外部依赖 ✅ 完全CORS支持 ✅ 详细日志记录 ✅ MCP协议2024-11-05 ✅ 适用于ChatGPT连接器

许可证

麻省理工学院

鸣谢

内置🦔 对于MCP社区