一个强大的 Python 包,用于与 Model Context Protocol (MCP) 服务进行交互。支持 npm 部署的任意 MCP 服务。
- 🚀 持久化连接: 支持 stdio 服务的长连接(Connection Pooling),大幅提升连续调用性能。
- 🤖 LLM 适配: 一键生成 OpenAI/Anthropic 标准的工具描述(Schema),直接喂给大模型。
- ⚡ 异步支持: 提供
AsyncMCPClient,兼容 Python 3.9+ 的异步编程。 - 🎯 简单易用: 兼容 Cursor/Claude 格式的
config.json,几行代码即可使用。 - 🛡️ 健壮容错: 自动探测
npx路径,智能过滤非 JSON 日志,增强 Windows 环境兼容性。
pip install -r requirements.txt复制配置模板并修改:
cp config_template.json my_config.jsonfrom py_mcp_call import load_config_manager
# 加载配置(自动管理持久化连接)
manager = load_config_manager("my_config.json")
# 1. 自动生成大模型工具描述 (LLM Tool Schema)
openai_tools = manager.get_llm_tools("windows-cli", format="openai")
# 2. 极速调用工具(复用进程,无需重复启动)
result = manager.call_service("windows-cli", "tools/call", {
"name": "execute_command",
"arguments": {"shell": "cmd", "command": "dir"}
})
print(f"命令执行结果: {result}")
# 3. 退出时清理资源
manager.close_all()| 服务名 | npm 包 | 功能描述 |
|---|---|---|
| filesystem | @modelcontextprotocol/server-filesystem |
文件系统操作 |
| windows-cli | @simonb97/server-win-cli |
Windows 命令行 |
| github | @modelcontextprotocol/server-github |
GitHub 集成 |
| brave-search | @modelcontextprotocol/server-brave-search |
Brave 搜索 |
加载 config.json 并返回管理器。管理器会自动处理长连接。
将 MCP 服务的工具列表转换为 LLM(OpenAI/Anthropic)可识别的 Schema 格式。
同步调用工具,内部自动复用进程。
异步客户端,支持 await client.call(...)。
优化后的版本通过持久化连接池技术,使连续调用响应时间从原来的 ~1.5s 降低至 < 1ms。
MIT License