fix(mcp): combine content and structuredContent when both present (#7118)
When an MCP server returns both content (model-oriented text) and structuredContent (machine-oriented JSON), the client now combines them instead of discarding content. The text content becomes the primary result (what the agent reads), and structuredContent is included as supplementary metadata. Previously, structuredContent took full precedence — causing data loss for servers like Desktop Commander that put the actual file text in content and metadata in structuredContent. MCP spec guidance: for conversational/agent UX, prefer content.
This commit is contained in:
@@ -1255,9 +1255,17 @@ def _make_tool_handler(server_name: str, tool_name: str, tool_timeout: float):
|
||||
parts.append(block.text)
|
||||
text_result = "\n".join(parts) if parts else ""
|
||||
|
||||
# Prefer structuredContent (machine-readable JSON) over plain text
|
||||
# Combine content + structuredContent when both are present.
|
||||
# MCP spec: content is model-oriented (text), structuredContent
|
||||
# is machine-oriented (JSON metadata). For an AI agent, content
|
||||
# is the primary payload; structuredContent supplements it.
|
||||
structured = getattr(result, "structuredContent", None)
|
||||
if structured is not None:
|
||||
if text_result:
|
||||
return json.dumps({
|
||||
"result": text_result,
|
||||
"structuredContent": structured,
|
||||
})
|
||||
return json.dumps({"result": structured})
|
||||
return json.dumps({"result": text_result})
|
||||
|
||||
|
||||
Reference in New Issue
Block a user