Python: normalize empty MCP tool output to null (#4683)

* Python: normalize empty MCP tool output to null

* Python: hardcode null for empty MCP output
This commit is contained in:
Eduard van Valkenburg
2026-03-13 21:03:48 +01:00
committed by GitHub
Unverified
parent c67d3523ae
commit 052ba7be07
2 changed files with 7 additions and 5 deletions
+2 -3
View File
@@ -4,6 +4,7 @@ from __future__ import annotations
import asyncio
import base64
import json
import logging
import re
import sys
@@ -87,8 +88,6 @@ def _parse_prompt_result_from_mcp(
Returns:
A string representation of the prompt result.
"""
import json
parts: list[str] = []
for message in mcp_type.messages:
content = message.content
@@ -194,7 +193,7 @@ def _parse_tool_result_from_mcp(
result.append(Content.from_text(str(item)))
if not result:
result.append(Content.from_text(""))
result.append(Content.from_text("null"))
return result
+5 -2
View File
@@ -195,13 +195,16 @@ def test_parse_tool_result_from_mcp_meta_not_in_string():
def test_parse_tool_result_from_mcp_empty_content():
"""Test that empty content produces list with empty text Content."""
"""Test that empty MCP content normalizes to JSON null text content."""
mcp_result = types.CallToolResult(content=[])
result = _parse_tool_result_from_mcp(mcp_result)
assert isinstance(result, list)
assert len(result) == 1
assert result[0].type == "text"
assert result[0].text == ""
assert result[0].text == "null"
function_result = Content.from_function_result(call_id="call_null", result=result)
assert function_result.result == "null"
def test_parse_tool_result_from_mcp_audio_content():