fix: models get an unreadable dump instead of the MCP server's error message (#32942)

* fix: MCP tool errors reach the model as a Python list dump instead of the server's message

When an MCP tool server answered a call with an error, the model got a Python dump of the whole result list, with quotes, annotations and meta fields, instead of the error text the server wrote. Line breaks in the message were escaped as well, so a validation error listing the missing argument was hard to read and models tended to retry the same broken call. The model now gets the server's error text as written, the same way successful results already arrive.

Fixes #32918

* refac: drop the fallback text for MCP errors with no message

The error now carries only the text the server sent.

* fix: keep a readable error when an MCP error has no text

MCP error results that only carry images, audio or resources now give the model a short error message instead of an empty one.
This commit is contained in:
Classic298 2026-10-24 23:21:30 +02:00 • committed by GitHub
parent 637da15918
commit 37af63b649
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -170,9 +170,10 @@ class MCPClient:
result_dict = result.model_dump(mode='json', by_alias=True)
result_content = result_dict['content']
texts = [item['text'] for item in result_content if item['type'] == 'text']
if result.is_error:
raise Exception(result_content)
raise Exception('\n'.join(texts) or 'MCP tool call returned an error.')
structured_content = result_dict.get('structuredContent')
if structured_content is None:
@ -180,7 +181,6 @@ class MCPClient:
# Compare serialized JSON: Python equality treats True and 1 as the same value.
structured_json = JSONCodec.dumps(structured_content, sort_keys=True)
texts = [item['text'] for item in result_content if item['type'] == 'text']
text_jsons = []
for text in texts:
try: