Skip to content

from_openapi: a media-type schema's own $defs are dropped from the tool schema when components/schemas is non-empty #5708

Closed

Description

@BlueX888

What happened?

An OpenAPI 3.1 media-type schema can declare its own $defs and reference them with #/$defs/.... When components/schemas is non-empty, FastMCP.from_openapi() builds the tool schema's $defs from the component schemas only: the media-type schema's own $defs never make it into the input schema and are replaced by the component-derived ones in the output schema, so its #/$defs/... references dangle. Calling the tool then fails with jsonschema's PointerToNowhere when the result is validated against the advertised output schema, even though the API returned a valid body.

Expected: the advertised schemas keep the media-type schema's own $defs alongside the component schemas.

Example Code

import asyncio

from fastmcp import Client, FastMCP

SPEC = {
    "openapi": "3.1.0",
    "info": {"title": "demo", "version": "1.0.0"},
    "servers": [{"url": "https://api.example.com"}],
    "components": {"schemas": {"Item": {"type": "object", "properties": {"name": {"type": "string"}}}}},
    "paths": {
        "/items": {
            "post": {
                "operationId": "createItem",
                "requestBody": {"required": True, "content": {"application/json": {"schema": {
                    "type": "object",
                    "properties": {
                        "item": {"$ref": "#/components/schemas/Item"},
                        "extra": {"$ref": "#/$defs/Extra"},
                    },
                    "$defs": {"Extra": {"type": "string"}},
                }}}},
                "responses": {"200": {"description": "ok", "content": {"application/json": {"schema": {
                    "type": "object",
                    "properties": {
                        "item": {"$ref": "#/components/schemas/Item"},
                        "extra": {"$ref": "#/$defs/Extra"},
                    },
                    "$defs": {"Extra": {"type": "string"}},
                }}}},
            }
        }
    },
}


async def main():
    mcp = FastMCP.from_openapi(SPEC)
    async with Client(mcp) as client:
        tools = await client.list_tools()

    schema = tools[0].inputSchema
    print("extra property:", schema["properties"]["extra"])
    print("'Extra' in inputSchema $defs:", "Extra" in schema.get("$defs", {}))

    out_schema = tools[0].output_schema
    print("'Extra' in outputSchema $defs:", "Extra" in (out_schema or {}).get("$defs", {}))


asyncio.run(main())
# extra property: {'$ref': '#/$defs/Extra'}
# 'Extra' in inputSchema $defs: False
# 'Extra' in outputSchema $defs: False

Version Information

fastmcp 4.1.0 (pip) and main @ e2fcc41, MCP 2.3.0, Python 3.13.15, macOS 26.6 (arm64)

Activity

  1. BlueX888 commented on Oct 12, 2026

    @BlueX888
    ContributorAuthor

    Reproduced this (the MRE in the issue body, against both pip fastmcp 4.1.0 and main @ e2fcc41). Root cause: _combine_schemas_and_map_params builds the input schema's $defs from route.request_schemas alone and extract_output_schema_from_responses overwrites the output schema's $defs with the component-derived ones, so the media-type schema's own $defs are dropped on both paths

    Minimal reproduction: the python MRE from the issue body, observed output:

    extra property: {'$ref': '#/$defs/Extra'}
    'Extra' in inputSchema $defs: False
    'Extra' in outputSchema $defs: False
    

    A regression test that fails on unpatched main with PointerToNowhere: '/$defs/Extra' does not exist ... and passes with the fix is ready on fix/prep-openapi-inline-defs-clobbered (commit 0d15b2d).

    Happy to open a PR with this fix, or to be assigned if you prefer to take it.

  2. added
    bug
    openapi
    on Oct 12, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

bugopenapi

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions