import { Client, ClientOptions } from '@modelcontextprotocol/sdk/client/index.js'; import { SSEClientTransport, SSEClientTransportOptions } from '@modelcontextprotocol/sdk/client/sse.js'; import { StreamableHTTPClientTransport, StreamableHTTPClientTransportOptions } from '@modelcontextprotocol/sdk/client/streamableHttp.js'; import { z, ZodObject, ZodLiteral, ZodType } from 'zod'; import { MessageChannelClientTransport } from '@opentiny/next'; import { Result, Request, Notification, Implementation, ServerCapabilities, LoggingLevel, CompleteRequest, CallToolRequest, ListToolsRequest, GetPromptRequest, SubscribeRequest, UnsubscribeRequest, ListPromptsRequest, ReadResourceRequest, ListResourcesRequest, ListResourceTemplatesRequest, ElicitRequest, CreateMessageRequest, ListRootsRequest, ToolListChangedNotification, PromptListChangedNotification, ResourceListChangedNotification, ResourceUpdatedNotification, LoggingMessageNotification } from '@modelcontextprotocol/sdk/types.js'; import { Transport } from '@modelcontextprotocol/sdk/shared/transport.js'; import { RequestOptions, NotificationOptions, RequestHandlerExtra } from '@modelcontextprotocol/sdk/shared/protocol.js'; /** * Options for configuring the server transport. */ export interface ClientConnectOptions { url: string; token?: string; sessionId?: string; type?: 'channel' | 'sse' | 'stream' | 'socket'; agent?: boolean; builtin?: boolean; onError?: (error: Error) => void; } type SendRequestT = Request; type SendNotificationT = Notification; type SendResultT = Result; /** * An MCP client on top of a pluggable transport. * The client will automatically begin the initialization flow with the server when connect() is called. */ export declare class WebMcpClient { readonly client: Client; transport: Transport | undefined; constructor(clientInfo?: Implementation, options?: ClientOptions); /** * Connects the client to a transport via the specified option. */ connect(options: Transport | ClientConnectOptions): Promise<{ transport: Transport; sessionId: string; }>; /** * Callback for when the connection is closed for any reason. * * This is invoked when close() is called as well. */ onclose?: () => void; /** * Callback for when an error occurs. * * Note that errors are not necessarily fatal; they are used for reporting any kind of exceptional condition out of band. */ onerror?: (error: Error) => void; /** * Closes the connection. */ close(): Promise; /** * After initialization has completed, this will be populated with the server's reported capabilities. */ getServerCapabilities(): ServerCapabilities | undefined; /** * After initialization has completed, this will be populated with information about the server's name and version. */ getServerVersion(): Implementation | undefined; /** * After initialization has completed, this may be populated with information about the server's instructions. */ getInstructions(): string | undefined; /** * Sends a ping to the server to check if it is still connected. */ ping(options?: RequestOptions): Promise<{ _meta?: { [x: string]: unknown; progressToken?: string | number | undefined; "io.modelcontextprotocol/related-task"?: { taskId: string; } | undefined; } | undefined; }>; /** * Sends a completion request to the server. */ complete(params: CompleteRequest['params'], options?: RequestOptions): Promise<{ [x: string]: unknown; completion: { [x: string]: unknown; values: string[]; total?: number | undefined; hasMore?: boolean | undefined; }; _meta?: { [x: string]: unknown; progressToken?: string | number | undefined; "io.modelcontextprotocol/related-task"?: { taskId: string; } | undefined; } | undefined; }>; /** * Sends a request for setting the logging level to the server. */ setLoggingLevel(level: LoggingLevel, options?: RequestOptions): Promise<{ _meta?: { [x: string]: unknown; progressToken?: string | number | undefined; "io.modelcontextprotocol/related-task"?: { taskId: string; } | undefined; } | undefined; }>; /** * Gets the prompt with the given params from the server. */ getPrompt(params: GetPromptRequest['params'], options?: RequestOptions): Promise<{ [x: string]: unknown; messages: { role: "user" | "assistant"; content: { type: "text"; text: string; annotations?: { audience?: ("user" | "assistant")[] | undefined; priority?: number | undefined; lastModified?: string | undefined; } | undefined; _meta?: Record | undefined; } | { type: "image"; data: string; mimeType: string; annotations?: { audience?: ("user" | "assistant")[] | undefined; priority?: number | undefined; lastModified?: string | undefined; } | undefined; _meta?: Record | undefined; } | { type: "audio"; data: string; mimeType: string; annotations?: { audience?: ("user" | "assistant")[] | undefined; priority?: number | undefined; lastModified?: string | undefined; } | undefined; _meta?: Record | undefined; } | { type: "resource"; resource: { uri: string; text: string; mimeType?: string | undefined; _meta?: Record | undefined; } | { uri: string; blob: string; mimeType?: string | undefined; _meta?: Record | undefined; }; annotations?: { audience?: ("user" | "assistant")[] | undefined; priority?: number | undefined; lastModified?: string | undefined; } | undefined; _meta?: Record | undefined; } | { uri: string; name: string; type: "resource_link"; description?: string | undefined; mimeType?: string | undefined; annotations?: { audience?: ("user" | "assistant")[] | undefined; priority?: number | undefined; lastModified?: string | undefined; } | undefined; _meta?: { [x: string]: unknown; } | undefined; icons?: { src: string; mimeType?: string | undefined; sizes?: string[] | undefined; theme?: "light" | "dark" | undefined; }[] | undefined; title?: string | undefined; }; }[]; _meta?: { [x: string]: unknown; progressToken?: string | number | undefined; "io.modelcontextprotocol/related-task" /** * Registers a handler for the create LLM message request. */ ?: { taskId: string; } | undefined; } | undefined; description?: string | undefined; }>; /** * Lists all prompts available on the server. */ listPrompts(params?: ListPromptsRequest['params'], options?: RequestOptions): Promise<{ [x: string]: unknown; prompts: { name: string; description?: string | undefined; arguments?: { name: string; description?: string | undefined; required?: boolean | undefined; }[] | undefined; _meta?: { [x: string]: unknown; } | undefined; icons?: { src: string; mimeType?: string | undefined; sizes?: string[] | undefined; theme?: "light" | "dark" | undefined; }[] | undefined; title?: string | undefined; }[]; _meta?: { [x: string]: unknown; progressToken?: string | number | undefined; "io.modelcontextprotocol/related-task"?: { taskId: string; } | undefined; } | undefined; nextCursor?: string | undefined; }>; /** * Lists all resources available on the server. */ listResources(params?: ListResourcesRequest['params'], options?: RequestOptions): Promise<{ [x: string]: unknown; resources: { uri: string; name: string; description?: string | undefined; mimeType?: string | undefined; annotations?: { audience?: ("user" | "assistant")[] | undefined; priority?: number | undefined; lastModified?: string | undefined; } | undefined; _meta?: { [x: string]: unknown; } | undefined; icons?: { src: string; mimeType?: string | undefined; sizes?: string[] | undefined; theme?: "light" | "dark" | undefined; }[] | undefined; title?: string | undefined; }[]; _meta?: { [x: string]: unknown; progressToken?: string | number | undefined; "io.modelcontextprotocol/related-task"?: { taskId: string; } | undefined; } | undefined; nextCursor?: string | undefined; }>; /** * Lists all resource templates available on the server. */ listResourceTemplates(params?: ListResourceTemplatesRequest['params'], options?: RequestOptions): Promise<{ [x: string]: unknown; resourceTemplates: { uriTemplate: string; name: string; description?: string | undefined; mimeType?: string | undefined; annotations?: { audience?: ("user" | "assistant")[] | undefined; priority?: number | undefined; lastModified?: string | undefined; } | undefined; _meta?: { [x: string]: unknown; } | undefined; icons?: { src: string; mimeType?: string | undefined; sizes?: string[] | undefined; theme?: "light" | "dark" | undefined; }[] | undefined; title?: string | undefined; }[]; _meta?: { [x: string]: unknown; progressToken?: string | number | undefined; "io.modelcontextprotocol/related-task"?: { taskId: string; } | undefined; } | undefined; nextCursor?: string | undefined; }>; /** * Reads the resource with the given params from the server. */ readResource(params: ReadResourceRequest['params'], options?: RequestOptions): Promise<{ [x: string]: unknown; contents: ({ uri: string; text: string; mimeType?: string | undefined; _meta?: Record | undefined; } | { uri: string; blob: string; mimeType?: string | undefined; _meta?: Record | undefined; })[]; _meta?: { [x: string]: unknown; progressToken?: string | number | undefined; "io.modelcontextprotocol/related-task"?: { taskId: string; } | undefined; } | undefined; }>; /** * Subscribes to a resource on the server. */ subscribeResource(params: SubscribeRequest['params'], options?: RequestOptions): Promise<{ _meta?: { [x: string]: unknown; progressToken?: string | number | undefined; "io.modelcontextprotocol/related-task"?: { taskId: string; } | undefined; } | undefined; }>; /** * Unsubscribes from a resource on the server. */ unsubscribeResource(params: UnsubscribeRequest['params'], options?: RequestOptions): Promise<{ _meta?: { [x: string]: unknown; progressToken?: string | number | undefined; "io.modelcontextprotocol/related-task"?: { taskId: string; } | undefined; } | undefined; }>; /** * Calls a tool on the server with the given parameters. */ callTool(params: CallToolRequest['params'], options?: RequestOptions): Promise<{ [x: string]: unknown; content: ({ type: "text"; text: string; annotations?: { audience?: ("user" | "assistant")[] | undefined; priority?: number | undefined; lastModified?: string | undefined; } | undefined; _meta?: Record | undefined; } | { type: "image"; data: string; mimeType: string; annotations?: { audience?: ("user" | "assistant")[] | undefined; priority?: number | undefined; lastModified?: string | undefined; } | undefined; _meta?: Record | undefined; } | { type: "audio"; data: string; mimeType: string; annotations?: { audience?: ("user" | "assistant")[] | undefined; priority?: number | undefined; lastModified?: string | undefined; } | undefined; _meta?: Record | undefined; } | { type: "resource"; resource: { uri: string; text: string; mimeType?: string | undefined; _meta?: Record | undefined; } | { uri: string; blob: string; mimeType?: string | undefined; _meta?: Record | undefined; }; annotations?: { audience?: ("user" | "assistant")[] | undefined; priority?: number | undefined; lastModified?: string | undefined; } | undefined; _meta?: Record | undefined; } | { uri: string; name: string; type: "resource_link"; description?: string | undefined; mimeType?: string | undefined; annotations?: { audience?: ("user" | "assistant")[] | undefined; priority?: number | undefined; lastModified?: string | undefined; } | undefined; _meta?: { [x: string]: unknown; } | undefined; icons?: { src: string; mimeType?: string | undefined; sizes?: string[] | undefined; theme?: "light" | "dark" | undefined; }[] | undefined; title?: string | undefined; })[]; _meta?: { [x: string]: unknown; progressToken?: string | number | undefined; "io.modelcontextprotocol/related-task"?: { taskId: string; } | undefined; } | undefined; structuredContent?: Record | undefined; isError?: boolean | undefined; } | { [x: string]: unknown; toolResult: unknown; _meta?: { [x: string]: unknown; progressToken?: string | number | undefined; "io.modelcontextprotocol/related-task"?: { taskId: string; } | undefined; } | undefined; }>; /** * Lists all tools available on the server. */ listTools(params?: ListToolsRequest['params'], options?: RequestOptions): Promise<{ [x: string]: unknown; tools: { inputSchema: { [x: string]: unknown; type: "object"; properties?: Record | undefined; required?: string[] | undefined; }; name: string; description?: string | undefined; outputSchema?: { [x: string]: unknown; type: "object"; properties?: Record | undefined; required?: string[] | undefined; } | undefined; annotations?: { title?: string | undefined; readOnlyHint?: boolean | undefined; destructiveHint?: boolean | undefined; idempotentHint?: boolean | undefined; openWorldHint?: boolean | undefined; } | undefined; execution?: { taskSupport?: "optional" | "required" | "forbidden" | undefined; } | undefined; _meta?: Record | undefined; icons?: { src: string; mimeType?: string | undefined; sizes?: string[] | undefined; theme?: "light" | "dark" | undefined; }[] | undefined; title?: string | undefined; }[]; _meta?: { [x: string]: unknown; progressToken?: string | number | undefined; "io.modelcontextprotocol/related-task"?: { taskId: string; } | undefined; } | undefined; nextCursor?: string | undefined; }>; /** * Sends a notification for the roots list changed event to the server. */ sendRootsListChanged(): Promise; /** * Sends a request and wait for a response. * * Do not use this method to emit notifications! Use notification() instead. */ request>(request: SendRequestT, resultSchema: T, options?: RequestOptions): Promise>; /** * Emits a notification, which is a one-way message that does not expect a response. */ notification(notification: SendNotificationT, options?: NotificationOptions): Promise; /** * Registers a handler to invoke when this protocol object receives a request with the given method. * * Note that this will replace any previous request handler for the same method. */ setRequestHandler; }>>(requestSchema: T, handler: (request: z.infer, extra: RequestHandlerExtra) => SendResultT | Promise): void; /** * Removes the request handler for the given method. */ removeRequestHandler(method: string): void; /** * Registers a handler to invoke when this protocol object receives a notification with the given method. * * Note that this will replace any previous notification handler for the same method. */ setNotificationHandler; }>>(notificationSchema: T, handler: (notification: z.infer) => void | Promise): void; /** * Removes the notification handler for the given method. */ removeNotificationHandler(method: string): void; /** * Registers a handler for the elicitation request. */ onElicit(handler: (request: ElicitRequest, extra: RequestHandlerExtra) => SendResultT | Promise): void; /** * Registers a handler for the create LLM message request. */ onCreateMessage(handler: (request: CreateMessageRequest, extra: RequestHandlerExtra) => SendResultT | Promise): void; /** * Registers a handler for the list roots request. */ onListRoots(handler: (request: ListRootsRequest, extra: RequestHandlerExtra) => SendResultT | Promise): void; /** * Registers a handler for the tool list changed notification. */ onToolListChanged(handler: (notification: ToolListChangedNotification) => void | Promise): void; /** * Registers a handler for the prompt list changed notification. */ onPromptListChanged(handler: (notification: PromptListChangedNotification) => void | Promise): void; /** * Registers a handler for the resource list changed notification. */ onResourceListChanged(handler: (notification: ResourceListChangedNotification) => void | Promise): void; /** * Registers a handler for the resource updated notification. */ onResourceUpdated(handler: (notification: ResourceUpdatedNotification) => void | Promise): void; /** * Registers a handler for the logging message notification. */ onLoggingMessage(handler: (notification: LoggingMessageNotification) => void | Promise): void; /** * Close the transport for window.addEventListener('pagehide') */ onPagehide(event: PageTransitionEvent): Promise; } /** * Creates a new SSEClientTransport instance. */ export declare const createSSEClientTransport: (url: URL, opts?: SSEClientTransportOptions) => SSEClientTransport; /** * Creates a new StreamableHTTPClientTransport instance. */ export declare const createStreamableHTTPClientTransport: (url: URL, opts?: StreamableHTTPClientTransportOptions) => StreamableHTTPClientTransport; /** * Creates a new MessageChannelClientTransport instance. */ export declare const createMessageChannelClientTransport: (endpoint: string, globalObject?: object) => MessageChannelClientTransport; /** * Checks if the transport is a SSEClientTransport. */ export declare const isSSEClientTransport: (transport: unknown) => transport is SSEClientTransport; /** * Checks if the transport is a StreamableHTTPClientTransport. */ export declare const isStreamableHTTPClientTransport: (transport: unknown) => transport is StreamableHTTPClientTransport; /** * Checks if the transport is a MessageChannelClientTransport. */ export declare const isMessageChannelClientTransport: (transport: unknown) => transport is MessageChannelClientTransport; /** * Checks if the client is an instance of MCP Client. */ export declare const isMcpClient: (client: unknown) => client is Client; export {};