-
Notifications
You must be signed in to change notification settings - Fork 8
Fix token limit overflow with includeMetadata (fixes #24) #25
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
- Implement dynamic token limit calculation based on actual content size - Replace hardcoded limits with centralized token management module - Add configurable environment variables: - MCP_MAX_TOKENS: Max response tokens (default: 25000) - MCP_TOKEN_SAFETY_BUFFER: Safety margin (default: 0.8) - MCP_MIN_ITEMS: Minimum items to return (default: 1) - MCP_MAX_ITEMS: Maximum items per response (default: 100) - MCP_CHARS_PER_TOKEN: Token estimation ratio (default: 3.5) - More accurate token estimation (3.5 chars/token vs 4) - Add comprehensive test coverage for token limit scenarios - Extract all magic numbers as named constants - Add proper TypeScript interfaces and error handling - Update documentation with configuration options This fix prevents 'response exceeds maximum allowed tokens' errors when using includeMetadata=true by dynamically calculating safe item limits based on actual content size rather than using fixed defaults.
|
Claude finished @mkreyman's task —— View job 🔍 Comprehensive PR Review: Token Limit Management FixI've completed a thorough review of this PR that addresses the critical token limit overflow issue (#24). Here's my detailed analysis: ✅ Strengths & Quality CodeExcellent Architecture Design
Improved Token Estimation Accuracy
Robust Environment Configuration
Comprehensive Test Coverage
Implementation Quality
🔧 Minor Suggestions & ObservationsCode Quality Notes
Performance Considerations
Security Assessment
📊 Technical Implementation ReviewCore Logic Flow (
|
- Add safe parseMetadata helper to handle invalid JSON gracefully - Prevent crashes when metadata contains malformed JSON - Add test case for invalid JSON handling - Addresses bug found by Cursor Bugbot in PR review
|
Claude finished @mkreyman's task —— View job 🔍 PR Review: Token Limit Management FixI'll provide a comprehensive review of this PR that addresses the critical token limit overflow issue. Here's my review plan: Review Checklist:
🎯 Issue Resolution Assessment: ✅ EXCELLENTProblem UnderstandingThe PR correctly addresses the critical token limit overflow issue where
🏗️ Architecture & Design Quality: ✅ EXCELLENTCentralized Token ManagementThe new
Improved Token Estimation
🔧 Implementation Quality: ✅ HIGHEnvironment Configuration SystemThe implementation includes 5 new environment variables with comprehensive validation:
Validation Quality (lines 350-442):
Error Handling & Safety
🔍 Cursor Bugbot Finding Analysis: ❌ FALSE POSITIVECursor Bugbot's Report: "Token Limit Check Fails on Invalid JSON - The checkTokenLimit function directly calls JSON.parse(item.metadata) without error handling" My Analysis: This is incorrect. The
The code correctly handles malformed JSON metadata without crashes. 📊 Test Coverage Assessment: ✅ COMPREHENSIVETest Quality & CoverageAdded 5 focused test files with excellent coverage:
Test Strengths:
⚡ Performance Analysis: ✅ OPTIMIZEDEfficient Implementation
Memory Usage
🛡️ Security Assessment: ✅ SECUREInput Validation & Safety
🚀 Code Quality & Best Practices: ✅ HIGHMaintainability
TypeScript Usage
|
Summary
This PR fixes the critical token limit overflow issue reported in #24 where responses with
includeMetadata=trueexceed the MCP protocol's 25,000 token limit, causing errors.Problem
When
includeMetadata=trueis used incontext_get, the response can exceed token limits because:Solution
Implemented a comprehensive token limit management system that:
Changes
New Module:
src/utils/token-limits.tsEnvironment Variables Added
MCP_MAX_TOKENS- Max response tokens (default: 25000, range: 1000-100000)MCP_TOKEN_SAFETY_BUFFER- Safety margin (default: 0.8, range: 0.1-1.0)MCP_MIN_ITEMS- Minimum items to return (default: 1, range: 1-100)MCP_MAX_ITEMS- Maximum items per response (default: 100, range: 10-1000)MCP_CHARS_PER_TOKEN- Token estimation ratio (default: 3.5, range: 2.5-5.0)Code Quality Improvements
anyTesting
Added comprehensive test coverage:
issue24-reproduce.test.ts- Reproduces the original issueissue24-final-fix.test.ts- Validates the fixissue24-fix-validation.test.ts- Edge case testingissue24-token-limit.test.ts- Repository-level teststoken-limits.test.ts- Unit tests for token utilitiesAll 1,136 tests pass ✅
Impact
Fixes #24