feat: Introduce common utility functions

This commit is contained in:
Aleksander Grygier 2025-12-29 10:35:46 +01:00
parent 18ee0acb3e
commit 94fef3508a
2 changed files with 34 additions and 0 deletions

View File

@ -0,0 +1,23 @@
import type { MCPTransportType } from '$lib/types/mcp';
/**
* Detects the MCP transport type from a URL.
* WebSocket URLs (ws:// or wss://) use 'websocket', others use 'streamable_http'.
*/
export function detectMcpTransportFromUrl(url: string): MCPTransportType {
const normalized = url.trim().toLowerCase();
return normalized.startsWith('ws://') || normalized.startsWith('wss://')
? 'websocket'
: 'streamable_http';
}
/**
* Generates a valid MCP server ID from user input.
* Returns the trimmed ID if valid, otherwise generates 'server-{index+1}'.
*/
export function generateMcpServerId(id: unknown, index: number): string {
if (typeof id === 'string' && id.trim()) {
return id.trim();
}
return `server-${index + 1}`;
}

View File

@ -0,0 +1,11 @@
/**
* Normalizes a value to a positive number, returning the fallback if invalid.
* Handles both string and number inputs.
*/
export function normalizePositiveNumber(value: unknown, fallback: number): number {
const parsed = typeof value === 'string' ? Number.parseFloat(value) : Number(value);
if (!Number.isFinite(parsed) || parsed <= 0) {
return fallback;
}
return parsed;
}