export type CreateApiMcpArgs = {'mcp-session-id'?: string | undefined, accept: string, body: {[key: string]: unknown}}; export type CreateApiMcpOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; /** * Handle MCP client request * * POST /mcp */ export const createApiMcp = async (args: CreateApiMcpArgs, options?: CreateApiMcpOptions) => { const {baseUrl = "http://localhost:3001", fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") if (args['mcp-session-id'] != null) headers.set("mcp-session-id", String(args['mcp-session-id'])) if (args.accept != null) headers.set("accept", String(args.accept)) const url = `${baseUrl.replace(/\/+$/, '')}/mcp${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "POST", headers, body: JSON.stringify(args.body)}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return undefined };