-
Notifications
You must be signed in to change notification settings - Fork 20
feat: add proxy support for mcp server CF-2438 #81
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
3784188
avoid using experimental class from unidici
nedaKaighobadi c3b17b3
remove tls disabling
nedaKaighobadi afd9d67
remove unused node-fetch
nedaKaighobadi e73b228
normalize URL
nedaKaighobadi 951b71e
readd tls disabling
nedaKaighobadi File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,83 @@ | ||
| import { Agent, ProxyAgent, setGlobalDispatcher } from 'undici'; | ||
| import type { Dispatcher } from 'undici'; | ||
|
|
||
| // Matches a request hostname against the NO_PROXY / no_proxy list. | ||
| // Handles exact matches and suffix matches (e.g. "example.com" also covers "sub.example.com"). | ||
| function matchesNoProxy(hostname: string): boolean { | ||
| const noProxy = process.env.NO_PROXY ?? process.env.no_proxy; | ||
| if (!noProxy) return false; | ||
| const lower = hostname.toLowerCase(); | ||
| return noProxy | ||
| .split(',') | ||
| .map(h => h.trim().toLowerCase().replace(/^\./, '')) | ||
| .filter(Boolean) | ||
| .some(entry => entry === '*' || lower === entry || lower.endsWith('.' + entry)); | ||
| } | ||
|
|
||
| // Ensures a proxy URL has a protocol prefix. A bare host:port (e.g. "localhost:8080") | ||
| // is treated as HTTP, matching the convention used by curl and most proxy tools. | ||
| function normalizeProxyUrl(url: string): string { | ||
| return /^https?:\/\//i.test(url) ? url : `http://${url}`; | ||
| } | ||
|
|
||
| // Routes each fetch() call through HTTPS_PROXY or HTTP_PROXY based on the request | ||
| // protocol, bypassing the proxy for any host that matches NO_PROXY. | ||
| // Extends Agent so all Dispatcher methods are already implemented. | ||
| class ProxyRoutingDispatcher extends Agent { | ||
| private readonly _httpsProxy: ProxyAgent | undefined; | ||
| private readonly _httpProxy: ProxyAgent | undefined; | ||
|
|
||
| constructor(httpProxy: string | undefined, httpsProxy: string | undefined, disableSSL: boolean) { | ||
| super(); | ||
| // requestTls — TLS to the target server through the CONNECT tunnel. | ||
| // proxyTls — TLS to the proxy itself (relevant when proxy URL is HTTPS). | ||
| const tlsOpts = disableSSL ? { rejectUnauthorized: false } : undefined; | ||
| const proxyOpts = tlsOpts ? { requestTls: tlsOpts, proxyTls: tlsOpts } : {}; | ||
| this._httpsProxy = httpsProxy | ||
| ? new ProxyAgent({ uri: normalizeProxyUrl(httpsProxy), ...proxyOpts }) | ||
| : undefined; | ||
| this._httpProxy = httpProxy | ||
| ? new ProxyAgent({ uri: normalizeProxyUrl(httpProxy), ...proxyOpts }) | ||
| : undefined; | ||
| } | ||
|
|
||
| dispatch(options: Dispatcher.DispatchOptions, handler: Dispatcher.DispatchHandlers): boolean { | ||
|
nedaKaighobadi marked this conversation as resolved.
|
||
| const origin = options.origin instanceof URL ? options.origin : new URL(String(options.origin)); | ||
|
nedaKaighobadi marked this conversation as resolved.
|
||
|
|
||
| if (!matchesNoProxy(origin.hostname)) { | ||
| const proxy = | ||
| origin.protocol === 'https:' | ||
| ? (this._httpsProxy ?? this._httpProxy) | ||
| : (this._httpProxy ?? this._httpsProxy); | ||
| if (proxy) return proxy.dispatch(options, handler); | ||
| } | ||
|
|
||
| return super.dispatch(options, handler); | ||
| } | ||
|
nedaKaighobadi marked this conversation as resolved.
|
||
| } | ||
|
|
||
| /** | ||
| * Reads proxy configuration from environment variables and patches the global | ||
| * fetch dispatcher used by Node.js native fetch. | ||
| * | ||
| * Supported env vars: | ||
| * HTTPS_PROXY / https_proxy — proxy for HTTPS requests | ||
| * HTTP_PROXY / http_proxy — proxy for HTTP requests | ||
| * NO_PROXY / no_proxy — comma-separated list of hosts to bypass | ||
| * NODE_TLS_REJECT_UNAUTHORIZED=0 — disable TLS certificate verification | ||
| * | ||
| * For corporate environments that use SSL inspection (MITM proxies), prefer adding the | ||
| * corporate CA certificate to Node's trust store rather than disabling verification: | ||
| * NODE_EXTRA_CA_CERTS=/path/to/corporate-ca.pem | ||
| * Node reads this at startup and it applies to all TLS connections, including | ||
| * the inner tunnel established through a CONNECT proxy. | ||
| */ | ||
| export function applyProxyConfig(): void { | ||
|
nedaKaighobadi marked this conversation as resolved.
|
||
| const httpsProxy = process.env.HTTPS_PROXY ?? process.env.https_proxy; | ||
| const httpProxy = process.env.HTTP_PROXY ?? process.env.http_proxy; | ||
|
|
||
| if (!httpsProxy && !httpProxy) return; | ||
|
|
||
| const disableSSL = process.env.NODE_TLS_REJECT_UNAUTHORIZED === '0'; | ||
| setGlobalDispatcher(new ProxyRoutingDispatcher(httpProxy, httpsProxy, disableSSL)); | ||
| } | ||
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.
Uh oh!
There was an error while loading. Please reload this page.