ExperimentalZod schema type (inferred)
Task tool definition with name, description, input schema, and task handler
The registered task tool definition (for re-export and type inference)
MCP Tasks is an experimental SDK feature
import { defineTask, text, z } from 'mcp-server-framework';
export const longComputation = defineTask({
name: 'long_computation',
description: 'Perform a long-running computation',
input: z.object({ data: z.string() }),
taskSupport: 'required',
taskHandler: {
createTask: async (args, ctx) => {
const task = await ctx.taskStore.createTask({ ttl: 300_000 });
startBackgroundWork(task.taskId, args);
return { task };
},
getTask: async (_args, ctx) => ctx.taskStore.getTask(ctx.taskId),
getTaskResult: async (_args, ctx) => ctx.taskStore.getTaskResult(ctx.taskId),
},
});
// Optional task support — tool can be called normally OR as a task
export const flexibleTool = defineTask({
name: 'flexible_process',
description: 'Process that supports both sync and async execution',
input: z.object({ items: z.array(z.string()) }),
taskSupport: 'optional',
taskHandler: {
createTask: async (args, ctx) => {
const task = await ctx.taskStore.createTask({});
processInBackground(task.taskId, args);
return { task };
},
getTask: async (_args, ctx) => ctx.taskStore.getTask(ctx.taskId),
getTaskResult: async (_args, ctx) => ctx.taskStore.getTaskResult(ctx.taskId),
},
});
Define a task tool with automatic registration.
Task tools support long-running operations via the MCP Tasks protocol. Instead of blocking until completion, the client receives a task ID and can poll for status updates and results.
The tool is automatically registered in the global task tool registry. The framework uses the SDK's
experimental.tasks.registerToolTask()internally.