fix: repair tool schemas before AJV compilation to prevent MissingRefError#355
Merged
Conversation
…Error The MCP SDK (≥1.27) introduced Client.cacheToolMetadata() which eagerly compiles outputSchema with AJV during listTools(). When the Stitch backend returns schemas with $ref to missing $defs (e.g. #/$defs/ScreenInstance), AJV throws MissingRefError before the SDK's schema repair code can run. Fix: - Extract schema repair into a standalone module (schema-repair.ts) that scans for $ref targets and injects well-known stub definitions - In StitchToolClient.listTools(): use raw request() instead of Client.listTools() to bypass AJV compilation, then apply repair - In proxy refreshTools(): apply repair before re-serving tools to MCP clients whose AJV validators would also crash The repair module: - Scans both inputSchema and outputSchema for $ref targets - Only injects stubs for referenced-but-missing well-known defs - Never overwrites existing $defs - Handles ScreenInstance, SelectedScreenInstance, and File types Includes 13 unit tests covering injection, preservation, nesting, unknown refs, and null safety.
This was referenced May 12, 2026
davideast
added a commit
to davideast/stitch-mcp
that referenced
this pull request
May 12, 2026
Fixes AJV MissingRefError crash on tool list by upgrading to the SDK version that repairs schemas before AJV compilation. See google-labs-code/stitch-sdk#355
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Problem
The MCP SDK (≥1.27) introduced
Client.cacheToolMetadata()which eagerly compilesoutputSchemawith AJV duringlistTools(). When the Stitch backend returns schemas with$refto missing$defs(e.g.#/$defs/ScreenInstance,#/$defs/SelectedScreenInstance), AJV throws aMissingRefErrorbefore the SDK's existing schema repair code can run.This causes a hard crash for all consumers:
stitch-mcpCLI (bun run src/cli.ts tool list→ crash)StitchProxyStitchToolClient.listTools()Root Cause
The schema repair in
client.tsran afterthis.client.listTools(), butClient.listTools()crashes internally during AJV compilation before returning.Solution
1. New
schema-repair.tsmoduleExtracted schema repair into a standalone, reusable module that:
inputSchemaandoutputSchemafor$reftargets recursively$defs(preserves backend-provided definitions)ScreenInstance,SelectedScreenInstance, andFiletypes2.
StitchToolClient.listTools()— bypass AJV compilationReplaced
this.client.listTools()with a rawthis.client.request()call usingListToolsResultSchema. This bypassescacheToolMetadata()entirely, allowing schema repair to run first.3.
StitchProxy.refreshTools()— repair before re-servingApplied
repairToolSchemas()in the proxy'srefreshTools()so tools are repaired before being re-served to downstream MCP clients whose AJV validators would also crash.4. Public API export
Exported
repairToolSchemasandrepairSchemafrom the SDK's public API for consumers that need custom tool processing pipelines.Testing
schema-repair.test.tscovering injection, preservation, nesting, unknown refs, null safety0.3.5tarball installed institch-mcp,tool listworksFiles Changed
packages/sdk/src/schema-repair.tspackages/sdk/src/client.tslistTools()uses rawrequest()+repairToolSchemas()packages/sdk/src/proxy/client.tsrefreshTools()appliesrepairToolSchemas()packages/sdk/src/index.tsrepairToolSchemas,repairSchemapackages/sdk/test/unit/schema-repair.test.tspackages/sdk/package.json0.3.4→0.3.5