@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`.
124 lines (119 loc) • 3.78 kB
TypeScript
import { IOType } from 'node:child_process';
import { Stream } from 'node:stream';
import { z } from 'zod/v4';
declare const JSONRPCMessageSchema: z.ZodUnion<readonly [z.ZodObject<{
jsonrpc: z.ZodLiteral<"2.0">;
id: z.ZodUnion<readonly [z.ZodString, z.ZodNumber]>;
method: z.ZodString;
params: z.ZodOptional<z.ZodObject<{
_meta: z.ZodOptional<z.ZodObject<{}, z.core.$loose>>;
}, z.core.$loose>>;
}, z.core.$strict>, z.ZodObject<{
jsonrpc: z.ZodLiteral<"2.0">;
method: z.ZodString;
params: z.ZodOptional<z.ZodObject<{
_meta: z.ZodOptional<z.ZodObject<{}, z.core.$loose>>;
}, z.core.$loose>>;
}, z.core.$strict>, z.ZodObject<{
jsonrpc: z.ZodLiteral<"2.0">;
id: z.ZodUnion<readonly [z.ZodString, z.ZodNumber]>;
result: z.ZodObject<{
_meta: z.ZodOptional<z.ZodObject<{}, z.core.$loose>>;
}, z.core.$loose>;
}, z.core.$strict>, z.ZodObject<{
jsonrpc: z.ZodLiteral<"2.0">;
id: z.ZodUnion<readonly [z.ZodString, z.ZodNumber]>;
error: z.ZodObject<{
code: z.ZodNumber;
message: z.ZodString;
data: z.ZodOptional<z.ZodUnknown>;
}, z.core.$strip>;
}, z.core.$strict>]>;
type JSONRPCMessage = z.infer<typeof JSONRPCMessageSchema>;
/**
* Transport interface for MCP (Model Context Protocol) communication.
* Maps to the `Transport` interface in the MCP spec.
*/
type MCPTransportSendOptions = {
/**
* Cancels the transport operation for this message.
*/
signal?: AbortSignal;
/**
* Associates an outgoing message with an incoming request.
*/
relatedRequestId?: string | number;
/**
* Resumes a previously interrupted request.
*/
resumptionToken?: string;
/**
* Receives updated resumption tokens from transports that support them.
*/
onresumptiontoken?: (token: string) => void;
};
type MCPTransportCloseOptions = {
/**
* Cancels transport cleanup.
*/
signal?: AbortSignal;
};
interface MCPTransport {
/**
* Initialize and start the transport
*/
start(): Promise<void>;
/**
* Send a JSON-RPC message through the transport
* @param message The JSON-RPC message to send
* @param options Optional request-scoped cancellation options
*/
send(message: JSONRPCMessage, options?: MCPTransportSendOptions): Promise<void>;
/**
* Clean up and close the transport
* @param options Optional cancellation options for transport cleanup
*/
close(options?: MCPTransportCloseOptions): Promise<void>;
/**
* Event handler for transport closure
*/
onclose?: () => void;
/**
* Event handler for transport errors
*/
onerror?: (error: Error) => void;
/**
* Event handler for received messages
*/
onmessage?: (message: JSONRPCMessage) => void;
/**
* The protocol version negotiated during initialization.
*/
protocolVersion?: string;
/**
* Set the protocol version negotiated during initialization.
*/
setProtocolVersion?(version: string): void;
}
interface StdioConfig {
command: string;
args?: string[];
env?: Record<string, string>;
stderr?: IOType | Stream | number;
cwd?: string;
}
declare class StdioMCPTransport implements MCPTransport {
private process?;
private abortController;
private readBuffer;
private serverParams;
onclose?: () => void;
onerror?: (error: unknown) => void;
onmessage?: (message: JSONRPCMessage) => void;
constructor(server: StdioConfig);
start(): Promise<void>;
private processReadBuffer;
close(): Promise<void>;
send(message: JSONRPCMessage): Promise<void>;
}
export { StdioMCPTransport as Experimental_StdioMCPTransport, type StdioConfig };