Skip to content

A reactive notebook for Python — run reproducible experiments, query with SQL, execute as a script, deploy as an app, and version with git. Stored as pure Python. All in a modern, AI-native editor.

fix(ai): pass real JSON schema through for MCP/backend tools (#10573)

Rob91317597a156
diff --git a/marimo/_ai/_pydantic_ai_utils.py b/marimo/_ai/_pydantic_ai_utils.py
index 3856868742..2282820e29 100644
--- a/marimo/_ai/_pydantic_ai_utils.py
+++ b/marimo/_ai/_pydantic_ai_utils.py
@@ -58,7 +58,7 @@ def form_toolsets(
Returns a tuple of the toolset and whether deferred tool requests are needed.
"""
- from pydantic_ai import CallDeferred, FunctionToolset
+ from pydantic_ai import CallDeferred, FunctionToolset, Tool
toolset = FunctionToolset()
deferred_tool_requests = False
@@ -87,8 +87,17 @@ def form_toolsets(
return asdict(result)
tool_fn.__name__ = tool.name
- toolset.add_function(
- tool_fn, name=tool.name, description=tool.description
+ # Use the tool's real JSON schema instead of letting pydantic-ai
+ # infer one from tool_fn's signature (which is always the generic
+ # `(_tool_name, **kwargs)` closure above, regardless of the actual
+ # tool's parameters).
+ toolset.add_tool(
+ Tool.from_schema(
+ function=tool_fn,
+ name=tool.name,
+ description=tool.description,
+ json_schema=tool.parameters,
+ )
)
return toolset, deferred_tool_requests
diff --git a/tests/_ai/test_pydantic_utils.py b/tests/_ai/test_pydantic_utils.py
index c9415997bc..e6628a6822 100644
--- a/tests/_ai/test_pydantic_utils.py
+++ b/tests/_ai/test_pydantic_utils.py
@@ -203,6 +203,36 @@ class TestFormToolsets:
# Verify tool_invoker was NOT called for frontend tools
tool_invoker.assert_not_called()
+ def test_form_toolsets_preserves_tool_schema(self):
+ # Regression test: the JSON schema pydantic-ai exposes to the model
+ # must be the tool's real `parameters`, not a generic schema
+ # inferred from the internal `tool_fn(_tool_name, **kwargs)`
+ # closure signature.
+ tool_invoker = AsyncMock()
+ schema = {
+ "type": "object",
+ "properties": {
+ "query": {"type": "string", "description": "Search query"},
+ "max_results": {"type": "integer"},
+ },
+ "required": ["query"],
+ }
+ tool = ToolDefinition(
+ name="search_docs",
+ description="Search documentation",
+ parameters=schema,
+ source="mcp",
+ mode=["manual"],
+ )
+ toolset, _ = form_toolsets([tool], tool_invoker)
+
+ pydantic_tool = toolset.tools["search_docs"]
+ exposed_schema = pydantic_tool.function_schema.json_schema
+
+ assert exposed_schema["properties"] == schema["properties"]
+ assert exposed_schema.get("required") == ["query"]
+ assert "_tool_name" not in exposed_schema.get("properties", {})
+
class TestConvertToPydanticMessages:
def test_convert_empty_messages(self):