@ai-sdk/mcp
Version:
167 lines • 5.21 kB
TypeScript
import "@ai-sdk/provider-utils";
import { z } from "zod/v4";
import { AISDKError } from "@ai-sdk/provider";
import { IOType } from "node:child_process";
import { Stream } from "node:stream";
//#region src/tool/json-rpc-message.d.ts
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>>;
resultType: z.ZodOptional<z.ZodString>;
}, z.core.$loose>;
}, z.core.$strict>, z.ZodObject<{
jsonrpc: z.ZodLiteral<"2.0">;
id: z.ZodOptional<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>;
//#endregion
//#region src/tool/mcp-transport.d.ts
/**
* 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;
/**
* Request-specific HTTP headers produced from MCP tool parameters.
*/
headers?: Record<string, string>;
/**
* 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 {
/**
* Whether this transport can probe for stateless MCP protocol versions.
*
* Custom transports default to the legacy initialization flow unless they
* explicitly opt in.
*/
supportsProtocolVersionDiscovery?: boolean;
/**
* Whether the client waits for the `server/discover` response instead of
* treating a slow response as a legacy server.
*
* Transports that always deliver a response, such as Streamable HTTP, set
* this so a slow modern server is not downgraded to `initialize`. Other
* transports keep a short probe timeout because a legacy server may never
* answer the unknown method.
*/
waitsForProtocolVersionDiscovery?: boolean;
/**
* Whether this transport mirrors x-mcp-header tool parameters into request
* headers.
*/
supportsMcpToolParameterHeaders?: boolean;
/**
* 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;
}
//#endregion
//#region src/tool/mcp-stdio/mcp-stdio-transport.d.ts
interface StdioConfig {
command: string;
args?: string[];
env?: Record<string, string>;
stderr?: IOType | Stream | number;
cwd?: string;
shell?: boolean | string;
}
export declare class StdioMCPTransport implements MCPTransport {
readonly supportsProtocolVersionDiscovery = true;
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>;
}
//#endregion
//#region src/tool/mcp-stdio/index.d.ts
/**
* @deprecated Use `StdioMCPTransport` instead.
*/
export declare const Experimental_StdioMCPTransport: typeof StdioMCPTransport;
/**
* @deprecated Use `StdioMCPTransport` instead.
*/
export type Experimental_StdioMCPTransport = StdioMCPTransport;
//#endregion
export type { StdioConfig };
//# sourceMappingURL=index.d.ts.map