import {z} from 'zod' export type CreateApiApiV1McpArgs = {'X-Query-Validation'?: 'strict' | undefined, 'Mcp-Session-Id'?: string | undefined, 'MCP-Protocol-Version'?: ('2025-11-25' | '2025-06-18' | '2025-03-26' | '2024-11-05') | undefined, body: {jsonrpc: '2.0', id?: (string | number | null) | undefined, method: 'initialize' | 'notifications/initialized' | 'notifications/cancelled' | 'ping' | 'tools/list' | 'tools/call', params?: Record | undefined}}; export type CreateApiApiV1McpOptions = {baseUrl?: string | undefined, bearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const createApiApiV1McpResponse = z.object({jsonrpc: z.literal("2.0"), id: z.union([z.string(), z.number()]).nullable(), result: z.object({}).optional(), error: z.object({code: z.number().int(), message: z.string()}).optional()}); export const validateResponse = async ( schema: { readonly '~standard': { readonly validate: (value: unknown) => ({ readonly value: Output; readonly issues?: undefined } | { readonly issues: ReadonlyArray<{ readonly message: string; readonly path?: ReadonlyArray | undefined }> }) | Promise<{ readonly value: Output; readonly issues?: undefined } | { readonly issues: ReadonlyArray<{ readonly message: string; readonly path?: ReadonlyArray | undefined }> }> } }, value: unknown ): Promise => { const result = await schema['~standard'].validate(value) if (result.issues !== undefined) { const detail = result.issues .map(({ message, path }) => path?.length ? `${path.map((segment) => String(typeof segment === 'object' ? segment.key : segment)).join('.')}: ${message}` : message ) .join('; ') throw new Error(`Response does not match the schema: ${detail}`, { cause: { issues: result.issues } }) } return result.value }; /** * Remote MCP endpoint (JSON-RPC) * * POST /api/v1/mcp * * Also accepts auth: oauth2 */ export const createApiApiV1Mcp = async (args: CreateApiApiV1McpArgs, options?: CreateApiApiV1McpOptions) => { const {baseUrl = "https://api.0xinsider.com", fetch: fetchFn = fetch, bearerAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (args['X-Query-Validation'] != null) headers.set("X-Query-Validation", String(args['X-Query-Validation'])) if (args['Mcp-Session-Id'] != null) headers.set("Mcp-Session-Id", String(args['Mcp-Session-Id'])) if (args['MCP-Protocol-Version'] != null) headers.set("MCP-Protocol-Version", String(args['MCP-Protocol-Version'])) if (bearerAuth !== undefined) headers.set('authorization', `Bearer ${bearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/api/v1/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 validateResponse(createApiApiV1McpResponse, await res.json()) };