WIP: Add proxy support for MCP transports and OAuth calls#78
Draft
WIP: Add proxy support for MCP transports and OAuth calls#78
Conversation
The MCP SDK's StreamableHTTPClientTransport uses its own internal fetch that ignores undici's global dispatcher, so HTTP_PROXY/HTTPS_PROXY env vars were silently bypassed for all MCP server traffic, OAuth token refresh, and x402 payment signing. Add a proxy-aware fetch utility (src/lib/proxy-fetch.ts) that explicitly routes through EnvHttpProxyAgent, and inject it into: - StreamableHTTPClientTransport via its `fetch` option - OAuth discovery and token refresh calls - x402 fetch middleware base fetch https://claude.ai/code/session_01PFhD1k8hSaXZ3Crmwb9ure
Member
|
Let's merge: Into a single helper function, to have this functionality in just one place. Also, let's ensure our E2E tests cover this - they should have caught this bug before |
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 fixes HTTP proxy support (
HTTP_PROXY/HTTPS_PROXYenvironment variables) for MCP server connections, OAuth token refresh, and x402 payment signing. Previously, these operations bypassed the global proxy dispatcher because the MCP SDK'sStreamableHTTPClientTransportmanages its own HTTP connections and doesn't respect undici'ssetGlobalDispatcher().Key Changes
New module
src/lib/proxy-fetch.ts: Provides a proxy-aware fetch function that explicitly routes throughEnvHttpProxyAgent, ensuring proxy support works everywhere including inside the MCP SDK transport and OAuth utility callsinitProxyFetch(): Initializes the proxy agent with optional TLS settings (must be called once at process startup)proxyFetch(): A fetch wrapper that routes through the configured HTTP proxy, with fallback to a default agent if initialization wasn't calledUpdated
src/core/transports.ts: Injects the proxy-awareproxyFetchfunction intoStreamableHTTPClientTransportoptions so MCP server HTTP connections respect proxy environment variablesUpdated
src/lib/auth/oauth-utils.ts: Replaced nativefetchcalls withproxyFetchin OAuth discovery and token refresh operationsUpdated entry points (
src/cli/index.tsandsrc/bridge/index.ts): CallinitProxyFetch()at startup to initialize the proxy agent with the same insecure TLS settings as the global dispatcherUpdated
src/bridge/index.ts: CastproxyFetchtoFetchLikewhen creating the x402 fetch middleware to ensure payment signing requests also respect proxy settingsUpdated test (
test/unit/lib/auth/oauth-utils.test.ts): Changed mock to spy onproxyFetchModule.proxyFetchinstead of globalfetchImplementation Details
The solution uses undici's
EnvHttpProxyAgentwhich respectsHTTP_PROXY,HTTPS_PROXY, andNO_PROXYenvironment variables. By explicitly passing this agent as thedispatcheroption to fetch calls, we ensure proxy routing works for all HTTP operations regardless of how the HTTP client is configured internally.https://claude.ai/code/session_01PFhD1k8hSaXZ3Crmwb9ure