Skip to content

MCP prompt commands silently discard embedded text resources #55181

Done

Description

@dakjdakd

Description

MCP prompt commands silently discard embedded text resources returned by prompts/get. The command is discovered and executed, and the server response reaches OpenCode, but the resource body is missing from the prompt submitted to the session.

This matters for prompts that provide both an instruction and the material needed to follow it. For example, a server can return “Review this policy” as a text message and include the policy as an embedded resource in the next message. OpenCode currently submits only the instruction. A prompt consisting entirely of an embedded text resource is submitted with an empty text body instead.

The omission is easy to miss because there is no error identifying unsupported or discarded content. An assistant may ask for the missing document or answer without the reference material the server intended to provide. The problem occurs in the shared MCP command implementation, rather than in a particular terminal renderer.

Embedded resources are part of the MCP prompt specification. A resource can carry its content directly in resource.text; it does not need to be fetched separately through resources/read. The text can be plain prose, source code, Markdown, or structured text such as JSON. Its MIME type therefore does not have to be text/plain.

The cause is in packages/core/src/plugin/command.ts. The command maps each returned message through promptMessageText, joins the results with newlines, and trims the final string before calling ctx.session.prompt. The helper accepts only content.type === "text". A valid content.type === "resource" message with a resource.text field falls through to the empty-string result. The response has already been accepted by the MCP client; this is a loss during the command's conversion to session input.

The expected behavior is to preserve the body of an embedded text resource in its position among the other prompt messages. The ordinary text-message path should retain its existing joining and trimming behavior. This report concerns resources that already contain a text field. It does not request image or audio support, base64 blob decoding, or an additional resource-fetching mechanism.

A small fix can extend the existing conversion helper to return content.resource.text for embedded text resources. The helper can use the content type from Mcp.PromptResult because the MCP client already owns validation of the response. Taking the body directly also follows the existing text-resource conversion in packages/core/src/mcp/client.ts and avoids changing the visible format of ordinary text prompts.

Plugins

No custom OpenCode plugin is required. The affected path is the built-in MCP prompt command integration.

OpenCode version

Development branch at 23e3493ab2efb6b8c00725cfd652b7f946af5343 (package version 2.0.28).

Steps to reproduce

  1. Use an MCP server that advertises a prompt named review, configured under the server name docs.

  2. Have its prompts/get handler return the following valid response:

    {
      "messages": [
        {
          "role": "user",
          "content": {
            "type": "text",
            "text": "Review this policy:"
          }
        },
        {
          "role": "user",
          "content": {
            "type": "resource",
            "resource": {
              "uri": "policy://review",
              "mimeType": "application/json",
              "text": "{\"region\":\"EU\"}"
            }
          }
        }
      ]
    }
  3. Invoke /docs:review and inspect the text passed to the session prompt.

  4. The current result is Review this policy:. The expected result is Review this policy:\n{"region":"EU"}.

  5. Repeat with only the resource message. The current result is an empty string; the expected result is {"region":"EU"}.

These two cases were reproduced through the actual command plugin and command registry, with a controlled MCP response and a session-input capture. The accompanying regression tests run with bun test test/plugin/command.test.ts from packages/core and check the exact submitted text for an instruction followed by an embedded JSON text resource and for an embedded plain-text resource on its own.

Screenshot and/or share link

The defect is visible in the submitted session input; the response and exact expected/actual strings above are sufficient to reproduce it without a screenshot.

Operating System

Windows. The affected conversion is platform-independent.

Terminal

PowerShell; reproduced through the core command tests with Bun 1.4.2.

Linked fix

A focused fix is available in #55182.

Activity

  1. opencode-agent commented on Oct 13, 2026

    @opencode-agent
    Contributor

    Thanks for the detailed report. I reproduced this with a test that loads the real command plugin and command registry. A stubbed MCP service returned your docs:review response, and a fake session recorded the submitted prompt text.

    • Text message plus embedded resource: the session got Review this policy:, not Review this policy:\n{"region":"EU"}.
    • Embedded resource only: the session got an empty string, not {"region":"EU"}.

    This still happens on the latest dev (23e3493ab). As you pointed out, promptMessageText in packages/core/src/plugin/command.ts only accepts type: "text" content. The script below reproduces it. Your PR #55182 proposes a fix.

    Reproduction script (repro.sh)
    #!/usr/bin/env bash
    # Reproduces #55181: run from a checkout of anomalyco/opencode (dev), after `bun install`.
    # Usage: REPO=/path/to/opencode bash repro.sh
    set -euo pipefail
    REPO="${REPO:-/workspace/repo}"
    cat > "$REPO/packages/core/test/plugin/mcp-prompt-resource-repro.test.ts" <<'TS'
    import { describe, expect } from "bun:test"
    import { DateTime, Effect, Layer } from "effect"
    import { Command } from "@opencode/core/command"
    import { Bus } from "@opencode/core/bus"
    import { AppNodeBuilder } from "@opencode/core/effect/app-node-builder"
    import { Location } from "@opencode/core/location"
    import { Mcp } from "@opencode/core/mcp/index"
    import { CommandPlugin } from "@opencode/core/plugin/command"
    import { AbsolutePath } from "@opencode/core/schema"
    import { Session } from "@opencode/schema/session"
    import { SessionInbox } from "@opencode/schema/session-inbox"
    import { SessionMessage } from "@opencode/schema/session-message"
    import { LayerNode } from "@opencode/util/effect/layer-node"
    import { emptyMcp } from "../fixture/mcp"
    import { location } from "../fixture/location"
    import { testEffect } from "../lib/effect"
    import { host } from "./host"
    
    const directory = AbsolutePath.make("/repo")
    const locationLayer = Layer.succeed(Location.Service, Location.Service.of(location({ directory }, { projectDirectory: directory })))
    
    const text = { role: "user", content: { type: "text", text: "Review this policy:" } }
    const resource = {
      role: "user",
      content: { type: "resource", resource: { uri: "policy://review", mimeType: "application/json", text: '{"region":"EU"}' } },
    }
    let messages: any[] = []
    const mcp = Mcp.Service.of({
      ...emptyMcp,
      prompts: () => Effect.succeed([{ server: "docs", name: "review" } as any]),
      prompt: (input) => Effect.succeed({ server: input.server, name: input.name, messages } as any),
    })
    
    const it = testEffect(
      AppNodeBuilder.build(LayerNode.group([Command.node, Mcp.node, Bus.node]), [
        Mcp.node.replace(Layer.succeed(Mcp.Service, mcp)),
        Location.node.replace(locationLayer),
      ]),
    )
    
    describe("#55181 MCP prompt embedded text resource", () => {
      it.effect("keeps resource.text in the submitted prompt", () =>
        Effect.gen(function* () {
          const command = yield* Command.Service
          const sent: string[] = []
          yield* CommandPlugin.Plugin.effect(
            host({
              command: { list: () => Effect.die("unused"), transform: command.transform, reload: command.reload },
              session: {
                prompt: (input) =>
                  Effect.sync(() => {
                    sent.push(input.text)
                    return SessionInbox.User.make({
                      id: SessionMessage.ID.make("msg_test"),
                      sessionID: input.sessionID,
                      time: { created: DateTime.makeUnsafe(0) },
                      type: "user",
                      payload: { text: input.text },
                      delivery: input.delivery ?? "steer",
                    })
                  }),
              },
            }),
          ).pipe(Effect.provideService(Location.Service, Location.Service.of(location({ directory }, { projectDirectory: directory }))))
    
          const run = (m: any[]) => {
            messages = m
            return command.execute({
              name: "docs:review",
              invocation: { sessionID: Session.ID.make("ses_test"), prompt: { text: "" }, delivery: "steer" },
            })
          }
          yield* run([text, resource])
          yield* run([resource])
          console.log("submitted:", JSON.stringify(sent))
          expect(sent).toEqual(['Review this policy:\n{"region":"EU"}', '{"region":"EU"}'])
        }),
      )
    })
    TS
    cd "$REPO/packages/core"
    bun test test/plugin/mcp-prompt-resource-repro.test.ts

    Run it: bash repro.sh

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

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions