Skip to content

Conversation

@usvimal
Copy link
Contributor

@usvimal usvimal commented Jan 23, 2026

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.

When an MCP tool returns a result where content is undefined or not
an array, the for...of loop throws 'TypeError: {} is not iterable'.

This commonly happens with built-in Claude tools like discard/extract
which may return non-standard content formats.

Added Array.isArray() guard to safely handle non-array content values.
@github-actions
Copy link
Contributor

Thanks for your contribution!

This PR doesn't have a linked issue. All PRs must reference an existing issue.

Please:

  1. Open an issue describing the bug/feature (if one doesn't exist)
  2. Add Fixes #<number> or Closes #<number> to this PR description

See CONTRIBUTING.md for details.

@github-actions
Copy link
Contributor

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

No duplicate PRs found

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.

1 participant