MCP流式HTTP客户端与服务器

提供基于MCP协议的流式HTTP客户端和服务器实现示例,支持Python和TypeScript跨语言交互,适用于构建交互式聊天应用。

  • by invariantlabs-ai
  • TypeScript
  • 本地部署
  • 131 星标
  • 更新于
在 GitHub 查看 invariantlabs-ai/mcp-streamable-http

使用说明

MCP流式HTTP——Python和Typescript示例

此存储库提供了MCP(模型上下文协议)的示例实现 可流式传输的HTTP客户端和服务器 在Python和Typescript中,基于规范:📄 MCP流式HTTP规范.

您可以完全使用Python或TypeScript设置客户端+服务器堆栈。这个例子还演示了跨语言兼容性,允许Python客户端与TypeScript服务器通信,反之亦然。

🚀 入门指南

1.克隆存储库

git clone https://github.com/invariantlabs-ai/mcp-streamable-http.git
cd python-example

2.Python示例

1.添加您的Anthropic API密钥

更新 .env 文件内部 python-example/client 包含以下内容的目录:

ANTHROPIC_API_KEY=your_api_key_here

2.设置服务器

cd python-example/server
pip install .
python weather.py

默认情况下,服务器将在以下时间启动 http://localhost:8123.
如果要指定其他端口,请使用 --port 标志:

python weather.py --port=9000

3.设置客户端

cd ../client
pip install .

4.运行客户端

python client.py

这将启动一个 交互式聊天循环 使用MCP流式HTTP协议。
如果在其他端口上启动MCP服务器,请使用 --mcp-localhost-port 标志:

python client.py --mcp-localhost-port=9000

3.打字示例

1.添加您的Anthropic API密钥

更新 .env 文件内部 typescript-example/client 包含以下内容的目录:

ANTHROPIC_API_KEY=your_api_key_here

2.设置服务器

cd typescript-example/server
npm install && npm run build
node build/index.js

默认情况下,服务器将在以下时间启动 http://localhost:8123.
如果要指定其他端口,请使用 --port 标志:

node build/index.js --port=9000

3.设置客户端

cd ../client
npm install && npm run build

4.运行客户端

node build/index.js

这将启动一个 交互式聊天循环 使用MCP流式HTTP协议。
如果在其他端口上启动MCP服务器,请使用 --mcp-localhost-port 标志:

node build/index.js --mcp-localhost-port=9000

💬 查询示例

在客户端聊天界面中,您可以提出以下问题:

  • “萨克拉门托有天气警报吗?”
  • “纽约市的天气怎么样?”
  • “告诉我明天波士顿的天气预报。”

客户端将请求转发到本地MCP天气服务器,并使用Anthropic的Claude语言模型返回结果。使用的MCP传输层将是流式HTTP。