mcpField
M
MCP Gateway, Server, and Client

MCP Gateway, Server, and Client

本地服务
github.com/boilingdata开发工具9163 周前更新

关于 MCP Gateway, Server, and Client

这是一个 MCP 网关与示例项目,可将 stdio 传输桥接到 HTTP SSE 端点,适用于暂不支持远程服务的 Claude Desktop。项目同时提供示例服务端、客户端以及 MCP Inspector 测试方法,便于独立开发与调试。

主要功能

  1. 01stdio 到 HTTP SSE 的传输桥接
  2. 02可通过 npx 直接运行
  3. 03支持环境变量配置主机与端口
  4. 04示例服务端与客户端
  5. 05配合 MCP Inspector 进行测试

适用场景

  1. 01让 Claude Desktop 连接 HTTP SSE 类型的服务
  2. 02在 Claude Desktop 之外独立开发和调试 SSE 服务

MCP 相关

该项目作为网关,将 stdio 类型的 MCP 连接转换为 HTTP SSE 端点,并附带示例 MCP 服务端与客户端。

配置

本地服务的配置在各客户端中通用,复制后粘贴到客户端的 MCP 配置文件即可。

mcp.json · 通用配置
{
"mcpServers": {
"and-gw": {
"command": "npx",
"args": [
"-y",
"mcp-server-and-gw"
]
}
}
}
启动命令npx -y mcp-server-and-gw

本地服务需要在你的电脑上安装运行环境(如 Node.js 18+)。各客户端的配置文件位置不同,请参考客户端文档。

安装包
npmmcp-server-and-gw

工具

还没有获取到工具列表

本地服务需要在你的电脑上运行后才能读取工具,维护者暂未提供工具列表。

详细介绍

MCP 网关、服务和客户端

只要 Claude Desktop 尚不支持连接远程服务,你就可以使用此脚本,将 stdio 桥接到 HTTP SSE(Server-Sent Events)端点。

安装 mcp-server-and-gw

# 1. install
npm install -g mcp-server-and-gw
# 2. Or run directly with npx
npx mcp-server-and-gw http://localhost:8808/
# ...you can use environment variables too
MCP_HOST=localhost MCP_PORT=8808 npx mcp-server-and-gw

在 Claude 中添加配置

桥接脚本使用 node javasscript 编写,但你的服务代码可以使用任意语言。

一个将 stdio 传输桥接到 HTTP SSE 传输的 Model Context Protocol 网关 src/mcp-server-and-gw.ts。

## 1. Build
yarn install
yarn build

## 2. Copy the code or update the claude_desktop_config.json
##    NOTE: Ensure that npx is in the PATH, or use full path like /opt/homebrew/bin/npx
echo '{
  "mcpServers": {
    "Claude Gateway Example": {
      "command": "npx",
      "args": [
        "mcp-server-and-gw", "http://localhost:8808/"
      ]
    }
  }
}' > ~/Library/Application\ Support/Claude/claude_desktop_config.json

## 3. Start server so that claude can connect to it for discoverying its resources, tools, etc.
PORT=8808 node examples/server.js

## 4. Start Claude Desktop

示例服务和客户端

你也可以独立于 Claude Desktop 开发 SSE 服务,以便更快地迭代。例如,运行 src/server.ts,并使用 src/client.ts 作为客户端。

启动服务后,在另一个终端启动客户端,即可看到服务输出。

% node examples/server.js
Server is running on port 8808

--> Received connection: /sse
New SSE connection.
--> Received message (post)
{
  jsonrpc: '2.0',
  id: 0,
  method: 'initialize',
  params: {
    protocolVersion: '2024-11-05',
    capabilities: {},
    clientInfo: { name: 'example-client', version: '1.0.0' }
  }
}
<-- 202 Accepted
--> Received message (post)
{ jsonrpc: '2.0', method: 'notifications/initialized' }
<-- 202 Accepted
--> Received message (post)
{ jsonrpc: '2.0', id: 1, method: 'resources/list' }
<-- 202 Accepted
--> Received message (post)
{ jsonrpc: '2.0', id: 2, method: 'tools/list' }
<-- 202 Accepted
--> Received message (post)
{
  jsonrpc: '2.0',
  id: 3,
  method: 'tools/call',
  params: { name: 'query', arguments: { sql: 'SELECT 42;' } }
}
<-- 202 Accepted

启动客户端

% node examples/client.js
Connecting...
Connected: { resources: {}, tools: {}, templates: {} }
{ resources: [] }
{
  tools: [
    {
      name: 'query',
      description: 'Run a read-only SQL query on a DuckDB database',
      inputSchema: { type: 'object', properties: { sql: { type: 'string' } } }
    },
    {
      name: 'visualise',
      description: 'Visualise SQL query results as an Apache ECharts chart. Provide the SQL clause that produces the data for the visualisation. Provide chart JSON configuration for Apache ECharts.',
      inputSchema: {
        type: 'object',
        properties: { sql: { type: 'string' }, chart: { type: 'string' } }
      }
    }
  ]
}
{
  content: [ { type: 'text', text: '[\n  {\n    "42": 42\n  }\n]' } ],
  isError: false
}

使用 MCP Inspector 测试

在一个终端中启动示例服务

node examples/server.js

……然后在另一个终端中启动网关

npx @modelcontextprotocol/inspector node ./build/mcp-server-and-gw.js

评论

支持 Markdown

第一次接触 MCP?

从协议是什么,到在客户端里装上第一个服务,这几条回答覆盖了大多数入门问题。

一个开放协议,规定了 AI 应用如何以统一方式连接外部工具与数据源。客户端负责与模型对话,MCP 服务负责提供工具、资源和提示模板,两者通过标准消息通信,同一个服务可以被不同客户端复用。