Skip to content

fix(opencode): normalize MCP structured arguments - #32812

Closed
davilima6 wants to merge 2 commits into
anomalyco:devfrom
davilima6:fix/mcp-params
Closed

davilima6 wants to merge 2 commits into
anomalyco:devfrom
davilima6:fix/mcp-params

Conversation

@davilima6

@davilima6 davilima6 commented Jun 18, 2026 •

Copy link
Copy Markdown

Issue for this PR

Related to #28472

Type of change

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

What does this PR do?

Some setups hand MCP servers an object or array argument as a JSON string, which the server then rejects with expected object, received string. When the advertised schema says a field is an object or array, this PR parses that JSON string back into a structured value before calling client.callTool. Empty strings become {}/[]; invalid JSON is left untouched so the server still returns its own error. It is a no-op when the argument is already structured.

How did you verify your code works?

  • bun test test/mcp/catalog.test.ts and bun run typecheck from packages/opencode; bun run lint from the repo root (0 errors).
  • Instrumented the forwarding boundary and ran real tool calls through OpenCode against a real MCP server, across 5 models / 3 providers (OpenAI gpt-5.4, gpt-5.4-mini; Copilot gpt-4.1, gpt-5.4; Gemini 2.5 Flash) and 3 object-schema shapes (shallow object, free-form object, deeply nested object like ms-365 send-mail body). With current models the argument always arrives as a proper object and the unfixed path already accepts it. When a JSON string is forced, the unfixed path returns the exact -32602 ... expected object, received string and this change accepts it.

Scope and limitations

This is a tolerance shim. It only does anything when an upstream component (an older model, a proxy, or a re-serializing SDK) emits a JSON string for an object/array field; otherwise it is a no-op. I could not reproduce the original failure with current models in clean OpenCode, so this is defensive rather than a guaranteed fix for a specific server. Note that next-devtools' nextjs_call advertises args as type: "string", so this change does not affect it. Schema detection is intentionally minimal: anyOf/oneOf/$ref and string | object unions are not handled. Happy to extend, or to drop this if you'd rather not carry it.

Screenshots / recordings

Not a UI change.

Checklist

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

@github-actions github-actions Bot added the needs:compliance This means the issue will auto-close after 2 hours. label Jun 18, 2026
@github-actions github-actions Bot removed the needs:compliance This means the issue will auto-close after 2 hours. label Jun 18, 2026
@github-actions

Copy link
Copy Markdown
Contributor

Thanks for updating your PR! It now meets our contributing guidelines. 👍

@github-actions

Copy link
Copy Markdown
Contributor

Thanks for your contribution!

This PR doesn't have a linked issue. All PRs must reference an existing issue.

Please:

  1. Open an issue describing the bug/feature (if one doesn't exist)
  2. Add Fixes #<number> or Closes #<number> to this PR description

See CONTRIBUTING.md for details.

@davilima6
davilima6 marked this pull request as draft June 23, 2026 05:03
@github-actions

Copy link
Copy Markdown
Contributor

Automated PR Cleanup

Thank you for contributing to opencode.

Due to the high volume of PRs from users and AI agents, we periodically close older PRs using automated criteria so maintainers can focus review time on the most active and community-supported contributions.

This PR was closed because it matched the following cleanup criteria:

  • The PR was created more than 1 month ago
  • The PR had fewer than 2 positive reactions
  • Positive reactions are counted as thumbs-up, heart, celebration, or rocket reactions on the PR

PRs created within the last month are not affected by this cleanup.

If you believe this PR was closed incorrectly, or if you are still actively working on it, please leave a comment explaining why it should be reopened. A maintainer can review and reopen it if appropriate.

Thanks again for taking the time to contribute.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant