Edit File Lines MCP Server

Edit File Lines MCP Server

用于对文件进行基于行的编辑的MCP Server。

概述

这是一个基于 TypeScript 的 MCP 服务器,提供精确的文本文件行编辑工具,支持字符串和正则表达式匹配。

基本使用流程

1. 编辑文件行 (edit_file_lines)

此工具允许您:

  • 替换整行内容
  • 替换特定文本片段
  • 使用正则表达式进行复杂匹配
  • 在应用更改前进行预览

基本示例:

{
  "p": "src/components/App.tsx",
  "e": [{
    "startLine": 2,
    "endLine": 2,
    "content": "primary",
    "strMatch": "blue"
  }],
  "dryRun": true
}

响应将显示变更预览并提供状态ID。

2. 应用编辑 (approve_edit)

审核预览后,使用获得的状态ID应用更改:

{
  "stateId": "fcbf740a"
}

高级用例

多行替换

{
  "p": "src/components/App.tsx",
  "e": [{
    "startLine": 16,
    "endLine": 19,
    "content": "    <div className={cardClass}>\n      <h2 className=\"title\">{title}</h2>\n      <p className=\"subtitle\">{subtitle}</p>\n    </div>",
    "regexMatch": "<div[^>]*>[\\s\\S]*?</div>"
  }],
  "dryRun": true
}

复杂的结构修改

{
  "p": "src/components/App.tsx",
  "e": [{
    "startLine": 7,
    "endLine": 12,
    "content": "export const Card = ({\n  title,\n  subtitle = \"New default\",\n  theme = \"modern\",\n  size = \"responsive\"\n}) => {",
    "regexMatch": "export const Card[\\s\\S]*?\\) => \\{"
  }],
  "dryRun": true
}

编辑参数说明

  • p: 文件路径
  • e: 编辑操作数组
    • startLine/endLine: 要编辑的行范围
    • content: 新内容
    • strMatch/regexMatch: 匹配方式
  • dryRun: 设为true时预览更改而不应用

最佳实践

  1. 总是先使用dryRun: true预览更改
  2. 确认更改无误后,再使用approve_edit应用
  3. 精确指定行范围以避免意外修改
  4. 对复杂编辑使用正则表达式匹配