[PR #10141] fix: handle non-array MCP tool result.content to prevent TypeError #13352

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

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

State: open
Merged: No


Summary

Fixes TypeError: {} is not iterable error when MCP tools (like Claude's built-in discard and extract context management tools) return results with non-array content values.

Problem

In packages/opencode/src/session/prompt.ts:770, the code iterates over result.content assuming it's always an array:

for (const contentItem of result.content) {

When an MCP tool returns content as undefined, {}, or any non-array value, this throws:

TypeError: {} is not iterable

Solution

Added an Array.isArray() guard to safely handle non-array content:

const contentArray = Array.isArray(result.content) ? result.content : []
for (const contentItem of contentArray) {

Testing

Reproduced the error using Claude's built-in discard and extract tools, verified the fix prevents the error while maintaining correct behavior for standard MCP tool results.

**Original Pull Request:** https://github.com/anomalyco/opencode/pull/10141 **State:** open **Merged:** No --- ## Summary Fixes `TypeError: {} is not iterable` error when MCP tools (like Claude's built-in `discard` and `extract` context management tools) return results with non-array `content` values. ## Problem In `packages/opencode/src/session/prompt.ts:770`, the code iterates over `result.content` assuming it's always an array: ```typescript for (const contentItem of result.content) { ``` When an MCP tool returns `content` as `undefined`, `{}`, or any non-array value, this throws: ``` TypeError: {} is not iterable ``` ## Solution Added an `Array.isArray()` guard to safely handle non-array content: ```typescript const contentArray = Array.isArray(result.content) ? result.content : [] for (const contentItem of contentArray) { ``` ## Testing Reproduced the error using Claude's built-in `discard` and `extract` tools, verified the fix prevents the error while maintaining correct behavior for standard MCP tool results.
yindo added the pull-request label 2026-02-16 18:18:12 -05:00
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: anomalyco/opencode#13352