Skip to content

Conversation

olebedev
Copy link

@olebedev olebedev commented Oct 2, 2025

This enhancement builds upon the existing fix for 'bufio.Scanner: token too long' errors by adding configurable buffer sizes with a much larger default (1MB).

Changes:

  • Add bufferSize field to Stdio struct with 1MB default (vs 4KB default)
  • Add WithBufferSize() option function for custom buffer configuration
  • Update constructors to use bufio.NewReaderSize() with configurable buffer
  • Add GetBufferSize() and SetBufferSize() utility methods
  • Add comprehensive tests for buffer size configuration and large messages
  • Add benchmarks for different buffer sizes and message sizes

Features:

  • Backward compatible: existing code automatically gets 1MB buffer
  • Configurable: users can set custom buffer sizes for specific needs
  • Efficient: reduces buffer reallocations for large messages
  • Well-tested: comprehensive test suite including 5MB+ messages

Usage:
// Default usage (1MB buffer)
client := transport.NewStdio("server", env, args...)

// Custom buffer for very large responses
client := transport.NewStdioWithOptions("server", env, args,
transport.WithBufferSize(10 * 1024 * 1024)) // 10MB

Fixes large MCP tool responses, base64 content, and verbose error messages that previously caused 'token too long' errors with the default 4KB buffer.

Summary by CodeRabbit

  • New Features

    • Added configurable stdio buffer size with a 1MB default, enabling better handling of large messages.
    • Allows querying the current buffer size and updating it for future connections; invalid values fall back to the default.
  • Tests

    • Added comprehensive tests and benchmarks for buffer size configuration and large message handling to validate behavior across scenarios.

This enhancement builds upon the existing fix for 'bufio.Scanner: token too long'
errors by adding configurable buffer sizes with a much larger default (1MB).

Changes:
- Add bufferSize field to Stdio struct with 1MB default (vs 4KB default)
- Add WithBufferSize() option function for custom buffer configuration
- Update constructors to use bufio.NewReaderSize() with configurable buffer
- Add GetBufferSize() and SetBufferSize() utility methods
- Add comprehensive tests for buffer size configuration and large messages
- Add benchmarks for different buffer sizes and message sizes

Features:
- Backward compatible: existing code automatically gets 1MB buffer
- Configurable: users can set custom buffer sizes for specific needs
- Efficient: reduces buffer reallocations for large messages
- Well-tested: comprehensive test suite including 5MB+ messages

Usage:
// Default usage (1MB buffer)
client := transport.NewStdio("server", env, args...)

// Custom buffer for very large responses
client := transport.NewStdioWithOptions("server", env, args,
    transport.WithBufferSize(10 * 1024 * 1024)) // 10MB

Fixes large MCP tool responses, base64 content, and verbose error messages
that previously caused 'token too long' errors with the default 4KB buffer.
Copy link
Contributor

coderabbitai bot commented Oct 2, 2025

Walkthrough

Adds a configurable buffer size to Stdio transport: new DefaultBufferSize constant, option WithBufferSize, getters/setters, and wiring through IO initialization and spawnCommand. Introduces comprehensive tests and benchmarks for buffer configuration and large message handling. No other modules changed.

Changes

Cohort / File(s) Summary
Stdio transport implementation
client/transport/stdio.go
Adds DefaultBufferSize, bufferSize field, GetBufferSize/SetBufferSize methods, and WithBufferSize option; applies buffer sizing in NewIO and spawnCommand via bufio.NewReaderSize; validates non-positive sizes by falling back to default.
Tests and benchmarks
client/transport/stdio_buffer_test.go
New tests and benchmarks covering default/custom buffer sizes, SetBufferSize behavior, large JSON-RPC message handling across sizes, NewIO defaulting, and performance across buffer sizes; includes helper writeCloser and payload generator.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~30 minutes

Possibly related PRs

Suggested reviewers

  • dugenkui03

Pre-merge checks and finishing touches

❌ Failed checks (2 warnings)
Check name Status Explanation Resolution
Description Check ⚠️ Warning The pull request description does not adhere to the repository’s required template, as it omits the “## Description” heading, a Fixes #<issue_number> line, the “Type of Change” section with checkboxes, and the “Checklist” section. Without these mandatory sections, the description is incomplete and inconsistent with project guidelines. Please revise the pull request description to align with the repository template by adding a “## Description” heading with a concise summary, including a Fixes #<issue_number> line if applicable, and filling out the “Type of Change” and “Checklist” sections with appropriate checkbox selections.
Docstring Coverage ⚠️ Warning Docstring coverage is 50.00% which is insufficient. The required threshold is 80.00%. You can run @coderabbitai generate docstrings to improve docstring coverage.
✅ Passed checks (1 passed)
Check name Status Explanation
Title Check ✅ Passed The title "fix: enhanced buffer handling" accurately and concisely summarizes the primary change to buffer handling and follows the conventional prefix for bug fixes. It clearly conveys that the pull request focuses on improving buffer configuration without extraneous details.
✨ Finishing touches
  • 📝 Generate Docstrings
🧪 Generate unit tests
  • Create PR with unit tests
  • Post copyable unit tests in a comment

📜 Recent review details

Configuration used: CodeRabbit UI

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 61b5d9e and 48830c6.

📒 Files selected for processing (2)
  • client/transport/stdio.go (7 hunks)
  • client/transport/stdio_buffer_test.go (1 hunks)
🧰 Additional context used
🧠 Learnings (3)
📓 Common learnings
Learnt from: ezynda3
PR: mark3labs/mcp-go#0
File: :0-0
Timestamp: 2025-08-08T15:38:52.931Z
Learning: In mark3labs/mcp-go, for stdio line reading (e.g., client/transport/stdio.go), maintainers prefer bufio.Reader over bufio.Scanner because Scanner’s default 64 KiB token limit is problematic for long lines. If Scanner is used, its buffer limit should be raised via Scanner.Buffer.
Learnt from: ezynda3
PR: mark3labs/mcp-go#0
File: :0-0
Timestamp: 2025-08-08T15:37:18.458Z
Learning: In mark3labs/mcp-go stdio transport, using bufio.Scanner without calling Scanner.Buffer imposes a 64 KiB per-line limit; to support very long lines safely, set a larger max via Scanner.Buffer or use a streaming Reader/json.Decoder approach.
📚 Learning: 2025-08-08T15:38:52.931Z
Learnt from: ezynda3
PR: mark3labs/mcp-go#0
File: :0-0
Timestamp: 2025-08-08T15:38:52.931Z
Learning: In mark3labs/mcp-go, for stdio line reading (e.g., client/transport/stdio.go), maintainers prefer bufio.Reader over bufio.Scanner because Scanner’s default 64 KiB token limit is problematic for long lines. If Scanner is used, its buffer limit should be raised via Scanner.Buffer.

Applied to files:

  • client/transport/stdio.go
  • client/transport/stdio_buffer_test.go
📚 Learning: 2025-08-08T15:37:18.458Z
Learnt from: ezynda3
PR: mark3labs/mcp-go#0
File: :0-0
Timestamp: 2025-08-08T15:37:18.458Z
Learning: In mark3labs/mcp-go stdio transport, using bufio.Scanner without calling Scanner.Buffer imposes a 64 KiB per-line limit; to support very long lines safely, set a larger max via Scanner.Buffer or use a streaming Reader/json.Decoder approach.

Applied to files:

  • client/transport/stdio.go
  • client/transport/stdio_buffer_test.go
🧬 Code graph analysis (2)
client/transport/stdio.go (1)
server/stdio.go (1)
  • StdioOption (49-49)
client/transport/stdio_buffer_test.go (1)
client/transport/stdio.go (5)
  • DefaultBufferSize (21-21)
  • Stdio (27-51)
  • NewStdioWithOptions (123-146)
  • WithBufferSize (74-81)
  • NewIO (93-105)

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@olebedev olebedev closed this Oct 2, 2025
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.

1 participant