From 65c7254d57e942bd75bbdd7a00460bb8d0191b4a Mon Sep 17 00:00:00 2001 From: NNIIOVO Date: Sat, 14 Nov 2026 13:49:38 -0600 Subject: [PATCH] Python: keep $defs at the root of wrapped MCP workflow tool schemas Signed-off-by: NNIIOVO --- .../_workflow_tool.py | 4 ++ .../tests/hosting_mcp/test_workflow_tool.py | 47 +++++++++++++++++++ 2 files changed, 51 insertions(+) diff --git a/python/packages/hosting-mcp/agent_framework_hosting_mcp/_workflow_tool.py b/python/packages/hosting-mcp/agent_framework_hosting_mcp/_workflow_tool.py index 870368d79e9..63ba5168523 100644 --- a/python/packages/hosting-mcp/agent_framework_hosting_mcp/_workflow_tool.py +++ b/python/packages/hosting-mcp/agent_framework_hosting_mcp/_workflow_tool.py @@ -67,11 +67,15 @@ def _tool_for_workflow(self, workflow: WorkflowT) -> types.Tool: input_adapter = self._input_adapter(workflow) input_schema = input_adapter.json_schema() if input_schema.get("type") != "object": + # Keep $defs at the root, where the schema's "#/$defs/..." references point. A schema with + # its own $id is a separate resource whose references resolve inside it, so leave it alone. + defs = input_schema.pop("$defs", None) if input_schema.get("$id") in (None, "", "#") else None input_schema = { "type": "object", "properties": {self.argument_name: input_schema}, "required": [self.argument_name], "additionalProperties": False, + **({"$defs": defs} if defs is not None else {}), } return types.Tool( diff --git a/python/packages/hosting-mcp/tests/hosting_mcp/test_workflow_tool.py b/python/packages/hosting-mcp/tests/hosting_mcp/test_workflow_tool.py index b5c5ffbce05..fe6a1a65a53 100644 --- a/python/packages/hosting-mcp/tests/hosting_mcp/test_workflow_tool.py +++ b/python/packages/hosting-mcp/tests/hosting_mcp/test_workflow_tool.py @@ -6,6 +6,7 @@ from typing import Any from unittest.mock import AsyncMock, patch +import jsonschema import pytest from agent_framework import ( Executor, @@ -19,6 +20,7 @@ from agent_framework_hosting import WorkflowState from mcp import Client, MCPError, types from mcp.server import Server, ServerRequestContext +from pydantic import ConfigDict, RootModel from pytest import raises from agent_framework_hosting_mcp import WorkflowMCPTool @@ -32,6 +34,12 @@ class WorkflowInput: repeat: int +class WorkflowInputBatch(RootModel[list[WorkflowInput]]): + """List input that is its own schema resource, with an $id.""" + + model_config = ConfigDict(json_schema_extra={"$id": "https://example.invalid/batch"}) + + def create_workflow(): """Create a workflow with an object-shaped input.""" @@ -84,6 +92,45 @@ async def uppercase(value: str, ctx: WorkflowContext[object, str]) -> None: assert result.content == [types.TextContent(type="text", text="HELLO")] +async def test_workflow_tool_wrapped_input_keeps_defs_resolvable() -> None: + """A wrapped non-object input keeps its $defs at the root, so "#/$defs/..." references still resolve.""" + + @executor(id="total") + async def total(values: list[WorkflowInput], ctx: WorkflowContext[object, str]) -> None: + await ctx.yield_output(str(sum(value.repeat for value in values))) + + workflow = WorkflowBuilder(start_executor=total, name="total", output_from=[total]).build() + tool: WorkflowMCPTool[Any] = WorkflowMCPTool(workflow, argument_name="values") + + schema = (await tool.list_tools()).tools[0].input_schema + arguments = {"values": [{"text": "a", "repeat": 2}]} + + assert "WorkflowInput" in schema["$defs"] + assert "$defs" not in schema["properties"]["values"] + jsonschema.validate(arguments, schema) + with raises(jsonschema.ValidationError): + jsonschema.validate({"values": [{"text": "a"}]}, schema) + result = await tool.call_tool("total", arguments) + assert result.content == [types.TextContent(type="text", text="2")] + + +async def test_workflow_tool_wrapped_input_with_own_id_keeps_its_defs() -> None: + """A wrapped input schema with its own $id resolves its references itself, so its $defs stay with it.""" + + @executor(id="batch") + async def batch(values: WorkflowInputBatch, ctx: WorkflowContext[object, str]) -> None: + await ctx.yield_output(str(len(values.root))) + + workflow = WorkflowBuilder(start_executor=batch, name="batch", output_from=[batch]).build() + tool: WorkflowMCPTool[Any] = WorkflowMCPTool(workflow, argument_name="values") + + schema = (await tool.list_tools()).tools[0].input_schema + + assert "$defs" not in schema + assert "WorkflowInput" in schema["properties"]["values"]["$defs"] + jsonschema.validate({"values": [{"text": "a", "repeat": 2}]}, schema) + + async def test_workflow_tool_serializes_structured_output_as_json_text() -> None: @executor(id="structured") async def structured(value: str, ctx: WorkflowContext[object, dict[str, str]]) -> None: