0
Fork 0
mirror of https://github.com/mastra-ai/mastra.git synced 2026-11-17 08:44:46 +00:00

feat(mcp): record each MCP tool task as one trace

A task was spread over many unrelated traces: the creating tools/call was
recorded as an ERROR, attempts had no span at all, every tasks/update and
tasks/cancel started a new root, and every tasks/get poll added another.
Raw task ids, which are bearer tokens, also reached span input and errors.

- The tools/call that creates a task is the root span, ends OK, and carries
  the task's hash as metadata.mcpTaskKey. Its {traceId, spanId} is kept with
  the task (background envelope, in-memory record).
- Every attempt, including 'auto' first runs, resumes, and attempts
  recovered on another instance, is a "task attempt <tool> #n" child of the
  root, with attempt/resumed/recovered/mcpTaskStatus in metadata. The tool's
  own spans nest under it. Parentage uses resumedFromSpanId, as workflow and
  agent resume do; tracingOptions.parentSpanId would store extra roots.
- tasks/update and tasks/cancel join the task trace via the store's new
  optional traceOf; tasks/get is untraced. Their span input is the task key.
- The root is flushed when the task starts: a trace whose root was never
  stored drops out of listTraces, which Studio's trace list uses.
- Store errors and the handle placeholder no longer contain the task id.
- taskKeyOf moves to shared/tasks/task-id.ts, identical to the client side.

Store interface additions are optional: MCPTaskCreateParams.trace,
MCPTaskAttempt.trace, MCPTaskStore.traceOf. A store without them runs
tasks untraced.

Each change has a test that fails with it reverted (resumedFromSpanId,
handle detection, untraced tasks/get, task-key span input, parentOf, root
flush).

Co-Authored-By: mastracode <284800079+mastra-platform[bot]@users.noreply.github.com>
This commit is contained in:
Daniel Lew 2026-10-27 17:56:15 -04:00
commit 53265bdfea
16 changed files with 858 additions and 65 deletions