Skip to content

fix(mcp): tolerate invalid tool output schemas - #26530

Closed
nicolascancino wants to merge 4 commits into
anomalyco:devfrom
nicolascancino:fix/mcp-output-schema-ref
Closed

nicolascancino wants to merge 4 commits into
anomalyco:devfrom
nicolascancino:fix/mcp-output-schema-ref

Conversation

@nicolascancino

Copy link
Copy Markdown

Issue for this PR

Closes #26529

Type of change

  • Bug fix
  • New feature
  • Refactor / code improvement
  • Documentation

What does this PR do?

Makes MCP tool loading resilient when a server returns a valid tools/list response whose outputSchema contains refs the SDK cannot resolve.

The normal client.listTools() path still runs first. If it fails with a schema reference error, OpenCode retries tools/list with a tolerant result schema that keeps inputSchema validation and treats outputSchema as opaque.

This fixes Google Stitch-style failures where the MCP transport connects but OpenCode marks the server as failed while validating tool output schemas.

How did you verify your code works?

cd packages/opencode
bun test test/mcp/lifecycle.test.ts --timeout 30000

Result:

20 pass
0 fail
51 expect() calls
Ran 20 tests across 1 file. [2.13s]

Screenshots / recordings

N/A — non-UI change.

Checklist

  • I have tested my changes locally
  • I have not included unrelated changes in this PR

@kitlangton

Copy link
Copy Markdown
Contributor

Thanks for putting this fix together. I opened #26614 based on this approach, rebased onto current dev, with a narrower fallback and additional regression coverage for both the Stitch-style schema-ref failure and the non-schema failure path.

Closing this one in favor of #26614 so we have a clean branch for merge. Credit to @nicolascancino for the original fix direction here.

@kitlangton kitlangton closed this May 10, 2026
auto-merge was automatically disabled May 10, 2026 01:20

Pull request was closed

@Arcadi4 Arcadi4 mentioned this pull request May 27, 2026
1 task done
androidand pushed a commit to androidand/opencode-skein that referenced this pull request Sep 19, 2026
Closes anomalyco#26529.

When an MCP server returns a tool whose \`outputSchema\` contains a
broken \`$ref\` (e.g. Google Stitch's \`#/$defs/ScreenInstance\`), the
SDK's typed \`listTools()\` validator throws and opencode marks the
ENTIRE server as failed — losing every other valid tool the server
exposes.

Catch the schema-reference errors and retry with a tolerant schema
(\`looseObject\` + \`outputSchema: z.unknown().optional()\`) via the raw
\`request\` path so the bad tool's schema is accepted as opaque while
the others load normally.

Equivalent fix shape to anomalyco#26530 (nicolascancino) — kept his approach
since it's correct. Bundles our reproducer test from
\`kit/issue-reproducers\` so the regression is locked in.

Verified red → green → red → green:
- pre-fix: server marked \`failed\`
- post-fix: server stays \`connected\`, valid tool present
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

MCP tools fail to load when outputSchema contains unresolved refs

2 participants