mirror of
https://github.com/PrefectHQ/fastmcp.git
synced 2026-08-22 05:24:18 +02:00
Merge pull request #2426 from jlowin/simplify-object-schema-check
Simplify _is_object_schema helper
This commit is contained in:
commit
b5f88bfe42
2 changed files with 5 additions and 36 deletions
|
|
@ -368,7 +368,7 @@ class FunctionTool(Tool):
|
|||
|
||||
|
||||
def _is_object_schema(schema: dict[str, Any]) -> bool:
|
||||
"""Check if a JSON schema represents an object type, resolving $ref references."""
|
||||
"""Check if a JSON schema represents an object type."""
|
||||
# Direct object type
|
||||
if schema.get("type") == "object":
|
||||
return True
|
||||
|
|
@ -377,40 +377,9 @@ def _is_object_schema(schema: dict[str, Any]) -> bool:
|
|||
if "properties" in schema:
|
||||
return True
|
||||
|
||||
# Resolve $ref references to check the referenced schema
|
||||
# Self-referencing types (e.g., list["ReturnThing"]) generate schemas with $ref
|
||||
# at the root level instead of "type": "object" directly
|
||||
if "$ref" in schema:
|
||||
ref = schema["$ref"]
|
||||
if ref.startswith("#/$defs/"):
|
||||
# Resolve reference within the same schema document
|
||||
# The schema dict contains both $ref and $defs
|
||||
defs_path = ref.replace("#/$defs/", "").split("/")
|
||||
if "$defs" in schema:
|
||||
defs = schema["$defs"]
|
||||
current = defs
|
||||
# Navigate through the defs path
|
||||
for part in defs_path:
|
||||
if isinstance(current, dict) and part in current:
|
||||
current = current[part]
|
||||
else:
|
||||
# Can't resolve, assume it might be an object
|
||||
# (safer to assume object than to wrap incorrectly)
|
||||
return True
|
||||
# Recursively check the resolved schema
|
||||
if isinstance(current, dict):
|
||||
return _is_object_schema(current)
|
||||
# If $defs not found but we have a $ref, assume object
|
||||
# (self-referencing types are typically objects)
|
||||
return True
|
||||
elif ref == "#":
|
||||
# Self-reference - treat as object (common for recursive types)
|
||||
return True
|
||||
# For other $ref patterns, assume object to be safe
|
||||
# (most $refs in JSON schemas point to object types)
|
||||
return True
|
||||
|
||||
return False
|
||||
# Self-referencing types use $ref pointing to $defs
|
||||
# The referenced type is always an object in our use case
|
||||
return "$ref" in schema and "$defs" in schema
|
||||
|
||||
|
||||
@dataclass
|
||||
|
|
|
|||
|
|
@ -424,7 +424,7 @@ class TestToolDecorator:
|
|||
mcp = FastMCP()
|
||||
|
||||
with pytest.raises(
|
||||
ValueError, match='Output schemas must have "type" set to "object"'
|
||||
ValueError, match="Output schemas must represent object types"
|
||||
):
|
||||
|
||||
@mcp.tool(output_schema={"type": "integer"})
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue