From 46c5bca942f856c02a7ff951fa2e9d83af695a8e Mon Sep 17 00:00:00 2001 From: Aleksander Grygier Date: Thu, 29 Jan 2026 16:29:04 +0100 Subject: [PATCH] refactor: Proxy utility --- .../webui/src/lib/services/mcp.service.ts | 18 ++---------- .../server/webui/src/lib/utils/cors-proxy.ts | 28 +++++++++++++++++++ 2 files changed, 30 insertions(+), 16 deletions(-) create mode 100644 tools/server/webui/src/lib/utils/cors-proxy.ts diff --git a/tools/server/webui/src/lib/services/mcp.service.ts b/tools/server/webui/src/lib/services/mcp.service.ts index df09c92321..72341307b2 100644 --- a/tools/server/webui/src/lib/services/mcp.service.ts +++ b/tools/server/webui/src/lib/services/mcp.service.ts @@ -42,7 +42,7 @@ import type { import { MCPConnectionPhase, MCPLogLevel, MCPTransportType } from '$lib/enums'; import { DEFAULT_MCP_CONFIG } from '$lib/constants/mcp'; import { throwIfAborted, isAbortError } from '$lib/utils'; -import { base } from '$app/paths'; +import { buildProxiedUrl } from '$lib/utils/cors-proxy'; interface ToolResultContentItem { type: string; @@ -77,20 +77,6 @@ export class MCPService { }; } - /** - * Build a proxied URL that routes through llama-server's CORS proxy. - * @param targetUrl - The original MCP server URL - * @returns URL pointing to the CORS proxy with target encoded - */ - private static buildProxiedUrl(targetUrl: string): URL { - const proxyPath = `${base}/cors-proxy`; - const proxyUrl = new URL(proxyPath, window.location.origin); - - proxyUrl.searchParams.set('url', targetUrl); - - return proxyUrl; - } - /** * Create transport based on server configuration. * Supports WebSocket, StreamableHTTP (modern), and SSE (legacy) transports. @@ -135,7 +121,7 @@ export class MCPService { }; } - const url = useProxy ? this.buildProxiedUrl(config.url) : new URL(config.url); + const url = useProxy ? buildProxiedUrl(config.url) : new URL(config.url); if (useProxy && import.meta.env.DEV) { console.log(`[MCPService] Using CORS proxy for ${config.url} -> ${url.href}`); diff --git a/tools/server/webui/src/lib/utils/cors-proxy.ts b/tools/server/webui/src/lib/utils/cors-proxy.ts new file mode 100644 index 0000000000..e09d31d0ff --- /dev/null +++ b/tools/server/webui/src/lib/utils/cors-proxy.ts @@ -0,0 +1,28 @@ +/** + * CORS Proxy utility for routing requests through llama-server's CORS proxy. + */ + +import { base } from '$app/paths'; + +/** + * Build a proxied URL that routes through llama-server's CORS proxy. + * @param targetUrl - The original URL to proxy + * @returns URL pointing to the CORS proxy with target encoded + */ +export function buildProxiedUrl(targetUrl: string): URL { + const proxyPath = `${base}/cors-proxy`; + const proxyUrl = new URL(proxyPath, window.location.origin); + + proxyUrl.searchParams.set('url', targetUrl); + + return proxyUrl; +} + +/** + * Get a proxied URL string for use in fetch requests. + * @param targetUrl - The original URL to proxy + * @returns Proxied URL as string + */ +export function getProxiedUrlString(targetUrl: string): string { + return buildProxiedUrl(targetUrl).href; +}