article
README
🚀 基于 SSE 的 MCP 服务器和客户端示例 MCP
本项目展示了基于 SSE 的 MCP 服务器以及独立 MCP 客户端工具的工作模式。其灵感源自此处的原始讨论。通过本示例,用户可以了解如何实现解耦的 MCP 服务器与客户端进程,为实际应用提供参考。
🚀 快速开始
环境准备
⚠️ 重要提示
请确保在
.env或环境变量中提供ANTHROPIC_API_KEY。
运行示例
启动服务器
uv run weather.py
启动客户端
uv run client.py http://0.0.0.0:8080/sse
运行效果示例
Initialized SSE client...
Listing tools...
Connected to server with tools: ['get_alerts', 'get_forecast']
MCP Client Started!
Type your queries or 'quit' to exit.
Query: whats the weather like in Spokane?
I can help you check the weather forecast for Spokane, Washington. I'll use the get_forecast function, but I'll need to use Spokane's latitude and longitude coordinates.
Spokane, WA is located at approximately 47.6587° N, 117.4260° W.
[Calling tool get_forecast with args {'latitude': 47.6587, 'longitude': -117.426}]
Based on the current forecast for Spokane:
Right now it's sunny and cold with a temperature of 37°F and ...
✨ 主要特性
本项目基于 SSE 实现的 MCP 服务器和客户端具有以下特性:
- 解耦性:MCP 服务器可以作为一个独立运行的进程,客户端可以随时连接、使用并断开与服务器的连接。这与基于 STDIO 的模式不同,后者要求客户端作为子进程启动服务器。
📦 安装指南
通过 Smithery 安装
要通过 Smithery 自动安装基于 SSE 的 MCP 服务器和客户端,可使用以下命令:
npx -y @smithery/cli install @sidharthrajaram/mcp-sse --client claude
💻 使用示例
服务器
weather.py 是一个基于 SSE 的 MCP 服务器,它提供了一些基于国家气象局 API 的工具。该服务器改编自 MCP 文档中的 示例 STDIO 服务器实现。
基础用法
默认情况下,服务器运行在 0.0.0.0:8080,可使用以下命令启动:
uv run weather.py
高级用法
可以通过命令行参数配置服务器的主机和端口,例如:
uv run weather.py --host <your host> --port <your port>
客户端
client.py 是一个 MCP 客户端,用于连接并使用基于 SSE 的 MCP 服务器中的工具。该客户端改编自 MCP 文档中的 示例 STDIO 客户端实现。
基础用法
默认情况下,客户端通过命令行参数连接到 SSE 端点,例如:
uv run client.py http://0.0.0.0:8080/sse
微信扫一扫