Skip to content

DereferenceRefsMiddleware crashes with RecursionError on JSON Pointer style $ref (non-$defs) #3893

@maxime-gautre

Description

@maxime-gautre

What happened?

DereferenceRefsMiddleware crashes with a RecursionError when schemas contain circular $ref using JSON Pointer style (e.g. #/properties/nodes/items) instead of $defs-based style (e.g. #/$defs/Node).

This is the format emitted by .NET/C# MCP servers using System.Text.Json's schema generator. There is no $defs section, and $ref pointers reference locations directly within the schema tree.

C#'s System.Text.Json schema generator uses JSON Pointer $ref in two cases:

  • Recursive types (e.g. a Node with Children: IReadOnlyList) → circular $ref
  • Shared types (e.g. the same complex type appearing in multiple properties) → non-circular $ref

As a workaround, we disable the built-in middleware (dereference_schemas=False).

Example Code

from fastmcp.utilities.json_schema import dereference_refs

schema = {
    "type": "object",
    "properties": {
        "nodes": {
            "type": "array",
            "items": {
                "type": "object",
                "properties": {
                    "value": {"type": "string"},
                    "children": {
                        "type": "array",
                        "items": {"$ref": "#/properties/nodes/items"},
                    },
                },
            },
        },
    },
}

dereference_refs(schema)  # RecursionError

Version Information

- fastmcp 3.2.0
- jsonref 1.1.0
- Python 3.13
- Upstream MCP server: C# ModelContextProtocol.AspNetCore 1.2.0

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working. Reports of errors, unexpected behavior, or broken functionality.serverRelated to FastMCP server implementation or server-side functionality.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions