Purpose
mcp-text-editor is a Model Context Protocol (MCP) server designed to provide secure, efficient, line-oriented text file operations in a client-server architecture. It implements the MCP protocol, ensuring reliable file editing with robust conflict detection and resolution. The line-oriented design makes it suitable for applications that require synchronized file access, such as collaborative editing tools, automated text processing systems, or any scenario where multiple processes need to safely modify text files. Partial file access capability is especially valuable for LLM-based tools, because loading only the necessary parts of a file helps reduce token consumption.
Key Capabilities
- Line-oriented text file editing and reading
- Intelligent partial file access to minimize token usage in LLM applications
- Read text file contents by line range
- Read multiple ranges of multiple files in a single operation
- Line-based patch application with correct handling of line number offsets
- Conflict detection when editing text file contents
- Flexible character encoding support (utf-8, shift_jis, latin1, etc.)
- Support for multi-file operations
- Correct handling of concurrent edits through hash-based validation
- Memory-efficient handling of large files
- Robust error handling using custom error types
- Atomic multi-file operations
Usage
Requirements
- Python 3.11 or later
- A POSIX-compatible operating system (Linux, macOS, etc.) or Windows
- Sufficient disk space for text file operations
- File system read and write permissions
Installation
- Install Python 3.11+
- Install uv (recommended) or pip
- Create a virtual environment and install dependencies
Install via Smithery: To install Text Editor Server for Claude Desktop automatically via Smithery :
Manual installation:
- Install Python 3.13+
- Start the server:
Tool Description
The server provides multiple tools for text file operations:
get_text_file_contents: Reads the contents of one or more text files by line range.
- Accepts a files array, where each file contains one or more lines using start and endScope. File paths must be absolute paths.
- file_path: absolute path of the text file
- start: first line to read (1-based)
- end: last line to read (inclusive); null means read to the end of the file
- encoding: encoding of all requested files (default: utf-8)
- The top-level object is keyed by absolute file path. file_hash protects the entire file state; each range_hash protects the corresponding range.
patch_text_file_contents: Apply one or more non-overlapping patches to a file. First read the target range, and pass in the current file_hash and each matching range_hash. Important notes:
- Immediately before editing, use get_text_file_contents to obtain the current hashes.
- Use start and end; line_start and line_end are not accepted.
- Patches are applied from bottom to top and must not overlap.
- Line numbers are 1-based.
- For specialized operations such as appending, inserting, or deleting, use append_text_file_contents, insert_text_file_contents, or delete_text_file_contents.
- Read and patch calls use the same encoding.
Common usage pattern
- Call get_text_file_contents to obtain the exact range to replace.
- Read file_hash and the range_hash for that range from the keyed response.
- Call patch_text_file_contents with these hashes and the same range.
- If the result is an error, re-read the file and try again.
Applicable scenarios
- Collaborative editing tools that require synchronized file access
- Automated text processing systems
- Scenarios where multiple processes need to safely modify text files
- LLM-based tools that need to reduce token consumption
Limitations
- Requires Python 3.11 or later
- Requires a POSIX-compatible operating system or Windows
- Requires sufficient disk space and file system read/write permissions
- Patches must not overlap and are applied from bottom to top
- Line numbers are 1-based
- Read and patch calls must use the same encoding
- File paths must be absolutePath
Error Handling
The server handles various error situations:
- Permission errors
- Hash mismatch (concurrent edit detection)
- Invalid patch ranges
- Overlapping patches
- Encoding errors (when a file cannot be decoded with the specified encoding)
- Line numbers out of bounds
Security Considerations
- File path validation: The server validates all file paths to prevent directory traversal attacks
- Access control: Appropriate file system permissions should be set to restrict access to authorized directories
- Hash verification: All file modifications use SHA-256 hash verification to prevent race conditions
- Input sanitization: All user input is properly sanitized and validated
- Error handling: Sensitive information is not exposed in error messages
Troubleshooting
- Permission denied: Check file and directory permissions; ensure the server process has the necessary read and write access
- Hash mismatch and range hash errors: The file was modified by another process; the content to be replaced has changed; run get_text_file_contents to obtain the new hash
- Encoding issues: Verify that the file encoding matches the specified encoding; use utf-8 for new files; check the file for a BOM marker
- Connection issues: Verify that the server is running and accessible; check network configuration and firewall settings
- Performance issues: For large files, consider using smaller line ranges; monitor system resources (memory, disk space); use the appropriate encoding for the file type
Development
- Clone the repository
- Create and activate a Python virtual environment
- Install development dependencies: uv pip install -e ".[dev]"
- Run tests: make all
Code quality tools:
- Ruff for linting
- Black for code formatting
- isort for import sorting
- mypy for type checking
- pytest-cov for test coverage
Tests are located in the tests directory and can be run with pytest. Current test coverage: 90%.
Contribution guidelines:
- Fork the repository
- Create a feature branch
- Make changes
- Run tests and code quality checks
- Submit a pull request
The project uses Python type hints throughout the codebase. Please ensure that any contributions maintain this. All error situations should be handled appropriately and return meaningful error messages. The server should not crash due to invalid input or file operations. New features should include appropriate tests. Try to maintain or improve the current test coverage. All code should be formatted with Black and pass Ruff linting. Import sorting should be handled by isort.