mcpField
M
MCP Text Editor Server

MCP Text Editor Server

本地服务
github.com/nathanonn开发工具077 周前更新

关于 MCP Text Editor Server

MCP Text Editor Server 为 Claude 等 MCP 客户端提供指定目录内的文件读取与编辑工具,支持按行修改,并可通过自动备份恢复更改。

主要功能

  1. 01查看文件和目录列表
  2. 02替换指定文本
  3. 03按行编辑并查看对比
  4. 04插入文本和创建文件
  5. 05恢复自动备份

适用场景

  1. 01检查项目文件和目录
  2. 02修改指定文本或代码
  3. 03创建文件或插入内容
  4. 04撤销最近一次文件修改

MCP 相关

该 MCP 服务向 MCP 客户端提供文件查看和修改工具。路径校验可确保操作限于配置的基础目录内。

配置

本地服务的配置在各客户端中通用,复制后粘贴到客户端的 MCP 配置文件即可。

mcp.json · 通用配置
{
"mcpServers": {
"text-editor-server": {
"command": "npx",
"args": [
"-y",
"tsc"
]
}
}
}
启动命令npx -y tsc

本地服务需要在你的电脑上安装运行环境(如 Node.js 18+)。各客户端的配置文件位置不同,请参考客户端文档。

安装包
npmtsc

工具

还没有获取到工具列表

本地服务需要在你的电脑上运行后才能读取工具,维护者暂未提供工具列表。

详细介绍

MCP Text Editor Server

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

功能

此服务实现了与 Anthropic 文本编辑器工具相同的命令,并提供增强版的 edit 命令:

  • 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
    • Windows:%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 for Desktop。

命令参考

View 命令

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

参数:

  • path(必需):相对于基础目录的文件或目录路径
  • view_range(可选):由两个整数构成的数组,用于指定要查看的行范围(例如,[1, 10])
    • 行号从 1 开始
    • 将结束行设为 -1,即可读取到文件末尾

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

Str_Replace 命令

替换文件中的指定文本。最适合简单、精准的文本替换。

参数:

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

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

Edit 命令

编辑文件中的一段行范围。编辑较大的代码块时,这比 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 行后插入一段文档字符串” “在 app.js 第 1 行添加一条新的导入语句”

创建命令

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

参数:

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

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

撤销编辑命令

从备份中还原文件。

参数:

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

在 Claude 中的使用示例: “撤销我对 README.md 的上次编辑” “还原对 server.js 所做的更改”

安全功能

该服务包含多项重要的安全措施:

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

对话示例

以下是一些可以尝试与 Claude 进行的对话示例:

  • “你能创建一个计算斐波那契数列的新 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

许可证

MIT

参与贡献

欢迎贡献!请随时提交 Pull Request。

评论

支持 Markdown

第一次接触 MCP?

从协议是什么,到在客户端里装上第一个服务,这几条回答覆盖了大多数入门问题。

一个开放协议,规定了 AI 应用如何以统一方式连接外部工具与数据源。客户端负责与模型对话,MCP 服务负责提供工具、资源和提示模板,两者通过标准消息通信,同一个服务可以被不同客户端复用。