refactor: Proxy utility
This commit is contained in:
parent
944765138e
commit
46c5bca942
|
|
@ -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}`);
|
||||
|
|
|
|||
|
|
@ -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;
|
||||
}
|
||||
Loading…
Reference in New Issue