mcpField
M
mcp-proxy

mcp-proxy

本地服务
github.com/sparfenyuk开发工具2.8k2 周前更新

关于 mcp-proxy

mcp-proxy 是一个在 stdio 与 SSE/Streamable HTTP 传输之间转换的 MCP 代理工具。它适用于客户端只支持 stdio、但需要连接远程服务,或需要通过网络暴露本地 stdio 服务的场景。

主要功能

  1. 01stdio 连接远程 SSE 或 Streamable HTTP 服务
  2. 02将本地 stdio 服务通过 SSE 暴露
  3. 03支持请求头与多种 OAuth/认证参数
  4. 04可配置多个命名服务
  5. 05提供 PyPI 安装与容器镜像

适用场景

  1. 01让 Claude Desktop 通过 stdio 连接远程 MCP 服务
  2. 02通过网络端口共享本地 stdio MCP 服务
  3. 03在 Docker Compose 中部署代理服务

MCP 相关

mcp-proxy 作为传输层桥接器,一侧连接 MCP 客户端,另一侧连接远程或本地 MCP 服务,并可按命名服务路径分别暴露多个服务。

配置

本地服务的配置在各客户端中通用,复制后粘贴到客户端的 MCP 配置文件即可。

暂未收录安装方式

我们还没有获取到这个服务的安装包或远程地址,请按项目 README 中的说明安装。

代码仓库

工具

还没有获取到工具列表

本地服务需要在你的电脑上运行后才能读取工具,维护者暂未提供工具列表。

详细介绍

mcp-proxy

GitHub License PyPI - Python Version PyPI - Downloads codecov

关于

mcp-proxy 是一种可在不同服务传输方式之间切换的工具。支持两种模式:

  1. stdio 转 SSE/StreamableHTTP
  2. SSE 转 stdio

1. stdio 转 SSE/StreamableHTTP

运行一个通过 stdio 连接到远程 SSE 服务的代理服务。

此模式允许 Claude Desktop 等客户端通过 SSE 与远程服务通信,即使原生并不支持这种方式。

graph LR
    A["Claude Desktop"] <--> |stdio| B["mcp-proxy"]
    B <--> |SSE| C["External MCP Server"]

    style A fill:#ffe6f9,stroke:#333,color:black,stroke-width:2px
    style B fill:#e6e6ff,stroke:#333,color:black,stroke-width:2px
    style C fill:#e6ffe6,stroke:#333,color:black,stroke-width:2px
1.1 配置

此模式要求将 MCP 服务的 SSE 端点 URL 作为程序的第一个参数。如果服务使用 Streamable HTTP 传输,请传入 --transport=streamablehttp,确保在 mcp-proxy 端启用该传输方式。

参数

名称必填描述示例
command_or_url是要连接的 MCP 服务 SSE 端点http://example.io/sse
--headers否用于 MCP 服务 SSE 连接的请求头Authorization 'Bearer my-secret-access-token'
--transport否决定连接 MCP 服务时使用的传输协议。可以是 'sse' 或 'streamablehttp'streamablehttp
--client-id否用于身份验证的 OAuth2 客户端 IDyour_client_id
--client-secret否用于身份验证的 OAuth2 客户端密钥your_client_secret
--token-url否用于身份验证的 OAuth2 令牌端点 URLhttps://auth.example.com/oauth/token
--username否用于身份验证的用户名user
--password否用于身份验证的密码password
--digest否使用摘要身份验证

环境变量

名称必填描述示例
API_ACCESS_TOKEN否可替代 --headers Authorization 'Bearer <API_ACCESS_TOKEN>' 使用YOUR_TOKEN
1.2 使用示例

mcp-proxy 应由 MCP 客户端启动,因此必须相应地进行配置。

对于 Claude Desktop,配置项可以如下所示:

{
  "mcpServers": {
    "mcp-proxy": {
      "command": "mcp-proxy",
      "args": [
        "http://example.io/sse"
      ],
      "env": {
        "API_ACCESS_TOKEN": "access-token"
      }
    }
  }
}

2. SSE 到 stdio

运行代理服务,公开一个 SSE 服务,并连接到本地 stdio 服务。

这允许远程连接到本地 stdio 服务。mcp-proxy 会打开一个端口以侦听 SSE 请求, 并启动一个处理 MCP 请求的本地 stdio 服务。

graph LR
    A["LLM Client"] <-->|SSE| B["mcp-proxy"]
    B <-->|stdio| C["Local MCP Server"]

    style A fill:#ffe6f9,stroke:#333,color:black,stroke-width:2px
    style B fill:#e6e6ff,stroke:#333,color:black,stroke-width:2px
    style C fill:#e6ffe6,stroke:#333,color:black,stroke-width:2px
2.1 配置

此模式要求设置 --sse-port 参数。可以设置 --sse-host 参数,以指定 SSE 服务监听的主机 IP 地址。可以使用 --env 参数向本地 stdio 服务传递其他环境变量。本地 stdio 服务的命令行参数必须放在分隔符 -- 之后。

参数

名称必需说明示例
command_or_url是启动 MCP stdio 服务的命令uvx mcp-server-fetch
--port否,随机可用端口MCP 服务监听的端口8080
--host否,默认为 127.0.0.1MCP 服务监听的主机 IP 地址0.0.0.0
--env否要传递给 MCP stdio 服务的其他环境变量。可多次使用。FOO BAR
--cwd否要传递给 MCP stdio 服务进程的工作目录。/tmp
--pass-environment否启动服务时传递所有环境变量--no-pass-environment
--allow-origin否SSE 服务允许的来源。可多次使用。默认不允许 CORS。--allow-origin "*"
--expose-header否添加到 Access-Control-Expose-Headers 的标头。可多次使用。默认为 mcp-session-id。--expose-header Custom-Header
--stateless否为 streamable http 传输启用无状态模式。默认值为 False--no-stateless
--named-server NAME COMMAND_STRING否定义一个命名的 stdio 服务。--named-server fetch 'uvx mcp-server-fetch'
--named-server-config FILE_PATH否定义命名 stdio 服务的 JSON 文件路径。--named-server-config /path/to/servers.json
--sse-port (deprecated)否,随机可用端口SSE 服务监听的端口8080
--sse-host (deprecated)否,默认为 127.0.0.1SSE 服务监听的主机 IP 地址0.0.0.0
2.2 使用示例

启动监听端口 8080 并连接到本地 MCP 服务的 mcp-proxy 服务:

# Start the MCP server behind the proxy
mcp-proxy uvx mcp-server-fetch

# Start the MCP server behind the proxy with a custom port
# (deprecated) mcp-proxy --sse-port=8080 uvx mcp-server-fetch
mcp-proxy --port=8080 uvx mcp-server-fetch

# Start the MCP server behind the proxy with a custom host and port
# (deprecated) mcp-proxy --sse-host=0.0.0.0 --sse-port=8080 uvx mcp-server-fetch
mcp-proxy --host=0.0.0.0 --port=8080 uvx mcp-server-fetch

# Start the MCP server behind the proxy with a custom user agent
# Note that the `--` separator is used to separate the `mcp-proxy` arguments from the `mcp-server-fetch` arguments
# (deprecated) mcp-proxy --sse-port=8080 -- uvx mcp-server-fetch --user-agent=YourUserAgent
mcp-proxy --port=8080 -- uvx mcp-server-fetch --user-agent=YourUserAgent

# Start multiple named MCP servers behind the proxy
mcp-proxy --port=8080 --named-server fetch 'uvx mcp-server-fetch' --named-server fetch2 'uvx mcp-server-fetch'

# Start multiple named MCP servers using a configuration file
mcp-proxy --port=8080 --named-server-config ./servers.json

# Start the MCP server with CORS enabled and custom exposed headers
mcp-proxy --port=8080 --allow-origin='*' --expose-header Custom-Header uvx mcp-server-fetch

命名服务

  • NAME 用于 URL 路径 /servers/NAME/。
  • COMMAND_STRING 是启动服务的命令(例如,'uvx mcp-server-fetch')。
    • 可以多次使用。
    • 如果使用 --named-server-config,则会忽略此参数。
  • FILE_PATH - 如果提供,该文件将成为命名服务的唯一来源,并且会忽略 --named-server CLI 参数。

如果指定了默认服务(即未使用 --named-server 或 --named-server-config 的 command_or_url 参数),则可通过根路径访问(例如,http://127.0.0.1:8080/sse)。

命名服务(无论通过 --named-server 还是 --named-server-config 定义)都可通过 /servers/<server-name>/ 访问(例如,http://127.0.0.1:8080/servers/fetch1/sse)。 /status 端点提供全局状态。

--named-server-config 的 JSON 配置文件格式:

JSON 文件应遵循以下结构:

{
  "mcpServers": {
    "fetch": {
      "disabled": false,
      "timeout": 60,
      "command": "uvx",
      "args": [
        "mcp-server-fetch"
      ],
      "transportType": "stdio"
    },
    "github": {
      "timeout": 60,
      "command": "npx",
      "args": [
        "-y",
        "@modelcontextprotocol/server-github"
      ],
      "env": {
        "GITHUB_PERSONAL_ACCESS_TOKEN": "<YOUR_TOKEN>"
      },
      "transportType": "stdio"
    }
  }
}
  • mcpServers:一个字典,其中每个键都是服务名称(用于 URL 路径,例如 /servers/fetch/),对应的值是定义该服务的对象。
  • command:(必填)要为 stdio 服务执行的命令。
  • args:(可选)命令的参数列表。默认为空列表。
  • enabled:(可选)如果为 false,则会跳过此服务定义。默认为 true。
  • timeout 和 transportType:这些字段存在于标准 MCP 客户端配置中,但 mcp-proxy 加载命名服务时目前会忽略它们。传输类型默认是 "stdio"。

安装

通过 PyPI 安装

此软件包的稳定版本可在 PyPI 仓库获取。可使用以下命令安装:

# Option 1: With uv (recommended)
uv tool install mcp-proxy

# Option 2: With pipx (alternative)
pipx install mcp-proxy

安装后,可使用 mcp-proxy 命令运行服务。请参阅上文各模式的配置选项。

通过 Github 仓库安装(最新版本)

可使用以下命令从 git 仓库安装此软件包的最新版本:

uv tool install git+https://github.com/sparfenyuk/mcp-proxy

说明

如果已安装该服务,可使用 uv tool upgrade --reinstall 命令更新。

说明

如果要删除该服务,请使用 uv tool uninstall mcp-proxy 命令。

作为容器安装

从 0.3.2 版本开始,可以拉取并运行相应的容器镜像。 发布的镜像以多平台清单的形式提供在 GHCR 和 Docker Hub 上,支持 linux/amd64 和 linux/arm64; Docker 会根据主机架构自动选择匹配的镜像。

docker run --rm -t ghcr.io/sparfenyuk/mcp-proxy:v0.12.0 --help
docker run --rm -t sparfenyuk/mcp-proxy:v0.12.0 --help
故障排除
  • 问题:Claude Desktop 无法启动服务:日志中出现 ENOENT 错误代码

    解决方案:尝试使用二进制文件的完整路径。为此,请打开终端并运行命令which mcp-proxy( macOS、Linux)或 where.exe mcp-proxy(Windows)。然后,将输出路径用作 'command' 属性的值:

      "fetch": {
        "command": "/full/path/to/bin/mcp-proxy",
        "args": [
          "http://localhost:8932/sse"
        ]
      }
    

扩展容器镜像

你可以扩展 mcp-proxy 容器镜像,以包含其他可执行文件。例如,默认不包含 uv,但你可以创建包含它的自定义镜像:

# file: mcp-proxy.Dockerfile

FROM ghcr.io/sparfenyuk/mcp-proxy:latest

# Install the 'uv' package
RUN python3 -m ensurepip && pip install --no-cache-dir uv

ENV PATH="/usr/local/bin:$PATH" \
    UV_PYTHON_PREFERENCE=only-system

ENTRYPOINT ["catatonit", "--", "mcp-proxy"]

Docker Compose 设置

使用自定义 Dockerfile,可以在 Docker Compose 文件中定义服务:

services:
  mcp-proxy-custom:
    build:
      context: .
      dockerfile: mcp-proxy.Dockerfile
    network_mode: host
    restart: unless-stopped
    ports:
      - 8096:8096
    command: "--pass-environment --port=8096 --sse-host 0.0.0.0 uvx mcp-server-fetch"

说明

别忘了设置 --pass-environment 参数,否则会出现“在受管理的安装或搜索路径中找不到解释器”错误

命令行参数

usage: mcp-proxy [-h] [--version] [-H KEY VALUE]
                 [--transport {sse,streamablehttp}] [--verify-ssl [VALUE]]
                 [--no-verify-ssl] [-e KEY VALUE] [--cwd CWD]
                 [--client-id CLIENT_ID] [--client-secret CLIENT_SECRET] [--token-url TOKEN_URL]
                 [--pass-environment | --no-pass-environment]
                 [--log-level LEVEL] [--debug | --no-debug]
                 [--named-server NAME COMMAND_STRING]
                 [--named-server-config FILE_PATH] [--port PORT] [--host HOST]
                 [--stateless | --no-stateless] [--sse-port SSE_PORT]
                 [--sse-host SSE_HOST]
                 [--allow-origin ALLOW_ORIGIN [ALLOW_ORIGIN ...]]
                 [--expose-header HEADER]
                 [command_or_url] [args ...]

Start the MCP proxy in one of two possible modes: as a client or a server.

positional arguments:
  command_or_url        Command or URL to connect to. When a URL, will run an SSE/StreamableHTTP client. Otherwise, if --named-server is not used, this will be the command for the default stdio client. If --named-server is used, this argument is ignored for stdio mode unless no default server is desired. See corresponding options for more details.

options:
  -h, --help            show this help message and exit
  --version             Show the version and exit

SSE/StreamableHTTP client options:
  -H, --headers KEY VALUE
                        Headers to pass to the SSE server. Can be used multiple times.
  --transport {sse,streamablehttp}
                        The transport to use for the client. Default is SSE.
  --verify-ssl [VALUE]  Control SSL verification when acting as a client. Use without a value to force verification, pass 'false' to disable, or provide a path to a PEM bundle.
  --no-verify-ssl       Disable SSL verification (alias for --verify-ssl false).
  --client-id CLIENT_ID
                        OAuth2 client ID for authentication
  --client-secret CLIENT_SECRET
                        OAuth2 client secret for authentication
  --token-url TOKEN_URL
                        OAuth2 token URL for authentication
  --username USERNAME   Username for authentication
  --password PASSWORD   Password for authentication
  --digest              Use digest authentication

stdio client options:
  args                  Any extra arguments to the command to spawn the default server. Ignored if only named servers are defined.
  -e, --env KEY VALUE   Environment variables used when spawning the default server. Can be used multiple times. For named servers, environment is inherited or passed via --pass-environment.
  --cwd CWD             The working directory to use when spawning the default server process. Named servers inherit the proxy's CWD.
  --pass-environment, --no-pass-environment
                        Pass through all environment variables when spawning all server processes.
  --log-level LEVEL     Set the log level. Default is INFO.
  --debug, --no-debug   Enable debug mode with detailed logging output. Equivalent to --log-level DEBUG. If both --debug and --log-level are provided, --debug takes precedence.
  --named-server NAME COMMAND_STRING
                        Define a named stdio server. NAME is for the URL path /servers/NAME/. COMMAND_STRING is a single string with the command and its arguments (e.g., 'uvx mcp-server-fetch --timeout 10'). These servers inherit the proxy's CWD and environment from --pass-environment.
  --named-server-config FILE_PATH
                        Path to a JSON configuration file for named stdio servers. If provided, this will be the exclusive source for named server definitions, and any --named-server CLI arguments will be ignored.

SSE server options:
  --port PORT           Port to expose an SSE server on. Default is a random port
  --host HOST           Host to expose an SSE server on. Default is 127.0.0.1
  --stateless, --no-stateless
                        Enable stateless mode for streamable http transports. Default is False
  --sse-port SSE_PORT   (deprecated) Same as --port
  --sse-host SSE_HOST   (deprecated) Same as --host
  --allow-origin ALLOW_ORIGIN [ALLOW_ORIGIN ...]
                        Allowed origins for the SSE server. Can be used multiple times. Default is no CORS allowed.
  --expose-header HEADER
                        Headers to expose via Access-Control-Expose-Headers. Defaults to 'Mcp-Session-Id'. Can be used multiple times.

Examples:
  mcp-proxy http://localhost:8080/sse
  mcp-proxy --no-verify-ssl https://server.local/sse
  mcp-proxy --transport streamablehttp http://localhost:8080/mcp
  mcp-proxy --transport streamablehttp --username user --password pass https://server.local/mcp
  mcp-proxy --headers Authorization 'Bearer YOUR_TOKEN' http://localhost:8080/sse
  mcp-proxy --client-id CLIENT_ID --client-secret CLIENT_SECRET --token-url https://auth.example.com/token http://localhost:8080/sse
  mcp-proxy --port 8080 -- your-command --arg1 value1 --arg2 value2
  mcp-proxy --named-server fetch 'uvx mcp-server-fetch' --port 8080
  mcp-proxy your-command --port 8080 -e KEY VALUE -e ANOTHER_KEY ANOTHER_VALUE
  mcp-proxy your-command --port 8080 --allow-origin='*'
  mcp-proxy your-command --port 8080 --allow-origin='*' --expose-header Custom-Header
配置文件示例
{
  "mcpServers": {
    "fetch": {
      "enabled": true,
      "timeout": 60,
      "command": "uvx",
      "args": [
        "mcp-server-fetch"
      ],
      "transportType": "stdio"
    },
    "github": {
      "timeout": 60,
      "command": "npx",
      "args": [
        "-y",
        "@modelcontextprotocol/server-github"
      ],
      "env": {
        "GITHUB_PERSONAL_ACCESS_TOKEN": "<YOUR_TOKEN>"
      },
      "transportType": "stdio"
    }
  }
}

测试

通过使用 mcp-server-fetch 服务运行 mcp-proxy,检查该服务。你可以使用 inspector 工具测试目标服务。

# Run the stdio server called mcp-server-fetch behind the proxy over SSE
mcp-proxy --port=8080 uvx mcp-server-fetch &

# Connect to the SSE proxy server spawned above using another instance of mcp-proxy given the URL of the SSE server
mcp-proxy http://127.0.0.1:8080/sse

# Send CTRL+C to stop the second server

# Bring the first server to the foreground
fg

# Send CTRL+C to stop the first server

评论

支持 Markdown

第一次接触 MCP?

从协议是什么,到在客户端里装上第一个服务,这几条回答覆盖了大多数入门问题。

一个开放协议,规定了 AI 应用如何以统一方式连接外部工具与数据源。客户端负责与模型对话,MCP 服务负责提供工具、资源和提示模板,两者通过标准消息通信,同一个服务可以被不同客户端复用。