MCP代理
启用使用SSE传输与远程MCP服务器交互的功能,而不是使用STDIO,以增强通信能力。
MCP 服务配置
复制以下 JSON 到 OPClaw 或其他 MCP 客户端的配置文件中即可使用
{
"mcpServers": {
"mcp-proxy": {
"args": [
"http://example.io/sse"
],
"command": "mcp-proxy",
"env": {
"API_ACCESS_TOKEN": "access-token"
}
}
}
}
该服务需要配置环境变量:SSE_URL
服务介绍
mcp-proxy
关于
mcp-proxy 是一个允许您在服务器传输之间切换的工具。支持两种模式:
- stdio 到 SSE
- SSE 到 stdio
1. stdio 到 SSE
运行一个从 stdio 连接到远程 SSE 服务器的代理服务器。
此模式允许像 Claude Desktop 这样的客户端通过 SSE 与远程服务器通信,即使它不原生支持 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 Server SSE 端点的 URL 作为程序的第一个参数传递。
参数
| 名称 | 必需 | 描述 | 示例 |
|---|---|---|---|
command_or_url |
是 | 要连接的 MCP 服务器 SSE 端点 | http://example.io/sse |
--headers |
否 | 用于 MCP 服务器 SSE 连接的标头 | Authorization 'Bearer my-secret-access-token' |
环境变量
| 名称 | 必需 | 描述 | 示例 |
|---|---|---|---|
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 |
--sse-port |
否,默认为随机可用 | SSE 服务器监听的端口号 | 8080 |
--sse-host |
否,默认为 127.0.0.1 |
SSE 服务器将要监听的主机 IP 地址 | 0.0.0.0 |
--env |
否 | 传递给 MCP stdio 服务器的额外环境变量 | FOO=BAR |
--pass-environment |
否 | 在启动服务器时传递所有环境变量 | --no-pass-environment |
--allow-origin |
否 | 在启动服务器时允许跨源请求 | --allow-cors "*" |
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
mcp-proxy --sse-port=8080 uvx mcp-server-fetch
# Start the MCP server behind the proxy with a custom host and port
mcp-proxy --sse-host=0.0.0.0 --sse-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
mcp-proxy --sse-port=8080 -- uvx mcp-server-fetch --user-agent=YourUserAgent
这将启动一个可以在 http://127.0.0.1:8080/sse 连接的 MCP 服务器。
安装
通过 Smithery 安装
要通过 Smithery 自动安装适用于 Claude Desktop 的 MCP Proxy:
npx -y @smithery/cli install mcp-proxy --client claude
通过 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
[!NOTE]
如果你已经安装了服务器,可以使用uv tool upgrade --reinstall命令更新它。
[!NOTE]
如果你想删除服务器,可以使用uv tool uninstall mcp-proxy命令。
作为容器安装
从版本 0.3.2 开始,可以拉取并运行相应的容器镜像:
docker run -t ghcr.io/sparfenyuk/mcp-proxy:v0.3.2-alpine --help
扩展容器镜像
您可以扩展 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 [ "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 --sse-port=8096 --sse-host 0.0.0.0 uvx mcp-server-fetch"
[!NOTE]
不要忘记设置--pass-environment参数,否则您会遇到“在托管安装或搜索路径中找不到解释器”的错误。
命令行参数
usage: mcp-proxy [-h] [-H KEY VALUE] [-e KEY VALUE] [--pass-environment | --no-pass-environment] [--sse-port SSE_PORT] [--sse-host SSE_HOST]
[--allow-origin ALLOW_ORIGIN [ALLOW_ORIGIN ...]]
[command_or_url] [args ...]
Start the MCP proxy in one of two possible modes: as an SSE or stdio client.
positional arguments:
command_or_url Command or URL to connect to. When a URL, will run an SSE client, otherwise will run the given command and connect as a stdio client. See corresponding options for more details.
options:
-h, --help show this help message and exit
SSE client options:
-H KEY VALUE, --headers KEY VALUE
Headers to pass to the SSE server. Can be used multiple times.
stdio client options:
args Any extra arguments to the command to spawn the server
-e KEY VALUE, --env KEY VALUE
Environment variables used when spawning the server. Can be used multiple times.
--pass-environment, --no-pass-environment
Pass through all environment variables when spawning the server.
SSE server options:
--sse-port SSE_PORT Port to expose an SSE server on. Default is a random port
--sse-host SSE_HOST Host to expose an SSE server on. Default is 127.0.0.1
--allow-origin ALLOW_ORIGIN [ALLOW_ORIGIN ...]
Allowed origins for the SSE server. Can be used multiple times. Default is no CORS allowed.
Examples:
mcp-proxy http://localhost:8080/sse
mcp-proxy --headers Authorization 'Bearer YOUR_TOKEN' http://localhost:8080/sse
mcp-proxy --sse-port 8080 -- your-command --arg1 value1 --arg2 value2
mcp-proxy your-command --sse-port 8080 -e KEY VALUE -e ANOTHER_KEY ANOTHER_VALUE
mcp-proxy your-command --sse-port 8080 --allow-origin='*'
测试
通过与 mcp-server-fetch 服务器一起运行来检查 mcp-proxy 服务器。您可以使用 inspector 工具 来测试目标服务器。
# Run the stdio server called mcp-server-fetch behind the proxy over SSE
mcp-proxy --sse-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