只要Claude Desktop不支持连接到远程服务器,您就可以使用此脚本从标准输入输出(stdio)运行到HTTP SSE(服务器发送事件)端点的桥接。
# 1. 安装
npm install -g mcp-server-and-gw
# 2. 或者直接使用npx运行
npx mcp-server-and-gw http://localhost:8808/
# ...您也可以使用环境变量
MCP_HOST=localhost MCP_PORT=8808 npx mcp-server-and-gw
桥接脚本是Node JavaScript,但您的服务器代码可以使用任何您喜欢的语言。
一个从stdio到HTTP SSE传输的Model Context Protocol网关src/mcp-server-and-gw.ts。
## 1. 构建
yarn install
yarn build
## 2. 复制代码或更新 claude_desktop_config.json
## 注意:确保npx在PATH中,或者使用完整路径如 /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. 启动服务器,以便Claude可以连接并发现其资源、工具等。
PORT=8808 node examples/server.js
## 4. 启动Claude Desktop
您还可以独立于Claude Desktop开发SSE服务器,以获得更快的迭代。例如,运行src/server.ts并使用src/client.ts作为客户端。
启动服务器,一旦在另一个终端上启动客户端,您会看到服务器输出。
% node examples/server.js
服务器正在8808端口运行
--> 接收到连接: /sse
新的SSE连接。
--> 接收到消息(post)
{
jsonrpc: '2.0',
id: 0,
method: 'initialize',
params: {
protocolVersion: '2024-11-05',
capabilities: {},
clientInfo: { name: 'example-client', version: '1.0.0' }
}
}
<-- 202 已接受
--> 接收到消息(post)
{ jsonrpc: '2.0', method: 'notifications/initialized' }
<-- 202 已接受
--> 接收到消息(post)
{ jsonrpc: '2.0', id: 1, method: 'resources/list' }
<-- 202 已接受
--> 接收到消息(post)
{ jsonrpc: '2.0', id: 2, method: 'tools/list' }
<-- 202 已接受
--> 接收到消息(post)
{
jsonrpc: '2.0',
id: 3,
method: 'tools/call',
params: { name: 'query', arguments: { sql: 'SELECT 42;' } }
}
<-- 202 已接受
启动客户端
% node examples/client.js
正在连接...
已连接: { resources: {}, tools: {}, templates: {} }
{ resources: [] }
{
tools: [
{
name: 'query',
description: '在一个DuckDB数据库上执行只读SQL查询',
inputSchema: { type: 'object', properties: { sql: { type: 'string' } } }
},
{
name: 'visualise',
description: '将SQL查询结果可视化为Apache ECharts图表。提供生成可视化数据的SQL子句。提供Apache ECharts的图表JSON配置。',
inputSchema: {
type: 'object',
properties: { sql: { type: 'string' }, chart: { type: 'string' } }
}
}
]
}
{
content: [ { type: 'text', text: '[\n {\n "42": 42\n }\n]' } ],
isError: false
}
在一个终端上启动示例服务器
node examples/server.js
...在另一个终端上启动网关
npx @modelcontextprotocol/inspector node ./build/mcp-server-and-gw.js