fix(mcp): communicate tool errors to LLM to prevent hallucinated success (#2036)
Browse filesWhen MCP tool calls fail (e.g., timeout), the error was shown in the UI
but not sent to the LLM. This caused the LLM to hallucinate success
because it never received feedback about the failure.
Now error messages are included in toolMessages so the LLM can
acknowledge failures and respond appropriately.
src/lib/server/textGeneration/mcp/toolInvocation.ts
CHANGED
|
@@ -300,6 +300,9 @@ export async function* executeToolCalls({
|
|
| 300 |
toolRuns.push({ name, parameters: r.paramsClean, output });
|
| 301 |
// For the LLM follow-up call, we keep only the textual output
|
| 302 |
toolMessages.push({ role: "tool", tool_call_id: id, content: output });
|
|
|
|
|
|
|
|
|
|
| 303 |
}
|
| 304 |
}
|
| 305 |
|
|
|
|
| 300 |
toolRuns.push({ name, parameters: r.paramsClean, output });
|
| 301 |
// For the LLM follow-up call, we keep only the textual output
|
| 302 |
toolMessages.push({ role: "tool", tool_call_id: id, content: output });
|
| 303 |
+
} else {
|
| 304 |
+
// Communicate error to LLM so it doesn't hallucinate success
|
| 305 |
+
toolMessages.push({ role: "tool", tool_call_id: id, content: `Error: ${r.error}` });
|
| 306 |
}
|
| 307 |
}
|
| 308 |
|