Return run outputs in MCP run reads and wait results

Merged
RY
21d ago
+474-52

Summary

Closes #3630.

An MCP client couldn't get a run's result from the run itself. dagu_read target=run, dagu_execute with wait: true and the dagu://runs/... resources returned status and steps but no outputs, so a chat client had to dig through step logs or call REST.

How it happens: start any DAG with a step that has output: GREETING, wait for it to finish, then call dagu_read with target=run. The response has no outputs, while GET /dag-runs/{name}/{dagRunId}/outputs returns {"greeting": "hello"}.

Changes

  • New API.GetDAGRunDetailsWithOutputsData loads a run's details and its outputs in one pass, under the same read timeout and visibility checks as GetDAGRunDetailsData and GetSubDAGRunDetailsData. For a root run it builds the details through getDAGRunDetailsData, the same code REST and SSE use, which now also hands back the attempt and status it read, so the outputs come from that attempt with no second lookup. For a sub run it checks the child's workspace first, then does one extra attempt lookup, since FindSubDAGRunStatus doesn't return its attempt. GetSubDAGRunDetailsData now shares getSubDAGRunDetails with it.
  • Outputs are only read for a run with a terminal status (finished and won't change again), since outputs.json is written before that status. The check is a new ir.Status.IsTerminal(), and the MCP wait loop's isTerminalStatus now calls it too, so the two can't disagree when a status is added.
  • dagu_read target=run (root and subRunId), dagu_execute wait results and both run resources now carry outputs. A run without outputs keeps today's shape.
  • Limits: 50 values, 4 KiB per value, 32 KiB in total. A value that doesn't fit is left out whole, never cut, and outputsOmitted says how many were left out. If nothing fits, there's no outputs field, only outputsOmitted.
  • If outputs can't be read, or they were written by a different attempt than the attemptId being returned, the run still comes back with outputsError text. That text is only "outputs could not be read", since the raw file error can include server paths. The API logs the full error, including a failed sub-run attempt lookup, tagged with the root and sub run name and ID.
  • The resources keep the REST dagRunDetails body and add the same fields at the top level beside it. The body is serialized once, and an unexpected details type is passed through unchanged.
  • Specs 020, 021 and 036 and the tool reference in internal/service/mcp/reference.go describe the new fields.

Not in this PR: step-level outputs, as the issue says. The caps count raw bytes, not JSON-escaped size, and outputsOmitted is a count, not a list of names. The whole outputs.json is still read before the caps apply, the same as the REST endpoint.

Testing

  • TestBoundedRunOutputs, TestBoundedRunOutputsLimits and TestWithRunOutputs (internal/service/mcp) cover the resource body shape and no outputs, normal outputs, a read error, another attempt's outputs, the count, per-value and total limits, and the case where no value fits. TestStatus_IsTerminal (internal/ir) covers every status plus an unknown one.
  • TestReadRunOutputs (spec021) reads a parent and a sub-DAG run through the read tool and the resources. TestReadRunWithoutOutputs checks a run with no outputs gets no new fields. TestExecuteStartWithWaitReturnsRunResult (spec036) now checks run.outputs.
  • Before the fix: run has no outputs: map[attemptId:... status:4 ...], and in spec036 expected: map[result:done] actual: <nil>. After: all pass.
  • Ran go vet, go test and golangci-lint on ./internal/ir/..., ./internal/service/mcp/..., ./internal/service/frontend/..., ./conformance/spec020_mcp_server/..., ./conformance/spec021_mcp_read_tool/... and ./conformance/spec036_mcp_execute_tool/.... I didn't run make test, make conformance or the UI checks. The sub-run access rule and the new log lines aren't covered by a test.

Environment: main @ 52aee31 + f8ad9b98, go1.27.3, darwin/arm64.

Checklist

  • Code follows the project style guidelines
  • Self-review of the code has been performed
  • Tests have been added or updated as needed
  • Documentation has been updated as needed
  • Changes have been tested locally