diff --git a/sdk/python/src/b2a_sdk/decorators.py b/sdk/python/src/b2a_sdk/decorators.py index 528d84bf4..5ce4b02e1 100644 --- a/sdk/python/src/b2a_sdk/decorators.py +++ b/sdk/python/src/b2a_sdk/decorators.py @@ -13,12 +13,25 @@ import asyncio import contextvars +import enum import functools import inspect +import json import time import traceback -from collections.abc import Awaitable, Callable, Coroutine -from typing import Any, ParamSpec, TypeVar, cast, get_type_hints +import types +from collections.abc import Awaitable, Callable, Coroutine, Mapping, Sequence +from typing import ( + Any, + Literal, + ParamSpec, + TypeVar, + Union, + cast, + get_args, + get_origin, + get_type_hints, +) from .client import B2AClient, DryRunSimulation @@ -89,6 +102,158 @@ def _notify_registration( pass +_JSON_SCHEMA_SCALARS: dict[Any, str] = { + str: "string", + int: "integer", + float: "number", + bool: "boolean", + bytes: "string", + type(None): "null", +} + + +def _json_schema_enum(values: Any) -> dict[str, Any]: + """Build an ``enum`` fragment from literal values or enum members. + + Enum members contribute their ``.value``. If any value still cannot be + written as strict JSON (bytes, or a non-finite number), the fragment is + ``{}`` (any value) so the schema stays serializable. + """ + choices = [value.value if isinstance(value, enum.Enum) else value for value in values] + try: + json.dumps(choices, allow_nan=False) + except (TypeError, ValueError): + return {} + return {"enum": choices} + + +_DEFINITION_KEYWORDS = ("$defs", "definitions") + + +def _rewrite_refs(node: Any, renames: dict[str, str]) -> Any: + """Return ``node`` with every ``$ref`` in ``renames`` pointed at its new name.""" + if isinstance(node, dict): + return { + key: renames.get(value, value) + if key == "$ref" and isinstance(value, str) + else _rewrite_refs(value, renames) + for key, value in node.items() + } + if isinstance(node, list): + return [_rewrite_refs(item, renames) for item in node] + return node + + +def _hoist_definitions(schema: dict[str, Any], defs: dict[str, dict[str, Any]]) -> dict[str, Any]: + """Move a model schema's definitions into ``defs`` and return the rest. + + Pydantic writes ``$ref`` values such as ``#/$defs/Model`` that resolve from + the document root. The caller places ``defs`` at the root of the tool's + schema so those references still resolve when the model schema is nested. + Two different models that share a name get distinct names. + """ + found = { + keyword: schema[keyword] + for keyword in _DEFINITION_KEYWORDS + if isinstance(schema.get(keyword), dict) + } + if not found: + return schema + body = {key: value for key, value in schema.items() if key not in found} + + # Pick a root name for every incoming definition. A name is shared with a + # stored definition only when the two are equal after the incoming refs + # are rewritten, so a definition that merely points at a conflicting one + # is renamed too and the stored graph is never changed. + incoming = [(keyword, name) for keyword, names in found.items() for name in names] + targets = {(keyword, name): name for keyword, name in incoming} + suffixes = dict.fromkeys(incoming, 1) + while True: + renames = { + f"#/{keyword}/{name}": f"#/{keyword}/{target}" + for (keyword, name), target in targets.items() + if target != name + } + rewritten = { + (keyword, name): _rewrite_refs(found[keyword][name], renames) + for keyword, name in incoming + } + settled = True + for keyword, name in incoming: + stored = defs.get(keyword, {}).get(targets[keyword, name]) + if stored is None or stored == rewritten[keyword, name]: + continue + settled = False + others = { + target + for (other_keyword, other), target in targets.items() + if other_keyword == keyword and other != name + } | (set(found[keyword]) - {name}) + while True: + suffixes[keyword, name] += 1 + candidate = f"{name}_{suffixes[keyword, name]}" + if candidate not in others: + break + targets[keyword, name] = candidate + if settled: + break + + for keyword, name in incoming: + defs.setdefault(keyword, {})[targets[keyword, name]] = rewritten[keyword, name] + return cast(dict[str, Any], _rewrite_refs(body, renames)) + + +def _json_schema_for_hint( + hint: Any, defs: dict[str, dict[str, Any]] | None = None +) -> dict[str, Any]: + """Map a Python type annotation to a JSON Schema fragment. + + Unknown annotations map to ``{}`` (any value) so the emitted schema stays + valid JSON Schema instead of carrying a Python type name. + + Model definitions are collected into ``defs`` (keyed by ``$defs`` or + ``definitions``) for the caller to place at the root of the schema. + """ + if defs is None: + defs = {} + if hint is Any or hint is object: + return {} + if hint in _JSON_SCHEMA_SCALARS: + return {"type": _JSON_SCHEMA_SCALARS[hint]} + if hasattr(hint, "model_json_schema"): + return _hoist_definitions(cast(dict[str, Any], hint.model_json_schema()), defs) + if isinstance(hint, type) and issubclass(hint, enum.Enum): + return _json_schema_enum(hint) + + origin = get_origin(hint) + args = get_args(hint) + if origin is Literal: + return _json_schema_enum(args) + if origin is Union or origin is types.UnionType: + options = [_json_schema_for_hint(arg, defs) for arg in args] + if any(option == {} for option in options): + return {} + return {"anyOf": options} + if hint in (list, tuple, set, frozenset) or ( + isinstance(origin, type) and issubclass(origin, (list, tuple, set, frozenset, Sequence)) + ): + schema: dict[str, Any] = {"type": "array"} + if origin is tuple and args and args[-1] is not Ellipsis: + schema["prefixItems"] = [_json_schema_for_hint(arg, defs) for arg in args] + schema["minItems"] = schema["maxItems"] = len(args) + elif args: + schema["items"] = _json_schema_for_hint(args[0], defs) + return schema + if hint is dict or (isinstance(origin, type) and issubclass(origin, Mapping)): + schema = {"type": "object"} + if len(args) == 2: + schema["additionalProperties"] = _json_schema_for_hint(args[1], defs) + return schema + if hasattr(hint, "schema") and callable(hint.schema): + return _hoist_definitions(cast(dict[str, Any], hint.schema()), defs) + return {} + + def _extract_schema_from_func(func: Callable) -> tuple[dict | None, dict | None]: """Extract input/output schemas from a function signature.""" try: @@ -97,21 +262,20 @@ def _extract_schema_from_func(func: Callable) -> tuple[dict | None, dict | None] properties = {} required = [] + defs: dict[str, dict[str, Any]] = {} for param_name, param in sig.parameters.items(): if param_name in ("self", "cls"): continue + if param.kind in (param.VAR_POSITIONAL, param.VAR_KEYWORD): + continue + hint = hints.get(param_name) if hint is None: properties[param_name] = {"type": "string"} - elif hasattr(hint, "model_json_schema"): - properties[param_name] = hint.model_json_schema() - elif hasattr(hint, "schema"): - properties[param_name] = hint.schema() else: - type_str = getattr(hint, "__name__", str(hint)) - properties[param_name] = {"type": type_str} + properties[param_name] = _json_schema_for_hint(hint, defs) if param.default is inspect.Parameter.empty: required.append(param_name) @@ -125,6 +289,8 @@ def _extract_schema_from_func(func: Callable) -> tuple[dict | None, dict | None] if properties else None ) + if input_schema is not None: + input_schema.update(defs) return_type = hints.get("return") output_schema = None diff --git a/sdk/python/tests/test_mcp_tool_schema.py b/sdk/python/tests/test_mcp_tool_schema.py new file mode 100644 index 000000000..84732fbbe --- /dev/null +++ b/sdk/python/tests/test_mcp_tool_schema.py @@ -0,0 +1,341 @@ +"""@mcp_tool registers JSON Schema types, not Python type names (issue #362).""" + +from __future__ import annotations + +import enum +import json +from typing import Any, Literal, Optional, cast + +import pytest + +try: + import pydantic +except ImportError: # the SDK does not depend on pydantic + pydantic = None # type: ignore[assignment] + +from b2a_sdk import decorators +from b2a_sdk.decorators import mcp_tool + +JSON_SCHEMA_TYPES = {"string", "integer", "number", "boolean", "object", "array", "null"} + + +def _all_type_values(schema: Any) -> list[Any]: + found: list[Any] = [] + if isinstance(schema, dict): + if "type" in schema: + found.append(schema["type"]) + for value in schema.values(): + found.extend(_all_type_values(value)) + elif isinstance(schema, list): + for item in schema: + found.extend(_all_type_values(item)) + return found + + +@pytest.fixture +def registrations(monkeypatch: pytest.MonkeyPatch) -> list[dict[str, Any]]: + captured: list[dict[str, Any]] = [] + monkeypatch.setattr(decorators, "_registration_callbacks", []) + + def record(service_id: str, func: Any, input_schema: Any, output_schema: Any) -> None: + captured.append({"service_id": service_id, "input_schema": input_schema}) + + decorators.register_mcp_tool_callback(record) + return captured + + +def test_example_tool_schema_uses_json_schema_types( + registrations: list[dict[str, Any]], +) -> None: + @mcp_tool(service_id="text-analyzer") + async def analyze_text(text: str, operation: str = "summarize", max_length: int = 100) -> dict: + return {} + + schema = registrations[0]["input_schema"] + assert schema == { + "type": "object", + "properties": { + "text": {"type": "string"}, + "operation": {"type": "string"}, + "max_length": {"type": "integer"}, + }, + "required": ["text"], + } + + +class Mode(enum.Enum): + FAST = "fast" + SLOW = "slow" + + +def test_common_annotations_map_to_valid_json_schema( + registrations: list[dict[str, Any]], +) -> None: + @mcp_tool(service_id="many-types") + async def tool( + ratio: float, + enabled: bool, + tags: list[str], + pair: tuple[int, str], + counts: dict[str, int], + note: Optional[str] = None, # noqa: UP045 - the typing form is what users write + limit: int | None = None, + mode: Mode = Mode.FAST, + level: Literal["low", "high"] = "low", + extra: Any = None, + *args: Any, + **kwargs: Any, + ) -> dict: + return {} + + props = registrations[0]["input_schema"]["properties"] + assert props["ratio"] == {"type": "number"} + assert props["enabled"] == {"type": "boolean"} + assert props["tags"] == {"type": "array", "items": {"type": "string"}} + assert props["pair"] == { + "type": "array", + "prefixItems": [{"type": "integer"}, {"type": "string"}], + "minItems": 2, + "maxItems": 2, + } + assert props["counts"] == {"type": "object", "additionalProperties": {"type": "integer"}} + assert props["note"] == {"anyOf": [{"type": "string"}, {"type": "null"}]} + assert props["limit"] == {"anyOf": [{"type": "integer"}, {"type": "null"}]} + assert props["mode"] == {"enum": ["fast", "slow"]} + assert props["level"] == {"enum": ["low", "high"]} + assert props["extra"] == {} + assert "args" not in props and "kwargs" not in props + assert set(_all_type_values(props)) <= JSON_SCHEMA_TYPES + + +def test_unannotated_parameter_stays_string(registrations: list[dict[str, Any]]) -> None: + @mcp_tool(service_id="untyped") + async def tool(value): # type: ignore[no-untyped-def] + return value + + assert registrations[0]["input_schema"]["properties"] == {"value": {"type": "string"}} + + +class Blob(enum.Enum): + RAW = b"raw" + + +def test_literal_enum_members_become_their_values( + registrations: list[dict[str, Any]], +) -> None: + @mcp_tool(service_id="literal-enum") + async def tool( + mode: Literal[Mode.FAST], + mixed: Literal[Mode.SLOW, "auto", 3, None] = "auto", + ) -> dict: + return {} + + schema = registrations[0]["input_schema"] + assert schema["properties"]["mode"] == {"enum": ["fast"]} + assert schema["properties"]["mixed"] == {"enum": ["slow", "auto", 3, None]} + assert json.loads(json.dumps(schema)) == schema + + +def test_values_json_cannot_hold_fall_back_to_any( + registrations: list[dict[str, Any]], +) -> None: + @mcp_tool(service_id="non-json-values") + async def tool(blob: Blob, raw: Literal[b"raw"], member: Literal[Blob.RAW]) -> dict: + return {} + + schema = registrations[0]["input_schema"] + assert schema["properties"] == {"blob": {}, "raw": {}, "member": {}} + assert json.loads(json.dumps(schema)) == schema + + +def _resolve(schema: dict[str, Any], ref: str) -> Any: + assert ref.startswith("#/") + node: Any = schema + for part in ref[2:].split("/"): + node = node[part] + return node + + +def _all_refs(node: Any) -> list[str]: + found: list[str] = [] + if isinstance(node, dict): + for key, value in node.items(): + if key == "$ref" and isinstance(value, str): + found.append(value) + else: + found.extend(_all_refs(value)) + elif isinstance(node, list): + for item in node: + found.extend(_all_refs(item)) + return found + + +class _ItemHolder: + """Stands in for a model class: exposes the schema hook @mcp_tool reads.""" + + value_type = "integer" + + @classmethod + def model_json_schema(cls) -> dict[str, Any]: + return { + "$defs": { + "Item": { + "type": "object", + "properties": {"value": {"type": cls.value_type}}, + "required": ["value"], + } + }, + "type": "object", + "properties": {"item": {"$ref": "#/$defs/Item"}}, + "required": ["item"], + } + + +class _TextItemHolder(_ItemHolder): + value_type = "string" + + +def test_nested_model_refs_resolve_from_the_schema_root( + registrations: list[dict[str, Any]], +) -> None: + @mcp_tool(service_id="nested-models") + async def tool( + owner: _ItemHolder, + backup: _ItemHolder | None = None, + team: list[_ItemHolder] = [], # noqa: B006 - the default is never mutated + pair: tuple[_ItemHolder, int] | None = None, + by_name: dict[str, _ItemHolder] = {}, # noqa: B006 - the default is never mutated + ) -> dict: + return {} + + schema = registrations[0]["input_schema"] + item = _ItemHolder.model_json_schema()["$defs"]["Item"] + assert schema["$defs"] == {"Item": item} + refs = _all_refs(schema) + assert refs == ["#/$defs/Item"] * 5 + assert all(_resolve(schema, ref) == item for ref in refs) + + props = schema["properties"] + assert '"$defs":' not in json.dumps(props) + holder = { + key: value for key, value in _ItemHolder.model_json_schema().items() if key != "$defs" + } + assert props["owner"] == holder + assert props["backup"] == {"anyOf": [holder, {"type": "null"}]} + assert props["team"] == {"type": "array", "items": holder} + assert props["pair"]["anyOf"][0]["prefixItems"] == [holder, {"type": "integer"}] + assert props["by_name"] == {"type": "object", "additionalProperties": holder} + assert json.loads(json.dumps(schema)) == schema + + +def test_same_named_definitions_from_different_models_stay_distinct( + registrations: list[dict[str, Any]], +) -> None: + @mcp_tool(service_id="name-clash") + async def tool(a: _ItemHolder, b: list[_TextItemHolder], c: _ItemHolder | None = None) -> dict: + return {} + + schema = registrations[0]["input_schema"] + assert set(schema["$defs"]) == {"Item", "Item_2"} + props = schema["properties"] + a_ref = props["a"]["properties"]["item"]["$ref"] + b_ref = props["b"]["items"]["properties"]["item"]["$ref"] + c_ref = props["c"]["anyOf"][0]["properties"]["item"]["$ref"] + assert _resolve(schema, a_ref)["properties"]["value"] == {"type": "integer"} + assert _resolve(schema, b_ref)["properties"]["value"] == {"type": "string"} + assert c_ref == a_ref + + +if pydantic is not None and int(pydantic.VERSION.split(".")[0]) >= 2: + + class Address(pydantic.BaseModel): + city: str + + class Person(pydantic.BaseModel): + name: str + address: Address + + +@pytest.mark.skipif( + pydantic is None or int(pydantic.VERSION.split(".")[0]) < 2, + reason="needs pydantic 2 or newer", +) +def test_pydantic_nested_model_refs_resolve(registrations: list[dict[str, Any]]) -> None: + @mcp_tool(service_id="pydantic-nested") + async def tool(owner: Person, team: list[Person], backup: Person | None = None) -> dict: + return {} + + schema = registrations[0]["input_schema"] + assert set(schema["$defs"]) == {"Address"} + refs = _all_refs(schema) + assert refs == ["#/$defs/Address"] * 3 + for ref in refs: + assert _resolve(schema, ref)["properties"]["city"]["type"] == "string" + assert '"$defs":' not in json.dumps(schema["properties"]) + assert schema["required"] == ["owner", "team"] + + +class _BoxHolder: + """Two levels of definitions: the tool refs Box, and Box refs Item.""" + + value_type = "integer" + + @classmethod + def model_json_schema(cls) -> dict[str, Any]: + return { + "$defs": { + "Box": {"type": "object", "properties": {"item": {"$ref": "#/$defs/Item"}}}, + "Item": {"type": "object", "properties": {"value": {"type": cls.value_type}}}, + }, + "type": "object", + "properties": {"box": {"$ref": "#/$defs/Box"}}, + } + + +class _TextBoxHolder(_BoxHolder): + value_type = "string" + + +def _item_value_type(schema: dict[str, Any], holder: dict[str, Any]) -> str: + box = _resolve(schema, holder["properties"]["box"]["$ref"]) + item = _resolve(schema, box["properties"]["item"]["$ref"]) + return cast(str, item["properties"]["value"]["type"]) + + +def test_equal_definitions_that_ref_conflicting_ones_are_not_merged( + registrations: list[dict[str, Any]], +) -> None: + @mcp_tool(service_id="graph-clash") + async def tool( + a: _BoxHolder, + b: _TextBoxHolder, + c: list[_BoxHolder], + d: _TextBoxHolder | None = None, + ) -> dict: + return {} + + schema = registrations[0]["input_schema"] + props = schema["properties"] + assert _item_value_type(schema, props["a"]) == "integer" + assert _item_value_type(schema, props["b"]) == "string" + assert _item_value_type(schema, props["c"]["items"]) == "integer" + assert _item_value_type(schema, props["d"]["anyOf"][0]) == "string" + assert set(schema["$defs"]) == {"Box", "Item", "Box_2", "Item_2"} + assert props["c"]["items"] == props["a"] + assert props["d"]["anyOf"][0] == props["b"] + + +class Ratio(enum.Enum): + UNBOUNDED = float("inf") + + +def test_non_finite_enum_values_fall_back_to_any( + registrations: list[dict[str, Any]], +) -> None: + @mcp_tool(service_id="non-finite") + async def tool(ratio: Ratio, limit: Literal[Ratio.UNBOUNDED]) -> dict: + return {} + + schema = registrations[0]["input_schema"] + assert schema["properties"] == {"ratio": {}, "limit": {}} + assert json.loads(json.dumps(schema, allow_nan=False)) == schema