MCP TypeScript SDK (V2)
    Preparing search index...

    Experimental task features for MCP clients.

    Access via client.experimental.tasks:

    const stream = client.experimental.tasks.callToolStream({ name: 'tool', arguments: {} });
    const task = await client.experimental.tasks.getTask(taskId);
    Index

    Constructors

    Methods

    • Experimental

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

      This method provides streaming access to tool execution, allowing you to observe intermediate task status updates for long-running tool calls. Automatically validates structured output if the tool has an outputSchema.

      Parameters

      • params: {
            _meta?: {
                "io.modelcontextprotocol/related-task"?: { taskId: string };
                progressToken?: string | number;
                [key: string]: unknown;
            };
            arguments?: { [key: string]: unknown };
            name: string;
            task?: { ttl?: number };
        }

        Tool call parameters (name and arguments)

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

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

        • Optionalarguments?: { [key: string]: unknown }

          Arguments to pass to the tool.

        • name: string

          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: RequestOptions

        Optional request options (timeout, signal, task creation params, etc.)

      Returns AsyncGenerator<
          ResponseMessage<
              {
                  _meta?: {
                      "io.modelcontextprotocol/related-task"?: { taskId: string };
                      progressToken?: string | number;
                      [key: string]: unknown;
                  };
                  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?: 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";
                      }
                      | {
                          _meta?: { [key: string]: unknown };
                          annotations?: {
                              audience?: ((...) | (...))[];
                              lastModified?: string;
                              priority?: number;
                          };
                          description?: string;
                          icons?: {
                              mimeType?: string;
                              sizes?: (...)[];
                              src: string;
                              theme?: "light" | "dark";
                          }[];
                          mimeType?: string;
                          name: string;
                          title?: string;
                          type: "resource_link";
                          uri: string;
                      }
                  )[];
                  isError?: boolean;
                  structuredContent?: Record<string, unknown>;
                  [key: string]: unknown;
              },
          >,
          void,
          void,
      >

      AsyncGenerator that yields ResponseMessage objects

      const stream = client.experimental.tasks.callToolStream({ name: 'myTool', arguments: {} });
      for await (const message of stream) {
      switch (message.type) {
      case 'taskCreated': {
      console.log('Tool execution started:', message.task.taskId);
      break;
      }
      case 'taskStatus': {
      console.log('Tool status:', message.task.status);
      break;
      }
      case 'result': {
      console.log('Tool result:', message.result);
      break;
      }
      case 'error': {
      console.error('Tool error:', message.error);
      break;
      }
      }
      }
    • Experimental

      Cancels a running task.

      Parameters

      • taskId: string

        The task identifier

      • Optionaloptions: RequestOptions

        Optional request options

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

    • Experimental

      Gets the current status of a task.

      Parameters

      • taskId: string

        The task identifier

      • Optionaloptions: RequestOptions

        Optional request options

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

      The task status

    • Experimental

      Lists tasks with optional pagination.

      Parameters

      • Optionalcursor: string

        Optional pagination cursor

      • Optionaloptions: RequestOptions

        Optional request options

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

      List of tasks with optional next cursor

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

      This method provides streaming access to request processing, allowing you to observe intermediate task status updates for task-augmented requests.

      Type Parameters

      Parameters

      • request: {
            method: string;
            params?: {
                _meta?: {
                    "io.modelcontextprotocol/related-task"?: { taskId: string };
                    progressToken?: string | number;
                    [key: string]: unknown;
                };
                [key: string]: unknown;
            };
        }

        The request to send

      • resultSchema: T

        Zod schema for validating the result

      • Optionaloptions: RequestOptions

        Optional request options (timeout, signal, task creation params, etc.)

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

      AsyncGenerator that yields ResponseMessage objects

      const stream = client.experimental.tasks.requestStream(request, CallToolResultSchema, 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;
      }
      }
      }