Skip to content

MCP HTTP 客户端忽略 NO_PROXY #171

Closed

Description

@astarforbae

问题描述

a3s-code 8.6.0(e42f0c70,reqwest 0.12.28)注册 HTTP MCP 时不遵守 NO_PROXY / no_proxy。

streamable-http 和 http+sse 都用 build_reqwest_client 建客户端(core/src/mcp/transport/streamable_http.rs、core/src/mcp/transport/http_sse.rs)。该函数(core/src/llm/http.rs)从 Client::builder().no_proxy() 起步,再按 HTTP_PROXY / HTTPS_PROXY 挂上 reqwest::Proxy::http 和 Proxy::https。两个代理都没有排除列表。explicit_proxy_config_from_env 只读取 http_proxy、HTTP_PROXY、https_proxy、HTTPS_PROXY。

reqwest 0.12.28 里 Proxy::new 把 no_proxy 留成 None。这个取值下,写在 NO_PROXY 里的主机仍会被送进已配置的代理。函数注释写明客户端构造会遵守标准代理环境变量;NO_PROXY 属于这些变量,当前实现把它丢掉了。

MCP OAuth(core/src/mcp/oauth.rs)和模型 HTTP 调用用的 ReqwestHttpClient 也走同一个 builder。

streamable-http 连接失败时表现为:

Failed to connect MCP server: MCP server returned HTTP 502 Bad Gateway:

core/src/mcp/transport/streamable_http.rs 把状态格式化成 MCP server returned HTTP {}: {}。core/src/agent_api/session_extensions.rs 再包成 Failed to connect MCP server: {e}。响应体为空时,冒号后面就没有内容。http+sse 使用同一套状态格式。

做过的测试

测试版本:a3s-code 8.6.0。

  1. 核对 e42f0c70 上的 build_reqwest_client 和 explicit_proxy_config_from_env。代理 URL 变量被复制到 Proxy::http / Proxy::https。全程没有调用 NoProxy::from_env()。仓库内测试 test_explicit_proxy_config_from_env_prefers_lowercase_vars、test_explicit_proxy_config_from_env_falls_back_to_http_for_https、test_build_reqwest_client_accepts_proxy_env_urls 覆盖的是代理 URL 的选择,以及客户端能否建出来。clear_proxy_env 只删除那四个代理 URL 变量。现有测试没有设置 NO_PROXY,也没有检查被点名的主机是否绕过代理。

  2. 在进程环境设置了 HTTP_PROXY / HTTPS_PROXY 的情况下,连接 http://playwright-mcp:3080 上的 streamable-http MCP 服务器。session.add_mcp_server 在任何模型调用之前失败:

RuntimeError: Tool error: playwright: Failed to connect MCP server: MCP server returned HTTP 502 Bad Gateway:
  1. 另一次检查没有启动 a3s-code,也没有调用模型:经同一个代理对 http://playwright-mcp:3080/ 发 HTTP GET。结果是 status 502,reason Bad Gateway,body 长度 0,Content-Length: 0。这与 MCP 错误字符串里的空响应体一致。

预期行为

NO_PROXY 或 no_proxy 列出某台主机时,发往该主机的 MCP HTTP 请求应直接连接。列表之外的主机仍使用 HTTP_PROXY / HTTPS_PROXY。

Proxy::http 和 Proxy::https 都应带上环境变量里的排除列表:

builder = builder.proxy(
    reqwest::Proxy::http(http_proxy)
        .with_context(|| format!("Invalid HTTP proxy URL: {http_proxy}"))?
        .no_proxy(reqwest::NoProxy::from_env()),
);

Proxy::https 同样加上 .no_proxy(reqwest::NoProxy::from_env())。NoProxy::from_env() 返回 Option<NoProxy>,这正是 Proxy::no_proxy 接受的类型。全部 MCP HTTP 传输都经过 build_reqwest_client,这一处修改同时覆盖 streamable-http、http+sse 和 MCP OAuth。

Activity

  1. ZhiXiao-Lin commented on Sep 25, 2026

    @ZhiXiao-Lin

    Fixed: build_reqwest_client now attaches NoProxy::from_env() to both Proxy::http and Proxy::https. Tests cover building with HTTP_PROXY + NO_PROXY and clearing no_proxy/NO_PROXY in the proxy env harness.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions