-
Notifications
You must be signed in to change notification settings - Fork 1.8k
mcp: convert tool/prompt schemas eagerly at registration time #1861
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
ravyg
wants to merge
2
commits into
modelcontextprotocol:main
Choose a base branch
from
ravyg:fix/1847-eager-schema-conversion
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+362
−7
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🔴 Both tool.update() and prompt.update() mutate the registered object's schema field before calling the potentially-throwing conversion function, leaving the object in an inconsistent state if conversion fails. After a failed update, argsSchema/inputSchema holds the new broken schema while cachedArguments/inputJsonSchema remain stale, needsExecutorRegen/needsHandlerRegen are never set (executor/handler not regenerated), and any concurrent callback change is silently dropped; fix by computing the new JSON schema/arguments before mutating state.
Extended reasoning...
What the bug is and how it manifests
In both
_createRegisteredTool.update()and_createRegisteredPrompt.update(), this PR introduces a pattern where the registered object's schema field is mutated before the conversion function that can throw is called. If the conversion function throws (e.g., cycle detection from #1563, or any other schema validation error), the object is left in a partially-updated, inconsistent state.The specific code paths that trigger it
For tools (lines 829–835 in the new code):
For prompts (lines 738–744 in the new code):
Why existing code doesn't prevent it
Before this PR,
update()never calledstandardSchemaToJsonSchemaorpromptArgumentsFromStandardSchema— those conversions happened lazily in the list handlers. This PR moves the conversion intoupdate()(correctly) but forgets to guard the mutation against conversion failure.Impact
After a failed
tool.update({ paramsSchema: cycleSchema, callback: newCb }):inputSchema= new cyclic schema (broken)inputJsonSchema= stale old JSON schema →tools/listserves wrong schemaneedsExecutorRegennever set → executor NOT regeneratedcallbackupdate silently dropped (theif (updates.callback)…block is after the throw and never reached)validateToolInputreadstool.inputSchemadirectly, so subsequenttools/callrequests will attempt to validate against the new cyclic/broken schemaAfter a failed
prompt.update({ argsSchema: badSchema }):argsSchema= new broken schemacachedArguments= stale old arguments →prompts/listserves wrong argumentscurrentArgsSchema(closure variable used for future handler regeneration, line 743) is never updated, so any laterupdate({ callback })will regenerate the handler with the old schema whileregisteredPrompt.argsSchemasays the new one — a persistent inconsistency that outlives the failing callThe same mutation-before-throw pattern at lines 845–848 for
outputSchemaleavesoutputSchemaupdated whileoutputJsonSchemaremains stale.Step-by-step proof
const tool = server.registerTool('foo', { inputSchema: zodGoodSchema }, cb)→
tool.inputSchema= goodSchema,tool.inputJsonSchema= good JSON, executor uses goodSchematool.update({ paramsSchema: zodCycleSchema, callback: newCb })→ line 830:
tool.inputSchema= cycleSchema ✓ (mutated)→ line 833:
standardSchemaToJsonSchema(cycleSchema, 'input')throws (cycle detection)→ exception propagates; lines 834+ never execute
inputSchema= cycleSchema,inputJsonSchema= good JSON (stale), executor still uses goodSchema,newCbnever installedtools/list→ returns good JSON schema (stale) — appears finetools/call→validateToolInputreadstool.inputSchema(cycleSchema) → crash or unexpected validation behaviorHow to fix it
Compute the new value before mutating state:
Apply the same pattern for
outputSchemaand forregisteredPrompt.argsSchema/cachedArguments.