SSE服务器与客户端

基于SSE的MCP服务器和客户端实现,支持工具调用和云原生应用场景。

  • by goyatparmod
  • Python
  • 本地部署
  • 更新于
在 GitHub 查看 goyatparmod/sse-mcp

使用说明

基于SSE的服务器和客户端 主控程序

这展示了基于SSE的MCP服务器和使用其中工具的独立MCP客户端的工作模式。基于最初的讨论 这里.

用法

备注:确保供应 ANTHROPIC_API_KEY 在……里面 .env 或者作为环境变量。

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 ...

为什么?

这意味着MCP服务器现在可以是一些正在运行的进程,代理(客户端)可以随时随地连接、使用和断开连接。换句话说,基于SSE的服务器和客户端可以是解耦的进程(甚至可能在解耦的节点上)。与基于STDIO的模式相比,这是不同的,更适合“云原生”用例,在STDIO模式中,客户端本身将服务器作为子流程生成。

通过Smithery安装

通过以下方式自动安装基于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 是连接到基于SSE的MCP服务器并使用其工具的MCP客户端。改编自MCP文件 示例STDIO客户端实现。

默认情况下,客户端连接到命令行参数中提供的SSE端点,如下所示:

uv run client.py http://0.0.0.0:8080/sse