文本编辑器服务器

一个实现Anthropic文本编辑器工具功能的MCP服务器,允许Claude和其他MCP客户端在指定目录内读取和修改文件。

  • by nathanonn
  • TypeScript
  • 本地部署
  • 更新于
在 GitHub 查看 nathanonn/mcp-text-editor-server

使用说明

MCP文本编辑器服务器

实现Anthropic文本编辑器工具功能的模型上下文协议(MCP)服务器。此服务器允许Claude和其他MCP客户端读取和修改指定目录中的文件。

特性

服务器实现了与Anthropic的文本编辑器工具相同的命令,以及增强的编辑命令:

  • view:检查文件内容或列出目录内容

    • 查看整个文件或带有行号的特定行范围
    • 使用文件元数据浏览目录列表
  • str_replace:进行精确的文本替换

    • 用新文本替换特定字符串
    • 可选择限制更换次数
    • 最适合简单、有针对性的替换
  • edit:按行号编辑代码块

    • 用新内容替换整个行范围
    • 对于重大代码修改,比str_replace更安全
    • 显示更改比较之前/之后
  • insert:在特定行号处添加新内容

    • 在指定行后插入文本
    • 添加超出当前文件长度的内容
  • create:创建新文件或覆盖现有文件

    • 创建具有指定内容的文件
    • 可选地覆盖现有文件
  • undo_edit:从备份中还原文件

    • 恢复到上次修改前的状态
    • 每次编辑都会自动创建备份

安装

先决条件

  • Node.js 16或更高版本
  • TypeScript

建立

  1. 创建新项目:

    mkdir text-editor-mcp
    cd text-editor-mcp
    npm init -y
    
  2. 安装依赖项:

    npm install @modelcontextprotocol/sdk zod
    npm install -D @types/node typescript
    
  3. 创建TypeScript配置: 创建一个 tsconfig.json 文件:

    {
      "compilerOptions": {
        "target": "ES2022",
        "module": "Node16",
        "moduleResolution": "Node16",
        "outDir": "./build",
        "esModuleInterop": true,
        "strict": true
      },
      "include": ["*.ts"]
    }
    
  4. 保存源代码: 将文本编辑器服务器代码另存为 text-editor-server.ts 在你的项目中。

  5. 编译:

    npx tsc
    

用法

运行服务器

node build/text-editor-server.js /path/to/working/directory

如果不指定目录,则默认为 ./texteditor-data.

与Claude for Desktop集成

  1. 打开Claude for Desktop配置文件:

    • macOS: ~/Library/Application Support/Claude/claude_desktop_config.json
    • 窗户: %APPDATA%\Claude\claude_desktop_config.json
  2. 将服务器添加到您的配置中:

    {
      "mcpServers": {
        "text-editor": {
          "command": "node",
          "args": ["/absolute/path/to/build/text-editor-server.js", "/absolute/path/to/working/directory"]
        }
      }
    }
    
  3. 重新启动桌面版的Claude。

命令参考

查看命令

查看文件内容或目录列表。

参数:

  • path (必填):相对于基本目录的文件或目录路径
  • view_range (可选):指定要查看的行范围的两个整数数组(例如。, [1, 10])
    • 行号为1-索引
    • 使用 -1 使最后一行读到文件的末尾

Claude中的示例用法: “显示main.py的内容” “显示server.js的第10-20行” “列出项目目录中的文件”

Str_Replace命令

替换文件中的特定文本。最适合简单、有针对性的文本替换。

参数:

  • path (必填):相对于基本目录的文件路径
  • old_str (必填):要替换的文本
  • new_str (必填):新文本
  • count (可选):要替换的出现次数(如果未指定,则全部替换)

Claude中的示例用法: “在utils.js中将'function'替换为'method'” “将error_handler.py中第一次出现的'error'替换为'exception'”

编辑命令

编辑文件中的一系列行。这比 str_replace 用于编辑较大的代码块。

参数:

  • path (必填):相对于基本目录的文件路径
  • start_line (必填):要编辑的第一行编号(从1开始)
  • end_line (必填):要编辑的最后一行(从1开始,或-1表示“到文件末尾”)
  • new_content (必填):替换指定行范围的新内容

Claude中的示例用法: “编辑server.js中的第10-15行以实现正确的错误处理” “将第25行开始的函数替换为此改进版本”

插入命令

在特定行号处插入文本。

参数:

  • path (必填):相对于基本目录的文件路径
  • line_number (必填):要插入的行号(从1开始)
  • text (必填):要插入的文本

Claude中的示例用法: “在main.py的第5行后插入一个docstring” “在app.js的第1行添加一个新的导入语句”

创建命令

使用指定内容创建新文件。

参数:

  • path (必填):相对于基本目录的文件路径
  • content (必填):要写入文件的内容
  • overwrite (可选):如果文件存在,是否覆盖(默认值:false)

Claude中的示例用法: “使用这些设置创建一个名为config.json的新文件” “创建一个打印‘Hello World’的Python脚本”

撤消编辑命令

从备份中还原文件。

参数:

  • path (必填):相对于基本目录的文件路径

Claude中的示例用法: “撤消我上次对README.md的编辑” “将更改还原到server.js”

安全功能

服务器包括几个重要的安全措施:

  1. 路径验证:确保操作保持在指定的基本目录内
  2. 自动备份:在修改文件之前创建备份
  3. 错误处理:为失败的操作提供明确的错误消息
  4. 目录创建:如果基本目录不存在,则自动创建它

对话示例

以下是一些与克劳德尝试的对话示例:

  • “你能创建一个新的Python脚本来计算斐波那契数列吗?”
  • “我有一个JavaScript文件有一些语法错误。你能检查一下并修复它们吗?”
  • “请为我的Python文件中的所有函数添加适当的文档字符串。”
  • “我需要重构此代码以使用async/await。你能帮忙吗?”
  • “使用以下设置创建配置文件…”

Claude使用规则

这些规则帮助Claude确定何时以及如何使用文本编辑器工具:

For ANY request involving viewing, editing, creating, or modifying files:

1. When you need to view file content:
   + Use the "view" command with the file path
   + For large files, use view_range parameter to see specific sections

2. When you need to edit existing files:
   + First use "view" to see the current content
   + For simple text replacements, use "str_replace" with old_str and new_str
   + For editing blocks of code or multiple lines, use "edit" with start_line, end_line, and new_content
   + For adding new content at a specific location, use "insert" with line_number and text

3. When you need to create new files:
   + Use "create" with path and content parameters
   + If replacing an existing file, set overwrite to true

4. If a user is unhappy with changes:
   + Use "undo_edit" to revert the most recent change to a file

Always confirm you've made the requested changes and summarize what you've done. When possible, show the relevant portions of the file before and after changes.

您可以在Claude提示中包含这些规则,以帮助指导模型何时以及如何使用文本编辑器工具。

Claude使用规则

这些规则帮助Claude确定何时以及如何使用文本编辑器工具:

For ANY request involving viewing, editing, creating, or modifying files:

1. When you need to view file content:
   + Use the "view" command with the file path
   + For large files, use view_range parameter to see specific sections

2. When you need to edit existing files:
   + First use "view" to see the current content
   + For simple replacements, use "str_replace" with old_str and new_str
   + For adding new content at a specific location, use "insert" with line_number and text

3. When you need to create new files:
   + Use "create" with path and content parameters
   + If replacing an existing file, set overwrite to true

4. If a user is unhappy with changes:
   + Use "undo_edit" to revert the most recent change to a file

Always confirm you've made the requested changes and summarize what you've done. When possible, show the relevant portions of the file before and after changes.

您可以在Claude提示中包含这些规则,以帮助指导模型何时以及如何使用文本编辑器工具。

故障排除

如果您遇到问题:

  1. 检查服务器日志
  2. 验证路径是否正确
  3. 确保工作目录可写
  4. 检查Claude for Desktop配置是否正确
  5. 配置更改后重新启动Claude for Desktop

许可证

麻省理工学院

贡献

欢迎投稿!请随时提交拉取请求。