Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 24 additions & 2 deletions tools/mcp_tool.py
Original file line number Diff line number Diff line change
Expand Up @@ -907,11 +907,23 @@ def _resolve_model(self, preferences) -> Optional[str]:

@staticmethod
def _extract_tool_result_text(block) -> str:
"""Extract text from a ToolResultContent block."""
"""Extract text from a ToolResultContent block.

Handles both TextContent blocks (.text) and EmbeddedResource
blocks (.resource.text) per the MCP spec.
"""
if not hasattr(block, "content") or block.content is None:
return ""
items = block.content if isinstance(block.content, list) else [block.content]
return "\n".join(item.text for item in items if hasattr(item, "text"))
parts: List[str] = []
for item in items:
if hasattr(item, "text") and item.text:
parts.append(item.text)
elif hasattr(item, "resource"):
resource = item.resource
if hasattr(resource, "text") and resource.text:
parts.append(resource.text)
return "\n".join(parts)

def _convert_messages(self, params) -> List[dict]:
"""Convert MCP SamplingMessages to OpenAI format.
Expand Down Expand Up @@ -3174,6 +3186,10 @@ async def _call():
for block in (result.content or []):
if hasattr(block, "text"):
error_text += block.text
elif hasattr(block, "resource"):
resource = block.resource
if hasattr(resource, "text") and resource.text:
error_text += resource.text
return json.dumps({
"error": _sanitize_error(
error_text or "MCP tool returned an error"
Expand All @@ -3196,6 +3212,12 @@ async def _call():
if hasattr(block, "text") and block.text:
parts.append(block.text)
continue
# EmbeddedResource blocks carry payload in .resource.text
if hasattr(block, "resource"):
resource = block.resource
if hasattr(resource, "text") and resource.text:
parts.append(resource.text)
continue
image_tag = _cache_mcp_image_block(block)
if image_tag:
parts.append(image_tag)
Expand Down