MCP远程代理

一个轻量级的MCP stdio代理,通过OAuth2客户端凭证认证将请求转发到远程MCP HTTP服务器。

  • by ciprian-tibulca
  • TypeScript
  • 本地部署
  • 更新于
在 GitHub 查看 ciprian-tibulca/mcp-remote-proxy

使用说明

mcp远程代理

一个轻量级的MCP(模型上下文协议)stdio代理,通过OAuth2客户端凭据身份验证将请求转发到远程MCP HTTP服务器。

特性

  • MCP标准传输:从stdin读取JSON-RPC消息,将响应写入stdout
  • HTTP转发:将所有MCP请求转发到远程HTTP端点
  • OAuth2客户端凭据:自动获取和管理访问令牌
  • 自动令牌刷新:通过单次重试刷新401/403响应上的令牌
  • 并发安全:使用单航班模式在负载下刷新令牌
  • 光标兼容:作为Cursor IDE中的MCP服务器命令无缝工作
  • 无交互提示:通过CLI参数或环境变量进行所有配置
  • 安全:从不记录机密,授权标头被编辑

安装

通过npm(发布时)

npm install -g mcp-remote-proxy

通过GitHub上的npx

npx github:ciprian-tibulca/mcp-remote-proxy --remote-url https://api.example.com/mcp ...

来源

git clone https://github.com/ciprian-tibulca/mcp-remote-proxy.git
cd mcp-remote-proxy
npm install
npm run build
npm link  # Makes 'mcp-remote-proxy' available globally

用法

命令行

mcp-remote-proxy \
  --remote-url https://api.example.com/mcp \
  --token-url https://auth.example.com/oauth/token \
  --client-id my-client-id \
  --client-secret my-client-secret \
  --scope "read write"

环境变量

export MCP_REMOTE_URL=https://api.example.com/mcp
export MCP_TOKEN_URL=https://auth.example.com/oauth/token
export MCP_CLIENT_ID=my-client-id
export MCP_CLIENT_SECRET=my-client-secret
export MCP_SCOPE="read write"

mcp-remote-proxy

CLI选项

选项 环境变量 默认值 描述
--remote-url MCP_REMOTE_URL 必需的 远程MCP服务器基本URL
--token-url MCP_TOKEN_URL 必需的 OAuth2令牌端点URL
--client-id MCP_CLIENT_ID 必需的 OAuth2客户端ID
--client-secret MCP_CLIENT_SECRET 必需的 OAuth2客户端机密
--scope MCP_SCOPE 可选的 OAuth2范围
--remote-timeout-ms MCP_REMOTE_TIMEOUT_MS 30000 远程呼叫超时(毫秒)
--token-timeout-ms MCP_TOKEN_TIMEOUT_MS 10000 令牌调用超时(毫秒)
--log-level MCP_LOG_LEVEL info 日志级别:调试、信息、警告、错误、静默
-h, --help 显示帮助消息
-v, --version 显示版本

光标IDE配置

将其添加到您的Cursor MCP配置文件中(~/.cursor/mcp.json 或工作空间 .cursor/mcp.json):

使用CLI参数

{
  "mcpServers": {
    "my-remote-server": {
      "command": "npx",
      "args": [
        "github:ciprian-tibulca/mcp-remote-proxy",
        "--remote-url", "https://api.example.com/mcp",
        "--token-url", "https://auth.example.com/oauth/token",
        "--client-id", "your-client-id",
        "--client-secret", "your-client-secret",
        "--scope", "mcp:read mcp:write"
      ]
    }
  }
}

使用环境变量

{
  "mcpServers": {
    "my-remote-server": {
      "command": "npx",
      "args": ["github:ciprian-tibulca/mcp-remote-proxy"],
      "env": {
        "MCP_REMOTE_URL": "https://api.example.com/mcp",
        "MCP_TOKEN_URL": "https://auth.example.com/oauth/token",
        "MCP_CLIENT_ID": "your-client-id",
        "MCP_CLIENT_SECRET": "your-client-secret",
        "MCP_SCOPE": "mcp:read mcp:write",
        "MCP_LOG_LEVEL": "warn"
      }
    }
  }
}

使用已发布的npm包

{
  "mcpServers": {
    "my-remote-server": {
      "command": "npx",
      "args": [
        "mcp-remote-proxy@latest",
        "--remote-url", "https://api.example.com/mcp",
        "--token-url", "https://auth.example.com/oauth/token",
        "--client-id", "your-client-id",
        "--client-secret", "your-client-secret"
      ]
    }
  }
}

使用本地构建

{
  "mcpServers": {
    "my-remote-server": {
      "command": "node",
      "args": [
        "/path/to/mcp-remote-proxy/dist/cli.js",
        "--remote-url", "https://api.example.com/mcp",
        "--token-url", "https://auth.example.com/oauth/token",
        "--client-id", "your-client-id",
        "--client-secret", "your-client-secret"
      ]
    }
  }
}

运作原理

  1. 初创公司:代理验证配置并获取初始OAuth2访问令牌
  2. 听力:它从stdin读取换行符分隔的JSON-RPC消息
  3. 转发:每条消息都通过带有承载令牌的HTTP POST转发到远程MCP服务器
  4. 回应:远程响应被写入stdout
  5. 令牌刷新:如果远程返回401/403,则刷新令牌并重试一次请求

消息流

┌─────────────┐    stdin     ┌──────────────────┐    HTTP POST     ┌─────────────────┐
│   Cursor    │ ──────────▶  │ mcp-remote-proxy │ ───────────────▶ │ Remote MCP      │
│   (client)  │              │                  │  + Bearer token  │ Server (HTTP)   │
│             │ ◀──────────  │                  │ ◀─────────────── │                 │
└─────────────┘    stdout    └──────────────────┘    JSON-RPC      └─────────────────┘
                                     │
                                     │ On 401/403
                                     ▼
                              ┌──────────────────┐
                              │ OAuth2 Token     │
                              │ Endpoint         │
                              └──────────────────┘

发展

先决条件

  • Node.js 18+(推荐20+)
  • npm

设置

git clone https://github.com/ciprian-tibulca/mcp-remote-proxy.git
cd mcp-remote-proxy
npm install

构建

npm run build

测试

npm test

棉绒

npm run lint
npm run lint:fix  # Auto-fix issues

类型检查

npm run typecheck

本地测试

# Build and run locally
npm run build
echo '{"jsonrpc":"2.0","id":1,"method":"initialize","params":{}}' | \
  node dist/cli.js \
    --remote-url https://api.example.com/mcp \
    --token-url https://auth.example.com/oauth/token \
    --client-id test \
    --client-secret secret

错误处理

代理返回标准JSON-RPC错误:

代码 名称 描述
-32700 解析错误 收到无效的JSON
-32600 无效请求 JSON-RPC结构无效
-32601 找不到方法 未知方法
-32602 无效的参数 无效的方法参数
-32603 内部错误 内部代理错误
-32000 令牌错误 OAuth2令牌获取失败
-32001 远程错误 远程服务器返回错误
-32002 超时错误 请求超时

安全注意事项

  • 永远不要泄露秘密:将环境变量用于客户端机密
  • 修改日志:授权标头会在日志中自动编辑
  • stdout纯度:只有MCP JSON-RPC消息会发送到stdout;所有日志都转到stderr
  • 建议使用HTTPS:在生产中对远程URL和令牌URL都使用HTTPS

贡献

  1. 分叉存储库
  2. 创建要素分支: git checkout -b feature/my-feature
  3. 进行更改
  4. 运行测试: npm test
  5. 运行门楣: npm run lint
  6. 提交您的更改: git commit -m "Add my feature"
  7. 推到分支: git push origin feature/my-feature
  8. 打开拉取请求

许可证

麻省理工学院

相关项目