MCP网关服务

MCP网关服务是一个桥接工具,将stdio转换为HTTP SSE端点,用于连接Claude Desktop与远程服务器。

  • by boilingdata
  • JavaScript
  • 本地部署
  • 91 星标
  • 更新于
在 GitHub 查看 boilingdata/mcp-server-and-gw

使用说明

MCP网关、服务器和客户端

只要Claude Desktop不支持连接到远程服务器,您就可以使用此脚本运行从stdio到HTTP SSE(服务器发送事件)端点的网桥。

安装mcp服务器和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,但您的服务器代码可以是您使用的任何代码。

A. 模型上下文协议 网关 src/mcp-server和gw.ts 从 标准输入输出 向 HTTP SSE 运输。

## 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检验员测试

在一个终端上启动示例服务器

node examples/server.js

…以及另一个终端上的网关

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