[PR #12990] fix: handle undefined MCP tool output in stream processor #14467

Open
opened 2026-02-16 18:19:15 -05:00 by yindo · 0 comments
Owner

Original Pull Request: https://github.com/anomalyco/opencode/pull/12990

State: open
Merged: No


Summary

Fixes #12987 — Kubernetes MCP server tools (and potentially other MCP servers) can fail with TypeError: undefined is not an object (evaluating 'output.output.toLowerCase') when tool-result payloads are not in the expected nested shape.

Root Cause

session/processor.ts assumed every tool-result chunk had:

value.output = { output: string, metadata: object, title: string, ... }

For some dynamic/provider/MCP result shapes, that assumption can be false (e.g. raw content[] payloads), causing undefined access and downstream crashes.

Changes

  • packages/opencode/src/session/processor.ts

    • Normalize dynamic tool results safely before persisting:
      • use nested output.output when present
      • fallback to content[] text extraction when available
      • fallback to raw string output
      • final fallback to JSON stringification for unknown payloads
    • Guard metadata/title/attachments with shape checks to avoid unsafe property access.
  • packages/opencode/src/session/prompt.ts

    • Guard MCP wrapper handling with result.content ?? [] to avoid iterating undefined content.

Verification

  • bun run typecheck (package-level)
  • Push hook bun turbo typecheck (workspace-level)
  • Focused regression tests:
    • bun test test/session/prompt-special-chars.test.ts test/session/prompt-missing-file.test.ts test/mcp/headers.test.ts
**Original Pull Request:** https://github.com/anomalyco/opencode/pull/12990 **State:** open **Merged:** No --- ## Summary Fixes #12987 — Kubernetes MCP server tools (and potentially other MCP servers) can fail with `TypeError: undefined is not an object (evaluating 'output.output.toLowerCase')` when tool-result payloads are not in the expected nested shape. ## Root Cause `session/processor.ts` assumed every `tool-result` chunk had: ```ts value.output = { output: string, metadata: object, title: string, ... } ``` For some dynamic/provider/MCP result shapes, that assumption can be false (e.g. raw `content[]` payloads), causing undefined access and downstream crashes. ## Changes - **`packages/opencode/src/session/processor.ts`** - Normalize dynamic tool results safely before persisting: - use nested `output.output` when present - fallback to `content[]` text extraction when available - fallback to raw string output - final fallback to JSON stringification for unknown payloads - Guard metadata/title/attachments with shape checks to avoid unsafe property access. - **`packages/opencode/src/session/prompt.ts`** - Guard MCP wrapper handling with `result.content ?? []` to avoid iterating undefined content. ## Verification - `bun run typecheck` (package-level) ✅ - Push hook `bun turbo typecheck` (workspace-level) ✅ - Focused regression tests: - `bun test test/session/prompt-special-chars.test.ts test/session/prompt-missing-file.test.ts test/mcp/headers.test.ts` ✅
yindo added the pull-request label 2026-02-16 18:19:15 -05:00
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: anomalyco/opencode#14467