Add proxy-aware fetch for HTTP_PROXY/HTTPS_PROXY support#87
Merged
Conversation
HTTP_PROXY/HTTPS_PROXY environment variables were not respected by the MCP SDK's StreamableHTTPClientTransport (which manages its own HTTP connections) or by OAuth token discovery/refresh calls. Consolidate proxy setup into a single src/lib/proxy.ts module that both sets the global undici dispatcher and exports a proxy-aware fetch function for code that bypasses the global dispatcher (MCP SDK transport, OAuth utils, x402 middleware). https://claude.ai/code/session_01RiDkNSfSQcdczmcGcLHYJM
- Unit tests verify proxyFetch is injected into StreamableHTTPClientTransport by default, and that custom fetch (e.g. x402 middleware) takes priority - Add request counting to E2E proxy server (control API) and framework helpers (proxy_request_count, proxy_reset_count) for future use - Fix transports.ts type error with exactOptionalPropertyTypes https://claude.ai/code/session_01RiDkNSfSQcdczmcGcLHYJM
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
This PR adds comprehensive HTTP proxy support for all network requests in the application, including MCP server connections, OAuth token refresh, and x402 payment signing. Previously, the MCP SDK transport and OAuth calls bypassed the global dispatcher and ignored
HTTP_PROXY/HTTPS_PROXYenvironment variables.Key Changes
New
src/lib/proxy.tsmodule: Provides two functions for proxy-aware networking:initProxy(): Initializes the global undici dispatcher with proxy support from environment variables. Must be called once at process startup.proxyFetch(): A fetch function that explicitly routes through the configured HTTP proxy, for use in code that bypasses the global dispatcher (e.g., MCP SDK transport, OAuth calls).Updated CLI and bridge entry points: Both
src/cli/index.tsandsrc/bridge/index.tsnow callinitProxy()instead of manually creating and settingEnvHttpProxyAgent.MCP transport proxy support:
src/core/transports.tsnow passesproxyFetchto theStreamableHTTPClientTransportso it respects proxy environment variables.OAuth proxy support:
src/lib/auth/oauth-utils.tsnow usesproxyFetchfor OAuth discovery and token refresh endpoints.x402 middleware integration:
src/bridge/index.tspassesproxyFetchto the x402 fetch middleware for payment signing requests.Test updates: Updated
test/unit/lib/auth/oauth-utils.test.tsto mockproxyFetchinstead of globalfetch.Implementation Details
The solution addresses the limitation that undici's
setGlobalDispatcher()is not honored by libraries that manage their own HTTP connections. By providing an explicitproxyFetchfunction, code can opt-in to proxy support even when the global dispatcher is bypassed. The--insecureflag is also supported to disable TLS certificate verification for self-signed certificates.https://claude.ai/code/session_01RiDkNSfSQcdczmcGcLHYJM