MCP代理服务

一个Go实现的代理服务,支持MCP客户端通过OAuth连接到远程MCP服务器,提供双模式操作(Stdio或HTTP/SSE服务器)和灵活的传输策略。

  • by mikluko
  • Go
  • 本地部署
  • 1 星标
  • 更新于
在 GitHub 查看 mikluko/mcp-proxy

使用说明

mcp代理

代理的Go实现,通过OAuth支持将MCP客户端连接到远程MCP服务器。

特性

  • 双模式操作:Stdio(默认)或HTTP/SSE服务器
  • OAuth 2.0:完全支持PKCE、动态客户端注册和令牌刷新
  • 运输灵活性:HTTP和SSE,具有可配置的回退策略
  • 工具过滤:阻止特定工具暴露给客户端

安装

自制(macOS/Linux)

brew install mikluko/tap/mcp-proxy

去

go install github.com/mikluko/mcp-proxy/cmd/mcp-proxy@latest

二进制版本

下载自 GitHub发布.

用法

标准模式(默认)

通过stdio通信的MCP客户端的标准模式(Claude Code、Claude Desktop、Cursor、Windsurf)。

克劳德代码

claude mcp add remote-example mcp-proxy https://remote.mcp.server/sse

克劳德桌面,光标,风帆

{
  "mcpServers": {
    "remote-example": {
      "command": "mcp-proxy",
      "args": [
        "https://remote.mcp.server/sse"
      ]
    }
  }
}

HTTP/SSE服务器模式

作为HTTP服务器运行,通过SSE接受MCP客户端连接。适用于:

  • 基于Web的MCP客户端
  • 在多个客户端之间共享单个上游连接
  • stdio不可用的环境
# Listen on port 8080
mcp-proxy https://remote.mcp.server/sse --listen :8080

# Listen on specific interface
mcp-proxy https://remote.mcp.server/sse --listen localhost:8080

客户端通过以下方式连接:

  • SSE端点: GET /sse -为服务器建立SSE流→客户端消息
  • 消息端点: POST /message -发送客户端→服务器消息

自定义头

{
  "mcpServers": {
    "remote-example": {
      "command": "mcp-proxy",
      "args": [
        "https://remote.mcp.server/sse",
        "--header",
        "Authorization:Bearer ${AUTH_TOKEN}"
      ],
      "env": {
        "AUTH_TOKEN": "your-token"
      }
    }
  }
}

旗帜

标志 描述 默认值
--listen, -l HTTP模式的侦听地址(例如:8080) -
--header, -H 自定义标题(KEY:VALUE) -
--transport 上游传输策略(http优先、sse优先、仅http、仅sse) http优先
--host OAuth回调主机名 本地主机
--allow-http 允许非HTTPS连接 false
--log-level 日志级别(调试、信息、警告、错误) info
--enable-proxy 使用HTTP_PROXY/HTTPS_PROXY false
--ignore-tool 忽略匹配模式的工具(通配符) -
--auth-timeout OAuth回调超时(秒) 30
--resource OAuth资源参数 -
--static-oauth-client-metadata 静态OAuth客户端元数据(JSON或@file) -
--static-oauth-client-info 静态OAuth客户端信息(JSON或@文件) -

运输策略

控制mcp代理如何连接到上游服务器:

  • http-first (默认):尝试HTTP,在404时回退到SSE
  • sse-first:尝试SSE,在405上回退到HTTP
  • http-only/sse-only:无退路

多个实例

使用 --resource 要隔离OAuth会话,请执行以下操作:

{
  "mcpServers": {
    "tenant1": {
      "command": "mcp-proxy",
      "args": [
        "https://mcp.example.com/sse",
        "--resource",
        "https://tenant1.example.com/"
      ]
    },
    "tenant2": {
      "command": "mcp-proxy",
      "args": [
        "https://mcp.example.com/sse",
        "--resource",
        "https://tenant2.example.com/"
      ]
    }
  }
}

缓存位置

OAuth令牌和客户端注册存储在:

  • Linux: ~/.cache/mcp-proxy/
  • macOS: ~/Library/Caches/mcp-proxy/
  • 视窗: %LocalAppData%\mcp-proxy\

要清除身份验证状态,请执行以下操作:

rm -rf ~/.cache/mcp-proxy # Linux
rm -rf ~/Library/Caches/mcp-proxy # macOS

建筑

                     ┌─────────────────────────────────────────┐
                     │              mcp-proxy                  │
                     │                                         │
  ┌──────────┐       │  ┌────────────┐      ┌────────────────┐ │       ┌──────────┐
  │  Client  │──────▶│  │ Downstream │──────│     Proxy      │─│──────▶│ Upstream │
  │ (stdio   │       │  │ Transport  │      │   (filtering,  │ │       │  Server  │
  │  or SSE) │◀──────│  │            │◀─────│  modification) │◀│───────│          │
  └──────────┘       │  └────────────┘      └────────────────┘ │       └──────────┘
                     │     stdio or              HTTP or       │
                     │     HTTP/SSE                SSE         │
                     └─────────────────────────────────────────┘

目录结构

cmd/
└── mcp-proxy/          # Main CLI binary

internal/
├── auth/               # OAuth 2.0 implementation (PKCE, tokens, callback server)
├── config/             # Cross-platform cache directory management
├── proxy/              # Bidirectional JSON-RPC proxy with tool filtering
├── server/             # HTTP/SSE server for downstream client connections
├── stdio/              # Stdio transport for downstream client connections
└── transport/          # HTTP and SSE transports for upstream connections

pkg/
└── jsonrpc/            # JSON-RPC 2.0 message types

OAuth流

  1. auth.Provider.Initialize() 加载现有令牌或准备进行身份验证
  2. 401:启动回调服务器,打开浏览器到授权URL
  3. 回调接收代码,通过PKCE交换令牌
  4. 令牌持久化到缓存目录,到期时自动刷新

发布

通过goreleaser+GitHub Actions发布。创建:

  • 多平台二进制文件(linux/darwin/windows×amd64/arm64)
  • 自制配方 mikluko/homebrew-tap

现有技术

许可证

麻省理工学院