Skip to content

Commit b0db786

Browse files
committed
Add docstring, fix type and fallback
1 parent 57a5335 commit b0db786

1 file changed

Lines changed: 10 additions & 4 deletions

File tree

  • integrations/mcp/src/haystack_integrations/tools/mcp

integrations/mcp/src/haystack_integrations/tools/mcp/mcp_tool.py

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -63,9 +63,14 @@ def _resolve_headers(headers: dict[str, str | Secret] | None) -> dict[str, str]
6363
return resolved_headers
6464

6565

66-
def extract_first_text_element(result: str) -> str:
67-
# Per MCP spec, content[] may contain TextContent, ImageContent, AudioContent, etc.
68-
# Parse only first TextContent block (ToolInvoker requires dict, not list).
66+
def extract_first_text_element(result: str) -> str | dict[str, Any]:
67+
"""
68+
Return the first text content block from an MCP tool response.
69+
70+
MCP tool responses may include mixed content types such as text, image, or
71+
audio blocks. This helper extracts the first text block because the tool
72+
invoker expects a single parsed payload rather than the full content list.
73+
"""
6974
parsed: dict = json.loads(result)
7075
content: list = parsed.get("content", [])
7176
for block in content:
@@ -74,8 +79,9 @@ def extract_first_text_element(result: str) -> str:
7479
try:
7580
return json.loads(text)
7681
except (json.JSONDecodeError, TypeError):
77-
# No TextContent found, return full parsed response as fallback
7882
return text
83+
# No TextContent found, return full parsed response as fallback
84+
return parsed
7985

8086

8187
class AsyncExecutor:

0 commit comments

Comments
 (0)