| import type { MCPServerSettingsEntry, MCPResourceContent, MCPResourceInfo } from '$lib/types'; |
| import { |
| MCPTransportType, |
| MCPLogLevel, |
| UrlProtocol, |
| MimeTypePrefix, |
| MimeTypeIncludes, |
| UriPattern, |
| MimeTypeText |
| } from '$lib/enums'; |
| import { |
| DEFAULT_MCP_CONFIG, |
| MCP_SERVER_ID_PREFIX, |
| IMAGE_FILE_EXTENSION_REGEX, |
| CODE_FILE_EXTENSION_REGEX, |
| TEXT_FILE_EXTENSION_REGEX, |
| PROTOCOL_PREFIX_REGEX, |
| FILE_EXTENSION_REGEX, |
| DISPLAY_NAME_SEPARATOR_REGEX, |
| PATH_SEPARATOR, |
| RESOURCE_TEXT_CONTENT_SEPARATOR, |
| DEFAULT_RESOURCE_FILENAME |
| } from '$lib/constants'; |
| import { |
| Database, |
| File, |
| FileText, |
| Image, |
| Code, |
| Info, |
| AlertTriangle, |
| XCircle |
| } from '@lucide/svelte'; |
| import type { Component } from 'svelte'; |
| import type { MimeTypeUnion } from '$lib/types/common'; |
|
|
| |
| |
| |
| |
| export function detectMcpTransportFromUrl(url: string): MCPTransportType { |
| const normalized = url.trim().toLowerCase(); |
|
|
| return normalized.startsWith(UrlProtocol.WEBSOCKET) || |
| normalized.startsWith(UrlProtocol.WEBSOCKET_SECURE) |
| ? MCPTransportType.WEBSOCKET |
| : MCPTransportType.STREAMABLE_HTTP; |
| } |
|
|
| |
| |
| |
| |
| |
| |
| export function parseMcpServerSettings(rawServers: unknown): MCPServerSettingsEntry[] { |
| if (!rawServers) return []; |
|
|
| let parsed: unknown; |
|
|
| if (typeof rawServers === 'string') { |
| const trimmed = rawServers.trim(); |
| if (!trimmed) return []; |
|
|
| try { |
| parsed = JSON.parse(trimmed); |
| } catch (error) { |
| console.warn('[MCP] Failed to parse mcpServers JSON, ignoring value:', error); |
|
|
| return []; |
| } |
| } else { |
| parsed = rawServers; |
| } |
|
|
| if (!Array.isArray(parsed)) return []; |
|
|
| return parsed.map((entry, index) => { |
| const url = typeof entry?.url === 'string' ? entry.url.trim() : ''; |
| const headers = typeof entry?.headers === 'string' ? entry.headers.trim() : undefined; |
| const id = |
| typeof (entry as { id?: unknown })?.id === 'string' && (entry as { id?: string }).id?.trim() |
| ? (entry as { id: string }).id.trim() |
| : `${MCP_SERVER_ID_PREFIX}-${index + 1}`; |
|
|
| return { |
| id, |
| enabled: Boolean((entry as { enabled?: unknown })?.enabled), |
| url, |
| name: (entry as { name?: string })?.name, |
| requestTimeoutSeconds: DEFAULT_MCP_CONFIG.requestTimeoutSeconds, |
| headers: headers || undefined, |
| useProxy: Boolean((entry as { useProxy?: unknown })?.useProxy) |
| } satisfies MCPServerSettingsEntry; |
| }); |
| } |
|
|
| |
| |
| |
| |
| |
| |
| export function getMcpLogLevelIcon(level: MCPLogLevel): Component { |
| switch (level) { |
| case MCPLogLevel.ERROR: |
| return XCircle; |
| case MCPLogLevel.WARN: |
| return AlertTriangle; |
| default: |
| return Info; |
| } |
| } |
|
|
| |
| |
| |
| |
| |
| |
| export function getMcpLogLevelClass(level: MCPLogLevel): string { |
| switch (level) { |
| case MCPLogLevel.ERROR: |
| return 'text-destructive'; |
| case MCPLogLevel.WARN: |
| return 'text-yellow-600 dark:text-yellow-500'; |
| default: |
| return 'text-muted-foreground'; |
| } |
| } |
|
|
| |
| |
| |
| |
| |
| |
| export function isImageMimeType(mimeType?: MimeTypeUnion): boolean { |
| return mimeType?.startsWith(MimeTypePrefix.IMAGE) ?? false; |
| } |
|
|
| |
| |
| |
| |
| |
| |
| export function parseResourcePath(uri: string): string[] { |
| try { |
| const withoutProtocol = uri.replace(PROTOCOL_PREFIX_REGEX, ''); |
| return withoutProtocol.split(PATH_SEPARATOR).filter((p) => p.length > 0); |
| } catch { |
| return [uri]; |
| } |
| } |
|
|
| |
| |
| |
| |
| |
| |
| |
| export function getDisplayName(pathPart: string): string { |
| const withoutExt = pathPart.replace(FILE_EXTENSION_REGEX, ''); |
| return withoutExt |
| .split(DISPLAY_NAME_SEPARATOR_REGEX) |
| .map((word) => word.charAt(0).toUpperCase() + word.slice(1)) |
| .join(' '); |
| } |
|
|
| |
| |
| |
| |
| |
| |
| export function getResourceDisplayName(resource: MCPResourceInfo): string { |
| try { |
| const parts = parseResourcePath(resource.uri); |
| return parts[parts.length - 1] || resource.name || resource.uri; |
| } catch { |
| return resource.name || resource.uri; |
| } |
| } |
|
|
| |
| |
| |
| |
| |
| |
| |
| export function isCodeResource(mimeType?: MimeTypeUnion, uri?: string): boolean { |
| const mime = mimeType?.toLowerCase() || ''; |
| const u = uri?.toLowerCase() || ''; |
| return ( |
| mime.includes(MimeTypeIncludes.JSON) || |
| mime.includes(MimeTypeIncludes.JAVASCRIPT) || |
| mime.includes(MimeTypeIncludes.TYPESCRIPT) || |
| CODE_FILE_EXTENSION_REGEX.test(u) |
| ); |
| } |
|
|
| |
| |
| |
| |
| |
| |
| |
| export function isImageResource(mimeType?: MimeTypeUnion, uri?: string): boolean { |
| const mime = mimeType?.toLowerCase() || ''; |
| const u = uri?.toLowerCase() || ''; |
| return mime.startsWith(MimeTypePrefix.IMAGE) || IMAGE_FILE_EXTENSION_REGEX.test(u); |
| } |
|
|
| |
| |
| |
| |
| |
| |
| |
| export function getResourceIcon(mimeType?: MimeTypeUnion, uri?: string): Component { |
| const mime = mimeType?.toLowerCase() || ''; |
| const u = uri?.toLowerCase() || ''; |
|
|
| if (mime.startsWith(MimeTypePrefix.IMAGE) || IMAGE_FILE_EXTENSION_REGEX.test(u)) { |
| return Image; |
| } |
|
|
| if ( |
| mime.includes(MimeTypeIncludes.JSON) || |
| mime.includes(MimeTypeIncludes.JAVASCRIPT) || |
| mime.includes(MimeTypeIncludes.TYPESCRIPT) || |
| CODE_FILE_EXTENSION_REGEX.test(u) |
| ) { |
| return Code; |
| } |
|
|
| if (mime.includes(MimeTypePrefix.TEXT) || TEXT_FILE_EXTENSION_REGEX.test(u)) { |
| return FileText; |
| } |
|
|
| if (u.includes(UriPattern.DATABASE_KEYWORD) || u.includes(UriPattern.DATABASE_SCHEME)) { |
| return Database; |
| } |
|
|
| return File; |
| } |
|
|
| |
| |
| |
| |
| |
| |
| export function getResourceTextContent(content: MCPResourceContent[] | null | undefined): string { |
| if (!content) return ''; |
| return content |
| .filter((c): c is { uri: string; mimeType?: MimeTypeUnion; text: string } => 'text' in c) |
| .map((c) => c.text) |
| .join(RESOURCE_TEXT_CONTENT_SEPARATOR); |
| } |
|
|
| |
| |
| |
| |
| |
| |
| export function getResourceBlobContent( |
| content: MCPResourceContent[] | null | undefined |
| ): Array<{ uri: string; mimeType?: MimeTypeUnion; blob: string }> { |
| if (!content) return []; |
|
|
| return content.filter( |
| (c): c is { uri: string; mimeType?: MimeTypeUnion; blob: string } => 'blob' in c |
| ); |
| } |
|
|
| |
| |
| |
| |
| |
| |
| |
| export function downloadResourceContent( |
| text: string, |
| mimeType: MimeTypeUnion = MimeTypeText.PLAIN, |
| filename: string = DEFAULT_RESOURCE_FILENAME |
| ): void { |
| const blob = new Blob([text], { type: mimeType }); |
| const url = URL.createObjectURL(blob); |
| const a = document.createElement('a'); |
| a.href = url; |
| a.download = filename; |
| document.body.appendChild(a); |
| a.click(); |
| document.body.removeChild(a); |
| URL.revokeObjectURL(url); |
| } |
|
|