Semantic Edit MCP
[!CAUTION] This repository is written by AI as an experiment. Although I (jbr) review every line, the code quality is not necessarily identical to what I would have written. Caveat usor.
[!IMPORTANT] FOR AI ASSISTANTS: You cannot test changes to this MCP server without asking the user to restart Claude Desktop first. The MCP server must be recompiled and reloaded to test any code changes. Always pause and ask the user to restart Claude Desktop before attempting to test modifications.
A Model Context Protocol (MCP) server for semantic code editing using tree-sitter. This server provides safe, AST-aware code editing operations that preserve syntax structure and formatting.
✨ Features
- 🔍 Semantic node targeting: Find nodes by name, type, tree-sitter query, or position
- 🛡️ Safe structural editing: Replace, insert, wrap, or delete AST nodes while maintaining syntax
- ✅ Syntax validation: Validate code before and after edits to prevent breaking changes
- 👁️ Preview mode: Test operations safely with
preview_only: true
- see changes without applying them - 🎯 Specialized insertion tools: Smart, safe insertion at structural boundaries
- 💡 Enhanced error messages: Intelligent suggestions and fuzzy matching for targeting mistakes
- 🦀 Rust support: Currently supports Rust with extensible architecture for more languages
- ⚡ Transaction safety: All edits are validated before being applied to files
Installation
This project only builds on nightly rust because we use let chains
Usage
As an MCP Server
Start the server:
The server communicates via JSON-RPC over stdin/stdout and provides the following tools:
Available Tools
Note: All tools currently support Rust files only (.rs files). Other languages will be added in future releases.
All editing tools support a preview_only
parameter for safe testing:
Core Editing Tools
replace_node
Replace an entire AST node with new content.
insert_before_node
/ insert_after_node
Insert content before or after a specified node.
wrap_node
Wrap an existing node with new syntax.
✨ Specialized Insertion Tools (New!)
These tools provide safer, more semantic insertion at structural boundaries:
insert_after_struct
Insert content after a struct definition.
insert_after_enum
Insert content after an enum definition.
insert_after_impl
Insert content after an impl block.
insert_after_function
Insert content after a function definition.
insert_in_module
Smart module-level insertion with positioning control.
Utility Tools
validate_syntax
Validate code syntax.
Or validate content directly:
get_node_info
Get information about a node at a specific location.
👁️ Preview Mode
Safe Testing: All editing operations support a preview_only
parameter:
preview_only: true
: Shows what would happen without modifying files, output prefixed with "PREVIEW:"preview_only: false
(default): Actually applies the changes to files
This is perfect for:
- Testing complex operations safely
- Exploring AST structure and targeting
- AI agents "thinking through" edits before applying them
💡 Enhanced Error Messages
Get intelligent error messages with suggestions when targeting fails:
Before:
Error: Target node not found
Now:
Function 'mian' not found.
Available options: function: main, function: add, function: multiply
Did you mean: main
Features:
- Fuzzy matching: Suggests corrections for typos ("mian" → "main", "Pointt" → "Point")
- Available options: Lists all available functions, structs, enums, etc.
- Context-aware: Different suggestions based on what you're looking for
🎯 Node Selectors
Node selectors allow you to target specific AST nodes using different strategies:
By Position
By Name and Type (Recommended)
By Type Only
By Tree-sitter Query (Advanced)
🏗️ Architecture
The project is organized into focused modules:
server/
: MCP protocol handling and server implementationtools/
: Tool registry and core implementationsspecialized_tools/
: Specialized insertion toolsparsers/
: Tree-sitter integration and language-specific parsingeditors/
: Language-specific editing logic (currently Rust)operations/
: Core edit operations and node selectionvalidation/
: Syntax validation and error reportinghandlers/
: Request handling logicschemas/
: JSON schemas for tool parameters
🛡️ Safety Features
- 👁️ Preview Mode: Test operations with
preview_only: true
before applying - ✅ Syntax Validation: All edits are validated before being applied
- 🎯 AST-Aware Positioning: Edits respect semantic boundaries
- ⚡ Atomic Operations: File changes are applied atomically
- 📐 Format Preservation: Maintains indentation and structure context
- 💡 Smart Error Messages: Intelligent suggestions help avoid mistakes
- 🔒 Specialized Tools: Safe insertion at structural boundaries
🚀 Recent Improvements
Phase 1 Features (Completed)
- ✅ Preview Mode: Safe testing for all operations
- ✅ Enhanced Error Messages: Fuzzy matching and intelligent suggestions
- ✅ Specialized Insertion Tools: 5 new tools for safer editing
- ✅ Architecture Refactoring: Modular, maintainable codebase
- ✅ Extended Rust Support: Comprehensive enum, impl, and module support
🔮 Extending to New Languages
To add support for a new language:
- Add the tree-sitter grammar dependency to
Cargo.toml
- Create a new parser module in
src/parsers/
- Create a new editor module in
src/editors/
- Update the language detection and dispatch logic
📚 Examples
Preview a function replacement (safe testing)
Add a trait implementation after a struct
Add tests at module level
License
MIT OR Apache-2.0