Skip to content

Add Tasks to an MCP client ​

Start with a connected Client from @modelcontextprotocol/client. Create one task-enabled session for that connection:

ts
import {
  createTaskSessionFromClient,
  resultFromTaskOutcome,
} from "@modelcontextprotocol/ext-tasks/client";

const session = createTaskSessionFromClient(client, {
  endpointId: serverId,
});

endpointId is a stable identifier for the MCP server. It is used when task references are resumed on a later connection.

Call tools through the session:

ts
try {
  const execution = await session.callTool("generate_report", {
    quarter: "Q2",
  });
  const { outcome } = await execution.settle();

  const result = resultFromTaskOutcome(outcome);
} finally {
  await session.close();
}

callTool() returns a ToolExecution for immediate and task-backed results. settle() waits for its terminal outcome.

Next steps ​