Skip to content

[BUG] a tool result with a binary part (application/octet-stream) still breaks every later request in the session #59644

Open

Description

@angelnu

Description

A tool result that carries a media part whose type has no modality — mimeToModality() maps only image/, audio/, video/ and application/pdf — is passed straight to the provider. On the OpenAI-Chat route the whole request is rejected:

AI.Error: OpenAI Chat does not support media type application/octet-stream

The part stays in history, so every later turn of that session fails the same way, even when the tool is not called again. unsupportedMedia() in packages/core/src/session/model-request.ts is the guard written for exactly this class of part, but it returns early when there is no modality:

const modality = mimeToModality(mime)
if (!modality) return

Still the case on dev (4ba064f65306, checked today).

This is the residual of #54798 and #52448 — both were closed in the v2 sweep with the bug unfixed (the bot reproduced #54798 on v2.0.24 and on dev at the time), and the open PRs in that area (#52517, #56478, #57256, #53014) are broader than this one case, so it is still unguarded.

Steps to reproduce

  1. Register an MCP server whose tool returns a non-image binary part — the filesystem MCP server does this: the MCP blob content type for a .zip/.bin arrives as application/octet-stream.
  2. Call that tool in a session running on a model served through the OpenAI-Chat route.
  3. The turn fails with does not support media type application/octet-stream.
  4. Send any follow-up message in the same session: it fails identically.

Expected

The unknown media type should be handled like an unsupported modality: replaced, while the request is assembled, by the visible note that is already used for that case —

ERROR: Cannot read "<name>" (this model does not support application/octet-stream input). Inform the user.

— so the part never reaches the wire and a session that was already poisoned recovers on its next turn (no history rewrite or migration needed).

Actual

The unknown part reaches the wire, the request is rejected, and the session stays broken.

OpenCode version

2.0.26 where we hit it (self-hosted server); the code path is unchanged on dev (4ba064f65306).

Operating System

Linux (container, headless opencode serve) — the same failure is reported on macOS in #54798.

Terminal

Not applicable (server), the MCP tool call is what produces the part.

Plugins

None related; the part comes from an MCP server's tool result.


A three-line PR that adds the missing guard is open: #59249 (it keeps the existing note and adds tests for the user-media case, the nested tool-result case and an untouched supported type).

Activity

  1. opencode-agent commented on Oct 27, 2026

    @opencode-agent
    Contributor

    Thanks for the precise report. I reproduced this on 2.0.26. I used a minimal stdio MCP server whose tool returns an embedded resource blob with mimeType: application/octet-stream, plus a mock OpenAI Chat Completions server configured as an @ai-sdk/openai-compatible provider:

    1. Turn 1 calls the tool, then fails with OpenAI Chat does not support media type application/octet-stream.
    2. A plain follow-up with no tool call fails with the same error, and so does every later turn.

    On the latest dev (4ba064f65), part of this has changed:

    • New tool calls: since fix(core): keep unsupported MCP media out of tool attachments #57640, MCP results whose binary type can't be sent inline are saved to a file and replaced with a text note. A fresh session calling the same tool now works.
    • Existing sessions: a session that already holds such a part, like one created on 2.0.26, still fails on every turn with the same error when opened from dev. The error is raised before the request is sent. As you pointed out, unsupportedMedia() returns early when the media type has no modality, so a stored part like this never gets the replacement note.

    The script below reproduces both the 2.0.26 failure and the dev check. Related: #54798 and #52448 (same symptom, closed by #57640, which only covers new MCP results), and your PR #59249.

    Reproduction script (repro.sh)
    #!/usr/bin/env bash
    # Repro for #59644: an MCP tool result with an application/octet-stream blob poisons an OpenAI-Chat session.
    # Needs: node, bun, `opencode` (@opencode/[email protected]) on PATH. Optional: DEV_REPO=/path/to/opencode checkout (dev).
    # Files used (in this directory): mcp.mjs (stdio MCP server, tool get_blob returns an embedded resource blob
    # with mimeType application/octet-stream) and mock.mjs (mock OpenAI Chat Completions server on :4010 that calls
    # blob_get_blob, then replies with text). Both are shown below as heredocs so the script is self-contained.
    set -u
    R=$(cd "$(dirname "$0")" && pwd); W=$(mktemp -d); mkdir -p "$W/proj" "$W/home"
    cat > "$W/mcp.mjs" <<'JS'
    import readline from "node:readline"
    const rl = readline.createInterface({ input: process.stdin })
    const send = (m) => process.stdout.write(JSON.stringify(m) + "\n")
    rl.on("line", (line) => {
      const msg = JSON.parse(line); if (msg.id === undefined) return
      if (msg.method === "initialize") return send({ jsonrpc: "2.0", id: msg.id, result: { protocolVersion: msg.params.protocolVersion, capabilities: { tools: {} }, serverInfo: { name: "blob", version: "1.0.0" } } })
      if (msg.method === "tools/list") return send({ jsonrpc: "2.0", id: msg.id, result: { tools: [{ name: "get_blob", description: "Returns a binary file", inputSchema: { type: "object", properties: {} } }] } })
      if (msg.method === "tools/call") return send({ jsonrpc: "2.0", id: msg.id, result: { content: [
        { type: "text", text: "here is the file" },
        { type: "resource", resource: { uri: "file:///tmp/archive.zip", mimeType: "application/octet-stream", blob: Buffer.from([0x50,0x4b,3,4,0,1,2,3]).toString("base64") } } ] } })
      send({ jsonrpc: "2.0", id: msg.id, result: {} })
    })
    JS
    cat > "$W/mock.mjs" <<'JS'
    import http from "node:http"
    let n = 0
    http.createServer((req, res) => { let body = ""; req.on("data", (c) => (body += c)); req.on("end", () => {
      n++; const j = JSON.parse(body || "{}"); const s = JSON.stringify(j.messages ?? [])
      const names = (j.tools ?? []).map((t) => t.function?.name)
      console.error(`[mock] request #${n} last=${j.messages?.at(-1)?.role}`)
      res.writeHead(200, { "content-type": "text/event-stream" })
      const chunk = (d) => res.write(`data: ${JSON.stringify({ id: "c", object: "chat.completion.chunk", created: 0, model: "mock", choices: [d] })}\n\n`)
      const call = (id, name, args) => { chunk({ index: 0, delta: { role: "assistant", tool_calls: [{ index: 0, id, type: "function", function: { name, arguments: JSON.stringify(args) } }] } }); chunk({ index: 0, delta: {}, finish_reason: "tool_calls" }) }
      if (s.includes("call the tool") && !s.includes("call_blob") && names.includes("blob_get_blob")) call("call_blob", "blob_get_blob", {})
      else if (s.includes("call the tool") && !s.includes("call_blob") && names.length) call("wait_" + n, "shell", { command: "sleep 2", description: "wait for MCP" })
      else { chunk({ index: 0, delta: { role: "assistant", content: "MOCK-REPLY-OK" } }); chunk({ index: 0, delta: {}, finish_reason: "stop" }) }
      res.write("data: [DONE]\n\n"); res.end() }) }).listen(4010)
    JS
    cat > "$W/proj/opencode.json" <<JSON
    { "model": "mock/mock",
      "provider": { "mock": { "npm": "@ai-sdk/openai-compatible", "name": "Mock",
        "options": { "baseURL": "http://127.0.0.1:4010/v1", "apiKey": "x" },
        "models": { "mock": { "name": "mock", "tool_call": true } } } },
      "mcp": { "servers": { "blob": { "type": "local", "command": ["node", "$W/mcp.mjs"], "codemode": false } } },
      "permission": { "*": "allow" } }
    JSON
    node "$W/mock.mjs" 2>"$W/mock.log" & MOCK=$!
    trap 'kill $MOCK ${SRV:-} 2>/dev/null' EXIT
    cd "$W/proj"; export HOME="$W/home"; S=ses_repro0000000000000000059
    echo "== $(opencode --version): turn 1 (tool returns octet-stream blob)"
    opencode run --standalone --auto --session $S -m mock/mock "please call the tool" 2>&1 | tail -3
    echo "== $(opencode --version): turn 2 (plain follow-up, no tool call)"
    opencode run --standalone --auto --session $S -m mock/mock "hello again" 2>&1 | tail -2
    if [ -n "${DEV_REPO:-}" ]; then
      DEV="bun --conditions=browser --preload=$DEV_REPO/packages/tui/node_modules/@opentui/solid/scripts/preload.js $DEV_REPO/packages/cli/src/index.ts"
      D=$HOME/.local/share/opencode; cp "$D/opencode.db" "$D/opencode-local.db"   # dev builds use opencode-local.db
      $DEV serve --port 4098 --hostname 127.0.0.1 >"$W/serve.log" 2>&1 & SRV=$!; sleep 12
      export OPENCODE_SERVER_PASSWORD=$(awk '/password/{print $3}' "$W/serve.log")
      echo "== dev: follow-up in the session poisoned above"
      $DEV run --server http://127.0.0.1:4098 --auto --session $S -m mock/mock "hello again" 2>&1 | tail -2
      echo "== dev: fresh session, same MCP tool (entry point fixed by #57640)"
      $DEV run --server http://127.0.0.1:4098 --auto --session ${S}1 -m mock/mock "please call the tool" 2>&1 | tail -2
    fi

    Run it: bash repro.sh

  2. iceteaSA commented on Dec 1, 2026

    @iceteaSA

    The same request-building failure affects a text format on the Anthropic Messages route. On stock v2.4.3 and v2.4.2, a plugin tool returned one inline application/json file. #57640 handles MCP tool results, not files returned directly by plugin tools.

    OpenCode reports provider.invalid-request: Anthropic Messages does not support media type application/json. The turn ends with finish=error, and the mock endpoint receives no second request. The rejection is in the Anthropic Messages request builder and does not depend on the model ID. With text/plain or image/png, the endpoint receives the second request and the turn completes.

    Repro on stock v2.4.3 with anthropic/claude-opus-4-6 and a local Anthropic Messages fixture:

    • application/json: 1 request, finish=error
    • text/plain: 2 requests, finish=stop
    • image/png: 2 requests, finish=stop

    In v2.4.3, lowerMedia converts text/plain to a text document and rejects application/json. Could the tool-result path send JSON as a text document, like text/plain?

    Disclosure: this report was found and drafted with AI tools (OpenAI GPT and Anthropic Claude models).

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