Python MCP 服务器和客户端示例
安装
- 安装 Python 3.12 或更高版本
- 使用 Python 模块 pip 安装 requirements.txt 文件中的依赖项
运行服务器:
使用 uvicorn 运行 test_mcp_server 模块中的 app 应用,主机设为 0.0.0.0,工作进程数为 1,端口设为 8000
运行客户端以测试服务器:
运行 python test_mcp_client.py
经验
- 最好不要启用
json_response,因为在客户端关闭时会触发服务器错误。
> 示例 mcp = FastMCP("StatelessServer", stateless_http=True, json_response=True)
