Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from typing import Any
from unittest.mock import AsyncMock, patch

import jsonschema
import pytest
from agent_framework import (
Executor,
Expand All @@ -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
Expand All @@ -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."""

Expand Down Expand Up @@ -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:
Expand Down
Loading