Skip to content

serve: a failed MCP tool result goes back to the model; before, wire … - #69

Merged
Vasyl-Trefilov merged 1 commit into
RustingStudio:mainfrom
reidm:mcp-silent-fail
Nov 22, 2026
Merged

Vasyl-Trefilov merged 1 commit into
RustingStudio:mainfrom
reidm:mcp-silent-fail

Conversation

@reidm

@reidm reidm commented Nov 22, 2026 •

Copy link
Copy Markdown
Contributor

…dropped every message with error set, so the model's own call stayed unanswered and it repeated the same call

error on a chat message means two different things, and wire() treated them as one. On an assistant message it
marks a turn that never happened (the server was unreachable, no model loaded): there is nothing to send, dropping
it is right. On a tool message it marks the tool's answer, which is the reply the model asked for. Dropping that
left the assistant message with tool_calls and no role: "tool" result for it, so the model saw an unanswered call
and re-issued the same one instead of noticing the failure. The page showed the failure in red (the flag also
drives .tcall.bad) while the request sent back to the server did not carry it.

The filter now keeps tool messages whatever error says, and their content is prefixed "Error: " so a failed
result cannot read as a payload that happens to be prose; the guard leaves text already starting with "Error: "
or "The tool failed: " alone, which is what the protocol-error path produces. This matches what serve.rs does for
the Anthropic bridge.

chatMd had the same conflation, so a .md export left a failed tool result out and hid what went wrong in a chat
someone else would read; it now prints it as tool failed . The copy button and the continue button keep
skipping errored messages: no copy is wanted for a failure.

selftest gained 'tool errors go to the model, transport errors do not', which asserts both halves: the two failed
results survive with their tool_call_id attached and one gains the prefix, while the errored assistant message is
still dropped (w.length 4 of 5). The page is compiled in by include_str!, so a rebuild is needed to see this.

What changed and why

In the web-ui, agents will be made aware of failing tool calls. Tool call results no longer filtered out of the messages array sent back to the model.

This handles cases where, for example, a required parameter was missing for an MCP call. Now agents will know a call failed, and can act to correct it. Experience while using https://github.com/jchip/tacks

Here's an example of the problem:
image

Tool call fails (in this case, change was rejected), but the agent doesn't get notified and considers the operation a success.

Verification

Interrogated my model, who reported errors in tool calls were correctly reported.

image

It's difficult to set up a scenario where this error occurred 100% of the time because the content of the error existed in the content return. In practice, it works for me without any functional regression.

Checklist

  • I followed this repository's contribution guide and required checks.
  • I updated documentation or examples where needed.
  • I added regression coverage where it helps verify the change. <-- no regression coverage for web-ui
  • I have the right to contribute the included code and assets under this repository's contribution terms.

…dropped every message with error set, so the model's own call stayed unanswered and it repeated the same call

`error` on a chat message means two different things, and wire() treated
them as one. On an assistant message it
marks a turn that never happened (the server was unreachable, no model
loaded): there is nothing to send, dropping
it is right. On a tool message it marks the tool's answer, which is the
reply the model asked for. Dropping that
left the assistant message with tool_calls and no role: "tool" result
for it, so the model saw an unanswered call
and re-issued the same one instead of noticing the failure. The page
showed the failure in red (the flag also
drives .tcall.bad) while the request sent back to the server did not
carry it.

The filter now keeps tool messages whatever `error` says, and their
content is prefixed "Error: " so a failed
result cannot read as a payload that happens to be prose; the guard
leaves text already starting with "Error: "
or "The tool failed: " alone, which is what the protocol-error path
produces. This matches what serve.rs does for
the Anthropic bridge.

chatMd had the same conflation, so a .md export left a failed tool
result out and hid what went wrong in a chat
someone else would read; it now prints it as **tool failed <name>**. The
copy button and the continue button keep
skipping errored messages: no copy is wanted for a failure.

selftest gained 'tool errors go to the model, transport errors do not',
which asserts both halves: the two failed
results survive with their tool_call_id attached and one gains the
prefix, while the errored assistant message is
still dropped (w.length 4 of 5). The page is compiled in by
include_str!, so a rebuild is needed to see this.
@aletheore

aletheore Bot commented Nov 22, 2026

Copy link
Copy Markdown

🔍 Aletheore evidence diff

What changed
Deterministic, computed from this commit's real scan and import graph - posted on every run, whether or not Flash Review found anything.

  • src/web/index.html · modified · +14/-3

Aggregate deltas

  • Modules: +0
  • Dependency graph edges: +0
  • Commits: 1

@reidm
reidm marked this pull request as ready for review November 22, 2026 12:06
Comment thread src/web/index.html
// `error` means two different things. On an assistant message the turn never happened, so there is nothing to send.
// On a tool message it means the tool failed, and that failure is the answer the model is waiting for: dropping it
// leaves the call unanswered, and a model with an unanswered call repeats the same call instead of noticing.
return msgs.filter(x => !x.error || x.role === 'tool').map(x => {

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🟠 High · Correctness · 1 of 4

Tool results are kept even when their assistant turn with the calls is dropped as an error, which orphans them. An assistant message that issued tool calls is flagged error (for example, the stream dies after the calls were parsed) while tool results for those calls were already appended. The filter now drops the assistant message with its tool_calls but keeps the role 'tool' messages. The request then contains tool messages with no preceding assistant tool_calls, which the chat API rejects, so the conversation cannot continue.

Prompt for your AI agent
File: src/web/index.html
Line: 887
Issue: Tool results are kept even when their assistant turn with the calls is dropped as an error, which orphans them. An assistant message that issued tool calls is flagged error (for example, the stream dies after the calls were parsed) while tool results for those calls were already appended. The filter now drops the assistant message with its tool_calls but keeps the role 'tool' messages. The request then contains tool messages with no preceding assistant tool_calls, which the chat API rejects, so the conversation cannot continue.
Make the smallest change that fixes this, add a test, and change nothing unrelated.

Reply /dismiss (optionally with a reason) if this isn't helpful - Aletheore won't raise it again on this repo.

Comment thread src/web/index.html
// leaves the call unanswered, and a model with an unanswered call repeats the same call instead of noticing.
return msgs.filter(x => !x.error || x.role === 'tool').map(x => {
const imgs = x.images || [];
if (imgs.length) return { role: x.role, content: [{ type: 'text', text: x.content }, ...imgs.map(url => ({ type: 'image_url', image_url: { url } }))] };

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🟠 High · Correctness · 2 of 4

Image branch returns before the tool branch, so failed tool results with images lose tool_call_id and the error prefix. A tool message that has images and error set hits the earlier if (imgs.length) return. It is sent as a user-style content array with no tool_call_id and no 'Error:' marker, so the model never sees that the call failed, and the API may reject the unmatched tool message. The new error handling on line 891 never runs for this case.

Prompt for your AI agent
File: src/web/index.html
Line: 889
Issue: Image branch returns before the tool branch, so failed tool results with images lose tool_call_id and the error prefix. A tool message that has images and error set hits the earlier `if (imgs.length)` return. It is sent as a user-style content array with no tool_call_id and no 'Error:' marker, so the model never sees that the call failed, and the API may reject the unmatched tool message. The new error handling on line 891 never runs for this case.
Make the smallest change that fixes this, add a test, and change nothing unrelated.

Reply /dismiss (optionally with a reason) if this isn't helpful - Aletheore won't raise it again on this repo.

Comment thread src/web/index.html
if (imgs.length) return { role: x.role, content: [{ type: 'text', text: x.content }, ...imgs.map(url => ({ type: 'image_url', image_url: { url } }))] };
if (x.role === 'tool') return { role: 'tool', tool_call_id: x.id, content: x.content };
// Say it is an error in the text too: a failed result otherwise looks like a payload that happens to be prose.
if (x.role === 'tool') return { role: 'tool', tool_call_id: x.id, content: x.error && !/^(Error: |The tool failed: )/.test(x.content) ? `Error: ${x.content}` : x.content };

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🔵 Low · Maintainability · 3 of 4

Error status is recovered by sniffing the content string with a regex instead of being carried as data. Whether a failed result gets the 'Error: ' prefix depends on the text matching /^(Error: |The tool failed: )/. Any upstream change to how tool failures are worded silently changes what the model receives, and a tool whose output happens to begin with 'Error: ' is treated as already formatted. The prefix rule is duplicated across code that produces these strings and this regex.

Prompt for your AI agent
File: src/web/index.html
Line: 891
Issue: Error status is recovered by sniffing the content string with a regex instead of being carried as data. Whether a failed result gets the 'Error: ' prefix depends on the text matching /^(Error: |The tool failed: )/. Any upstream change to how tool failures are worded silently changes what the model receives, and a tool whose output happens to begin with 'Error: ' is treated as already formatted. The prefix rule is duplicated across code that produces these strings and this regex.
Make the smallest change that fixes this, add a test, and change nothing unrelated.

Reply /dismiss (optionally with a reason) if this isn't helpful - Aletheore won't raise it again on this repo.

Comment thread src/web/index.html
return w[1].tool_calls[0].function.arguments === '{"k":1}' && w[1].tool_calls[0].id === 'call_0_0' && w[2].role === 'tool' && w[2].tool_call_id === 'call_0_0' && w[2].content === 'r' && !w[3].tool_calls;
}],
// `error` on a tool message is the tool's answer, so it goes; on an assistant message the turn never happened.
['tool errors go to the model, transport errors do not', () => {

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🔵 Low · Maintainability · 4 of 4

New test covers only the text-tool-error path, not images, missing error text, or assistant-with-calls errors. The added test passes with the current code but does not exercise the image branch, a tool error whose content is undefined (which yields 'Error: undefined'), or an errored assistant message that carries calls. Regressions in those paths, including the orphaned-tool-message problem, would ship unnoticed.

Prompt for your AI agent
File: src/web/index.html
Line: 1304
Issue: New test covers only the text-tool-error path, not images, missing error text, or assistant-with-calls errors. The added test passes with the current code but does not exercise the image branch, a tool error whose content is undefined (which yields 'Error: undefined'), or an errored assistant message that carries calls. Regressions in those paths, including the orphaned-tool-message problem, would ship unnoticed.
Make the smallest change that fixes this, add a test, and change nothing unrelated.

Reply /dismiss (optionally with a reason) if this isn't helpful - Aletheore won't raise it again on this repo.

@aletheore

aletheore Bot commented Nov 22, 2026

Copy link
Copy Markdown

Aletheore Flash Lite review

What this PR does. This change makes failed MCP tool results go back to the model as tool responses, where previously they were silently dropped and left the call unanswered. Failed assistant turns are still excluded, and the failure text gets an "Error:" prefix so the model reads it as an error. The change also updates the markdown chat export to label failed tool results, and adds a test for the new behavior. It affects the model's view of tool-call history in the web chat client and users who export chats.

Merge confidence Review effort Merge risk
3 / 5 1 / 5 Medium
Area Files Lines
src/web 1 +14 −3

4 finding(s) posted as inline review comment(s).

# Severity Category Where
1 🟠 High Correctness index.html:887 Tool results are kept even when their assistant turn with the calls is dropped as an error, which orphans…
2 🟠 High Correctness index.html:889 Image branch returns before the tool branch, so failed tool results with images lose tool_call_id and the…
3 🔵 Low Maintainability index.html:891 Error status is recovered by sniffing the content string with a regex instead of being carried as data…
4 🔵 Low Maintainability index.html:1304 New test covers only the text-tool-error path, not images, missing error text, or assistant-with-calls…
Review details

Reviewed 1 file(s); skipped 0. Dismissed 0; outside PR diff 0. Grounding: 4 of 4 proposed finding(s).

Fix all with your agent
File: src/web/index.html
Line: 887
Issue: Tool results are kept even when their assistant turn with the calls is dropped as an error, which orphans them. An assistant message that issued tool calls is flagged error (for example, the stream dies after the calls were parsed) while tool results for those calls were already appended. The filter now drops the assistant message with its tool_calls but keeps the role 'tool' messages. The request then contains tool messages with no preceding assistant tool_calls, which the chat API rejects, so the conversation cannot continue.
Make the smallest change that fixes this, add a test, and change nothing unrelated.

File: src/web/index.html
Line: 889
Issue: Image branch returns before the tool branch, so failed tool results with images lose tool_call_id and the error prefix. A tool message that has images and error set hits the earlier `if (imgs.length)` return. It is sent as a user-style content array with no tool_call_id and no 'Error:' marker, so the model never sees that the call failed, and the API may reject the unmatched tool message. The new error handling on line 891 never runs for this case.
Make the smallest change that fixes this, add a test, and change nothing unrelated.

File: src/web/index.html
Line: 891
Issue: Error status is recovered by sniffing the content string with a regex instead of being carried as data. Whether a failed result gets the 'Error: ' prefix depends on the text matching /^(Error: |The tool failed: )/. Any upstream change to how tool failures are worded silently changes what the model receives, and a tool whose output happens to begin with 'Error: ' is treated as already formatted. The prefix rule is duplicated across code that produces these strings and this regex.
Make the smallest change that fixes this, add a test, and change nothing unrelated.

File: src/web/index.html
Line: 1304
Issue: New test covers only the text-tool-error path, not images, missing error text, or assistant-with-calls errors. The added test passes with the current code but does not exercise the image branch, a tool error whose content is undefined (which yields 'Error: undefined'), or an errored assistant message that carries calls. Regressions in those paths, including the orphaned-tool-message problem, would ship unnoticed.
Make the smallest change that fixes this, add a test, and change nothing unrelated.

@Vasyl-Trefilov
Vasyl-Trefilov merged commit ec5b528 into RustingStudio:main Nov 22, 2026
3 checks passed
@Vasyl-Trefilov

Copy link
Copy Markdown
Member

@reidm Merged as it is, ty. I read the change and ran the page's #selftest in headless Chromium on the merge ("selftest: ok"). I did not run a failing MCP server against it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants