Skip to content

feat: add McpTask for long-running Tools - #8631

Open
lloydrichards wants to merge 32 commits into
Effect-TS:mainfrom
lloydrichards:feat/mcp-tasks
Open

lloydrichards wants to merge 32 commits into
Effect-TS:mainfrom
lloydrichards:feat/mcp-tasks

Conversation

@lloydrichards

@lloydrichards lloydrichards commented Sep 30, 2026 •

Copy link
Copy Markdown
Contributor

Goal

Add long-running MCP tools that clients can poll, answer and cancel, using the Tasks extension (io.modelcontextprotocol/tasks, protocol 2026-07-28).

Server

McpTasks.toolkit(tools, { owner }) registers task tools. A call returns a task handle instead of an immediate result. An McpTasks.Execution service runs the tasks. You can provide it in three ways:

  • McpTasks.layerWorkflow(tools, handlers, { name }): durable. Tasks survive runner restarts and support Workflow Activities and compensation. A tool failure is recorded as a Workflow failure, so it triggers compensation.
  • McpTasks.layerMemory(tools, handlers): in-process. State is lost on restart.
  • A custom Execution backed by your own job service.

Handlers use McpTasks.TaskContext:

const task = yield* McpTasks.TaskContext
yield* task.setStatusMessage("approval", "Waiting for approval")
const approval = yield* task.requestInput({
  key: "approval",
  request: { method: "elicitation/create", params },
  response: Schema.Struct({ action: Schema.String })
})

Keys identify steps, like the names of Workflow Activities. Each requestInput call and each setStatusMessage call needs a key that is unique for the life of the task, and reusing a key is a defect. When a Workflow replay runs the same keyed step again, it is not recorded twice. That keeps the status and the outstanding inputs correct when replay skips or reorders work. When a durable race resolves, inputs requested by the losing branches are withdrawn. If replay resolves the race without running those branches, the inputs are withdrawn the next time the task suspends.

Task handles stay valid when a task tool is replaced. Tasks are advertised only while at least one task tool is registered. Without one, the tasks/* methods return method-not-found.

Client

McpClient.layerHttp({
  // ...
  extensions: { skills: true, tasks: true },
  input: { roots, sampling, elicitation }
})
  • extensions.tasks is true or { pollInterval, requestTimeout }. It is available in make, layerHttp and layerStdio, next to extensions.skills.
  • callTool returns the final result, whether the server answers immediately or creates a task.
  • startTool returns a ToolExecution with taskId, awaitResult and cancel. Several waiters on the same task share one polling worker.
  • input handlers answer the roots, sampling and elicitation requests that a task makes.

Scope

  • Only protocol 2026-07-28 is supported. The experimental tasks in 2025-11-25 follow a different spec and are not implemented.
  • cancel acknowledges that cancellation was requested. Later reads report what actually happened to the task.
  • Not in this version: task notifications, automatic fallback to inline execution, and task-specific deadlines.
  • This addresses the Skills coordination comment.

Validation

At 653859c:

  • vitest run packages/effect/test/ai: 1779 passed, 50 skipped.
  • pnpm test-types for typetest/ai: 250 of 250 pass on both TypeScript targets.
  • pnpm check, pnpm lint, pnpm circular, pnpm jsdocs and pnpm codegen pass with no changes.

This PR also shrinks the fixture in the McpClient discovery LimitError test from 150,000 prompts to 11, with maxItems: 10. The large fixture blocked the event loop for 1–2 s. Under load, that made the 2026-07-28 McpClient.toolkit test time out. That timeout also happens on main.

Known intermittent failures (not diagnosed)

These failures are not explained by the passing runs above. They are listed separately so they are not mistaken for resolved issues.

  • SQL reconstruction timeouts. Two McpTasksWorkflow.test.ts reconstruction tests timed out during development. They have not timed out since, across many sequential and parallel runs. Each step now runs through a stage helper with an 8 s budget. A stalled step fails with its name and the timings of the earlier steps, and any test slower than 4 s writes its stage timings to stderr.
  • Workerd stalls. In one full AI run during review, two McpServer/Workerd.test.ts cases (GET-first and warm-up-POST-first) failed with request stalled. Both passed when run alone and in a full rerun, with no code change. The cause is unknown.

Closes #6834

Closes EFF-3417

@changeset-bot

changeset-bot Bot commented Sep 30, 2026 •

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: 851fad3

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 40 packages
Name Type
effect Patch
@effect/opentelemetry Patch
@effect/vitest Patch
@effect/ai-amazon-bedrock Patch
@effect/ai-anthropic Patch
@effect/ai-cloudflare Patch
@effect/ai-openai-compat Patch
@effect/ai-openai Patch
@effect/ai-openrouter Patch
@effect/ai-typesafe Patch
@effect/atom-react Patch
@effect/atom-solid Patch
@effect/atom-vue Patch
@effect/chat-discord Patch
@effect/chat-slack Patch
@effect/chat-teams Patch
@effect/chat-telegram Patch
@effect/chat-whatsapp Patch
@effect/platform-browser Patch
@effect/platform-bun Patch
@effect/platform-cloudflare Patch
@effect/platform-deno Patch
@effect/platform-node-shared Patch
@effect/platform-node Patch
@effect/sql-clickhouse Patch
@effect/sql-d1 Patch
@effect/sql-libsql Patch
@effect/sql-mssql Patch
@effect/sql-mysql2 Patch
@effect/sql-pg Patch
@effect/sql-pglite Patch
@effect/sql-sqlite-bun Patch
@effect/sql-sqlite-do Patch
@effect/sql-sqlite-node Patch
@effect/sql-sqlite-react-native Patch
@effect/sql-sqlite-wasm Patch
@effect/docgen Patch
@effect/doctest Patch
@effect/graphql-generator Patch
@effect/openapi-generator Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@github-actions

github-actions Bot commented Sep 30, 2026 •

Copy link
Copy Markdown
Contributor

Bundle Size Analysis

Generated from PR build output; treat the content below as untrusted.

File Name Current Size Previous Size Difference
arbitrary-combinators.ts 40.12 KB 40.12 KB 0.00 KB (0.00%)
basic.ts 7.31 KB 7.31 KB 0.00 KB (0.00%)
batching.ts 11.24 KB 11.24 KB 0.00 KB (0.00%)
brand.ts 7.00 KB 7.00 KB 0.00 KB (0.00%)
cache.ts 11.75 KB 11.75 KB 0.00 KB (0.00%)
config.ts 23.15 KB 23.15 KB 0.00 KB (0.00%)
differ.ts 22.26 KB 22.26 KB 0.00 KB (0.00%)
http-client.ts 27.52 KB 27.52 KB 0.00 KB (0.00%)
http-router.ts 38.60 KB 38.60 KB 0.00 KB (0.00%)
logger.ts 12.19 KB 12.19 KB 0.00 KB (0.00%)
metric.ts 9.66 KB 9.66 KB 0.00 KB (0.00%)
optic.ts 7.24 KB 7.24 KB 0.00 KB (0.00%)
pubsub.ts 16.23 KB 16.23 KB 0.00 KB (0.00%)
queue.ts 13.13 KB 13.13 KB 0.00 KB (0.00%)
schedule.ts 11.99 KB 11.99 KB 0.00 KB (0.00%)
schema-bigdecimal.ts 14.31 KB 14.31 KB 0.00 KB (0.00%)
schema-binary.ts 42.74 KB 42.74 KB 0.00 KB (0.00%)
schema-class.ts 21.99 KB 21.99 KB 0.00 KB (0.00%)
schema-codegen.ts 30.75 KB 30.75 KB 0.00 KB (0.00%)
schema-fromJsonSchemaDocument.ts 30.60 KB 30.60 KB 0.00 KB (0.00%)
schema-representation-roundtrip.ts 28.30 KB 28.30 KB 0.00 KB (0.00%)
schema-string-transformation.ts 15.57 KB 15.57 KB 0.00 KB (0.00%)
schema-string.ts 12.51 KB 12.51 KB 0.00 KB (0.00%)
schema-template-literal.ts 16.46 KB 16.46 KB 0.00 KB (0.00%)
schema-toArbitrary.ts 39.67 KB 39.67 KB 0.00 KB (0.00%)
schema-toCodecJson.ts 21.17 KB 21.17 KB 0.00 KB (0.00%)
schema-toEquivalence.ts 21.33 KB 21.33 KB 0.00 KB (0.00%)
schema-toFormatter.ts 21.42 KB 21.42 KB 0.00 KB (0.00%)
schema-toJsonSchemaDocument.ts 26.46 KB 26.46 KB 0.00 KB (0.00%)
schema-toRepresentation.ts 21.51 KB 21.51 KB 0.00 KB (0.00%)
schema.ts 21.09 KB 21.09 KB 0.00 KB (0.00%)
stm.ts 14.48 KB 14.48 KB 0.00 KB (0.00%)
stream.ts 10.61 KB 10.61 KB 0.00 KB (0.00%)

@effect-janitor effect-janitor Bot added enhancement New feature or request 4.0 labels Oct 1, 2026

@effect-janitor effect-janitor Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewed the task execution layers, protocol integration, ownership checks, and added coverage. Reported one blocking dispatch issue affecting replacement of task tools with inline tools. Validation was by source inspection; no tests were run.

Reviewed at 880f9b5: 1 blocking, 0 nits.

Disagree? Reply in the finding's thread, or a maintainer can comment /janitor to re-run or dismiss this review.

lloydrichards and others added 28 commits November 11, 2026 13:03
Fold the client task tests into McpClient.test.ts, cut the server suite to
the negotiation, lifecycle, input, cancellation, ownership and execution
cases, keep two SQL reconstruction cases, and trim the typetests. Drop the
Toolkit withHandlers typetests and the unrelated McpServer test changes.
Replace the task binding with a Task outcome on core tool registrations
and task routes in McpCore, mirror the Skills layout for the tasks/*
methods, and rebuild the Workflow backend on keyed snapshot writes.
Simplify the public API: McpTasks.toolkit(tools, { owner }),
TaskContext.setStatusMessage(message), and extensions.tasks as a boolean
or polling options. Move toolkit registration helpers out of McpServer,
drop Toolkit.withHandlers and McpTasks.workflowRace, and update MCP.md.
…outing

Move to setStatusMessage(message) and extensions.tasks: true. Cover
withdrawal of a lost durable race input and late answers, Workflow
cancellation of waiting and running tasks, and mismatched Mcp-Name
headers on task methods.
Cover task handles surviving replacement by another task tool, status set
in a cached durable race arm, a __proto__ input key in the Workflow
backend, and the Tasks capability check for task notification
subscriptions. These fail until the implementation is fixed.

Give the SQL reconstruction stages wall-clock budgets so a stall names
its stage, and report stage timings when a test runs slowly.
…acement and replay

Route task handles by tool registration generation so a replacing task
toolkit does not take over existing handles. Take a status set in work a
replay skipped from the durable log. Build outstanding inputs with a
null-prototype record. Require the Tasks capability when a subscription
requests task notifications.
…ssages

Extend the durable race regression with a second race whose pending
branch replays while the other appends, and with a status message that
is reused after an earlier setter is skipped by replay.
Record input requests, withdrawals, status messages and completion as
changes that readers fold, instead of full snapshots written from a run's
partially replayed state. Identify input changes by key, match status
messages against the recorded status changes after the last match, and
withdraw recorded inputs a suspending run no longer waits for.
Extend the durable race regression with a reused message as the first
setter after skipped setters, and add cases for concurrent setters that
replay reorders and for a lost race input when the task next waits on a
durable clock. These fail until the implementation is fixed.
Restore TaskContext.setStatusMessage(key, message). Status changes are
recorded once per key, so replay that skips or reorders work no longer
changes the reported status. Keys must be unique per task in both
backends. Withdraw recorded inputs a Workflow run no longer waits for
whenever the run suspends, including on clocks and deferreds.
@lloydrichards

Copy link
Copy Markdown
Contributor Author

At the rate that PRs are being made and the amount of time I spend now on just fixing merge conflicts for these open prs, I'm gonne just say I'm marking this as stale and not wanted, unless I hear otherwise I'll close the PR in about a week. The issue is still open so this can be picked up again whenever.

This branch is waiting to be deployed

1 waiting deployment
fork — 851fad3e Waiting Nov 21, 2026 by lloydrichards via approval-gate #26984
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

add first-class support for tasks extension

5 participants