基于 SSE 的MCP服务器和客户端
这演示了基于 SSE 的 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 ...
Related MCP server: Node.js MCP Weather Server
为什么?
这意味着 MCP 服务器现在可以是一个正在运行的进程,代理(客户端)可以随时随地连接、使用和断开连接。换句话说,基于 SSE 的服务器和客户端可以是解耦的进程(甚至可能位于解耦的节点上)。这与基于 STDIO 的模式(客户端本身会将服务器作为子进程生成)不同,并且更适合“云原生”用例。
通过 Smithery 安装
要通过Smithery自动为 Claude Desktop 安装基于 SSE 的服务器和客户端:
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 --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