code-copy-mcp

code-copy-mcp

MCP server for copying and pasting code between files with security controls and validation.

Category
访问服务器

README

Code Copy MCP Server

Python MCP

A Model Context Protocol (MCP) server that enables code copy-paste operations between files with security controls and validation.

Table of Contents

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
) -> str

paste_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
) -> str

copy_entire_file

Copy the complete content of a source file.

copy_entire_file(
    source_file: str,
    include_line_numbers: bool = False
) -> str

search_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
) -> str

get_file_info

Get detailed information about a file.

get_file_info(file_path: str) -> str

Installation

Prerequisites

  • Python 3.11 or higher
  • UV package manager

Setup

  1. Clone or create the project:
cd /Users/map/Documents/Repos/code-copy-mcp
  1. Install dependencies:
uv install
  1. Configure allowed directories (optional):
cp .env.example .env
# Edit .env to set your ALLOWED_DIRECTORIES

Usage

Running the Server

uv run python mcp_server.py

Development Mode

# Activate virtual environment first
source .venv/bin/activate  # On Windows: .venv\Scripts\activate

# Run server
python mcp_server.py

Configuration

Create a .env file in the project root:

# Comma-separated list of allowed directories
ALLOWED_DIRECTORIES=/Users/yourname,/Users/yourname/Documents,/Users/yourname/Projects

If 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

  1. Copy code lines from a source file:

    copy_code("/path/to/source.py", start_line=10, end_line=20)
    
  2. Paste the code into target file:

    paste_code("/path/to/target.py", line_number=5, code=" copied_code_content ")
    
  3. 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

  1. Clone the repository:

    git clone https://github.com/mhattingpete/code-copy-mcp.git
    cd code-copy-mcp
    
  2. Install dependencies:

    uv sync
    # Or without UV: pip install -e .
    
  3. 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)

  1. Open Cursor settings (Cmd/Ctrl + ,)
  2. Navigate to ExtensionsMCP Servers
  3. Add 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

  1. Install the Claude Code
  2. 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:

  1. "Module not found" errors:

    # Ensure dependencies are installed
    uv install
    # Or with pip:
    pip install -e .
    
  2. Permission denied errors:

    • Check that the allowed directories are correctly configured
    • Ensure the directories exist and are accessible
  3. 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 50

The assistant will:

  1. Use get_file_info to examine the files
  2. Use copy_code with appropriate line numbers
  3. Use paste_code at 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 backups

The assistant will:

  1. Use search_and_replace on each file
  2. Verify changes with get_file_info
  3. Report the modifications made

Example 3: Copy entire file

User: Make a copy of template.py as new_template.py

The assistant will:

  1. Use copy_entire_file to get the content
  2. Use paste_code to 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 file

Dependencies

  • 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

推荐服务器

Baidu Map

Baidu Map

百度地图核心API现已全面兼容MCP协议,是国内首家兼容MCP协议的地图服务商。

官方
精选
JavaScript
Playwright MCP Server

Playwright MCP Server

一个模型上下文协议服务器,它使大型语言模型能够通过结构化的可访问性快照与网页进行交互,而无需视觉模型或屏幕截图。

官方
精选
TypeScript
Magic Component Platform (MCP)

Magic Component Platform (MCP)

一个由人工智能驱动的工具,可以从自然语言描述生成现代化的用户界面组件,并与流行的集成开发环境(IDE)集成,从而简化用户界面开发流程。

官方
精选
本地
TypeScript
Audiense Insights MCP Server

Audiense Insights MCP Server

通过模型上下文协议启用与 Audiense Insights 账户的交互,从而促进营销洞察和受众数据的提取和分析,包括人口统计信息、行为和影响者互动。

官方
精选
本地
TypeScript
VeyraX

VeyraX

一个单一的 MCP 工具,连接你所有喜爱的工具:Gmail、日历以及其他 40 多个工具。

官方
精选
本地
graphlit-mcp-server

graphlit-mcp-server

模型上下文协议 (MCP) 服务器实现了 MCP 客户端与 Graphlit 服务之间的集成。 除了网络爬取之外,还可以将任何内容(从 Slack 到 Gmail 再到播客订阅源)导入到 Graphlit 项目中,然后从 MCP 客户端检索相关内容。

官方
精选
TypeScript
Kagi MCP Server

Kagi MCP Server

一个 MCP 服务器,集成了 Kagi 搜索功能和 Claude AI,使 Claude 能够在回答需要最新信息的问题时执行实时网络搜索。

官方
精选
Python
e2b-mcp-server

e2b-mcp-server

使用 MCP 通过 e2b 运行代码。

官方
精选
Neon MCP Server

Neon MCP Server

用于与 Neon 管理 API 和数据库交互的 MCP 服务器

官方
精选
Exa MCP Server

Exa MCP Server

模型上下文协议(MCP)服务器允许像 Claude 这样的 AI 助手使用 Exa AI 搜索 API 进行网络搜索。这种设置允许 AI 模型以安全和受控的方式获取实时的网络信息。

官方
精选