Package Exports
- mcp-code-context
- mcp-code-context/dist/src/index.js
This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (mcp-code-context) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.
Readme
mcp-code-context
MCP server that compresses any codebase into LLM-ready semantic context and provides surgical code editing tools. AST-based read & write for TypeScript, JavaScript, PHP, Dart & Python.
🚀 Quick Start (Claude Desktop)
- Install:
npm install -g mcp-code-context - Configure: Add to
claude_desktop_config.json:
{
"mcpServers": {
"code-context": {
"command": "npx",
"args": ["-y", "mcp-code-context"]
}
}
}- Enjoy: Use symbols like
@code-contextto map repos or edit code surgically.
Works with Claude Desktop, Cursor, Windsurf, GitHub Copilot, Amazon Q, and any Model Context Protocol compatible client.
The Problem
LLMs working with code face two bottlenecks:
- Reading: Sending raw source files wastes the context window on function bodies and boilerplate. A 500-line file might contain only 30 lines of structural information the LLM needs.
- Writing: Rewriting entire files to change one function is error-prone, token-expensive, and risks corrupting unrelated code.
The Solution
mcp-code-context provides 11 tools — 5 for reading, 1 for cleanup, and 5 for writing — that operate at the symbol level (functions, classes, methods). Furthermore, tools support a className scope which correctly isolates identical symbol names in the same file (e.g. Flutter build() methods) to avoid reading or changing the wrong logic. Read tools extract structural skeletons. Write tools splice changes into the exact AST location.
| File | Original | Compressed | Reduction |
|---|---|---|---|
| PHP class (426 lines) | 426 | 60 | 85.9% |
| Dart repository (230 lines) | 230 | 30 | 87.0% |
| PHP config (68 lines) | 68 | 15 | 77.9% |
Reliability & Testing
Built to be robust and precise. Both read and write engines are tested against real-world, complex codebases (including nested generic types in Dart, complex interfaces in PHP, and multi-file rename operations) with a 100% test pass rate across all languages and operations.
Features
Read
- 🌳 AST-based compression — Real parsers for TypeScript/JavaScript (ts-morph) and PHP (php-parser). Brace-counting engine for Dart. Regex-based extraction for Python.
- 🔬 Surgical symbol extraction — Extract a single function, class, or method from a file by name. Use
classNameto scope disambiguation (e.g., getting multiplebuild()methods in Dart). - 💥 Impact analysis — Discover all files that depend on a given file before refactoring. Supports ES imports, CommonJS
require(), Python imports, PHPuse/require_once/include, and Dart imports. - 📁 Smart file walking — Respects
.gitignoreand.repomixignorerules. Automatically excludesnode_modules,dist,vendor,.git, etc. - 📄 Multi-format output — XML (optimized for LLM consumption) or Markdown (human-readable).
Write
- ✏️ Surgical symbol replacement — Replace a function, method, or class body without touching the rest of the file. Narrow down the target using the
classNameparameter. - ➕ Precise code insertion — Insert new code before/after a symbol, or inside a class at the start/end.
- 🔄 Repository-wide rename — Rename a symbol in its definition AND all files that import it, atomically.
- 🗑️ Safe symbol removal — Delete code with automatic dependency checking to prevent breakage.
- 🔍 Mandatory dry-run flow — Write tools return a preview diff and a
confirmationTokenby default. Changes are only applied after explicit confirmation. - 💾 Robust rolling backups — Automatically keeps the last 5 versions of modified files in a hidden
.mcp-backups/directory. - ⏪ Surgical rollback — Revert files to any of the 5 previous states using the new
rollback_filetool. - 🤖 Fuzzy symbol matching — When a symbol is not found, the server provides structured suggestions based on Levenshtein distance.
- 🔐 Private symbol support — Full support for
_and__prefixed symbols in Dart and Python.
Supported Languages
| Language | Read (Compress + Extract) | Write (Replace + Insert + Rename + Remove) | Import Analysis |
|---|---|---|---|
| TypeScript / JavaScript | ✅ AST (ts-morph) | ✅ AST (ts-morph) | ✅ |
| PHP | ✅ AST (php-parser) | ✅ AST + line-splice | ✅ |
| Dart | ✅ Brace-counting + regex | ✅ Brace-counting + regex | ✅ |
| Python | ✅ Regex-based | ✅ Indentation-aware | ✅ |
| Others (JSON, YAML, CSS, etc.) | Passthrough / truncation | — | — |
Installation
# Clone the repository
git clone https://github.com/YOUR_USERNAME/mcp-code-context.git
cd mcp-code-context
# Install dependencies
npm install
# Build
npm run buildConfiguration
Claude Desktop
Add to your claude_desktop_config.json:
{
"mcpServers": {
"mcp-code-context": {
"command": "node",
"args": ["/absolute/path/to/mcp-code-context/dist/index.js"]
}
}
}Cursor
Add to your Cursor MCP settings (.cursor/mcp.json):
{
"mcpServers": {
"mcp-code-context": {
"command": "node",
"args": ["/absolute/path/to/mcp-code-context/dist/index.js"]
}
}
}Windsurf
Add to your Windsurf MCP config:
{
"mcpServers": {
"mcp-code-context": {
"command": "node",
"args": ["/absolute/path/to/mcp-code-context/dist/index.js"]
}
}
}Amazon Q / Other MCP Clients
Any MCP-compatible client can use this server. The transport is stdio (JSON-RPC over stdin/stdout). Point your client to node dist/index.js.
Tools
Read Tools
1. get_semantic_repo_map
Generate a compressed architectural overview of an entire repository.
directoryPath(required) — Path to the repo rootformat(optional) —"xml"(default) or"markdown"
2. read_file_surgical
Read a file, or extract only a specific named symbol. Returns structured suggestions if the symbol is missing.
filePath(required) — Path to the source filesymbolName(optional) — Name of a function, class, method, or typeclassName(optional) — Scope the symbol to a specific class (to avoid duplicates)
3. analyze_impact
Find all files that depend on a given file.
filePath(required) — Path to the file being modifiedrootDir(optional) — Repository root (auto-detected)
4. read_file_lines
Read specific line ranges from a file without loading the entire content. More efficient than read_file_surgical for small fragments.
filePath(required) — Path to the source filestartLine(optional) — Starting line number (1-indexed)endLine(optional) — Ending line number (1-indexed)aroundPattern(optional) — Search pattern to find and return surrounding linescontextLines(optional) — Number of lines before/after pattern (default: 5)
5. search_code_pattern
Search for code patterns across multiple files with context. Respects .gitignore rules.
rootDir(required) — Repository root directorypattern(required) — Regular expression pattern to searchfileExtensions(optional) — Array of extensions to search (e.g.,[".ts", ".dart"])excludeDirs(optional) — Directories to exclude (default:["node_modules", "dist", "build"])showContext(optional) — Include surrounding lines (default: true)contextLines(optional) — Number of context lines (default: 3)maxResults(optional) — Maximum matches to return (default: 50)
6. rollback_file
Surgically restore a file to a previous state from the automated backup system.
filePath(required) — Path to the file to restoresteps(optional) — Number of versions to go back (1-5, default: 1)
7. clean_backups
Remove all backup files for a project to keep the working directory clean.
projectRoot(required) — Absolute path to the project root
Note: Backups are stored centrally at [project-root]/.mcp-backups/ to keep your project organized.
Write Tools (Phase 1: Preview)
All write tools follow a Two-Phase Workflow:
- Call without token: Returns a unified
diffand aconfirmationToken. - Call with token: Set
confirm: trueand provide the token to apply the changes.
7. write_file_surgical
Replace the full source code of a named symbol in a file.
filePath(required) — Path to the filesymbolName(required) — Symbol to replacenewContent(required) — Replacement code (signature + body)confirmationToken(optional) — Token from Phase 1 to apply changesconfirm(optional) — Set totrueto applyclassName(optional) — Scope the symbol to a specific class
8. insert_symbol
Insert new code at a precise location relative to an existing symbol.
filePath(required) — Path to the filecode(required) — Code to insertanchorSymbol(optional) — Symbol to position relative toposition(optional) —"before","after","inside_start","inside_end"className(optional) — Scope the anchor to a specific classconfirmationToken,confirm(optional)
9. rename_symbol
Rename a symbol across the entire repository (definition + all usages).
filePath(required) — File where the symbol is definedoldName(required) — Current namenewName(required) — New namerootDir(optional) — Repository rootconfirmationToken,confirm(optional)
10. remove_symbol
Safely remove a symbol from a file with dependency checking.
filePath(required) — Path to the filesymbolName(required) — Symbol to removeclassName(optional) — Scope the symbol to a specific classforce(optional) — Skip dependency checkconfirmationToken,confirm(optional)
Recommended Workflow
- Understand →
get_semantic_repo_mapto see the architecture - Read →
read_file_surgicalwith symbol name for specific implementations - Assess →
analyze_impactbefore modifying shared files - Edit (Preview) → Call write tools to generate a
diffandconfirmationToken - Confim → Call the same write tool with the token and
confirm: trueto apply - Recovery → Use
rollback_fileif something goes wrong after confirmation
Development
# Build
npm run build
# Run read tests (compression + extraction)
npm run build && node dist/tests/test-dart.js && node dist/tests/test-php.js
# Run write tests (replace, insert, rename, remove)
npm run build && node dist/tests/writers/test-write-smoke.js
# Run all tests
npm run build && node dist/tests/test-dart.js && node dist/tests/test-php.js && node dist/tests/writers/test-write-smoke.js
# Development (build + start)
npm run devTechnical Details
- Transport: stdio (JSON-RPC over stdin/stdout)
- Runtime: Node.js >= 18
- Protocol: Model Context Protocol
- AST Engines: ts-morph (TypeScript/JS), php-parser (PHP), brace-counting (Dart), regex (Python)
- Ignore Engine:
ignorenpm package (full .gitignore spec support) - Safety Features: Mandatory two-phase confirmation, rolling 5-version backups, fuzzy matching, dependency checking, surgical restoration.