Skip to content

πŸš€ The fast, Pythonic way to build MCP servers and clients.

Match Gemini function responses to tool-use IDs (#6244)

nate nowackd9bcffedd140
diff --git a/fastmcp_slim/fastmcp/client/sampling/handlers/google_genai.py b/fastmcp_slim/fastmcp/client/sampling/handlers/google_genai.py
index 23a6fa033..d586a1648 100644
--- a/fastmcp_slim/fastmcp/client/sampling/handlers/google_genai.py
+++ b/fastmcp_slim/fastmcp/client/sampling/handlers/google_genai.py
@@ -1,7 +1,7 @@
"""Google GenAI sampling handler with tool support for FastMCP 3.0."""
import base64
-from collections.abc import Sequence
+from collections.abc import Mapping, Sequence
from typing import Any, cast
from uuid import uuid4
@@ -202,6 +202,7 @@ def _sampling_content_to_google_genai_part(
| AudioContent
| ToolUseContent
| ToolResultContent,
+ tool_names_by_id: Mapping[str, str] | None = None,
) -> Part:
"""Convert MCP content to Google GenAI Part."""
if isinstance(content, TextContent):
@@ -249,18 +250,13 @@ def _sampling_content_to_google_genai_part(
raise ValueError(msg)
result_text = "".join(result_parts)
- # Extract function name from toolUseId
- # Our IDs are formatted as "{function_name}_{uuid8}", so extract the name.
- # Note: This is a limitation of MCP's ToolResultContent which only carries
- # toolUseId, while Google's FunctionResponse requires the function name.
- tool_use_id = content.tool_use_id
- if "_" in tool_use_id:
- # Split and rejoin all but the last part (the UUID suffix)
- parts = tool_use_id.rsplit("_", 1)
- function_name = parts[0]
- else:
- # Fallback: use the full ID as the name
- function_name = tool_use_id
+ function_name = (tool_names_by_id or {}).get(content.tool_use_id)
+ if function_name is None:
+ msg = (
+ "No matching ToolUseContent found for tool_use_id "
+ f"{content.tool_use_id!r}"
+ )
+ raise ValueError(msg)
return Part(
function_response=FunctionResponse(
@@ -278,6 +274,20 @@ def _convert_messages_to_google_genai_content(
) -> list[Content]:
"""Convert MCP messages to Google GenAI content."""
google_messages: list[Content] = []
+ tool_names_by_id: dict[str, str] = {}
+
+ for message in messages:
+ content = message.content
+ blocks = content if isinstance(content, list) else [content]
+ for item in blocks:
+ if isinstance(item, ToolUseContent):
+ if (
+ item.id in tool_names_by_id
+ and tool_names_by_id[item.id] != item.name
+ ):
+ msg = f"Conflicting function names for tool-use ID {item.id!r}"
+ raise ValueError(msg)
+ tool_names_by_id[item.id] = item.name
for message in messages:
content = message.content
@@ -285,7 +295,8 @@ def _convert_messages_to_google_genai_content(
# Handle list content (tool calls + results)
if isinstance(content, list):
parts: list[Part] = [
- _sampling_content_to_google_genai_part(item) for item in content
+ _sampling_content_to_google_genai_part(item, tool_names_by_id)
+ for item in content
]
if message.role == "user":
@@ -298,7 +309,7 @@ def _convert_messages_to_google_genai_content(
continue
# Handle single content item
- part = _sampling_content_to_google_genai_part(content)
+ part = _sampling_content_to_google_genai_part(content, tool_names_by_id)
if message.role == "user":
google_messages.append(UserContent(parts=[part]))
diff --git a/tests/client/sampling/handlers/test_google_genai_handler.py b/tests/client/sampling/handlers/test_google_genai_handler.py
index dd610c91a..03f32cbb8 100644
--- a/tests/client/sampling/handlers/test_google_genai_handler.py
+++ b/tests/client/sampling/handlers/test_google_genai_handler.py
@@ -247,10 +247,11 @@ def test_sampling_content_to_google_genai_part_tool_result():
content=[TextContent(type="text", text="Weather is sunny")],
)
- part = _sampling_content_to_google_genai_part(content)
+ part = _sampling_content_to_google_genai_part(
+ content, {"get_weather_abc123": "get_weather"}
+ )
assert part.function_response is not None
- # Function name is extracted from toolUseId by removing the UUID suffix
assert part.function_response.name == "get_weather"
assert part.function_response.response == {"result": "Weather is sunny"}
@@ -263,26 +264,40 @@ def test_sampling_content_to_google_genai_part_tool_result_empty():
content=[],
)
- part = _sampling_content_to_google_genai_part(content)
+ part = _sampling_content_to_google_genai_part(
+ content, {"my_tool_xyz789": "my_tool"}
+ )
assert part.function_response is not None
assert part.function_response.name == "my_tool"
assert part.function_response.response == {"result": ""}
-def test_sampling_content_to_google_genai_part_tool_result_no_underscore():
- """Test ToolResultContent when toolUseId has no underscore (fallback)."""
+def test_sampling_content_to_google_genai_part_tool_result_uses_paired_name():
+ """Tool-use IDs are opaque and must resolve through their paired call."""
content = ToolResultContent(
type="tool_result",
- tool_use_id="simplefunction",
+ tool_use_id="call_opaque_123",
content=[TextContent(type="text", text="Result")],
)
- part = _sampling_content_to_google_genai_part(content)
+ part = _sampling_content_to_google_genai_part(
+ content, {"call_opaque_123": "lookup_customer"}
+ )
- # When no underscore, the full ID is used as the name
assert part.function_response is not None
- assert part.function_response.name == "simplefunction"
+ assert part.function_response.name == "lookup_customer"
+
+
+def test_sampling_content_to_google_genai_part_tool_result_requires_matching_call():
+ content = ToolResultContent(
+ type="tool_result",
+ tool_use_id="call_opaque_123",
+ content=[TextContent(type="text", text="Result")],
+ )
+
+ with pytest.raises(ValueError, match="No matching ToolUseContent"):
+ _sampling_content_to_google_genai_part(content)
def test_convert_messages_with_tool_use():
@@ -316,6 +331,15 @@ def test_convert_messages_with_tool_result():
"""Test converting messages containing ToolResultContent."""
msgs = _convert_messages_to_google_genai_content(
messages=[
+ SamplingMessage(
+ role="assistant",
+ content=ToolUseContent(
+ type="tool_use",
+ id="get_weather_123",
+ name="get_weather",
+ input={},
+ ),
+ ),
SamplingMessage(
role="user",
content=ToolResultContent(
@@ -327,16 +351,89 @@ def test_convert_messages_with_tool_result():
],
)
- assert len(msgs) == 1
- assert isinstance(msgs[0], UserContent)
- assert msgs[0].parts[0].function_response is not None
- assert msgs[0].parts[0].function_response.name == "get_weather"
+ assert len(msgs) == 2
+ assert isinstance(msgs[1], UserContent)
+ assert msgs[1].parts[0].function_response is not None
+ assert msgs[1].parts[0].function_response.name == "get_weather"
+
+
+def test_convert_messages_matches_multiple_tool_results_by_id():
+ msgs = _convert_messages_to_google_genai_content(
+ messages=[
+ SamplingMessage(
+ role="assistant",
+ content=[
+ ToolUseContent(
+ type="tool_use",
+ id="call_opaque_123",
+ name="lookup_customer",
+ input={"id": "C-1"},
+ ),
+ ToolUseContent(
+ type="tool_use",
+ id="lookup_order_12345678",
+ name="lookup_order",
+ input={"id": "O-1"},
+ ),
+ ],
+ ),
+ SamplingMessage(
+ role="user",
+ content=[
+ ToolResultContent(
+ type="tool_result",
+ tool_use_id="lookup_order_12345678",
+ content=[TextContent(type="text", text="order")],
+ ),
+ ToolResultContent(
+ type="tool_result",
+ tool_use_id="call_opaque_123",
+ content=[TextContent(type="text", text="customer")],
+ ),
+ ],
+ ),
+ ]
+ )
+
+ assert isinstance(msgs[0], ModelContent)
+ function_calls = [part.function_call for part in msgs[0].parts]
+ assert all(function_call is not None for function_call in function_calls)
+ assert [
+ function_call.name
+ for function_call in function_calls
+ if function_call is not None
+ ] == [
+ "lookup_customer",
+ "lookup_order",
+ ]
+ assert isinstance(msgs[1], UserContent)
+ function_responses = [part.function_response for part in msgs[1].parts]
+ assert all(
+ function_response is not None for function_response in function_responses
+ )
+ assert [
+ function_response.name
+ for function_response in function_responses
+ if function_response is not None
+ ] == [
+ "lookup_order",
+ "lookup_customer",
+ ]
def test_convert_messages_with_multiple_content_blocks():
"""Test converting messages with multiple content blocks (list content)."""
msgs = _convert_messages_to_google_genai_content(
messages=[
+ SamplingMessage(
+ role="assistant",
+ content=ToolUseContent(
+ type="tool_use",
+ id="get_weather_xyz",
+ name="get_weather",
+ input={},
+ ),
+ ),
SamplingMessage(
role="user",
content=[
@@ -351,11 +448,11 @@ def test_convert_messages_with_multiple_content_blocks():
],
)
- assert len(msgs) == 1
- assert isinstance(msgs[0], UserContent)
- assert len(msgs[0].parts) == 2
- assert msgs[0].parts[0].text == "I need weather info."
- assert msgs[0].parts[1].function_response is not None
+ assert len(msgs) == 2
+ assert isinstance(msgs[1], UserContent)
+ assert len(msgs[1].parts) == 2
+ assert msgs[1].parts[0].text == "I need weather info."
+ assert msgs[1].parts[1].function_response is not None
def test_response_to_result_with_tools_text_only():