From 67f9329b839c2be6f659cd9201bdccd51816ab48 Mon Sep 17 00:00:00 2001 From: ChristophNetsch <56590412+ChristophNetsch@users.noreply.github.com> Date: Sat, 15 Nov 2025 16:50:00 +0100 Subject: [PATCH] fix(OpenAPIParser): Fix missing $defs for response schemas in experimental OpenAPI parser (#2398) * fix(OpenAPIParser): Fix missing for response schemas in experimental OpenAPI parser * chore: run linting --------- Co-authored-by: Christoph Netsch --- .../experimental/utilities/openapi/parser.py | 31 ++++++++++--------- 1 file changed, 17 insertions(+), 14 deletions(-) diff --git a/src/fastmcp/experimental/utilities/openapi/parser.py b/src/fastmcp/experimental/utilities/openapi/parser.py index 7b40ecba7..6833235a1 100644 --- a/src/fastmcp/experimental/utilities/openapi/parser.py +++ b/src/fastmcp/experimental/utilities/openapi/parser.py @@ -630,25 +630,28 @@ class OpenAPIParser( Returns: Dictionary containing only the schemas needed for outputs """ - needed_schemas = set() + if not responses or not all_schemas: + return {} + + needed_schemas: set[str] = set() - # Check responses for schema references for response in responses.values(): - if response.content_schema: - for content_schema in response.content_schema.values(): - # Check if this schema was originally a top-level $ref - if "x-fastmcp-top-level-schema" in content_schema: - schema_name = content_schema["x-fastmcp-top-level-schema"] - if schema_name in all_schemas: - needed_schemas.add(schema_name) + if not response.content_schema: + continue - # Extract all dependencies (transitive refs within the schema) - deps = self._extract_schema_dependencies( - content_schema, all_schemas + for content_schema in response.content_schema.values(): + deps = self._extract_schema_dependencies(content_schema, all_schemas) + needed_schemas.update(deps) + + schema_name = content_schema.get("x-fastmcp-top-level-schema") + if isinstance(schema_name, str) and schema_name in all_schemas: + needed_schemas.add(schema_name) + self._extract_schema_dependencies( + all_schemas[schema_name], + all_schemas, + collected=needed_schemas, ) - needed_schemas.update(deps) - # Return only the needed output schemas return { name: all_schemas[name] for name in needed_schemas if name in all_schemas }