toolhive icon indicating copy to clipboard operation
toolhive copied to clipboard

HttpClientBuilder.Build() ignores HTTP(S)_PROXY (auth-server OAuth/DCR calls bypass forward proxy)

Open schonmann opened this issue 2 months ago • 2 comments

Problem

  • HttpClientBuilder.Build() (pkg/networking/http_client.go) builds its http.Transport without a Proxy field.
  • That means clients it produces ignore HTTP_PROXY / HTTPS_PROXY / NO_PROXY (a zero-value http.Transport has Proxy: nil, unlike http.DefaultTransport).
  • The embedded auth server's upstream OAuth/DCR client is built this way (pkg/authserver/upstream/oauth2.go → newHTTPClientForHost → NewHostScopedClientBuilder().Build()).
  • Where the upstream is only reachable via an explicit forward proxy, the token-exchange and DCR calls bypass the proxy and fail even with the proxy env vars set.
  • The vMCP tool-forwarding path is unaffected, since it uses CloneDefaultTransportWithDialControl, which already sets Proxy: http.ProxyFromEnvironment.
flowchart LR
    Pod["MCPRemoteProxy pod<br/>(HTTPS_PROXY set)"]

    Pod -->|"tool-forwarding client<br/>CloneDefaultTransportWithDialControl"| P1["honors proxy"]
    Pod -->|"auth-server OAuth client<br/>Build → Proxy: nil"| P2["ignores proxy"]

    P1 --> FP["forward proxy"] --> UP1["upstream MCP host<br/>reachable"]
    P2 --> DIRECT["direct egress"] --> UP2["IP-allowlisted token<br/>endpoint → 403"]

    classDef ok fill:#e7f7ec,stroke:#2e7d32,color:#1b3d24;
    classDef bad fill:#fdecea,stroke:#c62828,color:#5b1a15;
    class P1,FP,UP1 ok;
    class P2,DIRECT,UP2 bad;

Expected behavior

  • Clients built by HttpClientBuilder.Build() honor HTTP(S)_PROXY / NO_PROXY, consistent with http.DefaultTransport and the vMCP path.

Proposed fix

  • Set Proxy: http.ProxyFromEnvironment on the transport in Build().
  • When the proxy address is itself private, the existing AllowPrivateIPs option covers reaching it.

I'm planning to submit a PR for this.

schonmann avatar Sep 16 '26 00:09 schonmann

Went ahead and raised a PR for this!

schonmann avatar Sep 16 '26 00:09 schonmann

EDIT: off-topic comment, traces are related to error missing http proxy configuration, likely already supported through

https://github.com/stacklok/toolhive/blob/3ca3c8f71152ad6e109dbb881be13ceb17a2cf3c/cmd/thv-operator/controllers/mcpserver_resource_overrides_test.go#L337-L370

Therefore hiding this comment, sorry for the noise


Thanks @schonmann for contributing this issue and the associated pr, looking forward to a merge and release to test it.

I'm hitting the same problem. If this can help others that would be searching from symptoms, here is the mcp inspector logs when trying to initialize and toolhive fails to validate token due to JWKS endpoint not being reachable without http proxy

body response from Toolhive:

{
  "error": "invalid_token",
  "error_description": "Invalid token: failed to parse token: token is unverifiable: error while executing keyfunc: failed to lookup JWKS: resource \"https://keycloak-00-marketplace-vc-crossplane.${redacted-ops-domain}/realms/service-manager-realm/protocol/openid-connect/certs\" is not ready"
} 
full mcp inspector logs

{
  "level": 30,
  "time": 1790784976638,
  "pid": 1,
  "hostname": "e38d521fecf9",
  "component": "InspectorClient",
  "category": "transport",
  "fetchRequest": {
    "url": "https://postgres-mcp-6a0b0b17-c3d7-433b-8c7c-8c7227db875b.${redacted-service-domain}/mcp",
    "method": "POST",
    "headers": {
      "accept": "application/json, text/event-stream",
      "authorization": "[REDACTED]",
      "content-type": "application/json"
    },
    "body": "{\"method\":\"initialize\",\"params\":{\"protocolVersion\":\"2025-11-25\",\"capabilities\":{\"sampling\":{},\"elicitation\":{\"form\":{},\"url\":{}},\"roots\":{\"listChanged\":true},\"tasks\":{\"list\":{},\"cancel\":{},\"requests\":{\"sampling\":{\"createMessage\":{}},\"elicitation\":{\"create\":{}}}},\"extensions\":{\"io.modelcontextprotocol/tasks\":{},\"io.modelcontextprotocol/ui\":{\"mimeTypes\":[\"text/html;profile=mcp-app\"],\"elicitation\":{}},\"io.modelcontextprotocol/skills\":{}}},\"clientInfo\":{\"name\":\"mcp-inspector\",\"version\":\"0.0.0\"}},\"jsonrpc\":\"2.0\",\"id\":0}"
  },
  "fetchResponse": {
    "status": 401,
    "statusText": "Unauthorized",
    "headers": {
      "connection": "keep-alive",
      "content-length": "331",
      "content-type": "application/json",
      "date": "Wed, 30 Sep 2026 16:16:16 GMT",
      "strict-transport-security": "max-age=31536000; includeSubDomains",
      "www-authenticate": "Bearer realm=\"https://keycloak-00-marketplace-vc-crossplane.${redacted-ops-domain}/realms/service-manager-realm\", resource_metadata=\"https://postgres-mcp-6a0b0b17-c3d7-433b-8c7c-8c7227db875b.${redacted-service-domain}/.well-known/oauth-protected-resource/mcp\", error=\"invalid_token\", error_description=\"failed to parse token: token is unverifiable: error while executing keyfunc: failed to lookup JWKS: resource \\\"https://keycloak-00-marketplace-vc-crossplane.${redacted-ops-domain}/realms/service-manager-realm/protocol/openid-connect/certs\\\" is not ready\""
    },
    "body": "{\"error\":\"invalid_token\",\"error_description\":\"Invalid token: failed to parse token: token is unverifiable: error while executing keyfunc: failed to lookup JWKS: resource \\\"https://keycloak-00-marketplace-vc-crossplane.${redacted-ops-domain}/realms/service-manager-realm/protocol/openid-connect/certs\\\" is not ready\"}"
  },
  "msg": "transport fetch"
}

gberche-orange avatar Sep 30 '26 17:09 gberche-orange