Skip to content

fix(core): preserve embedded MCP prompt text - #55182

Closed
dakjdakd wants to merge 2 commits into
anomalyco:devfrom
dakjdakd:mcp-prompt-resources
Closed

dakjdakd wants to merge 2 commits into
anomalyco:devfrom
dakjdakd:mcp-prompt-resources

Conversation

@dakjdakd

@dakjdakd dakjdakd commented Oct 13, 2026 •

Copy link
Copy Markdown

Issue for this PR

Closes #55181

Type of change

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

What does this PR do?

MCP prompt commands can return an instruction together with an embedded text resource containing the document, code, or other reference material the instruction depends on. OpenCode currently keeps the instruction but silently drops the resource body when it converts the MCP response into a session prompt. If the resource is the only message in the response, the generated prompt text is empty.

For example, a prompts/get response containing Review this policy: followed by an embedded JSON resource with text: '{"region":"EU"}' currently submits only Review this policy:. With this change, the session receives both pieces in order: Review this policy:\n{"region":"EU"}. A resource-only response likewise retains its text instead of becoming an empty prompt.

The problem is in promptMessageText in the built-in command plugin. The surrounding code already walks all returned messages and joins their converted content, but the helper recognizes only ordinary text messages. Embedded resources are a valid MCP prompt content type and may already carry the complete body in resource.text. There is no need to read the URI again or change MCP discovery, transport, or session admission to preserve that body.

This PR adds a text-resource branch to that helper and gives its parameter the existing Mcp.PromptResult message-content type. The response is already validated by the MCP client, so the helper can distinguish the supported content forms directly instead of accepting unknown and repeating generic object and property checks. Ordinary text still returns content.text; an embedded resource with a text field returns content.resource.text; the remaining content forms retain the existing empty-string result.

The check is based on the resource representation, rather than a text/plain MIME restriction. JSON, Markdown, and source files can all be represented as text resources, and the server has already supplied their text. The mixed-message regression uses application/json specifically to protect this behavior. The resource body is passed through directly, consistent with the existing text-resource handling in the MCP client. This does not add a URI prefix or otherwise change prompt formatting.

The scope is deliberately limited to embedded text resources. Images, audio, and resources represented by base64 blob fields are not decoded or turned into prompt text. This PR does not introduce media attachments, additional network requests, or changes to message-role handling. The fix is shared by callers of the built-in MCP command path.

How did you verify your code works?

Added two regression cases to the existing command-plugin test file. They register and execute the real command through Command.Service, provide controlled MCP responses using the existing test fixture, and capture the resulting session prompt. This checks the full conversion path rather than a separate copy of the helper's logic.

The cases cover an instruction followed by an embedded JSON text resource and a prompt containing only an embedded plain-text resource. Both assert the exact text submitted to the session. The original built-in init and review command test also continues to pass.

Validation:

  • cd packages/core && bun test test/plugin/command.test.ts: all 3 tests passed with the focused resource regressions and the existing built-in command test.
  • cd packages/core && bun typecheck: passed for the changed core source and tests, with GOMAXPROCS=2 and GOMEMLIMIT=1GiB.
  • Prettier check for both changed files and git diff origin/dev --check: passed.

The change touches only the command conversion and its regression tests.

Screenshots / recordings

Not applicable. This changes the text submitted to a session, with exact input and output assertions in the regression tests.

Checklist

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

@github-actions

Copy link
Copy Markdown
Contributor

The following comment was made by an LLM, it may be inaccurate:

@opencode-agent opencode-agent Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Looks good: embedded text resources from MCP prompts now reach the session prompt, both after an instruction and on their own. I copied the new tests onto the base branch, where the two embedded-resource cases fail; on this branch all 5 command-plugin tests pass, and typecheck in packages/core passes. Dropping the plain-string branch is safe, because getPrompt results are validated against the MCP schema before they get here, so content is always a typed object. Text resources are handled the same way as in tool results in mcp/client.ts.

Public API: unchanged (internal helper only).

One small point: the plain-text and media/blob test cases already pass on the base branch, so they don't cover the fix. They could be dropped to keep the change minimal.

@dakjdakd

Copy link
Copy Markdown
Author

Thanks for the review. Removed the standalone plain-text and media/blob cases in 125a79e so the new coverage stays focused on embedded text resources. The two resource regressions and the existing built-in command test all pass (3/3). I've updated the PR description to match.

@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.

MCP prompt commands silently discard embedded text resources

1 participant