MCP网关

MCP网关是一个REST到MCP的网关,通过可流式HTTP传输连接到Tableau MCP服务器,提供REST API端点、持久连接和请求跟踪等功能。

  • by jrunrun
  • JavaScript
  • 本地部署
  • 更新于
在 GitHub 查看 jrunrun/mcp-gateway

使用说明

MCP网关

休息→ MCP网关,通过Streamable HTTP传输连接到Tableau MCP服务器。

特性

  • ✅ MCP工具操作的REST API端点
  • ✅ 持久MCP连接(连接一次,重复使用)
  • ✅ 可选网关身份验证
  • ✅ 请求跟踪ID
  • ✅ 铁路就绪(绑定到0.0.0.0:$PORT)
  • ✅ 平滑关闭
  • ✅ 结构化错误响应

需求

  • Node.js 18+
  • 访问Tableau MCP服务器URL

本地开发

1.安装依赖项

npm install

2.设置环境变量

export TABLEAU_MCP_URL="https://your-mcp-server.railway.app/tableau-mcp"
export GATEWAY_SECRET="your-secret-key-here"  # Optional
export PORT=3000  # Optional, defaults to 3000

3.启动服务器

npm start

或者使用手表模式进行开发:

npm run dev

环境变量

变量 必填 描述
TABLEAU_MCP_URL ✅ 是 Tableau MCP服务器的完整URL(包括基本路径)
PORT ❌ 否 要监听的端口(默认值:3000,Railway注入此端口)
GATEWAY_SECRET ❌ 否 如果设置,则需要 x-gateway-secret 标头上/mcp路由
TABLEAU_MCP_AUTH_HEADER_NAME ❌ 否 要发送到Tableau MCP的可选身份验证标头名称
TABLEAU_MCP_AUTH_HEADER_VALUE ❌ 否 要发送到Tableau MCP的可选身份验证标头值

API终点

GET/健康

健康检查端点。

curl http://localhost:3000/health

答复:

{
  "ok": true,
  "mcpConnected": true
}

GET/mcp/工具

列出所有可用的MCP工具。

curl http://localhost:3000/mcp/tools \
  -H "x-gateway-secret: your-secret-key-here"

答复:

{
  "ok": true,
  "requestId": "uuid-here",
  "tools": [
    {
      "name": "tool-name",
      "description": "Tool description",
      "inputSchema": { ... }
    }
  ]
}

POST/mcp/tools/:toolName

调用特定的MCP工具。

curl -X POST http://localhost:3000/mcp/tools/example-tool \
  -H "Content-Type: application/json" \
  -H "x-gateway-secret: your-secret-key-here" \
  -d '{
    "arg1": "value1",
    "arg2": "value2"
  }'

成功响应:

{
  "ok": true,
  "requestId": "uuid-here",
  "result": {
    "content": [ ... ],
    "isError": false
  }
}

错误响应:

{
  "ok": false,
  "requestId": "uuid-here",
  "error": "CALL_TOOL_FAILED",
  "tool": "example-tool",
  "message": "Error details here"
}

工作流示例

  1. 检查健康状况:

    curl http://localhost:3000/health
    
  2. 列出可用工具:

    curl http://localhost:3000/mcp/tools \
      -H "x-gateway-secret: your-secret"
    
  3. 调用工具 (使用假设的Tableau工具的示例):

    curl -X POST http://localhost:3000/mcp/tools/query-tableau \
      -H "Content-Type: application/json" \
      -H "x-gateway-secret: your-secret" \
      -d '{
        "workbook": "Sales Dashboard",
        "view": "Overview"
      }'
    

铁路部署

1.部署到铁路

# Option 1: Deploy from GitHub
# - Connect your repository in Railway dashboard
# - Railway will auto-detect Node.js and use package.json

# Option 2: Deploy with Railway CLI
railway up

2.在铁路中设置环境变量

在铁路项目设置中,添加:

TABLEAU_MCP_URL=https://your-tableau-mcp.railway.app/tableau-mcp
GATEWAY_SECRET=your-secure-secret-here

可选(如果您的Tableau MCP需要身份验证):

TABLEAU_MCP_AUTH_HEADER_NAME=Authorization
TABLEAU_MCP_AUTH_HEADER_VALUE=Bearer your-token

3.重要铁路注意事项

  • ✅ 服务器自动绑定到 0.0.0.0 (铁路要求)
  • ✅ 服务器使用 process.env.PORT (铁路注入了这一点)
  • ✅ 无需在铁路中手动设置PORT
  • ✅ 健康终点位于 https://your-app.railway.app/health

4.验证部署

# Check health
curl https://your-app.railway.app/health

# List tools
curl https://your-app.railway.app/mcp/tools \
  -H "x-gateway-secret: your-secure-secret-here"

错误响应

所有错误都遵循以下结构:

{
  "ok": false,
  "requestId": "uuid-here",
  "error": "ERROR_CODE",
  "message": "Human-readable error message"
}

常见错误代码:

  • UNAUTHORIZED (401):网关密钥丢失或无效
  • NOT_FOUND (404):未找到路线
  • MCP_NOT_CONNECTED (503):MCP客户端未连接
  • LIST_TOOLS_FAILED (500):列出工具时出错
  • CALL_TOOL_FAILED (500):调用工具时出错
  • INTERNAL_ERROR (500):未经处理的服务器错误

Salesforce Apex测试

HttpRequest req = new HttpRequest();
req.setEndpoint('https://your-gateway.railway.app/mcp/tools/tool-name');
req.setMethod('POST');
req.setHeader('Content-Type', 'application/json');
req.setHeader('x-gateway-secret', 'your-secret');
req.setBody('{"param1": "value1"}');

Http http = new Http();
HttpResponse res = http.send(req);
System.debug(res.getBody());

建筑

┌──────────────┐         ┌──────────────┐         ┌──────────────┐
│   REST       │  HTTP   │  MCP         │   MCP   │   Tableau    │
│   Client     │────────▶│  Gateway     │────────▶│   MCP Server │
│ (curl/Apex)  │         │ (this server)│         │  (Railway)   │
└──────────────┘         └──────────────┘         └──────────────┘
                              │
                              │ Persistent
                              │ Connection
                              ▼
                    StreamableHTTPClientTransport

安全说明(POC)

  • 这是POC质量代码,不是生产硬化的
  • GATEWAY_SECRET 提供基本身份验证(不是OAuth/JWT)
  • 在生产环境中使用HTTPS(Railway会自动提供此功能)
  • 考虑生产使用的速率限制
  • 添加生产使用的请求验证

故障排除

“MCP_NOT_CONNECTED”错误

  • 检查 TABLEAU_MCP_URL 是正确的
  • 验证Tableau MCP服务器是否正在运行且可访问
  • 检查服务器日志中的连接错误

铁路502错误

  • 验证服务器是否绑定到 0.0.0.0 (不是 localhost)
  • 验证服务器是否正在使用 process.env.PORT
  • 检查铁路日志中的启动错误

“未经授权”错误

  • 确保 x-gateway-secret 标题匹配 GATEWAY_SECRET env 是
  • 标头名称不区分大小写,但值区分大小写

许可证

麻省理工学院