This is the documentation for v1.x — looking for the v2 beta documentation?
Skip to content

MCP TypeScript SDK / client

client

Classes

Client

Defined in: src/client/index.ts:226

An MCP client on top of a pluggable transport.

The client will automatically begin the initialization flow with the server when connect() is called.

To use with custom types, extend the base Request/Notification/Result types and pass them as type parameters:

typescript
// Custom schemas
const CustomRequestSchema = RequestSchema.extend({...})
const CustomNotificationSchema = NotificationSchema.extend({...})
const CustomResultSchema = ResultSchema.extend({...})

// Type aliases
type CustomRequest = z.infer<typeof CustomRequestSchema>
type CustomNotification = z.infer<typeof CustomNotificationSchema>
type CustomResult = z.infer<typeof CustomResultSchema>

// Create typed client
const client = new Client<CustomRequest, CustomNotification, CustomResult>({
  name: "CustomClient",
  version: "1.0.0"
})

Extends

Type Parameters

RequestT

RequestT extends Request = Request

NotificationT

NotificationT extends Notification = Notification

ResultT

ResultT extends Result = Result

Constructors

Constructor

new Client<RequestT, NotificationT, ResultT>(_clientInfo, options?): Client<RequestT, NotificationT, ResultT>

Defined in: src/client/index.ts:246

Initializes this client with the given name and version information.

Parameters
_clientInfo
description?

string = ...

An 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.

icons?

object[] = ...

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)
name

string = ...

Intended for programmatic or logical use, but used as a display name in past specs or fallback

title?

string = ...

Intended 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).

version

string = ...

websiteUrl?

string = ...

An optional URL of the website for this implementation.

options?

ClientOptions

Returns

Client<RequestT, NotificationT, ResultT>

Overrides

Protocol.constructor

Properties

fallbackNotificationHandler?

optional fallbackNotificationHandler?: (notification) => Promise<void>

Defined in: src/shared/protocol.ts:364

A handler to invoke for any notification types that do not have their own handler installed.

Parameters
notification
method

string = ...

params?

{[key: string]: unknown; _meta?: {[key: string]: unknown; io.modelcontextprotocol/related-task?: { taskId: string; }; progressToken?: string | number; }; } = ...

params._meta?

{[key: string]: unknown; io.modelcontextprotocol/related-task?: { taskId: string; }; progressToken?: string | number; } = ...

See MCP specification for notes on _meta usage.

{ taskId: string; } = ...

If specified, this request is related to the provided task.

string = ...

params._meta.progressToken?

string | number = ...

If 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.

Returns

Promise<void>

Inherited from

Protocol.fallbackNotificationHandler

fallbackRequestHandler?

optional fallbackRequestHandler?: (request, extra) => Promise<ResultT | ClientResult>

Defined in: src/shared/protocol.ts:359

A handler to invoke for any request types that do not have their own handler installed.

Parameters
request
id

string | number = RequestIdSchema

jsonrpc

"2.0" = ...

method

string = ...

params?

{[key: string]: unknown; _meta?: {[key: string]: unknown; io.modelcontextprotocol/related-task?: { taskId: string; }; progressToken?: string | number; }; } = ...

params._meta?

{[key: string]: unknown; io.modelcontextprotocol/related-task?: { taskId: string; }; progressToken?: string | number; } = ...

See General fields: _meta for notes on _meta usage.

{ taskId: string; } = ...

If specified, this request is related to the provided task.

string = ...

params._meta.progressToken?

string | number = ...

If 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.

extra

RequestHandlerExtra<RequestT | ClientRequest, NotificationT | ClientNotification>

Returns

Promise<ResultT | ClientResult>

Inherited from

Protocol.fallbackRequestHandler

onclose?

optional onclose?: () => void

Defined in: src/shared/protocol.ts:347

Callback for when the connection is closed for any reason.

This is invoked when close() is called as well.

Returns

void

Inherited from

Protocol.onclose

onerror?

optional onerror?: (error) => void

Defined in: src/shared/protocol.ts:354

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.

Parameters
error

Error

Returns

void

Inherited from

Protocol.onerror

Accessors

experimental
Get Signature

get experimental(): object

Defined in: src/client/index.ts:296

Experimental

Access experimental features.

WARNING: These APIs are experimental and may change without notice.

Returns

object

tasks

tasks: ExperimentalClientTasks<RequestT, NotificationT, ResultT>

transport
Get Signature

get transport(): Transport | undefined

Defined in: src/shared/protocol.ts:935

Returns

Transport | undefined

Inherited from

Protocol.transport

Methods

assertCanSetRequestHandler()

assertCanSetRequestHandler(method): void

Defined in: src/shared/protocol.ts:1444

Asserts that a request handler has not already been set for the given method, in preparation for a new one being automatically installed.

Parameters
method

string

Returns

void

Inherited from

Protocol.assertCanSetRequestHandler

assertCapability()

protected assertCapability(capability, method): void

Defined in: src/client/index.ts:458

Parameters
capability

"experimental" | "tools" | "tasks" | "extensions" | "logging" | "completions" | "prompts" | "resources"

method

string

Returns

void

assertCapabilityForMethod()

protected assertCapabilityForMethod(method): void

Defined in: src/client/index.ts:539

A 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.

Parameters
method

RequestT["method"]

Returns

void

Overrides

Protocol.assertCapabilityForMethod

assertNotificationCapability()

protected assertNotificationCapability(method): void

Defined in: src/client/index.ts:592

A 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.

Parameters
method

NotificationT["method"]

Returns

void

Overrides

Protocol.assertNotificationCapability

assertRequestHandlerCapability()

protected assertRequestHandlerCapability(method): void

Defined in: src/client/index.ts:614

A 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.

Parameters
method

string

Returns

void

Overrides

Protocol.assertRequestHandlerCapability

assertTaskCapability()

protected assertTaskCapability(method): void

Defined in: src/client/index.ts:655

A method to check if task creation is supported for the given request method.

This should be implemented by subclasses.

Parameters
method

string

Returns

void

Overrides

Protocol.assertTaskCapability

assertTaskHandlerCapability()

protected assertTaskHandlerCapability(method): void

Defined in: src/client/index.ts:659

A method to check if task handler is supported by the local side, for the given method to be handled.

This should be implemented by subclasses.

Parameters
method

string

Returns

void

Overrides

Protocol.assertTaskHandlerCapability

callTool()

callTool(params, resultSchema?, options?): Promise<{[key: string]: unknown; _meta?: {[key: string]: unknown; io.modelcontextprotocol/related-task?: { taskId: string; }; progressToken?: string | number; }; content: ({ _meta?: Record<string, unknown>; annotations?: { audience?: (... | ...)[]; lastModified?: string; priority?: number; }; text: string; type: "text"; } | { _meta?: Record<string, unknown>; annotations?: { audience?: (... | ...)[]; lastModified?: string; priority?: number; }; data: string; mimeType: string; type: "image"; } | { _meta?: Record<string, unknown>; annotations?: { audience?: (... | ...)[]; lastModified?: string; priority?: number; }; data: string; mimeType: string; type: "audio"; } | { _meta?: {[key: string]: unknown; }; annotations?: { audience?: (... | ...)[]; lastModified?: string; priority?: number; }; description?: string; icons?: object[]; mimeType?: string; name: string; size?: number; title?: string; type: "resource_link"; uri: string; } | { _meta?: Record<string, unknown>; annotations?: { audience?: (... | ...)[]; lastModified?: string; priority?: number; }; resource: { _meta?: Record<string, unknown>; mimeType?: string; text: string; uri: string; } | { _meta?: Record<string, unknown>; blob: string; mimeType?: string; uri: string; }; type: "resource"; })[]; isError?: boolean; structuredContent?: Record<string, unknown>; } | {[key: string]: unknown; _meta?: {[key: string]: unknown; io.modelcontextprotocol/related-task?: { taskId: string; }; progressToken?: string | number; }; toolResult: unknown; }>

Defined in: src/client/index.ts:714

Calls a tool and waits for the result. Automatically validates structured output if the tool has an outputSchema.

For task-based execution with streaming behavior, use client.experimental.tasks.callToolStream() instead.

Parameters
params
_meta?

{[key: string]: unknown; io.modelcontextprotocol/related-task?: { taskId: string; }; progressToken?: string | number; } = ...

See General fields: _meta for notes on _meta usage.

{ taskId: string; } = ...

If specified, this request is related to the provided task.

string = ...

_meta.progressToken?

string | number = ...

If 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.

arguments?

{[key: string]: unknown; } = ...

Arguments to pass to the tool.

name

string = ...

The name of the tool to call.

task?

{ 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.

task.ttl?

number = ...

resultSchema?

ZodObject<{ _meta: ZodOptional<ZodObject<{ io.modelcontextprotocol/related-task: ZodOptional<ZodObject<{ taskId: ZodString; }, $strip>>; progressToken: ZodOptional<ZodUnion<readonly [ZodString, ZodNumber]>>; }, $loose>>; content: ZodDefault<ZodArray<ZodUnion<readonly [ZodObject<{ _meta: ZodOptional<ZodRecord<..., ...>>; annotations: ZodOptional<ZodObject<..., ...>>; text: ZodString; type: ZodLiteral<"text">; }, $strip>, ZodObject<{ _meta: ZodOptional<ZodRecord<..., ...>>; annotations: ZodOptional<ZodObject<..., ...>>; data: ZodString; mimeType: ZodString; type: ZodLiteral<"image">; }, $strip>, ZodObject<{ _meta: ZodOptional<ZodRecord<..., ...>>; annotations: ZodOptional<ZodObject<..., ...>>; data: ZodString; mimeType: ZodString; type: ZodLiteral<"audio">; }, $strip>, ZodObject<{ _meta: ZodOptional<ZodObject<..., ...>>; annotations: ZodOptional<ZodObject<..., ...>>; description: ZodOptional<ZodString>; icons: ZodOptional<ZodArray<...>>; mimeType: ZodOptional<ZodString>; name: ZodString; size: ZodOptional<ZodNumber>; title: ZodOptional<ZodString>; type: ZodLiteral<"resource_link">; uri: ZodString; }, $strip>, ZodObject<{ _meta: ZodOptional<ZodRecord<..., ...>>; annotations: ZodOptional<ZodObject<..., ...>>; resource: ZodUnion<readonly [..., ...]>; type: ZodLiteral<"resource">; }, $strip>]>>>; isError: ZodOptional<ZodBoolean>; structuredContent: ZodOptional<ZodRecord<ZodString, ZodUnknown>>; }, $loose> | ZodUnion<[ZodObject<{ _meta: ZodOptional<ZodObject<{ io.modelcontextprotocol/related-task: ZodOptional<ZodObject<{ taskId: ...; }, $strip>>; progressToken: ZodOptional<ZodUnion<readonly [..., ...]>>; }, $loose>>; content: ZodDefault<ZodArray<ZodUnion<readonly [ZodObject<{ _meta: ...; annotations: ...; text: ...; type: ...; }, $strip>, ZodObject<{ _meta: ...; annotations: ...; data: ...; mimeType: ...; type: ...; }, $strip>, ZodObject<{ _meta: ...; annotations: ...; data: ...; mimeType: ...; type: ...; }, $strip>, ZodObject<{ _meta: ...; annotations: ...; description: ...; icons: ...; mimeType: ...; name: ...; size: ...; title: ...; type: ...; uri: ...; }, $strip>, ZodObject<{ _meta: ...; annotations: ...; resource: ...; type: ...; }, $strip>]>>>; isError: ZodOptional<ZodBoolean>; structuredContent: ZodOptional<ZodRecord<ZodString, ZodUnknown>>; }, $loose>, ZodObject<{ _meta: ZodOptional<ZodObject<{ io.modelcontextprotocol/related-task: ZodOptional<ZodObject<{ taskId: ...; }, $strip>>; progressToken: ZodOptional<ZodUnion<readonly [..., ...]>>; }, $loose>>; toolResult: ZodUnknown; }, $loose>]>

options?

RequestOptions

Returns

Promise<{[key: string]: unknown; _meta?: {[key: string]: unknown; io.modelcontextprotocol/related-task?: { taskId: string; }; progressToken?: string | number; }; content: ({ _meta?: Record<string, unknown>; annotations?: { audience?: (... | ...)[]; lastModified?: string; priority?: number; }; text: string; type: "text"; } | { _meta?: Record<string, unknown>; annotations?: { audience?: (... | ...)[]; lastModified?: string; priority?: number; }; data: string; mimeType: string; type: "image"; } | { _meta?: Record<string, unknown>; annotations?: { audience?: (... | ...)[]; lastModified?: string; priority?: number; }; data: string; mimeType: string; type: "audio"; } | { _meta?: {[key: string]: unknown; }; annotations?: { audience?: (... | ...)[]; lastModified?: string; priority?: number; }; description?: string; icons?: object[]; mimeType?: string; name: string; size?: number; title?: string; type: "resource_link"; uri: string; } | { _meta?: Record<string, unknown>; annotations?: { audience?: (... | ...)[]; lastModified?: string; priority?: number; }; resource: { _meta?: Record<string, unknown>; mimeType?: string; text: string; uri: string; } | { _meta?: Record<string, unknown>; blob: string; mimeType?: string; uri: string; }; type: "resource"; })[]; isError?: boolean; structuredContent?: Record<string, unknown>; } | {[key: string]: unknown; _meta?: {[key: string]: unknown; io.modelcontextprotocol/related-task?: { taskId: string; }; progressToken?: string | number; }; toolResult: unknown; }>

cancelTask()

protected cancelTask(params, options?): Promise<{ _meta?: {[key: string]: unknown; io.modelcontextprotocol/related-task?: { taskId: string; }; progressToken?: string | number; }; createdAt: string; lastUpdatedAt: string; pollInterval?: number; status: "working" | "input_required" | "completed" | "failed" | "cancelled"; statusMessage?: string; taskId: string; ttl: number | null; }>

Defined in: src/shared/protocol.ts:1295

Experimental

Cancels a specific task.

Use client.experimental.tasks.cancelTask() to access this method.

Parameters
params
taskId

string

options?

RequestOptions

Returns

Promise<{ _meta?: {[key: string]: unknown; io.modelcontextprotocol/related-task?: { taskId: string; }; progressToken?: string | number; }; createdAt: string; lastUpdatedAt: string; pollInterval?: number; status: "working" | "input_required" | "completed" | "failed" | "cancelled"; statusMessage?: string; taskId: string; ttl: number | null; }>

Inherited from

Protocol.cancelTask

close()

close(): Promise<void>

Defined in: src/shared/protocol.ts:942

Closes the connection.

Returns

Promise<void>

Inherited from

Protocol.close

complete()

complete(params, options?): Promise<{[key: string]: unknown; _meta?: {[key: string]: unknown; io.modelcontextprotocol/related-task?: { taskId: string; }; progressToken?: string | number; }; completion: {[key: string]: unknown; hasMore?: boolean; total?: number; values: string[]; }; }>

Defined in: src/client/index.ts:673

Parameters
params
_meta?

{[key: string]: unknown; io.modelcontextprotocol/related-task?: { taskId: string; }; progressToken?: string | number; } = ...

See General fields: _meta for notes on _meta usage.

{ taskId: string; } = ...

If specified, this request is related to the provided task.

string = ...

_meta.progressToken?

string | number = ...

If 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.

argument

{ name: string; value: string; } = ...

The argument's information

argument.name

string = ...

The name of the argument

argument.value

string = ...

The value of the argument to use for completion matching.

context?

{ arguments?: {[key: string]: string; }; } = ...

context.arguments?

{[key: string]: string; } = ...

Previously-resolved variables in a URI template or prompt.

ref

{ name: string; type: "ref/prompt"; } | { type: "ref/resource"; uri: string; } = ...

options?

RequestOptions

Returns

Promise<{[key: string]: unknown; _meta?: {[key: string]: unknown; io.modelcontextprotocol/related-task?: { taskId: string; }; progressToken?: string | number; }; completion: {[key: string]: unknown; hasMore?: boolean; total?: number; values: string[]; }; }>

connect()

connect(transport, options?): Promise<void>

Defined in: src/client/index.ts:464

Attaches to the given transport, starts it, and starts listening for messages.

The Protocol object assumes ownership of the Transport, replacing any callbacks that have already been set, and expects that it is the only user of the Transport instance going forward.

Parameters
transport

Transport

options?

RequestOptions

Returns

Promise<void>

Overrides

Protocol.connect

getInstructions()

getInstructions(): string | undefined

Defined in: src/client/index.ts:535

After initialization has completed, this may be populated with information about the server's instructions.

Returns

string | undefined

getPrompt()

getPrompt(params, options?): Promise<{[key: string]: unknown; _meta?: {[key: string]: unknown; io.modelcontextprotocol/related-task?: { taskId: string; }; progressToken?: string | number; }; description?: string; messages: object[]; }>

Defined in: src/client/index.ts:681

Parameters
params
_meta?

{[key: string]: unknown; io.modelcontextprotocol/related-task?: { taskId: string; }; progressToken?: string | number; } = ...

See General fields: _meta for notes on _meta usage.

{ taskId: string; } = ...

If specified, this request is related to the provided task.

string = ...

_meta.progressToken?

string | number = ...

If 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.

arguments?

{[key: string]: string; } = ...

Arguments to use for templating the prompt.

name

string = ...

The name of the prompt or prompt template.

options?

RequestOptions

Returns

Promise<{[key: string]: unknown; _meta?: {[key: string]: unknown; io.modelcontextprotocol/related-task?: { taskId: string; }; progressToken?: string | number; }; description?: string; messages: object[]; }>

getServerCapabilities()

getServerCapabilities(): { completions?: object; experimental?: {[key: string]: object; }; extensions?: {[key: string]: object; }; logging?: object; prompts?: { listChanged?: boolean; }; resources?: { listChanged?: boolean; subscribe?: boolean; }; tasks?: {[key: string]: unknown; cancel?: object; list?: object; requests?: {[key: string]: unknown; tools?: {[key: string]: unknown; call?: object; }; }; }; tools?: { listChanged?: boolean; }; } | undefined

Defined in: src/client/index.ts:521

After initialization has completed, this will be populated with the server's reported capabilities.

Returns
Type Literal

{ completions?: object; experimental?: {[key: string]: object; }; extensions?: {[key: string]: object; }; logging?: object; prompts?: { listChanged?: boolean; }; resources?: { listChanged?: boolean; subscribe?: boolean; }; tasks?: {[key: string]: unknown; cancel?: object; list?: object; requests?: {[key: string]: unknown; tools?: {[key: string]: unknown; call?: object; }; }; }; tools?: { listChanged?: boolean; }; }

completions?

optional completions?: object

Present if the server supports sending completions to the client.

experimental?

optional experimental?: object

Experimental, non-standard capabilities that the server supports.

Index Signature

[key: string]: object

extensions?

optional extensions?: object

Extensions that the server supports. Keys are extension identifiers (vendor-prefix/extension-name).

Index Signature

[key: string]: object

logging?

optional logging?: object

Present if the server supports sending log messages to the client.

prompts?

optional prompts?: object

Present if the server offers any prompt templates.

prompts.listChanged?

optional listChanged?: boolean

Whether this server supports issuing notifications for changes to the prompt list.

resources?

optional resources?: object

Present if the server offers any resources to read.

resources.listChanged?

optional listChanged?: boolean

Whether this server supports issuing notifications for changes to the resource list.

resources.subscribe?

optional subscribe?: boolean

Whether this server supports clients subscribing to resource updates.

tasks?

optional tasks?: object

Present if the server supports task creation.

Index Signature

[key: string]: unknown

tasks.cancel?

optional cancel?: object

Present if the server supports cancelling tasks.

tasks.list?

optional list?: object

Present if the server supports listing tasks.

tasks.requests?

optional requests?: object

Capabilities for task creation on specific request types.

Index Signature

[key: string]: unknown

tasks.requests.tools?

optional tools?: object

Task support for tool requests.

Index Signature

[key: string]: unknown

tasks.requests.tools.call?

optional call?: object

tools?

optional tools?: object

Present if the server offers any tools to call.

tools.listChanged?

optional listChanged?: boolean

Whether this server supports issuing notifications for changes to the tool list.


undefined

getServerVersion()

getServerVersion(): { description?: string; icons?: object[]; name: string; title?: string; version: string; websiteUrl?: string; } | undefined

Defined in: src/client/index.ts:528

After initialization has completed, this will be populated with information about the server's name and version.

Returns
Type Literal

{ description?: string; icons?: object[]; name: string; title?: string; version: string; websiteUrl?: string; }

description?

optional description?: string

An 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.

icons?

optional icons?: object[]

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)
name

name: string

Intended for programmatic or logical use, but used as a display name in past specs or fallback

title?

optional title?: string

Intended 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).

version

version: string

websiteUrl?

optional websiteUrl?: string

An optional URL of the website for this implementation.


undefined

getTask()

protected getTask(params, options?): Promise<{ _meta?: {[key: string]: unknown; io.modelcontextprotocol/related-task?: { taskId: string; }; progressToken?: string | number; }; createdAt: string; lastUpdatedAt: string; pollInterval?: number; status: "working" | "input_required" | "completed" | "failed" | "cancelled"; statusMessage?: string; taskId: string; ttl: number | null; }>

Defined in: src/shared/protocol.ts:1261

Experimental

Gets the current status of a task.

Use client.experimental.tasks.getTask() to access this method.

Parameters
params
_meta?

{[key: string]: unknown; io.modelcontextprotocol/related-task?: { taskId: string; }; progressToken?: string | number; } = ...

See General fields: _meta for notes on _meta usage.

{ taskId: string; } = ...

If specified, this request is related to the provided task.

string = ...

_meta.progressToken?

string | number = ...

If 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.

taskId

string = ...

options?

RequestOptions

Returns

Promise<{ _meta?: {[key: string]: unknown; io.modelcontextprotocol/related-task?: { taskId: string; }; progressToken?: string | number; }; createdAt: string; lastUpdatedAt: string; pollInterval?: number; status: "working" | "input_required" | "completed" | "failed" | "cancelled"; statusMessage?: string; taskId: string; ttl: number | null; }>

Inherited from

Protocol.getTask

getTaskResult()

protected getTaskResult<T>(params, resultSchema, options?): Promise<SchemaOutput<T>>

Defined in: src/shared/protocol.ts:1271

Experimental

Retrieves the result of a completed task.

Use client.experimental.tasks.getTaskResult() to access this method.

Type Parameters
T

T extends AnySchema

Parameters
params
_meta?

{[key: string]: unknown; io.modelcontextprotocol/related-task?: { taskId: string; }; progressToken?: string | number; } = ...

See General fields: _meta for notes on _meta usage.

{ taskId: string; } = ...

If specified, this request is related to the provided task.

string = ...

_meta.progressToken?

string | number = ...

If 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.

taskId

string = ...

resultSchema

T

options?

RequestOptions

Returns

Promise<SchemaOutput<T>>

Inherited from

Protocol.getTaskResult

listPrompts()

listPrompts(params?, options?): Promise<{[key: string]: unknown; _meta?: {[key: string]: unknown; io.modelcontextprotocol/related-task?: { taskId: string; }; progressToken?: string | number; }; nextCursor?: string; prompts: object[]; }>

Defined in: src/client/index.ts:685

Parameters
params?
_meta?

{[key: string]: unknown; io.modelcontextprotocol/related-task?: { taskId: string; }; progressToken?: string | number; } = ...

See General fields: _meta for notes on _meta usage.

{ taskId: string; } = ...

If specified, this request is related to the provided task.

string = ...

_meta.progressToken?

string | number = ...

If 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.

cursor?

string = ...

An opaque token representing the current pagination position. If provided, the server should return results starting after this cursor.

options?

RequestOptions

Returns

Promise<{[key: string]: unknown; _meta?: {[key: string]: unknown; io.modelcontextprotocol/related-task?: { taskId: string; }; progressToken?: string | number; }; nextCursor?: string; prompts: object[]; }>

listResources()

listResources(params?, options?): Promise<{[key: string]: unknown; _meta?: {[key: string]: unknown; io.modelcontextprotocol/related-task?: { taskId: string; }; progressToken?: string | number; }; nextCursor?: string; resources: object[]; }>

Defined in: src/client/index.ts:689

Parameters
params?
_meta?

{[key: string]: unknown; io.modelcontextprotocol/related-task?: { taskId: string; }; progressToken?: string | number; } = ...

See General fields: _meta for notes on _meta usage.

{ taskId: string; } = ...

If specified, this request is related to the provided task.

string = ...

_meta.progressToken?

string | number = ...

If 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.

cursor?

string = ...

An opaque token representing the current pagination position. If provided, the server should return results starting after this cursor.

options?

RequestOptions

Returns

Promise<{[key: string]: unknown; _meta?: {[key: string]: unknown; io.modelcontextprotocol/related-task?: { taskId: string; }; progressToken?: string | number; }; nextCursor?: string; resources: object[]; }>

listResourceTemplates()

listResourceTemplates(params?, options?): Promise<{[key: string]: unknown; _meta?: {[key: string]: unknown; io.modelcontextprotocol/related-task?: { taskId: string; }; progressToken?: string | number; }; nextCursor?: string; resourceTemplates: object[]; }>

Defined in: src/client/index.ts:693

Parameters
params?
_meta?

{[key: string]: unknown; io.modelcontextprotocol/related-task?: { taskId: string; }; progressToken?: string | number; } = ...

See General fields: _meta for notes on _meta usage.

{ taskId: string; } = ...

If specified, this request is related to the provided task.

string = ...

_meta.progressToken?

string | number = ...

If 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.

cursor?

string = ...

An opaque token representing the current pagination position. If provided, the server should return results starting after this cursor.

options?

RequestOptions

Returns

Promise<{[key: string]: unknown; _meta?: {[key: string]: unknown; io.modelcontextprotocol/related-task?: { taskId: string; }; progressToken?: string | number; }; nextCursor?: string; resourceTemplates: object[]; }>

listTasks()

protected listTasks(params?, options?): Promise<{[key: string]: unknown; _meta?: {[key: string]: unknown; io.modelcontextprotocol/related-task?: { taskId: string; }; progressToken?: string | number; }; nextCursor?: string; tasks: object[]; }>

Defined in: src/shared/protocol.ts:1285

Experimental

Lists tasks, optionally starting from a pagination cursor.

Use client.experimental.tasks.listTasks() to access this method.

Parameters
params?
cursor?

string

options?

RequestOptions

Returns

Promise<{[key: string]: unknown; _meta?: {[key: string]: unknown; io.modelcontextprotocol/related-task?: { taskId: string; }; progressToken?: string | number; }; nextCursor?: string; tasks: object[]; }>

Inherited from

Protocol.listTasks

listTools()

listTools(params?, options?): Promise<{[key: string]: unknown; _meta?: {[key: string]: unknown; io.modelcontextprotocol/related-task?: { taskId: string; }; progressToken?: string | number; }; nextCursor?: string; tools: object[]; }>

Defined in: src/client/index.ts:817

Parameters
params?
_meta?

{[key: string]: unknown; io.modelcontextprotocol/related-task?: { taskId: string; }; progressToken?: string | number; } = ...

See General fields: _meta for notes on _meta usage.

{ taskId: string; } = ...

If specified, this request is related to the provided task.

string = ...

_meta.progressToken?

string | number = ...

If 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.

cursor?

string = ...

An opaque token representing the current pagination position. If provided, the server should return results starting after this cursor.

options?

RequestOptions

Returns

Promise<{[key: string]: unknown; _meta?: {[key: string]: unknown; io.modelcontextprotocol/related-task?: { taskId: string; }; progressToken?: string | number; }; nextCursor?: string; tools: object[]; }>

notification()

notification(notification, options?): Promise<void>

Defined in: src/shared/protocol.ts:1303

Emits a notification, which is a one-way message that does not expect a response.

Parameters
notification

NotificationT | ClientNotification

options?

NotificationOptions

Returns

Promise<void>

Inherited from

Protocol.notification

ping()

ping(options?): Promise<{ _meta?: {[key: string]: unknown; io.modelcontextprotocol/related-task?: { taskId: string; }; progressToken?: string | number; }; }>

Defined in: src/client/index.ts:669

Parameters
options?

RequestOptions

Returns

Promise<{ _meta?: {[key: string]: unknown; io.modelcontextprotocol/related-task?: { taskId: string; }; progressToken?: string | number; }; }>

readResource()

readResource(params, options?): Promise<{[key: string]: unknown; _meta?: {[key: string]: unknown; io.modelcontextprotocol/related-task?: { taskId: string; }; progressToken?: string | number; }; contents: ({ _meta?: Record<string, unknown>; mimeType?: string; text: string; uri: string; } | { _meta?: Record<string, unknown>; blob: string; mimeType?: string; uri: string; })[]; }>

Defined in: src/client/index.ts:697

Parameters
params
_meta?

{[key: string]: unknown; io.modelcontextprotocol/related-task?: { taskId: string; }; progressToken?: string | number; } = ...

See General fields: _meta for notes on _meta usage.

{ taskId: string; } = ...

If specified, this request is related to the provided task.

string = ...

_meta.progressToken?

string | number = ...

If 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.

uri

string = ...

The URI of the resource to read. The URI can use any protocol; it is up to the server how to interpret it.

Format

uri

options?

RequestOptions

Returns

Promise<{[key: string]: unknown; _meta?: {[key: string]: unknown; io.modelcontextprotocol/related-task?: { taskId: string; }; progressToken?: string | number; }; contents: ({ _meta?: Record<string, unknown>; mimeType?: string; text: string; uri: string; } | { _meta?: Record<string, unknown>; blob: string; mimeType?: string; uri: string; })[]; }>

registerCapabilities()

registerCapabilities(capabilities): void

Defined in: src/client/index.ts:310

Registers 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).

Parameters
capabilities
elicitation?

{[key: string]: unknown; form?: {[key: string]: unknown; applyDefaults?: boolean; }; url?: object; } = ...

Present if the client supports eliciting user input.

elicitation.form?

{[key: string]: unknown; applyDefaults?: boolean; } = ...

elicitation.form.applyDefaults?

boolean = ...

elicitation.url?

object = ...

experimental?

{[key: string]: object; } = ...

Experimental, non-standard capabilities that the client supports.

extensions?

{[key: string]: object; } = ...

Extensions that the client supports. Keys are extension identifiers (vendor-prefix/extension-name).

roots?

{ listChanged?: boolean; } = ...

Present if the client supports listing roots.

roots.listChanged?

boolean = ...

Whether the client supports issuing notifications for changes to the roots list.

sampling?

{ context?: object; tools?: object; } = ...

Present if the client supports sampling from an LLM.

sampling.context?

object = ...

Present if the client supports context inclusion via includeContext parameter. If not declared, servers SHOULD only use includeContext: "none" (or omit it).

sampling.tools?

object = ...

Present if the client supports tool use via tools and toolChoice parameters.

tasks?

{[key: string]: unknown; cancel?: object; list?: object; requests?: {[key: string]: unknown; elicitation?: {[key: string]: unknown; create?: object; }; sampling?: {[key: string]: unknown; createMessage?: object; }; }; } = ...

Present if the client supports task creation.

tasks.cancel?

object = ...

Present if the client supports cancelling tasks.

tasks.list?

object = ...

Present if the client supports listing tasks.

tasks.requests?

{[key: string]: unknown; elicitation?: {[key: string]: unknown; create?: object; }; sampling?: {[key: string]: unknown; createMessage?: object; }; } = ...

Capabilities for task creation on specific request types.

tasks.requests.elicitation?

{[key: string]: unknown; create?: object; } = ...

Task support for elicitation requests.

tasks.requests.elicitation.create?

object = ...

tasks.requests.sampling?

{[key: string]: unknown; createMessage?: object; } = ...

Task support for sampling requests.

tasks.requests.sampling.createMessage?

object = ...

Returns

void

removeNotificationHandler()

removeNotificationHandler(method): void

Defined in: src/shared/protocol.ts:1469

Removes the notification handler for the given method.

Parameters
method

string

Returns

void

Inherited from

Protocol.removeNotificationHandler

removeRequestHandler()

removeRequestHandler(method): void

Defined in: src/shared/protocol.ts:1437

Removes the request handler for the given method.

Parameters
method

string

Returns

void

Inherited from

Protocol.removeRequestHandler

request()

request<T>(request, resultSchema, options?): Promise<SchemaOutput<T>>

Defined in: src/shared/protocol.ts:1098

Sends a request and waits for a response.

Do not use this method to emit notifications! Use notification() instead.

Type Parameters
T

T extends AnySchema

Parameters
request

RequestT | ClientRequest

resultSchema

T

options?

RequestOptions

Returns

Promise<SchemaOutput<T>>

Inherited from

Protocol.request

requestStream()

protected requestStream<T>(request, resultSchema, options?): AsyncGenerator<ResponseMessage<SchemaOutput<T>>, void, void>

Defined in: src/shared/protocol.ts:1008

Experimental

Sends a request and returns an AsyncGenerator that yields response messages. The generator is guaranteed to end with either a 'result' or 'error' message.

Type Parameters
T

T extends AnySchema

Parameters
request

RequestT | ClientRequest

resultSchema

T

options?

RequestOptions

Returns

AsyncGenerator<ResponseMessage<SchemaOutput<T>>, void, void>

Example
typescript
const stream = protocol.requestStream(request, resultSchema, options);
for await (const message of stream) {
  switch (message.type) {
    case 'taskCreated':
      console.log('Task created:', message.task.taskId);
      break;
    case 'taskStatus':
      console.log('Task status:', message.task.status);
      break;
    case 'result':
      console.log('Final result:', message.result);
      break;
    case 'error':
      console.error('Error:', message.error);
      break;
  }
}

Use client.experimental.tasks.requestStream() to access this method.

Inherited from

Protocol.requestStream

sendRootsListChanged()

sendRootsListChanged(): Promise<void>

Defined in: src/client/index.ts:886

Returns

Promise<void>

setLoggingLevel()

setLoggingLevel(level, options?): Promise<{ _meta?: {[key: string]: unknown; io.modelcontextprotocol/related-task?: { taskId: string; }; progressToken?: string | number; }; }>

Defined in: src/client/index.ts:677

Parameters
level

LoggingLevel

options?

RequestOptions

Returns

Promise<{ _meta?: {[key: string]: unknown; io.modelcontextprotocol/related-task?: { taskId: string; }; progressToken?: string | number; }; }>

setNotificationHandler()

setNotificationHandler<T>(notificationSchema, handler): void

Defined in: src/shared/protocol.ts:1455

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.

Type Parameters
T

T extends AnyObjectSchema

Parameters
notificationSchema

T

handler

(notification) => void | Promise<void>

Returns

void

Inherited from

Protocol.setNotificationHandler

setRequestHandler()

setRequestHandler<T>(requestSchema, handler): void

Defined in: src/client/index.ts:321

Override request handler registration to enforce client-side validation for elicitation.

Type Parameters
T

T extends AnyObjectSchema

Parameters
requestSchema

T

handler

(request, extra) => ResultT | ClientResult | Promise<ResultT | ClientResult>

Returns

void

Overrides

Protocol.setRequestHandler

subscribeResource()

subscribeResource(params, options?): Promise<{ _meta?: {[key: string]: unknown; io.modelcontextprotocol/related-task?: { taskId: string; }; progressToken?: string | number; }; }>

Defined in: src/client/index.ts:701

Parameters
params
_meta?

{[key: string]: unknown; io.modelcontextprotocol/related-task?: { taskId: string; }; progressToken?: string | number; } = ...

See General fields: _meta for notes on _meta usage.

{ taskId: string; } = ...

If specified, this request is related to the provided task.

string = ...

_meta.progressToken?

string | number = ...

If 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.

uri

string = ...

The URI of the resource to read. The URI can use any protocol; it is up to the server how to interpret it.

Format

uri

options?

RequestOptions

Returns

Promise<{ _meta?: {[key: string]: unknown; io.modelcontextprotocol/related-task?: { taskId: string; }; progressToken?: string | number; }; }>

unsubscribeResource()

unsubscribeResource(params, options?): Promise<{ _meta?: {[key: string]: unknown; io.modelcontextprotocol/related-task?: { taskId: string; }; progressToken?: string | number; }; }>

Defined in: src/client/index.ts:705

Parameters
params
_meta?

{[key: string]: unknown; io.modelcontextprotocol/related-task?: { taskId: string; }; progressToken?: string | number; } = ...

See General fields: _meta for notes on _meta usage.

{ taskId: string; } = ...

If specified, this request is related to the provided task.

string = ...

_meta.progressToken?

string | number = ...

If 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.

uri

string = ...

The URI of the resource to read. The URI can use any protocol; it is up to the server how to interpret it.

Format

uri

options?

RequestOptions

Returns

Promise<{ _meta?: {[key: string]: unknown; io.modelcontextprotocol/related-task?: { taskId: string; }; progressToken?: string | number; }; }>

Type Aliases

ClientOptions

ClientOptions = ProtocolOptions & object

Defined in: src/client/index.ts:135

Type Declaration

capabilities?

optional capabilities?: ClientCapabilities

Capabilities to advertise as being supported by this client.

jsonSchemaValidator?

optional jsonSchemaValidator?: jsonSchemaValidator

JSON Schema validator for tool output validation.

The validator is used to validate structured content returned by tools against their declared output schemas.

Default
ts
AjvJsonSchemaValidator
Example
typescript
// ajv
const client = new Client(
  { name: 'my-client', version: '1.0.0' },
  {
    capabilities: {},
    jsonSchemaValidator: new AjvJsonSchemaValidator()
  }
);

// @cfworker/json-schema
const client = new Client(
  { name: 'my-client', version: '1.0.0' },
  {
    capabilities: {},
    jsonSchemaValidator: new CfWorkerJsonSchemaValidator()
  }
);
listChanged?

optional listChanged?: ListChangedHandlers

Configure handlers for list changed notifications (tools, prompts, resources).

Example
typescript
const client = new Client(
  { name: 'my-client', version: '1.0.0' },
  {
    listChanged: {
      tools: {
        onChanged: (error, tools) => {
          if (error) {
            console.error('Failed to refresh tools:', error);
            return;
          }
          console.log('Tools updated:', tools);
        }
      },
      prompts: {
        onChanged: (error, prompts) => console.log('Prompts updated:', prompts)
      }
    }
  }
);

Functions

getSupportedElicitationModes()

getSupportedElicitationModes(capabilities): object

Defined in: src/client/index.ts:117

Determines which elicitation modes are supported based on declared client capabilities.

According to the spec:

  • An empty elicitation capability object defaults to form mode support (backwards compatibility)
  • URL mode is only supported if explicitly declared

Parameters

capabilities

{[key: string]: unknown; form?: {[key: string]: unknown; applyDefaults?: boolean; }; url?: object; } | undefined

The client's elicitation capabilities

Returns

object

An object indicating which modes are supported

supportsFormMode

supportsFormMode: boolean

supportsUrlMode

supportsUrlMode: boolean