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.
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 - 🦀 Rust support: Currently supports Rust with extensible architecture for more languages
- ⚡ Transaction safety: All edits are validated before being applied to files
Installation
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:
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.
validate_syntax
Validate code syntax.
Or validate content directly:
get_node_info
Get information about a node at a specific location.
Preview Mode
New in this release! All editing operations support a preview_only
parameter for safe exploration:
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
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 several modules:
parsers/
: Tree-sitter integration and language-specific parsingeditors/
: Language-specific editing logic (currently Rust)operations/
: Core edit operations and node selectionvalidation/
: Syntax validation and error reportingschemas/
: 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
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 documentation to a struct (actually apply changes)
Test wrapping code in a conditional
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