HTTP流式服务器

一个基于Model Context Protocol (MCP)和Express构建的无状态HTTP流式服务器,适用于无服务器环境和水平扩展。

  • by jiawang1
  • TypeScript
  • 本地部署
  • 更新于
在 GitHub 查看 jiawang1/mcp-demo

使用说明

MCP演示服务器

A. 无状态HTTP流 服务器采用模型上下文协议(MCP)和Express构建。

🌐 现场演示

生产URL: https://mcp-demo-d3cj.onrender.com/mcp

现在试试:

curl -X POST https://mcp-demo-d3cj.onrender.com/mcp \
  -H "Content-Type: application/json" \
  -H "Accept: application/json, text/event-stream" \
  -d '{"jsonrpc":"2.0","id":1,"method":"tools/list","params":{}}'

📚 文档

特性

  • ✅ 完全无国籍 -每个请求都是独立处理的
  • ✅ HTTP流 -基于HTTP POST的标准流传输
  • ✅ 易于扩展 -无会话管理,可横向扩展
  • ✅ 简单部署 -非常适合无服务器环境(Lambda、云函数)
  • ✅ 生产就绪 -住在https://mcp-demo-d3cj.onrender.com
  • ✅ 两个内置工具:
    • read_widget_resource:读取widgetResource.md文件
    • read_page_resource:读取pageResource.md文件

安装

npm install

本地运行

# Production mode
npm start

# Development mode (with hot reload)
npm run dev

# Test with MCP Inspector
npm run test

服务器在上运行 http://localhost:3000 默认情况下。

测试和调试

使用生产服务器

测试实时生产服务器:

# List available tools
curl -X POST https://mcp-demo-d3cj.onrender.com/mcp \
  -H "Content-Type: application/json" \
  -H "Accept: application/json, text/event-stream" \
  -d '{"jsonrpc":"2.0","id":1,"method":"tools/list","params":{}}'

# Call read_widget_resource tool
curl -X POST https://mcp-demo-d3cj.onrender.com/mcp \
  -H "Content-Type: application/json" \
  -H "Accept: application/json, text/event-stream" \
  -d '{"jsonrpc":"2.0","id":2,"method":"tools/call","params":{"name":"read_widget_resource","arguments":{}}}'

使用HTTP流测试客户端(推荐)

# First start the server
npm start

# In another terminal, run the test client
npm run test-streaming

测试客户端将:

  1. 列出所有可用工具
  2. 呼叫 read_widget_resource 工具
  3. 呼叫 read_page_resource 工具

使用curl进行本地测试

# List available tools
curl -X POST http://localhost:3000/mcp \
  -H "Content-Type: application/json" \
  -H "Accept: application/json, text/event-stream" \
  -d '{"jsonrpc":"2.0","id":1,"method":"tools/list","params":{}}'

# Call read_widget_resource tool
curl -X POST http://localhost:3000/mcp \
  -H "Content-Type: application/json" \
  -H "Accept: application/json, text/event-stream" \
  -d '{"jsonrpc":"2.0","id":2,"method":"tools/call","params":{"name":"read_widget_resource","arguments":{}}}'

# Check server health
curl http://localhost:3000/health

# Get server information
curl http://localhost:3000/

API终点

HTTP流传输

1.MCP端点

  • 路径: /mcp
  • 方法:POST
  • 描述:MCP JSON-RPC请求的HTTP流端点
  • 特性:完全无状态,每个请求都独立处理

其他终点

2.健康检查

  • 路径: /health
  • 方法:获取
  • 描述:检查服务器状态

3.根路径

  • 路径: /
  • 方法:获取
  • 描述:获取服务器信息和可用工具列表

可用工具

read_widget_resource

读取以下内容 src/resources/widgetResource.md 文件。

输入:不需要参数

输出:文件内容为文本

示例:

# Local
curl -X POST http://localhost:3000/mcp \
  -H "Content-Type: application/json" \
  -H "Accept: application/json, text/event-stream" \
  -d '{"jsonrpc":"2.0","id":1,"method":"tools/call","params":{"name":"read_widget_resource","arguments":{}}}'

# Production
curl -X POST https://mcp-demo-d3cj.onrender.com/mcp \
  -H "Content-Type: application/json" \
  -H "Accept: application/json, text/event-stream" \
  -d '{"jsonrpc":"2.0","id":1,"method":"tools/call","params":{"name":"read_widget_resource","arguments":{}}}'

read_page_resource

读取以下内容 src/resources/pageResource.md 文件。

输入:不需要参数

输出:文件内容为文本

示例:

# Local
curl -X POST http://localhost:3000/mcp \
  -H "Content-Type: application/json" \
  -H "Accept: application/json, text/event-stream" \
  -d '{"jsonrpc":"2.0","id":2,"method":"tools/call","params":{"name":"read_page_resource","arguments":{}}}'

# Production
curl -X POST https://mcp-demo-d3cj.onrender.com/mcp \
  -H "Content-Type: application/json" \
  -H "Accept: application/json, text/event-stream" \
  -d '{"jsonrpc":"2.0","id":2,"method":"tools/call","params":{"name":"read_page_resource","arguments":{}}}'

使用示例

检查服务器运行状况

# Local
curl http://localhost:3000/health

# Production
curl https://mcp-demo-d3cj.onrender.com/health

获取服务器信息

# Local
curl http://localhost:3000/

# Production
curl https://mcp-demo-d3cj.onrender.com/

MCP客户端集成

要连接到此MCP服务器,您的MCP客户端需要:

  1. 向发送HTTP POST请求 /mcp 端点
  2. 使用JSON-RPC 2.0格式
  3. 包含正确的Accept标头: application/json, text/event-stream
  4. 通过HTTP响应接收结果

配置示例

本地开发

{
  "mcpServers": {
    "mcp-demo-local": {
      "transport": {
        "type": "http",
        "url": "http://localhost:3000/mcp"
      }
    }
  }
}

生产

{
  "mcpServers": {
    "mcp-demo": {
      "transport": {
        "type": "http",
        "url": "https://mcp-demo-d3cj.onrender.com/mcp"
      }
    }
  }
}

使用MCP SDK

import { Client } from '@modelcontextprotocol/sdk/client/index.js';
import { StreamableHTTPClientTransport } from '@modelcontextprotocol/sdk/client/streamableHttp.js';

const client = new Client({
  name: 'my-client',
  version: '1.0.0',
});

// Use production server
const transport = new StreamableHTTPClientTransport(
  new URL('https://mcp-demo-d3cj.onrender.com/mcp')
);

// Or use local server
// const transport = new StreamableHTTPClientTransport(
//   new URL('http://localhost:3000/mcp')
// );

await client.connect(transport);

// List tools
const tools = await client.listTools();

// Call tool
const result = await client.callTool({
  name: 'read_widget_resource',
  arguments: {},
});

技术栈

  • @模型上下文协议/sdk:MCP协议实施
  • 快速:Web框架
  • 跨域资源共享:跨源资源共享支持
  • Node.js:ES模块

项目结构

mcp-demo/
├── src/
│   ├── index.js                 # Main server file
│   └── resources/
│       ├── widgetResource.md    # Widget resource file
│       └── pageResource.md      # Page resource file
├── test-http-streaming.js       # HTTP Streaming test client
├── package.json
├── README.md
└── LICENSE

环境变量

  • PORT:服务器端口(默认值:3000)
PORT=8080 npm start

发展

服务器使用ES模块,需要Node.js 14.0或更高版本。

在开发模式下,使用 --watch 自动重启标志:

npm run dev

部署

此服务器部署在 渲染 可在以下网址获得: https://mcp-demo-d3cj.onrender.com/mcp

您可以部署自己的实例:

  1. 分叉此存储库
  2. 连接到渲染器/Vercel/Railway
  3. 如果需要,设置环境变量
  4. 部署!

许可证

麻省理工学院