UNPKG

@ai-sdk/mcp

Version:

The **Model Context Protocol (MCP) client** for the [AI SDK](https://ai-sdk.dev/docs) lets you connect to MCP servers and use their tools with AI SDK functions like `generateText` and `streamText`.

69 lines (56 loc) 1.68 kB
import { parseJSON } from '@ai-sdk/provider-utils'; import { z } from 'zod/v4'; import { BaseParamsSchema, RequestSchema, ResultSchema } from './types'; const JSONRPC_VERSION = '2.0'; const JSONRPCRequestSchema = z .object({ jsonrpc: z.literal(JSONRPC_VERSION), id: z.union([z.string(), z.number().int()]), }) .merge(RequestSchema) .strict(); export type JSONRPCRequest = z.infer<typeof JSONRPCRequestSchema>; const JSONRPCResponseSchema = z .object({ jsonrpc: z.literal(JSONRPC_VERSION), id: z.union([z.string(), z.number().int()]), result: ResultSchema, }) .strict(); export type JSONRPCResponse = z.infer<typeof JSONRPCResponseSchema>; const JSONRPCErrorSchema = z .object({ jsonrpc: z.literal(JSONRPC_VERSION), id: z.union([z.string(), z.number().int()]), error: z.object({ code: z.number().int(), message: z.string(), data: z.optional(z.unknown()), }), }) .strict(); export type JSONRPCError = z.infer<typeof JSONRPCErrorSchema>; const JSONRPCNotificationSchema = z .object({ jsonrpc: z.literal(JSONRPC_VERSION), }) .merge( z.object({ method: z.string(), params: z.optional(BaseParamsSchema), }), ) .strict(); export type JSONRPCNotification = z.infer<typeof JSONRPCNotificationSchema>; export const JSONRPCMessageSchema = z.union([ JSONRPCRequestSchema, JSONRPCNotificationSchema, JSONRPCResponseSchema, JSONRPCErrorSchema, ]); export type JSONRPCMessage = z.infer<typeof JSONRPCMessageSchema>; export async function parseJSONRPCMessage( text: string, ): Promise<JSONRPCMessage> { return JSONRPCMessageSchema.parse(await parseJSON({ text })); }