Skip to content

fix: wire up dereference_refs() in tool schema pipeline#3170

Merged
jlowin merged 2 commits intorelease/2.xfrom
fix/v2-wire-dereference-refs
Feb 12, 2026
Merged

fix: wire up dereference_refs() in tool schema pipeline#3170
jlowin merged 2 commits intorelease/2.xfrom
fix/v2-wire-dereference-refs

Conversation

@jlowin
Copy link
Copy Markdown
Member

@jlowin jlowin commented Feb 12, 2026

PR #2861 backported dereference_refs() to v2 but never actually called it — the function exists and is tested in isolation, but nothing in the tool schema pipeline invokes it. As a result, $ref references are sent as-is to MCP clients, breaking clients like VS Code Copilot that don't handle them.

This wires it up by adding a dereference parameter to compress_schema() and enabling it at the two tool schema creation sites (input and output schemas in Tool.from_function). Schemas are now inlined before being sent to clients.

# compress_schema now accepts dereference=True to inline $ref
schema = compress_schema(schema, prune_titles=True, dereference=True)

Closes #3153

@jlowin jlowin added the bug Something isn't working. Reports of errors, unexpected behavior, or broken functionality. label Feb 12, 2026
@marvin-context-protocol marvin-context-protocol Bot added server Related to FastMCP server implementation or server-side functionality. openapi Related to OpenAPI integration, parsing, or code generation features. labels Feb 12, 2026
@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai Bot commented Feb 12, 2026

Walkthrough

The change adds an optional dereference step to JSON Schema compression and enables it when generating both input and output schemas in ParsedFunction.from_function. compress_schema gained a dereference: bool = False parameter and, when true, inlines $ref by calling dereference_refs(). A helper _strip_discriminator_mappings was added and is invoked after dereferencing to remove discriminator.mapping entries that reference removed $defs. ParsedFunction.from_function now calls compress_schema(..., dereference=True) for input and output schemas.

Possibly related PRs

🚥 Pre-merge checks | ✅ 6
✅ Passed checks (6 passed)
Check name Status Explanation
Title check ✅ Passed The title accurately describes the main change: wiring up the existing dereference_refs() function into the tool schema pipeline.
Description check ✅ Passed The description clearly explains the issue, solution, and provides code examples, though contributors checklist is not explicitly checked.
Linked Issues check ✅ Passed The PR directly addresses issue #3153 by implementing the missing call to dereference_refs() in the schema pipeline for both input and output schemas.
Out of Scope Changes check ✅ Passed All changes are scoped to enabling dereferencing in the schema pipeline: adding dereference parameter to compress_schema() and wiring it into Tool.from_function().
Docstring Coverage ✅ Passed Docstring coverage is 80.00% which is sufficient. The required threshold is 80.00%.
Merge Conflict Detection ✅ Passed ✅ No merge conflicts detected when merging into release/2.x

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing touches
  • 📝 Generate docstrings
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment
  • Commit unit tests in branch fix/v2-wire-dereference-refs

No actionable comments were generated in the recent review. 🎉


Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

Copy link
Copy Markdown

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: eed6a2ae9c

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment on lines +397 to +398
if dereference:
schema = dereference_refs(schema)
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Badge Preserve discriminator mappings when inlining $ref schemas

Calling compress_schema(..., dereference=True) now runs dereference_refs, which removes $defs after resolving only $ref nodes; this breaks discriminated-union schemas because Pydantic emits discriminator.mapping values like #/$defs/Cat as plain strings, so those mappings become dangling once $defs is stripped. For tools whose input/output annotations use discriminated unions, clients/validators that rely on discriminator.mapping can no longer resolve variants correctly.

Useful? React with 👍 / 👎.

@jlowin jlowin merged commit 280885d into release/2.x Feb 12, 2026
8 checks passed
@jlowin jlowin deleted the fix/v2-wire-dereference-refs branch February 12, 2026 21:51
Jazzcort added a commit to Jazzcort/fastmcp that referenced this pull request Apr 6, 2026
This addresses the issues found in PrefectHQ#3760. Currently, if a self-referencing Pydantic
data type is buried in a nested field, calling dereference_refs() introduces a
circular reference to the final schema. This results in a serialization error that
crashes the server during tools/list requests.

I checked v3.2.0 and noticed that dereference=True is no longer being passed to
compress_schema(), which essentially reverts PrefectHQ#3170. We should implement this same
behavior for the 2.x versions. While the community will eventually migrate to 3.x,
I think we still need to pull this fix in now because the bug causes a complete
server crash whenever tools/list is called.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working. Reports of errors, unexpected behavior, or broken functionality. openapi Related to OpenAPI integration, parsing, or code generation features. server Related to FastMCP server implementation or server-side functionality.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant