Fix IDE Companion Connection in Proxy Environments (#6308)

Co-authored-by: Jacob Richman <jacob314@gmail.com>
This commit is contained in:
Blackoutta
2025-08-20 08:32:08 +08:00
committed by GitHub
parent 6732665a08
commit d587c6f104
3 changed files with 92 additions and 3 deletions

View File

@@ -20,6 +20,7 @@ import { Client } from '@modelcontextprotocol/sdk/client/index.js';
import { StreamableHTTPClientTransport } from '@modelcontextprotocol/sdk/client/streamableHttp.js';
import * as os from 'node:os';
import * as path from 'node:path';
import { EnvHttpProxyAgent } from 'undici';
const logger = {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
@@ -325,6 +326,29 @@ export class IdeClient {
}
}
private createProxyAwareFetch() {
// ignore proxy for 'localhost' by deafult to allow connecting to the ide mcp server
const existingNoProxy = process.env['NO_PROXY'] || '';
const agent = new EnvHttpProxyAgent({
noProxy: [existingNoProxy, 'localhost'].filter(Boolean).join(','),
});
const undiciPromise = import('undici');
return async (url: string | URL, init?: RequestInit): Promise<Response> => {
const { fetch: fetchFn } = await undiciPromise;
const fetchOptions: RequestInit & { dispatcher?: unknown } = {
...init,
dispatcher: agent,
};
const options = fetchOptions as unknown as import('undici').RequestInit;
const response = await fetchFn(url, options);
return new Response(response.body as ReadableStream<unknown> | null, {
status: response.status,
statusText: response.statusText,
headers: response.headers,
});
};
}
private registerClientHandlers() {
if (!this.client) {
return;
@@ -389,6 +413,9 @@ export class IdeClient {
});
transport = new StreamableHTTPClientTransport(
new URL(`http://${getIdeServerHost()}:${port}/mcp`),
{
fetch: this.createProxyAwareFetch(),
},
);
await this.client.connect(transport);
this.registerClientHandlers();