Skip to content

Conversation

@roomote
Copy link
Contributor

@roomote roomote bot commented Jan 6, 2026

Summary

This PR attempts to address Issue #7042. Feedback and guidance are welcome.

Problem

Large MCP responses (e.g., from database queries, logs) can exceed the model's context window and stall the agent. The previous PR #7043 was closed because it used a static 50KB threshold instead of dynamically calculating available context.

Solution

This implementation follows the scoping requirements from @daniel-lxs:

Dynamic Context Awareness

  • Calculates available context dynamically based on:
    • Model's context window size
    • Current token usage in conversation
    • Reserved output tokens
    • A 50% budget allocation for MCP responses (similar to ReadFileTool's approach)

When Response Exceeds Budget

  • Saves the full response to .roo/tmp/mcp-responses/ directory
  • Returns a preview (first 50 lines) in context
  • Includes the file path so the agent can read specific sections using read_file tool
  • Provides suggested follow-up actions

No Static Thresholds

  • Decision is based entirely on available context, not fixed byte limits
  • Works correctly with both small and large context window models

Changes

  • src/core/tools/helpers/mcpResponseHandler.ts: New helper module for handling MCP response size
  • src/core/tools/UseMcpToolTool.ts: Integrated response handler
  • src/core/tools/accessMcpResourceTool.ts: Integrated response handler
  • src/core/tools/helpers/__tests__/mcpResponseHandler.spec.ts: Comprehensive tests

Testing

  • Added 14 unit tests covering various scenarios
  • All existing tests pass
  • TypeScript compiles without errors

Acceptance Criteria Addressed

  • System no longer stalls on large MCP responses
  • Decision uses the active model's context, conversation token usage, and a safety margin
  • Saved files are accessible to the agent and organized predictably
  • No mandatory user configuration required

- Add mcpResponseHandler helper to dynamically check if MCP responses fit within available context budget
- Calculate available budget based on model context window, current token usage, and reserved output tokens
- Save oversized responses to .roo/tmp/mcp-responses/ directory with a preview in context
- Integrate handler into UseMcpToolTool and accessMcpResourceTool
- Add comprehensive tests for the new functionality

Closes #7042
@roomote
Copy link
Contributor Author

roomote bot commented Jan 6, 2026

Rooviewer Clock   See task on Roo Cloud

Review completed. Found 1 issue to address:

  • Preview line count in message is hardcoded to 50 but should use the actual previewLines value (comment)

Mention @roomote in a comment to request specific changes to this pull request or fix all unresolved issues.


You can read the complete response using the read_file tool with the path above.

Preview (first ${DEFAULT_PREVIEW_LINES} lines):
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The preview line count shown in the message is hardcoded to DEFAULT_PREVIEW_LINES (50), but the actual preview is generated using the previewLines parameter which can be customized. If a caller passes a different value like { previewLines: 20 }, the message will incorrectly say "Preview (first 50 lines)" when only 20 lines are shown. Consider passing the actual previewLines value to this function and using it in the template.

Fix it with Roo Code or mention @roomote and request a fix.

@hannesrudolph hannesrudolph added the Issue/PR - Triage New issue. Needs quick review to confirm validity and assign labels. label Jan 6, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Issue/PR - Triage New issue. Needs quick review to confirm validity and assign labels.

Projects

Status: Triage

Development

Successfully merging this pull request may close these issues.

3 participants