π The fast, Pythonic way to build MCP servers and clients.
replace nested requestBody's #/components/ with
ShiWei6baa906813c6
diff --git a/src/fastmcp/utilities/openapi.py b/src/fastmcp/utilities/openapi.pyindex c56d9c558..4c08e6281 100644--- a/src/fastmcp/utilities/openapi.py+++ b/src/fastmcp/utilities/openapi.py@@ -872,6 +872,45 @@ def format_description_with_responses(return "\n".join(desc_parts)+def _replace_ref_with_defs(+ info: dict[str, Any], description: str | None = None+) -> dict[str, Any]:+ """+ Replace openapi $ref with jsonschema $defs++ Examples:+ - {"type": "object", "properties": {"$ref": "#/components/schemas/..."}}+ - {"$ref": "#/components/schemas/..."}+ - {"items": {"$ref": "#/components/schemas/..."}}+ - {"anyOf": [{"$ref": "#/components/schemas/..."}]}+ - {"allOf": [{"$ref": "#/components/schemas/..."}]}+ - {"oneOf": [{"$ref": "#/components/schemas/..."}]}++ Args:+ info: dict[str, Any]+ description: str | None++ Returns:+ dict[str, Any]+ """+ schema = info.copy()+ if properties := schema.get("properties"):+ for prop_name, prop_schema in properties.items():+ properties[prop_name] = _replace_ref_with_defs(prop_schema)+ elif ref_path := schema.get("$ref"):+ if ref_path.startswith("#/components/schemas/"):+ schema_name = ref_path.split("/")[-1]+ schema["$ref"] = f"#/$defs/{schema_name}"+ elif item_schema := schema.get("items"):+ schema["items"] = _replace_ref_with_defs(item_schema)+ for section in ["anyOf", "allOf", "oneOf"]:+ for i, item in enumerate(schema.get(section, [])):+ schema[section][i] = _replace_ref_with_defs(item)+ if info.get("description", description) and not schema.get("description"):+ schema["description"] = description+ return schema++def _combine_schemas(route: HTTPRoute) -> dict[str, Any]:"""Combines parameter and request body schemas into a single schema.@@ -889,38 +928,17 @@ def _combine_schemas(route: HTTPRoute) -> dict[str, Any]:for param in route.parameters:if param.required:required.append(param.name)-- # Copy the schema and add description if available- param_schema = param.schema_.copy() if isinstance(param.schema_, dict) else {}-- # Convert #/components/schemas references to #/$defs references- if isinstance(param_schema, dict) and "$ref" in param_schema:- ref_path = param_schema["$ref"]- if ref_path.startswith("#/components/schemas/"):- schema_name = ref_path.split("/")[-1]- param_schema["$ref"] = f"#/$defs/{schema_name}"-- # Also handle anyOf, allOf, oneOf references- for section in ["anyOf", "allOf", "oneOf"]:- if section in param_schema and isinstance(param_schema[section], list):- for i, item in enumerate(param_schema[section]):- if isinstance(item, dict) and "$ref" in item:- ref_path = item["$ref"]- if ref_path.startswith("#/components/schemas/"):- schema_name = ref_path.split("/")[-1]- param_schema[section][i]["$ref"] = f"#/$defs/{schema_name}"-- # Add parameter description to schema if available and not already present- if param.description and not param_schema.get("description"):- param_schema["description"] = param.description-- properties[param.name] = param_schema+ properties[param.name] = _replace_ref_with_defs(+ param.schema_.copy(), param.description+ )# Add request body if it existsif route.request_body and route.request_body.content_schema:# For now, just use the first content type's schemacontent_type = next(iter(route.request_body.content_schema))- body_schema = route.request_body.content_schema[content_type]+ body_schema = _replace_ref_with_defs(+ route.request_body.content_schema[content_type].copy()+ )body_props = body_schema.get("properties", {})# Add request body properties@@ -935,7 +953,6 @@ def _combine_schemas(route: HTTPRoute) -> dict[str, Any]:"properties": properties,"required": required,}-# Add schema definitions if availableif route.schema_definitions:result["$defs"] = route.schema_definitionsdiff --git a/tests/utilities/openapi/test_openapi.py b/tests/utilities/openapi/test_openapi.pyindex e229990d1..5d8f81b30 100644--- a/tests/utilities/openapi/test_openapi.py+++ b/tests/utilities/openapi/test_openapi.py@@ -6,7 +6,10 @@ import pytestfrom fastapi import Body, FastAPI, Path, Queryfrom pydantic import BaseModel, Field-from fastmcp.utilities.openapi import parse_openapi_to_http_routes+from fastmcp.utilities.openapi import (+ _combine_schemas,+ parse_openapi_to_http_routes,+)# --- Test Data: Static OpenAPI Schema Dictionaries --- #@@ -1023,6 +1026,9 @@ def test_openapi_30_reference_resolution(openapi_30_with_references):# or it still has a $ref fieldassert "properties" in category or "$ref" in category+ combined_schema = _combine_schemas(route)+ assert "#/$defs/" in combined_schema["properties"]["category"]["$ref"]+def test_openapi_31_reference_resolution(openapi_31_with_references):"""Test that references are correctly resolved in OpenAPI 3.1 schemas."""@@ -1057,6 +1063,9 @@ def test_openapi_31_reference_resolution(openapi_31_with_references):# or it still has a $ref fieldassert "properties" in category or "$ref" in category+ combined_schema = _combine_schemas(route)+ assert "#/$defs/" in combined_schema["properties"]["category"]["$ref"]+def test_consistent_output_across_versions(openapi_30_with_references, openapi_31_with_references