代码复制服务器

一个支持安全控制和验证的代码复制粘贴操作的模型上下文协议(MCP)服务器。

  • by mhattingpete
  • Python
  • 本地部署
  • 更新于
在 GitHub 查看 mhattingpete/code-copy-mcp

使用说明

代码复制MCP服务器

一种模型上下文协议(MCP)服务器,允许在具有安全控制和验证的文件之间进行代码复制粘贴操作。

目录

特性

  • 逐行代码复制:从源文件中提取特定的行范围
  • 精确粘贴:在目标文件中的精确行位置插入代码
  • 整个文件操作:复制完整的文件内容
  • 查找并替换:使用备份支持查找和替换文本模式
  • 文件信息:获取有关文件的元数据和统计信息
  • 安全控制:将操作限制在允许的目录中
  • 备份创建:文件修改前的自动备份

工具

复制码

从源文件中复制带有可选行号的代码行。

copy_code(
    source_file: str,
    start_line: int,
    end_line: Optional[int] = None,
    include_line_numbers: bool = False
) -> str

粘贴代码

将代码粘贴到目标文件中的特定行号。

paste_code(
    target_file: str,
    line_number: int,
    code: str,
    create_backup: bool = True
) -> str

copy_entire_file

复制源文件的完整内容。

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

搜索和替换

搜索和替换文件中的文本模式。

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(file_path: str) -> str

安装

先决条件

  • Python 3.11或更高版本
  • UV包管理器

设置

  1. 克隆或创建项目:
cd /Users/map/Documents/Repos/code-copy-mcp
  1. 安装依赖项:
uv install
  1. 配置允许的目录(可选):
cp .env.example .env
# Edit .env to set your ALLOWED_DIRECTORIES

用法

运行服务器

uv run python mcp_server.py

发展模式

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

# Run server
python mcp_server.py

配置

创建一个 .env 项目根目录中的文件:

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

如果未指定,则默认为:

  • 用户的主目录
  • 文档文件夹
  • 桌面文件夹
  • 项目文件夹(如果存在)

安全特性

  • 路径验证可防止目录遍历攻击
  • 文件权限检查确保读/写访问
  • 修改前自动创建备份
  • 仅限于配置的目录进行操作

工作流示例

  1. 从源文件复制代码行:

    copy_code("/path/to/source.py", start_line=10, end_line=20)
    
  2. 将代码粘贴到目标文件中:

    paste_code("/path/to/target.py", line_number=5, code=" copied_code_content ")
    
  3. 获取文件信息:

    get_file_info("/path/to/target.py")
    

安装和配置

先决条件

  • Python 3.11或更高版本
  • UV包管理器(推荐)或pip
  • 支持的MCP客户端之一

快速设置

  1. 克隆存储库:

    git clone https://github.com/mhattingpete/code-copy-mcp.git
    cd code-copy-mcp
    
  2. 安装依赖项:

    uv sync
    # Or without UV: pip install -e .
    
  3. 配置允许的目录(可选):

    cp .env.example .env
    # Edit .env to set your ALLOWED_DIRECTORIES
    

MCP客户端集成

1.克劳德桌面

添加到您的Claude Desktop配置文件中:

macOS: ~/Library/Application Support/Claude/claude_desktop_config.json 窗户: %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.光标(AI代码编辑器)

  1. 打开光标设置(Cmd/Ctrl + ,)
  2. 导航至 Extensions → MCP Servers
  3. 添加新服务器:
    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.克劳德密码

  1. 安装Claude代码
  2. 添加服务器配置:
    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"}}'
    

配置

环境变量

创建一个 .env 项目根目录中的文件:

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

安全配置

出于安全原因,服务器只允许在指定目录内进行操作。 确保包含要执行代码复制粘贴操作的所有目录。

故障排除

常见问题:

  1. “找不到模块”错误:

    # Ensure dependencies are installed
    uv install
    # Or with pip:
    pip install -e .
    
  2. 权限被拒绝错误:

    • 检查允许的目录是否配置正确
    • 确保目录存在并且可访问
  3. MCP服务器未出现:

    • 验证命令路径是否正确
    • 检查客户端日志中的错误消息
    • 配置更改后重新启动MCP客户端

用法示例

配置后,您可以使用MCP客户端中的工具:

示例1:将函数从一个文件复制到另一个文件

User: Copy the function calculate_total from utils.py and paste it into main.py at line 50

助理将:

  1. 使用 get_file_info 检查文件
  2. 使用 copy_code 使用适当的行号
  3. 使用 paste_code 在指定位置

示例2:用备份替换代码

User: Replace all occurrences of "old_method" with "new_method" in all Python files and create backups

助理将:

  1. 使用 search_and_replace 在每个文件上
  2. 验证更改 get_file_info
  3. 报告所做的修改

示例3:复制整个文件

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

助理将:

  1. 使用 copy_entire_file 获取内容
  2. 使用 paste_code 创建新文件

项目结构

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

依赖项

  • fastmcp:MCP服务器框架
  • pydantic:数据验证
  • loguru:结构化日志
  • python dotenv:环境文件支持

日志记录

日志被写入 ~/.code-copy-mcp/mcp_server.log 与:

  • 旋转1MB
  • 7天保留期
  • 用于故障排除的调试级日志记录