Provides AST-aware code editing operations for Rust files, including syntax validation, node targeting, and structural editing while maintaining syntax integrity.
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 prevent file corruption through comprehensive validation.
✨ Features
- 🌍 Multi-language support: Rust (full), JSON (full), more languages easily added
- 🛡️ Two-layer validation: Context validation + syntax validation prevents file corruption
- 🔍 Semantic node targeting: Find nodes by name, type, tree-sitter query, or position
- 👁️ Preview mode: Test operations safely with
preview_only: true
- see changes without applying them - 🎯 Specialized insertion tools: Smart, safe insertion at structural boundaries (Rust)
- 💡 Enhanced error messages: Intelligent suggestions and fuzzy matching for targeting mistakes
- ⚡ Transaction safety: All edits are validated before being applied to files
- 🏗️ Extensible architecture: Easy to add support for new programming languages
Language Support Status
- 🟢 Rust - Full support (parsing, editing, context validation, syntax validation)
- 🟢 JSON - Full support (parsing, editing, syntax validation)
- 🟡 Other languages - Syntax validation only (easy to extend, not yet implemented)
Installation
This project requires 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 (16 Total)
Core Multi-Language Editing Tools (4 tools)
All editing tools support full validation and work across supported languages:
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.
Analysis & Validation Tools (2 tools)
validate_syntax
Multi-language syntax validation.
Or validate content directly:
get_node_info
Multi-language node inspection.
Rust-Specific Safe Insertion Tools (5 tools)
These tools provide safer, more semantic insertion at structural boundaries for Rust files:
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.
🛡️ Comprehensive Validation System
Two-Layer Validation
- Context Validation (language-specific semantic rules)
- Prevents functions inside struct fields
- Prevents types inside function bodies
- Available for Rust, more languages planned
- Syntax Validation (all languages)
- Tree-sitter parsing validation
- Prevents syntax errors before writing files
- Works with any tree-sitter supported language
Validation Output Examples
👁️ 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
Perfect for testing complex operations safely before applying them.
💡 Enhanced Error Messages
Get intelligent error messages with suggestions when targeting fails:
Before:
Now:
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
Multiple ways to target nodes for editing:
By Position
By Name and Type (Recommended)
By Type Only
By Tree-sitter Query (Advanced)
🏗️ Architecture
Multi-language semantic editing with pluggable language support:
languages/
: Language-specific support (Rust, JSON, extensible)validation/
: Context validation and syntax validationtools/
: Core editing tools with full validationparsers/
: Multi-language tree-sitter integrationoperations/
: Core edit operations and node selection
🔮 Adding New Languages
Our architecture makes it easy to add new programming languages:
- Basic support (syntax validation only): ~2 hours
- Full support (with context validation): ~1 day
- See docs/adding-languages.md for complete guide
📚 Examples
Multi-Language Editing
Rust Function Replacement
JSON Property Addition
Safe Rust-Specific Operations
Add trait implementation after struct
Add tests at module level
🚀 Recent Achievements (December 2024)
✅ Multi-Language Architecture Complete
- Language-aware validation system
- JSON editing support
- Extensible language registry
- Syntax validation safety net for all languages
✅ Comprehensive Validation System
- Two-layer validation prevents file corruption
- Context validation for supported languages
- Syntax validation for all languages
- Zero file corruption incidents since implementation
✅ Enhanced Developer Experience
- Preview mode for safe testing
- Intelligent error messages with fuzzy matching
- Specialized tools for common Rust patterns
- Consistent validation across all tools
🔮 Future Enhancements
Next Language Targets
- Markdown - Documentation editing (in progress)
- Python - High demand, good tree-sitter support
- TypeScript - JavaScript ecosystem support
- YAML - Configuration files
Advanced Features
- Cross-language operations
- Project-aware validation
- Batch editing with transactions
- IDE integration (VS Code extension)
License
MIT OR Apache-2.0
This server cannot be installed
hybrid server
The server is able to function both locally and remotely, depending on the configuration or use case.
semantic-edit-mcp
Related MCP Servers
- PythonMIT License
- PythonMIT License
- GoMIT License
- PythonMIT License