-
Notifications
You must be signed in to change notification settings - Fork 161
fix: Set final statusMessage before storeTaskResult (#638) #680
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
jirispilka
wants to merge
14
commits into
master
Choose a base branch
from
fix/task-final-status-message-638
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
ec33d64
fix: set final statusMessage before storeTaskResult (#638)
claude 87017c7
test: remove misleading third test case from task.statusMessage tests
jirispilka 9a234ab
fix: ensure final statusMessage is set correctly during task result s…
jirispilka 9917447
test: add unit tests for task execution handling in ActorsMcpServer
jirispilka 35c643d
fix: prevent updating task status to 'cancelled' if already terminal
jirispilka e3d12aa
fix: update cancellation message to include tool prefix if available
jirispilka 77c3bb6
fix: update task status handling to correctly reflect 'failed' state
jirispilka 4d95f05
fix: store all task results as 'completed' to work around SDK limitation
jirispilka ef827fa
fix: enhance task result handling for x402 payment errors and improve…
jirispilka 5c87cfe
fix: normalize cancellation messages and handle task result storage e…
jirispilka 8805dec
fix: improve task result storage with cancellation handling and error…
jirispilka 558070b
fix: apply follow-up review thread changes
Copilot f8c119c
fix: log non-cancellation storage failures and ensure task result han…
jirispilka 0b8d77e
fix: handle task failure when storing results and update task status …
jirispilka File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,111 @@ | ||
| # Task status workaround: why errors are stored as 'completed' | ||
|
|
||
| ## The problem | ||
|
|
||
| The MCP SDK's `requestStream()` (in `shared/protocol.js`) handles terminal task states differently: | ||
|
|
||
| - **`'completed'`** → calls `getTaskResult()` → yields `{ type: 'result', result }` → client gets the full `CallToolResult` | ||
| - **`'failed'`** → yields `{ type: 'error', error: "Task {id} failed" }` → client gets a generic error, **stored result is discarded** | ||
| - **`'cancelled'`** → same as failed, generic error | ||
|
|
||
| This means any result stored with `status: 'failed'` is never delivered to the client. The actual error text, x402 payment payload, structured content — all lost. | ||
|
|
||
| ## What we do | ||
|
|
||
| We store **all** task results as `'completed'`, including errors. The error nature is conveyed through: | ||
|
|
||
| 1. **`isError: true`** in the `CallToolResult` payload — this is what clients (mcpc, Claude, Cursor) use to determine success/failure | ||
| 2. **`[error]` prefix** in `statusMessage` — so `tasks/list` and `tasks/get` polling clearly shows the task failed | ||
|
|
||
| ### Affected paths in `executeToolAndUpdateTask()` | ||
|
|
||
| | Path | Result | Status | statusMessage | | ||
| |---|---|---|---| | ||
| | Success | tool output | `'completed'` | `tool-name: completed` | | ||
| | SOFT_FAIL (actor not found, validation) | error text + `isError: true` | `'completed'` | `[error] tool-name: Actor not found...` | | ||
| | Payment required (pre-flight) | x402 payload + `isError: true` | `'completed'` | `[error] tool-name: payment required` | | ||
| | Payment required (catch 402) | x402 payload + `isError: true` | `'completed'` | `[error] tool-name: payment required` | | ||
| | Hard error (5xx, network, etc.) | error text + `isError: true` | `'completed'` | `[error] tool-name: error text...` | | ||
| | Aborted (signal) | none | `'cancelled'` | `tool-name: aborted by client` | | ||
|
|
||
| ### Why x402 payment specifically requires 'completed' | ||
|
|
||
| The mcpc bridge's `handlePaymentRequiredRetry()` inspects the tool result for `isError: true` + `x402Version` + `accepts` fields. If the task is stored as `'failed'`, the SDK never delivers the result, and the auto-pay retry never triggers. | ||
|
|
||
| ## What should be fixed upstream | ||
|
|
||
| ### SDK: `requestStream()` should deliver results for 'failed' tasks | ||
|
|
||
| Location: `@modelcontextprotocol/sdk/shared/protocol.js`, lines ~566-583 | ||
|
|
||
| ```javascript | ||
| // Current behavior (broken): | ||
| if (task.status === 'failed') { | ||
| yield { type: 'error', error: new McpError(ErrorCode.InternalError, `Task ${taskId} failed`) }; | ||
| } | ||
|
|
||
| // Desired behavior: | ||
| if (task.status === 'failed') { | ||
| const result = await this.getTaskResult({ taskId }, resultSchema, options); | ||
| yield { type: 'result', result }; // result has isError: true | ||
| } | ||
| ``` | ||
|
|
||
| The `'failed'` status was stored alongside a result via `storeTaskResult()`. The SDK should deliver that result, not discard it. The `isError` flag in the result already tells the client it's an error. | ||
|
|
||
| Alternatively, `requestStream()` could yield both the result and a status indicator, but the simplest fix is to treat `'failed'` the same as `'completed'` for result delivery. | ||
|
|
||
| ### SDK: `requestStream()` 'failed' error should include statusMessage | ||
|
|
||
| Even without delivering the full result, the generic `"Task {id} failed"` message should at least include `task.statusMessage`: | ||
|
|
||
| ```javascript | ||
| yield { | ||
| type: 'error', | ||
| error: new McpError(ErrorCode.InternalError, task.statusMessage || `Task ${taskId} failed`) | ||
| }; | ||
| ``` | ||
|
|
||
| ### SDK: `storeTaskResult()` should accept a statusMessage | ||
|
|
||
| Currently we use `storeTaskResultWithMessage()` which calls `updateTaskStatus('working', message)` then `storeTaskResult(status, result)` — two non-atomic calls. The SDK's `storeTaskResult()` should accept an optional `statusMessage` parameter to make this atomic. | ||
|
|
||
| This also leaves a small race with `tasks/cancel`: after the tool has already produced its payload, the task is briefly back in `'working'`, so a concurrent cancel can still win and the computed result is lost. | ||
|
|
||
| ### mcpc: `pollTask()` should fetch result for 'completed' tasks | ||
|
|
||
| **Scope:** `pollTask()` is the detached task polling fallback, used when a task was started via | ||
| `callToolDetached()` and polled manually later. The normal `callTool()` path is **not affected** — | ||
| it uses `callToolStream()` → SDK `requestStream()` → correctly calls `getTaskResult()` for | ||
| `'completed'` tasks. | ||
|
|
||
| Location: `mcp-cli/src/core/mcp-client.ts`, lines ~715-720 | ||
|
|
||
| ```javascript | ||
| // Current behavior (incomplete — detached polling only): | ||
| if (task.status === 'completed') { | ||
| return { content: [{ type: 'text', text: task.statusMessage || 'Task completed' }] }; | ||
| } | ||
|
|
||
| // Desired behavior: | ||
| if (task.status === 'completed') { | ||
| const result = await this.getTaskResult(taskId); | ||
| return result; | ||
| } | ||
| ``` | ||
|
|
||
| The detached polling fallback returns `statusMessage` as fake content instead of fetching the actual | ||
| stored result via `tasks/result`. The actual tool output (dataset items, actor run info, etc.) is lost. | ||
|
|
||
| **Normal mcpc path is correct:** | ||
| ``` | ||
| mcpc callTool → callToolStream → requestStream → completed → getTaskResult ✅ | ||
| mcpc pollTask → tasks/get loop → completed → statusMessage as content ❌ (detached only) | ||
| ``` | ||
|
|
||
| ## When to remove this workaround | ||
|
|
||
| Once the SDK's `requestStream()` delivers results for `'failed'` tasks, we can: | ||
| 1. Store errors as `'failed'` (semantically correct) | ||
| 2. Remove the `[error]` prefix from statusMessages | ||
| 3. Remove `storeTaskResultWithMessage()` if `storeTaskResult()` accepts statusMessage | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.