Initializes this client with the given name and version information.
Optionaldescription?: stringAn optional human-readable description of what this implementation does.
This can be used by clients or servers to provide context about their purpose and capabilities. For example, a server might describe the types of resources or tools it provides, while a client might describe its intended use case.
Optionalicons?: { mimeType?: string; sizes?: string[]; src: string; theme?: "light" | "dark" }[]Optional set of sized icons that the client can display in a user interface.
Clients that support rendering icons MUST support at least the following MIME types:
image/png - PNG images (safe, universal compatibility)image/jpeg (and image/jpg) - JPEG images (safe, universal compatibility)Clients that support rendering icons SHOULD also support:
image/svg+xml - SVG images (scalable but requires security precautions)image/webp - WebP images (modern, efficient format)Intended for programmatic or logical use, but used as a display name in past specs or fallback
Optionaltitle?: stringIntended for UI and end-user contexts — optimized to be human-readable and easily understood, even by those unfamiliar with domain-specific terminology.
If not provided, the name should be used for display (except for Tool,
where annotations.title should be given precedence over using name,
if present).
OptionalwebsiteUrl?: stringAn optional URL of the website for this implementation.
Optionaloptions: ClientOptionsProtected_OptionalfallbackA handler to invoke for any notification types that do not have their own handler installed.
OptionalfallbackA handler to invoke for any request types that do not have their own handler installed.
OptionaloncloseCallback for when the connection is closed for any reason.
This is invoked when close() is called as well.
OptionalonerrorCallback 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.
ExperimentalAccess experimental features.
WARNING: These APIs are experimental and may change without notice.
Asserts that a request handler has not already been set for the given method, in preparation for a new one being automatically installed.
ProtectedassertProtectedassertA method to check if a capability is supported by the remote side, for the given method to be called.
This should be implemented by subclasses.
ProtectedassertA method to check if a notification is supported by the local side, for the given method to be sent.
This should be implemented by subclasses.
ProtectedassertA method to check if a request handler is supported by the local side, for the given method to be handled.
This should be implemented by subclasses.
ProtectedassertA method to check if task creation is supported for the given request method.
This should be implemented by subclasses.
ProtectedassertA method to check if a task handler is supported by the local side, for the given method to be handled.
This should be implemented by subclasses.
ProtectedbuildBuilds the context object for request handlers. Subclasses must override to return the appropriate context type (e.g., ServerContext adds requestInfo).
Optional_transportInfo: MessageExtraInfoCalls a tool on the connected server and returns the result. Automatically validates structured output
if the tool has an outputSchema.
Tool results have two error surfaces: result.isError for tool-level failures (the tool ran but reported
a problem), and thrown ProtocolError for protocol-level failures or SdkError for
SDK-level issues (timeouts, missing capabilities).
For task-based execution with streaming behavior, use client.experimental.tasks.callToolStream() instead.
Optional_meta?: {See General fields: _meta for notes on _meta usage.
Optionalio.modelcontextprotocol/related-task?: { taskId: string }If specified, this request is related to the provided task.
OptionalprogressToken?: string | numberIf specified, the caller is requesting out-of-band progress notifications for this request (as represented by notifications/progress). The value of this parameter is an opaque token that will be attached to any subsequent notifications. The receiver is not obligated to provide these notifications.
Optionalarguments?: { [key: string]: unknown }Arguments to pass to the tool.
The name of the tool to call.
Optionaltask?: { ttl?: number }If specified, the caller is requesting task-augmented execution for this request.
The request will return a CreateTaskResult immediately, and the actual result can be
retrieved later via tasks/result.
Task augmentation is subject to capability negotiation - receivers MUST declare support for task augmentation of specific request types in their capabilities.
Optionaloptions: RequestOptionsProtectedcancelExperimentalCancels a specific task.
Use client.experimental.tasks.cancelTask() to access this method.
Optionaloptions: RequestOptionsCloses the connection.
Requests argument autocompletion suggestions from the server for a prompt or resource.
Optional_meta?: {See General fields: _meta for notes on _meta usage.
Optionalio.modelcontextprotocol/related-task?: { taskId: string }If specified, this request is related to the provided task.
OptionalprogressToken?: string | numberIf specified, the caller is requesting out-of-band progress notifications for this request (as represented by notifications/progress). The value of this parameter is an opaque token that will be attached to any subsequent notifications. The receiver is not obligated to provide these notifications.
The argument's information
The name of the argument
The value of the argument to use for completion matching.
Optionalcontext?: { arguments?: { [key: string]: string } }Optionalarguments?: { [key: string]: string }Previously-resolved variables in a URI template or prompt.
Optionaloptions: RequestOptionsConnects to a server via the given transport and performs the MCP initialization handshake.
Optionaloptions: RequestOptionsconst client = new Client({ name: 'my-client', version: '1.0.0' });
const transport = new StdioClientTransport({ command: 'my-mcp-server' });
await client.connect(transport);
const baseUrl = new URL(url);
try {
// Try modern Streamable HTTP transport first
const client = new Client({ name: 'my-client', version: '1.0.0' });
const transport = new StreamableHTTPClientTransport(baseUrl);
await client.connect(transport);
return { client, transport };
} catch {
// Fall back to legacy SSE transport
const client = new Client({ name: 'my-client', version: '1.0.0' });
const transport = new SSEClientTransport(baseUrl);
await client.connect(transport);
return { client, transport };
}
After initialization has completed, this may be populated with information about the server's instructions.
Retrieves a prompt by name from the server, passing the given arguments for template substitution.
Optional_meta?: {See General fields: _meta for notes on _meta usage.
Optionalio.modelcontextprotocol/related-task?: { taskId: string }If specified, this request is related to the provided task.
OptionalprogressToken?: string | numberIf specified, the caller is requesting out-of-band progress notifications for this request (as represented by notifications/progress). The value of this parameter is an opaque token that will be attached to any subsequent notifications. The receiver is not obligated to provide these notifications.
Optionalarguments?: { [key: string]: string }Arguments to use for templating the prompt.
The name of the prompt or prompt template.
Optionaloptions: RequestOptionsAfter initialization has completed, this will be populated with the server's reported capabilities.
Optionalcompletions?: objectPresent if the server supports sending completions to the client.
Optionalexperimental?: { [key: string]: object }Experimental, non-standard capabilities that the server supports.
Optionallogging?: objectPresent if the server supports sending log messages to the client.
Optionalprompts?: { listChanged?: boolean }Present if the server offers any prompt templates.
OptionallistChanged?: booleanWhether this server supports issuing notifications for changes to the prompt list.
Optionalresources?: { listChanged?: boolean; subscribe?: boolean }Present if the server offers any resources to read.
OptionallistChanged?: booleanWhether this server supports issuing notifications for changes to the resource list.
Optionalsubscribe?: booleanWhether this server supports clients subscribing to resource updates.
Optionaltasks?: {Present if the server supports task creation.
Optionalcancel?: objectPresent if the server supports cancelling tasks.
Optionallist?: objectPresent if the server supports listing tasks.
Optionalrequests?: { tools?: { call?: object; [key: string]: unknown }; [key: string]: unknown }Capabilities for task creation on specific request types.
Optionaltools?: { call?: object; [key: string]: unknown }Task support for tool requests.
Optionaltools?: { listChanged?: boolean }Present if the server offers any tools to call.
OptionallistChanged?: booleanWhether this server supports issuing notifications for changes to the tool list.
After initialization has completed, this will be populated with information about the server's name and version.
Optionaldescription?: stringAn optional human-readable description of what this implementation does.
This can be used by clients or servers to provide context about their purpose and capabilities. For example, a server might describe the types of resources or tools it provides, while a client might describe its intended use case.
Optionalicons?: { mimeType?: string; sizes?: string[]; src: string; theme?: "light" | "dark" }[]Optional set of sized icons that the client can display in a user interface.
Clients that support rendering icons MUST support at least the following MIME types:
image/png - PNG images (safe, universal compatibility)image/jpeg (and image/jpg) - JPEG images (safe, universal compatibility)Clients that support rendering icons SHOULD also support:
image/svg+xml - SVG images (scalable but requires security precautions)image/webp - WebP images (modern, efficient format)Intended for programmatic or logical use, but used as a display name in past specs or fallback
Optionaltitle?: stringIntended for UI and end-user contexts — optimized to be human-readable and easily understood, even by those unfamiliar with domain-specific terminology.
If not provided, the name should be used for display (except for Tool,
where annotations.title should be given precedence over using name,
if present).
OptionalwebsiteUrl?: stringAn optional URL of the website for this implementation.
ProtectedgetExperimentalGets the current status of a task.
Use client.experimental.tasks.getTask() to access this method.
Optional_meta?: {See General fields: _meta for notes on _meta usage.
Optionalio.modelcontextprotocol/related-task?: { taskId: string }If specified, this request is related to the provided task.
OptionalprogressToken?: string | numberIf specified, the caller is requesting out-of-band progress notifications for this request (as represented by notifications/progress). The value of this parameter is an opaque token that will be attached to any subsequent notifications. The receiver is not obligated to provide these notifications.
Optionaloptions: RequestOptionsProtectedgetExperimentalRetrieves the result of a completed task.
Use client.experimental.tasks.getTaskResult() to access this method.
Optional_meta?: {See General fields: _meta for notes on _meta usage.
Optionalio.modelcontextprotocol/related-task?: { taskId: string }If specified, this request is related to the provided task.
OptionalprogressToken?: string | numberIf specified, the caller is requesting out-of-band progress notifications for this request (as represented by notifications/progress). The value of this parameter is an opaque token that will be attached to any subsequent notifications. The receiver is not obligated to provide these notifications.
Optionaloptions: RequestOptionsLists available prompts. Results may be paginated — loop on nextCursor to collect all pages.
Returns an empty list if the server does not advertise prompts capability
(or throws if ClientOptions.enforceStrictCapabilities is enabled).
Optionalparams: {Optional_meta?: {See General fields: _meta for notes on _meta usage.
Optionalio.modelcontextprotocol/related-task?: { taskId: string }If specified, this request is related to the provided task.
OptionalprogressToken?: string | numberIf specified, the caller is requesting out-of-band progress notifications for this request (as represented by notifications/progress). The value of this parameter is an opaque token that will be attached to any subsequent notifications. The receiver is not obligated to provide these notifications.
Optionalcursor?: stringAn opaque token representing the current pagination position. If provided, the server should return results starting after this cursor.
Optionaloptions: RequestOptionsLists available resources. Results may be paginated — loop on nextCursor to collect all pages.
Returns an empty list if the server does not advertise resources capability
(or throws if ClientOptions.enforceStrictCapabilities is enabled).
Optionalparams: {Optional_meta?: {See General fields: _meta for notes on _meta usage.
Optionalio.modelcontextprotocol/related-task?: { taskId: string }If specified, this request is related to the provided task.
OptionalprogressToken?: string | numberIf specified, the caller is requesting out-of-band progress notifications for this request (as represented by notifications/progress). The value of this parameter is an opaque token that will be attached to any subsequent notifications. The receiver is not obligated to provide these notifications.
Optionalcursor?: stringAn opaque token representing the current pagination position. If provided, the server should return results starting after this cursor.
Optionaloptions: RequestOptionsLists available resource URI templates for dynamic resources. Results may be paginated — see listResources() for the cursor pattern.
Returns an empty list if the server does not advertise resources capability
(or throws if ClientOptions.enforceStrictCapabilities is enabled).
Optionalparams: {Optional_meta?: {See General fields: _meta for notes on _meta usage.
Optionalio.modelcontextprotocol/related-task?: { taskId: string }If specified, this request is related to the provided task.
OptionalprogressToken?: string | numberIf specified, the caller is requesting out-of-band progress notifications for this request (as represented by notifications/progress). The value of this parameter is an opaque token that will be attached to any subsequent notifications. The receiver is not obligated to provide these notifications.
Optionalcursor?: stringAn opaque token representing the current pagination position. If provided, the server should return results starting after this cursor.
Optionaloptions: RequestOptionsProtectedlistExperimentalLists tasks, optionally starting from a pagination cursor.
Use client.experimental.tasks.listTasks() to access this method.
Optionalparams: { cursor?: string }Optionaloptions: RequestOptionsLists available tools. Results may be paginated — loop on nextCursor to collect all pages.
Returns an empty list if the server does not advertise tools capability
(or throws if ClientOptions.enforceStrictCapabilities is enabled).
Optionalparams: {Optional_meta?: {See General fields: _meta for notes on _meta usage.
Optionalio.modelcontextprotocol/related-task?: { taskId: string }If specified, this request is related to the provided task.
OptionalprogressToken?: string | numberIf specified, the caller is requesting out-of-band progress notifications for this request (as represented by notifications/progress). The value of this parameter is an opaque token that will be attached to any subsequent notifications. The receiver is not obligated to provide these notifications.
Optionalcursor?: stringAn opaque token representing the current pagination position. If provided, the server should return results starting after this cursor.
Optionaloptions: RequestOptionsEmits a notification, which is a one-way message that does not expect a response.
Optionaloptions: NotificationOptionsSends a ping to the server to check connectivity.
Optionaloptions: RequestOptionsReads the contents of a resource by URI.
Optional_meta?: {See General fields: _meta for notes on _meta usage.
Optionalio.modelcontextprotocol/related-task?: { taskId: string }If specified, this request is related to the provided task.
OptionalprogressToken?: string | numberIf specified, the caller is requesting out-of-band progress notifications for this request (as represented by notifications/progress). The value of this parameter is an opaque token that will be attached to any subsequent notifications. The receiver is not obligated to provide these notifications.
The URI of the resource to read. The URI can use any protocol; it is up to the server how to interpret it.
Optionaloptions: RequestOptionsRegisters new capabilities. This can only be called before connecting to a transport.
The new capabilities will be merged with any existing capabilities previously given (e.g., at initialization).
Optionalelicitation?: {Present if the client supports eliciting user input.
Optionalexperimental?: { [key: string]: object }Experimental, non-standard capabilities that the client supports.
Optionalroots?: { listChanged?: boolean }Present if the client supports listing roots.
OptionallistChanged?: booleanWhether the client supports issuing notifications for changes to the roots list.
Optionalsampling?: { context?: object; tools?: object }Present if the client supports sampling from an LLM.
Optionalcontext?: objectPresent if the client supports context inclusion via includeContext parameter.
If not declared, servers SHOULD only use includeContext: "none" (or omit it).
Optionaltools?: objectPresent if the client supports tool use via tools and toolChoice parameters.
Optionaltasks?: {Present if the client supports task creation.
Optionalcancel?: objectPresent if the client supports cancelling tasks.
Optionallist?: objectPresent if the client supports listing tasks.
Optionalrequests?: {Capabilities for task creation on specific request types.
Optionalelicitation?: { create?: object; [key: string]: unknown }Task support for elicitation requests.
Optionalsampling?: { createMessage?: object; [key: string]: unknown }Task support for sampling requests.
Removes the notification handler for the given method.
Sends a request and waits for a response.
Do not use this method to emit notifications! Use notification() instead.
Optionaloptions: RequestOptionsProtectedrequestExperimentalSends a request and returns an AsyncGenerator that yields response messages.
The generator is guaranteed to end with either a 'result' or 'error' message.
Use client.experimental.tasks.requestStream() to access this method.
Optionaloptions: RequestOptionsNotifies the server that the client's root list has changed. Requires the roots.listChanged capability.
Sets the minimum severity level for log messages sent by the server.
Optionaloptions: RequestOptionsRegisters 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.
Registers a handler for server-initiated requests (sampling, elicitation, roots). The client must declare the corresponding capability for the handler to be accepted. Replaces any previously registered handler for the same method.
For sampling/createMessage and elicitation/create, the handler is automatically
wrapped with schema validation for both the incoming request and the returned result.
client.setRequestHandler('sampling/createMessage', async request => {
const lastMessage = request.params.messages.at(-1);
console.log('Sampling request:', lastMessage);
// In production, send messages to your LLM here
return {
model: 'my-model',
role: 'assistant' as const,
content: {
type: 'text' as const,
text: 'Response from the model'
}
};
});
Subscribes to change notifications for a resource. The server must support resource subscriptions.
Optional_meta?: {See General fields: _meta for notes on _meta usage.
Optionalio.modelcontextprotocol/related-task?: { taskId: string }If specified, this request is related to the provided task.
OptionalprogressToken?: string | numberIf specified, the caller is requesting out-of-band progress notifications for this request (as represented by notifications/progress). The value of this parameter is an opaque token that will be attached to any subsequent notifications. The receiver is not obligated to provide these notifications.
The URI of the resource to read. The URI can use any protocol; it is up to the server how to interpret it.
Optionaloptions: RequestOptionsUnsubscribes from change notifications for a resource.
Optional_meta?: {See General fields: _meta for notes on _meta usage.
Optionalio.modelcontextprotocol/related-task?: { taskId: string }If specified, this request is related to the provided task.
OptionalprogressToken?: string | numberIf specified, the caller is requesting out-of-band progress notifications for this request (as represented by notifications/progress). The value of this parameter is an opaque token that will be attached to any subsequent notifications. The receiver is not obligated to provide these notifications.
The URI of the resource to read. The URI can use any protocol; it is up to the server how to interpret it.
Optionaloptions: RequestOptions
An MCP client on top of a pluggable transport.
The client will automatically begin the initialization flow with the server when
connectis called.