code-copy-mcp
Click on "Deploy Server".
Wait a few minutes for the server to deploy. Once ready, it will show a "Started" state.
In the chat, type
@followed by the MCP server name and your instructions, e.g., "@code-copy-mcp copy lines 10-20 from source.py to target.py at line 5"
That's it! The server will respond to your query, and you can continue using it as needed.
Here is a step-by-step guide with screenshots.
Code Copy MCP Server
A Model Context Protocol (MCP) server that enables code copy-paste operations between files with security controls and validation.
Table of Contents
Related MCP server: Snippet Saver
Features
Line-by-line code copying: Extract specific line ranges from source files
Precise pasting: Insert code at exact line positions in target files
Entire file operations: Copy complete file contents
Search and replace: Find and replace text patterns with backup support
File information: Get metadata and statistics about files
Security controls: Restrict operations to allowed directories
Backup creation: Automatic backups before file modifications
Tools
copy_code
Copy code lines from a source file with optional line numbers.
copy_code(
source_file: str,
start_line: int,
end_line: Optional[int] = None,
include_line_numbers: bool = False
) -> strpaste_code
Paste code at a specific line number in a target file.
paste_code(
target_file: str,
line_number: int,
code: str,
create_backup: bool = True
) -> strcopy_entire_file
Copy the complete content of a source file.
copy_entire_file(
source_file: str,
include_line_numbers: bool = False
) -> strsearch_and_replace
Search and replace text patterns in files.
search_and_replace(
target_file: str,
search_pattern: str,
replacement: str,
case_sensitive: bool = True,
replace_all: bool = True,
create_backup: bool = True
) -> strget_file_info
Get detailed information about a file.
get_file_info(file_path: str) -> strInstallation
Prerequisites
Python 3.11 or higher
UV package manager
Setup
Clone or create the project:
cd /Users/map/Documents/Repos/code-copy-mcpInstall dependencies:
uv installConfigure allowed directories (optional):
cp .env.example .env
# Edit .env to set your ALLOWED_DIRECTORIESUsage
Running the Server
uv run python mcp_server.pyDevelopment Mode
# Activate virtual environment first
source .venv/bin/activate # On Windows: .venv\Scripts\activate
# Run server
python mcp_server.pyConfiguration
Create a .env file in the project root:
# Comma-separated list of allowed directories
ALLOWED_DIRECTORIES=/Users/yourname,/Users/yourname/Documents,/Users/yourname/ProjectsIf not specified, defaults to:
User's home directory
Documents folder
Desktop folder
Projects folder (if exists)
Security Features
Path validation prevents directory traversal attacks
File permission checks ensure read/write access
Automatic backup creation before modifications
Restricted operation to configured directories only
Example Workflow
Copy code lines from a source file:
copy_code("/path/to/source.py", start_line=10, end_line=20)Paste the code into target file:
paste_code("/path/to/target.py", line_number=5, code=" copied_code_content ")Get file information:
get_file_info("/path/to/target.py")
Installation & Configuration
Prerequisites
Python 3.11 or higher
UV package manager (recommended) or pip
One of the supported MCP clients
Quick Setup
Clone the repository:
git clone https://github.com/mhattingpete/code-copy-mcp.git cd code-copy-mcpInstall dependencies:
uv sync # Or without UV: pip install -e .Configure allowed directories (optional):
cp .env.example .env # Edit .env to set your ALLOWED_DIRECTORIES
MCP Client Integration
1. Claude Desktop
Add to your Claude Desktop configuration file:
macOS: ~/Library/Application Support/Claude/claude_desktop_config.json
Windows: %APPDATA%\Claude\claude_desktop_config.json
{
"mcpServers": {
"code-copy": {
"command": "/full/path/to/uv",
"args": ["--directory", "/full/path/to/code-copy-mcp", "run", "python", "mcp_server.py"],
"env": {
"ALLOWED_DIRECTORIES": "/Users/yourname,/Users/yourname/Documents,/Users/yourname/Projects"
}
}
}
}2. Cursor (AI Code Editor)
Open Cursor settings (
Cmd/Ctrl + ,)Navigate to
Extensions→MCP ServersAdd new server:
Name: Code Copy MCP Command: full/path/to/uv Arguments: --directory /full/path/to/code-copy-mcp run python mcp_server.py Environment Variables: ALLOWED_DIRECTORIES=/Users/yourname,/Users/yourname/Documents,/Users/yourname/Projects
3. Claude Code
Install the Claude Code
Add server configuration:
claude mcp add-json code-copy '{"type":"stdio","command":"full/path/to/uv","args":["--directory", "/full/path/to/code-copy-mcp", "run", "python", "mcp_server.py"],"env": {"ALLOWED_DIRECTORIES": "/Users/yourname,/Users/yourname/Documents,/Users/yourname/Projects"}}'
Configuration
Environment Variables
Create a .env file in the project root:
# Comma-separated list of directories where file operations are allowed
# Examples for different operating systems:
# macOS/Linux:
ALLOWED_DIRECTORIES=/Users/yourname,/Users/yourname/Documents,/Users/yourname/Projects
# Windows:
ALLOWED_DIRECTORIES=C:\Users\YourName,C:\Users\YourName\Documents,C:\Users\YourName\Projects
# If not specified, defaults to:
# - User's home directory
# - Documents folder
# - Desktop folder
# - Projects folder (if exists)Security Configuration
The server only allows operations within the specified directories for security reasons. Make sure to include all directories where you want to perform code copy-paste operations.
Troubleshooting
Common Issues:
"Module not found" errors:
# Ensure dependencies are installed uv install # Or with pip: pip install -e .Permission denied errors:
Check that the allowed directories are correctly configured
Ensure the directories exist and are accessible
MCP Server not appearing:
Verify the command path is correct
Check the client's logs for error messages
Restart the MCP client after configuration changes
Usage Examples
Once configured, you can use the tools within your MCP client:
Example 1: Copy function from one file to another
User: Copy the function calculate_total from utils.py and paste it into main.py at line 50The assistant will:
Use
get_file_infoto examine the filesUse
copy_codewith appropriate line numbersUse
paste_codeat the specified location
Example 2: Replace code with backup
User: Replace all occurrences of "old_method" with "new_method" in all Python files and create backupsThe assistant will:
Use
search_and_replaceon each fileVerify changes with
get_file_infoReport the modifications made
Example 3: Copy entire file
User: Make a copy of template.py as new_template.pyThe assistant will:
Use
copy_entire_fileto get the contentUse
paste_codeto create the new file
Project Structure
code-copy-mcp/
├── mcp_server.py # Main MCP server
├── tools/
│ ├── __init__.py # Package init
│ ├── validation.py # Security validation
│ └── copy_tools.py # Copy-paste tools
├── .env.example # Configuration template
├── pyproject.toml # UV project configuration
└── README.md # This fileDependencies
fastmcp: MCP server framework
pydantic: Data validation
loguru: Structured logging
python-dotenv: Environment file support
Logging
Logs are written to ~/.code-copy-mcp/mcp_server.log with:
Rotation at 1MB
7-day retention
DEBUG level logging for troubleshooting
Available Tools
5 toolscopy_codeA
Copy code lines from source file.
Args: source_file: Path to source file start_line: Starting line number (1-based) end_line: Ending line number (1-based), defaults to start_line include_line_numbers: Whether to include line numbers in output
Returns: Copied code content as string
Inputs: start_line (required), source_file (required), end_line, include_line_numbers. Returns: result
copy_entire_fileA
Copy entire content of source file.
Args: source_file: Path to source file include_line_numbers: Whether to include line numbers
Returns: Full file content as string
Inputs: source_file (required), include_line_numbers. Returns: result
get_file_infoC
Get basic information about a file.
Args: file_path: Path to file
Returns: File information as string
Inputs: file_path (required). Returns: result
paste_codeA
Paste code at specific line in target file.
Args: target_file: Path to target file line_number: Line number to paste at (1-based, inserts before this line) code: Code content to paste create_backup: Whether to create backup file before modification
Returns: Success message with details
Inputs: code (required), line_number (required), target_file (required), create_backup. Returns: result
search_and_replaceA
Search and replace text in target file.
Args: target_file: Path to target file search_pattern: Text to search for replacement: Replacement text case_sensitive: Whether search is case sensitive replace_all: Whether to replace all occurrences or just first create_backup: Whether to create backup file
Returns: Success message with replacement count
Inputs: replacement (required), target_file (required), search_pattern (required), replace_all, +2 more. Returns: result
Tool Schema Changelog
Recent tool additions, removals, and schema changes observed during successful MCP inspections.
Loading tool schema changelog…
TDQS
Scored across 5 tools
Each tool targets a distinct operation: full file read, partial read, metadata read, text replacement, and line insertion. There is no ambiguity because the descriptions clearly differentiate between copying content and modifying files.
All tool names follow a consistent verb_noun snake_case pattern, such as copy_entire_file, get_file_info, and paste_code. Even search_and_replace uses a verb-first structure, maintaining predictability.
With 5 tools, the server is well-scoped for its purpose of copying and manipulating code. The count is neither too sparse nor excessive, and each tool serves a clear role in the workflow.
The tools cover the core operations for code copying: reading an entire file, reading a range, replacing text, and inserting code. A minor gap is the lack of a create_file operation, but agents can work around this by using existing files.
Maintenance
Related MCP Connectors
One MCP server for all your MCP servers: code mode, per-tool permissions, approvals and audit.
Related MCP Servers
- AlicenseNot gradedqualityDmaintenanceMCP server for SSH remote execution, file transfer, and file editing with automatic backup/trash and ~/.ssh/config integration.117 npm1MIT
- AlicenseAqualityDmaintenanceAn MCP server that enables AI coding tools to save, list, and read code snippets as files in the local filesystem.3MIT
- AlicenseNot gradedqualityDmaintenanceAn MCP server that provides secure access to local file system operations.MIT
- AlicenseNot gradedqualityDmaintenanceAn MCP server that provides on-demand safety for AI coding workflows, enabling inspection, review, checkpointing, and rollback of risky actions.37 npm1MIT