Skip to content

fix: guard against undefined MCP tool output causing output.split crash#24618

Open
leandrosnx wants to merge 2 commits intoanomalyco:devfrom
leandrosnx:fix/mcp-tool-output-null-guard
Open

fix: guard against undefined MCP tool output causing output.split crash#24618
leandrosnx wants to merge 2 commits intoanomalyco:devfrom
leandrosnx:fix/mcp-tool-output-null-guard

Conversation

@leandrosnx
Copy link
Copy Markdown

Issue for this PR

Closes #24402

Type of change

  • Bug fix
  • New feature
  • Refactor / code improvement
  • Documentation

What does this PR do?

Fixes a crash (TypeError: undefined is not an object (evaluating 'output.split')) that occurs when MCP tools return results with no text content (e.g., only images or empty content arrays).

When an MCP tool result has no text content, part.state.output becomes undefined. This propagates to truncateToolOutput() and Truncate.output(), which try to call .length and .split("\n") on it respectively, causing the app to crash.

I added simple undefined/null guards:

  • if (!text) return "" in truncateToolOutput()
  • if (text == null) early return in Truncate.output()
  • ?? "" fallback at the call site in toModelMessagesEffect()

How did you verify your code works?

I tested this locally with an MCP server that returns non-text responses (specifically an image generation tool). After the changes, the crash no longer happens and the output is safely handled as an empty string.

Screenshots / recordings

N/A

Checklist

  • I have tested my changes locally
  • I have not included unrelated changes in this PR

When an MCP tool returns a result with no text content (e.g., only images
or empty content array), part.state.output can be undefined. This flows
into truncateToolOutput() and Truncate.output() which call .split() and
.length on the undefined value, causing:

  TypeError: undefined is not an object (evaluating 'output.split')

Fix:
- Add null guard in truncateToolOutput() returning empty string
- Add ?? fallback at call site in toModelMessagesEffect()
- Add null guard in Truncate.output() returning safe default
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

fix: MCP tool output undefined causes TypeError: output.split crash

1 participant