Skip to main content
Glama
oakenai

Edit File Lines MCP Server

by oakenai

ファイル行の編集 MCP サーバー

許可されたディレクトリ内のテキスト ファイルに対して正確な行ベースの編集を行うためのツールを提供する、TypeScript ベースの MCP サーバー。

特徴

メイン編集ツール

edit_file_lines

文字列または正規表現のパターンマッチングを使用して、ファイルの行単位で編集を行います。各編集では以下の操作が可能です。

  • 行全体を置換する

  • 行の書式を維持しながら特定のテキストの一致を置換する

  • 複雑な一致には正規表現パターンを使用する

  • 複数行と複数の編集を処理する

  • ドライランモードで変更をプレビューする

サンプルファイル ( src/components/App.tsx ):

// Basic component with props
const Button = ({ color = "blue", size = "md" }) => {
  return <button className={`btn-${color} size-${size}`}>Click me</button>;
};

// Component with multiple props and nested structure
export const Card = ({
  title,
  subtitle = "Default subtitle",
  theme = "light",
  size = "lg",
}) => {
  const cardClass = `card-${theme} size-${size}`;
  
  return (
    <div className={cardClass}>
      <h2>{title}</h2>
      <p>{subtitle}</p>
    </div>
  );
};

// Constants and configurations
const THEME = {
  light: { bg: "#ffffff", text: "#000000" },
  dark: { bg: "#000000", text: "#ffffff" },
};

const CONFIG = {
  apiUrl: "https://api.example.com",
  timeout: 5000,
  retries: 3,
};

使用例

  1. 単純な文字列の置換

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

出力:

Index: src/components/App.tsx
===================================================================
--- src/components/App.tsx        original
+++ src/components/App.tsx        modified
@@ -1,6 +1,6 @@
 // Basic component with props
-const Button = ({ color = "blue", size = "md" }) => {
+const Button = ({ color = "primary", size = "md" }) => {
   return Click me;
 };
 
 // Component with multiple props and nested structure

状態 ID: fcbf740a 変更を適用するには、approve_edit でこの ID を使用します。

  1. 構造を保持した複数行のコンテンツ

{
  "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
}

出力:

Index: src/components/App.tsx
===================================================================
--- src/components/App.tsx        original
+++ src/components/App.tsx        modified
@@ -13,10 +13,10 @@
   const cardClass = `card-${theme} size-${size}`;
   
   return (
     <div className={cardClass}>
-      <h2>{title}</h2>
-      <p>{subtitle}</p>
+      <h2 className="title">{title}</h2>
+      <p className="subtitle">{subtitle}</p>
     </div>
   );
 };

状態 ID: f2ce973f 変更を適用するには、approve_edit でこの ID を使用します。

  1. 複雑なJSX構造の変更

{
  "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
}

出力:

Index: src/components/App.tsx
===================================================================
--- src/components/App.tsx        original
+++ src/components/App.tsx        modified
@@ -5,11 +5,11 @@
 // Component with multiple props and nested structure
 export const Card = ({
   title,
-  subtitle = "Default subtitle",
-  theme = "light",
-  size = "lg",
+  subtitle = "New default",
+  theme = "modern",
+  size = "responsive"
 }) => {
   const cardClass = `card-${theme} size-${size}`;
   
   return (

状態 ID: f1f1d27b 変更を適用するには、approve_edit でこの ID を使用します。

  1. 空白スペースの保持による構成の更新

{
  "p": "src/components/App.tsx",
  "e": [{
    "startLine": 29,
    "endLine": 32,
    "content": "const CONFIG = {\n  baseUrl: \"https://api.newexample.com\",\n  timeout: 10000,\n  maxRetries: 5",
    "regexMatch": "const CONFIG[\\s\\S]*?retries: \\d+"
  }],
  "dryRun": true
}

出力:

Index: src/components/App.tsx
===================================================================
--- src/components/App.tsx        original
+++ src/components/App.tsx        modified
@@ -26,8 +26,8 @@
   dark: { bg: "#000000", text: "#ffffff" },
 };
 
 const CONFIG = {
-  apiUrl: "https://api.example.com",
-  timeout: 5000,
-  retries: 3,
+  baseUrl: "https://api.newexample.com",
+  timeout: 10000,
+  maxRetries: 5
 };

状態 ID: 20e93c34 変更を適用するには、approve_edit でこの ID を使用します。

  1. 柔軟な空白マッチング

{
  "p": "src/components/App.tsx",
  "e": [{
    "startLine": 9,
    "endLine": 9,
    "content": "description",
    "strMatch": "subtitle   =   \"Default subtitle\""  // Extra spaces are handled
  }],
  "dryRun": true
}

出力:

Index: src/components/App.tsx
===================================================================
--- src/components/App.tsx        original
+++ src/components/App.tsx        modified
@@ -5,9 +5,9 @@
 // Component with multiple props and nested structure
 export const Card = ({
   title,
-  subtitle = "Default subtitle",
+  description
   theme = "light",
   size = "lg",
 }) => {
   const cardClass = `card-${theme} size-${size}`;

追加ツール

approve_edit

前回のedit_file_linesのドライランからの変更を適用します。このツールは、安全性を考慮して2段階の編集プロセスを提供します。ワークフローの例を以下に示します。

  1. まず、ドライラン編集を行います。

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

出力:

Index: src/components/App.tsx
===================================================================
--- src/components/App.tsx        original
+++ src/components/App.tsx        modified
@@ -1,6 +1,6 @@
 // Basic component with props
-const Button = ({ color = "blue", size = "md" }) => {
+const Button = ({ color = "primary", size = "md" }) => {
   return <button className={`btn-${color} size-${size}`}>Click me</button>;
 };

状態 ID: fcbf740a 変更を適用するには、approve_edit でこの ID を使用します。

  1. 次に、状態 ID を使用して変更を承認します。

{
  "stateId": "fcbf740a"
}

出力:

Index: src/components/App.tsx
===================================================================
--- src/components/App.tsx        original
+++ src/components/App.tsx        modified
@@ -1,6 +1,6 @@
 // Basic component with props
-const Button = ({ color = "blue", size = "md" }) => {
+const Button = ({ color = "primary", size = "md" }) => {
   return <button className={`btn-${color} size-${size}`}>Click me</button>;
 };
  1. 変更を確認します。

{
  "path": "src/components/App.tsx",
  "lineNumbers": [2],
  "context": 1
}

出力:

Line 2:
  1: // Basic component with props
> 2: const Button = ({ color = "primary", size = "md" }) => {
  3:   return <button className={`btn-${color} size-${size}`}>Click me</button>;

セキュリティ上の理由により、州IDは一定期間後に有効期限が切れますのでご注意ください。有効期限切れまたは無効な州IDを使用しようとすると、エラーが発生します。

{
  "stateId": "invalid123"
}

出力:

Error: Invalid or expired state ID

get_file_lines

ファイル内の特定の行を、オプションのコンテキスト行を使用して検査します。このツールは、編集前に行の内容を確認するのに役立ちます。

{
  "path": "src/components/App.tsx",
  "lineNumbers": [1, 2, 3],
  "context": 1
}

出力:

Line 1:
> 1: // Basic component with props
  2: const Button = ({ color = "blue", size = "md" }) => {

Line 2:
  1: // Basic component with props
> 2: const Button = ({ color = "blue", size = "md" }) => {
  3:   return Click me;

Line 3:
  2: const Button = ({ color = "blue", size = "md" }) => {
> 3:   return Click me;
  4: };

search_file

ファイル内のテキストパターンまたは正規表現を検索し、特定の行番号とその周辺のコンテキストを見つけます。このツールは、 edit_file_linesで編集したい行を正確に特定するのに特に便利です。

特徴:

  • オプションで大文字と小文字を区別できるシンプルなテキスト検索

  • 正規表現のサポート

  • 単語全体の一致

  • 設定可能なコンテキストライン

  • 行番号、内容、行番号の周囲のコンテキストを返します

引数:

{
  path: string;          // Path to the file to search
  pattern: string;       // Search pattern (text or regex)
  type?: "text" | "regex"; // Type of search (default: "text")
  caseSensitive?: boolean; // Case-sensitive search (default: false)
  contextLines?: number;   // Number of context lines (default: 2, max: 10)
  maxMatches?: number;     // Maximum matches to return (default: 100)
  wholeWord?: boolean;     // Match whole words only (default: false)
  multiline?: boolean;     // Enable multiline regex mode (default: false)
}

使用例:

  1. シンプルなテキスト検索:

{
  "path": "src/components/App.tsx",
  "pattern": "const",
  "contextLines": 2
}

出力:

Found 6 matches in 0.9ms:
File size: 0.7KB

Match 1: Line 2, Column 1
----------------------------------------
     1 | // Basic component with props
>    2 | const Button = ({ color = "blue", size = "md" }) => {
     3 |   return <button className={`btn-${color} size-${size}`}>Click me</button>;
     4 | };

Match 2: Line 7, Column 8
----------------------------------------
     5 | 
     6 | // Component with multiple props and nested structure
>    7 | export const Card = ({
     8 |   title,
     9 |   subtitle = "Default subtitle",

Match 3: Line 13, Column 3
----------------------------------------
    11 |   size = "lg",
    12 | }) => {
>   13 |   const cardClass = `card-${theme} size-${size}`;
    14 |   
    15 |   return (

Match 4: Line 23, Column 4
----------------------------------------
    21 | };
    22 | 
>   23 | // Constants and configurations
    24 | const THEME = {
    25 |   light: { bg: "#ffffff", text: "#000000" },

Match 5: Line 24, Column 1
----------------------------------------
    22 | 
    23 | // Constants and configurations
>   24 | const THEME = {
    25 |   light: { bg: "#ffffff", text: "#000000" },
    26 |   dark: { bg: "#000000", text: "#ffffff" },

Match 6: Line 29, Column 1
----------------------------------------
    27 | };
    28 | 
>   29 | const CONFIG = {
    30 |   apiUrl: "https://api.example.com",
    31 |   timeout: 5000,
  1. 大文字と小文字を区別した単語全体の検索:

{
  "path": "src/components/App.tsx",
  "pattern": "props",
  "caseSensitive": true,
  "wholeWord": true,
  "contextLines": 1
}

出力:

Found 2 matches in 0.7ms:
File size: 0.7KB

Match 1: Line 1, Column 25
----------------------------------------
>    1 | // Basic component with props
     2 | const Button = ({ color = "blue", size = "md" }) => {

Match 2: Line 6, Column 28
----------------------------------------
     5 | 
>    6 | // Component with multiple props and nested structure
     7 | export const Card = ({
  1. JSX コンポーネントの検索:

{
  "path": "src/components/App.tsx",
  "pattern": "<[A-Z]\\w+\\s",
  "type": "regex",
  "contextLines": 1
}

出力:

Found 2 matches in 0.6ms:
File size: 0.7KB

Match 1: Line 3, Column 10
----------------------------------------
     2 | const Button = ({ color = "blue", size = "md" }) => {
>    3 |   return <button className={`btn-${color} size-${size}`}>Click me</button>;
     4 | };

Match 2: Line 16, Column 5
----------------------------------------
    15 |   return (
>   16 |     <div className={cardClass}>
    17 |       <h2>{title}</h2>

一般的なワークフロー:

  1. 検索して編集:

// First, search for the line
{
  "path": "src/config.ts",
  "pattern": "API_URL",
  "wholeWord": true
}

// Then use the returned line number in edit_file_lines
{
  "p": "src/config.ts",
  "e": [{
    "startLine": 23,  // Line number from search result
    "endLine": 23,
    "content": "export const API_URL = 'https://new-api.example.com';"
  }]
}
  1. すべての使用箇所を検索:

{
  "path": "src/components/App.tsx",
  "pattern": "\\buseMemo\\b",
  "type": "regex",
  "contextLines": 2,
  "maxMatches": 50
}
  1. 特定のプロップパターンを見つける:

{
  "path": "src/components/App.tsx",
  "pattern": "className=['\"]([^'\"]+)['\"]",
  "type": "regex",
  "contextLines": 1
}

重要な注意事項

  1. 空白処理

    • このツールは、文字列と正規表現の一致の両方で空白をインテリジェントに処理します。

    • 置換後の元のインデントは保持されます

    • トークン間の複数のスペースはマッチングのために正規化されます

  2. パターンマッチング

    • 文字列マッチ( strMatch )は空白が正規化される

    • 正規表現パターン( regexMatch )は先読みと後読みをサポートします

    • 同じ編集でstrMatchとregexMatch両方を使用することはできません

    • 重複する正規表現パターンが検出され、防止されます

  3. ベストプラクティス

    • 変更を確認するには、必ず最初にドライランを実行してください。

    • 変更を承認する前に、diff 出力を確認してください。

    • 編集操作を集中的かつアトミックに保つ

    • ユースケースに応じて適切なパターンマッチングを使用する

Related MCP server: mcp-editor

発達

依存関係をインストールします:

npm install

サーバーを構築します。

npm run build

自動リビルドを使用した開発の場合:

npm run watch

テスト

テスト スイートを実行します。

npm run test

追加のテストユーティリティ:

テストツールスクリプト

サンプル ファイルに対して MCP ツールを直接テストします。

npm run test:tools

このスクリプト:

  • テストフィクスチャを既知の状態にリセットします

  • MCPサーバーに接続します

  • 各ツールを順番にテストします。

    • get_file_lines

    • edit_file_lines (ドライラン)

    • approve_edit

  • 各操作の出力を表示します

  • 変更が正しく適用されたことを確認する

フィクスチャのリセットスクリプト

テストフィクスチャを元の状態にリセットします。

npm run reset:fixtures

このスクリプトは次の目的で使用します。

  • テスト前にテストファイルを既知の状態にリセットする

  • 失敗したテスト後のクリーンアップ

  • 一貫したテスト環境を確保する

  • 不足しているフィクスチャディレクトリを作成する

使用法

サーバーの起動時に、1 つ以上の許可されたディレクトリを指定する必要があります。

node build/index.js <allowed-directory> [additional-directories...]

セキュリティのため、すべてのファイル操作はこれらのディレクトリに制限されます。

環境変数

  • MCP_EDIT_STATE_TTL : 編集状態の有効期間(ミリ秒)(デフォルト:60000)。この期間を過ぎると編集状態は期限切れとなり、再作成が必要になります。

インストール

Claude Desktop で使用するには、サーバー設定を追加します。

MacOS の場合: ~/Library/Application Support/Claude/claude_desktop_config.json Windows の場合: %APPDATA%/Claude/claude_desktop_config.json

{
  "mcpServers": {
    "edit-file-lines": {
      "command": "node",
      "args": [
        "/path/to/edit-file-lines/build/index.js",
        "<allowed-directory>"
      ],
      "env": {
        "MCP_EDIT_STATE_TTL": "300000"  // Optional: Set custom TTL (in milliseconds)
      }
    }
  }
}

エラー処理

このツールは、一般的な問題に対して明確なエラー メッセージを提供します。

  1. 一致するものが見つかりません

Error: No string match found for "oldValue" on line 5
  1. 無効な正規表現

Error: Invalid regex pattern "([": Unterminated group
  1. 同じ行の複数の編集

Error: Line 5 is affected by multiple edits

セキュリティに関する考慮事項

  • すべてのファイル操作は明示的に許可されたディレクトリに制限されます

  • シンボリックリンクは、許可されたディレクトリからの脱出を防ぐために検証されます。

  • 親ディレクトリのトラバーサルは防止されます

  • 一貫したセキュリティチェックのためにパスの正規化が実行される

  • 無効な行番号と文字位置は拒否されます

  • 行末の正規化により、プラットフォーム間で一貫した動作が保証されます。

  • セキュリティのため、編集状態は60秒後に期限切れになります

  • 編集承認にはファイルパスと編集内容の完全一致が必要です

デバッグ

テストツールスクリプトを使用して、サンプルファイルに対してMCPツールを直接テストしてください。MCPインスペクターが役立つ場合もありますが、現時点では文字列値以外の入力の処理はサポートされていません。

Related MCP Connectors

Related MCP Servers

  • A
    license
    B
    quality
    D
    maintenance
    A TypeScript MCP server port of Anthropic's filesystem editing tools, allowing file manipulation via client-approved operations without automated writes to prevent system harm.
    5
    9
    MIT
  • A
    license
    A
    quality
    D
    maintenance
    A lightweight MCP server that provides 40 tools for TypeScript/JavaScript refactoring and code intelligence, directly mapping to TypeScript's tsserver protocol commands for accurate structural changes and workspace analysis.
    40
    29 npm
    3
    MIT