UI : fix SSE transport detection and routing through CORS proxy. Assi… (#24500)

* UI : fix SSE transport detection and routing through CORS proxy. Assisted-by: Antigravity

* ui : replace magic strings with constants in MCP transport handling
This commit is contained in:
Harapan Rachman
2026-06-17 08:26:30 +02:00
committed by GitHub
parent 51571722aa
commit bae36efa30
3 changed files with 82 additions and 5 deletions
+58 -1
View File
@@ -16,7 +16,8 @@ import {
DEFAULT_MCP_CONFIG,
DEFAULT_CLIENT_VERSION,
DEFAULT_IMAGE_MIME_TYPE,
MCP_PARTIAL_REDACT_HEADERS
MCP_PARTIAL_REDACT_HEADERS,
CORS_PROXY_ENDPOINT
} from '$lib/constants';
import {
MCPConnectionPhase,
@@ -236,6 +237,36 @@ export class MCPService {
return {
fetch: async (input, init) => {
if (useProxy && typeof window !== 'undefined') {
let requestUrlStr = '';
if (typeof input === 'string') {
requestUrlStr = input;
} else if (input instanceof URL) {
requestUrlStr = input.href;
}
if (requestUrlStr) {
const parsedRequestUrl = new URL(requestUrlStr, window.location.origin);
if (
parsedRequestUrl.origin === window.location.origin &&
!parsedRequestUrl.pathname.includes(CORS_PROXY_ENDPOINT)
) {
const originalConfigUrl = new URL(config.url);
const realTargetUrl = new URL(
parsedRequestUrl.pathname + parsedRequestUrl.search,
originalConfigUrl.origin
);
const proxiedUrl = buildProxiedUrl(realTargetUrl.href);
if (typeof input === 'string') {
input = proxiedUrl.href;
} else if (input instanceof URL) {
input = proxiedUrl;
}
}
}
}
const startedAt = performance.now();
const requestHeaders = new Headers(baseInit.headers);
@@ -403,6 +434,32 @@ export class MCPService {
};
}
if (config.transport === MCPTransportType.SSE) {
const url = useProxy ? buildProxiedUrl(config.url) : new URL(config.url);
const { fetch: diagnosticFetch, disable: stopPhaseLogging } = this.createDiagnosticFetch(
serverName,
config,
requestInit,
url,
useProxy,
onLog
);
if (import.meta.env.DEV && import.meta.env.VITE_DEBUG) {
console.log(`[MCPService] Creating SSE transport for ${url.href}`);
}
return {
transport: new SSEClientTransport(url, {
requestInit,
fetch: diagnosticFetch,
eventSourceInit: { fetch: diagnosticFetch }
}),
type: MCPTransportType.SSE,
stopPhaseLogging
};
}
const url = useProxy ? buildProxiedUrl(config.url) : new URL(config.url);
const { fetch: diagnosticFetch, disable: stopPhaseLogging } = this.createDiagnosticFetch(
serverName,