Make collect context tool production-ready - #1
Conversation
Co-authored-by: ch <ch@devora.no>
|
Cursor Agent can help with this pull request. Just |
There was a problem hiding this comment.
This is the final PR Bugbot will review for you during this billing cycle
Your free Bugbot reviews will reset on November 26
Details
You are on the Bugbot Free tier. On this plan, Bugbot will review limited PRs each billing cycle.
To receive Bugbot reviews on all of your PRs, visit the Cursor dashboard to activate Pro and start your 14-day free trial.
| logger.warn('No git changes found for changed strategy', { traceId }); | ||
| throw new ValidationError(errorMsg); | ||
| } | ||
| if (isDebugMode) { |
There was a problem hiding this comment.
Bug: Git History Present But No Modified Files
Using strategy='changed' in collectContext throws a ValidationError when no files are modified in a git repository. The error message "No git history detected" is misleading, as git history is present, but no files are currently changed. This should return an empty context instead of an error, as having no modified files is a valid state.
| logger.warn('Path traversal attempt blocked', { traceId, path }); | ||
| continue; | ||
| } | ||
|
|
There was a problem hiding this comment.
Pull Request
Description
This Pull Request introduces a comprehensive overhaul of the
collect_contexttool, making it fully production-ready, highly debuggable, and functionally robust. The changes address critical bugs related to Git awareness, globbing,.gitignoresupport, truncation, binary file handling, path limits, and caching correctness. Additionally, the PR includes extensive unit and integration tests, updated documentation for MCP Inspector usage, and a detailed debug report.The primary motivation is to enhance the reliability, security, and diagnostic capabilities of the
collect_contexttool, ensuring it provides accurate and relevant context to the AI assistant while being easy to troubleshoot in production environments.Type of Change
Related Issues
Fixes # (issue number)
Closes # (issue number)
Related to # (issue number)
Changes Made
examples/inspector-stdio.jsonandexamples/inspector-stdio-local.jsonfor easy debugging with MCP Inspector. Addedinspect:stdioscript topackage.json.CONTEXT_DEBUG=1environment flag. Each request now emits a uniquetraceIdincluded in all logs and the response'smetaobject.collect_contexttool now returns a structured result includingfiles,bytes,truncated,cacheKey, and ametaobject withtraceId,strategy,usedGit,elapsedMs, andnotes.strategy="changed"to usegit diff --name-only HEAD, with robust error handling for non-git repositories or empty changes. Detects current HEAD SHA for cache key generation.ignorelibrary to respect.gitignorerules. Added default excludes fornode_modules,.git, and common binary files. Supportsinclude[]andexclude[]glob patterns with POSIX path normalization.maxKBandmaxFiles. Implemented a per-file hard cap (256KB) and setstruncated=truewithnotesin the response when limits are hit.{cwd, headShaOrNone, argsHash}with a 10-minute TTL. Cache hits are surfaced inmeta.cwdsubtree and reject absolute paths outsidecwd. Redacts secrets in error messages and never emits file contents in logs.test/tools/collectContext.test.ts) covering all new features and edge cases. Developed integration tests (test/integration/collectContext.git.test.ts) using a temporary git repository and fixtures (test/fixtures/mini-repo/) for realistic scenarios. Addedtest:contextandtest:context:gitnpm scripts.docs/inspector-playbook.mdwith step-by-step instructions for using MCP Inspector. UpdatedREADME.mdwith an "Inspector (stdio) Quick Start" and a "Troubleshooting (Context Tool)" section.docs/context-debug-report.mdsummarizing findings, fixes, and any remaining edge cases.handleErrorandredactSecretsfor robustness.Testing
Test Coverage
Test Results
Documentation
Breaking Changes
None. The public contract of the
collect_contexttool remains compatible, with added fields in the response for enhanced diagnostics.Checklist
Code Quality
CONTEXT_DEBUGenv flag)Testing
Documentation
Security
ignoreadded, which is tiny and widely used)Additional Notes
This PR represents a significant step towards making the
collect_contexttool robust and production-ready. The focus has been on comprehensive debugging capabilities, reliable file collection logic, and strong security measures. The newCONTEXT_DEBUG=1flag, combined with thetraceIdin responses and logs, provides an unparalleled ability to diagnose and troubleshoot issues in real-time.Reviewer Notes
Please pay close attention to:
CONTEXT_DEBUG=1is enabled.strategy="changed"and error handling for non-git scenarios..gitignorerules are correctly applied and default binary/large file exclusions work as expected.README.mdanddocs/inspector-playbook.mdare clear and accurate.