Loading...
  • ビルド
  • 管理
  • モデルと料金
  • クライアントSDK
  • APIリファレンス
Search...
⌘K
Log in
テキストエディターツール
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...

Solutions

  • AI agents
  • Code modernization
  • Coding
  • Customer support
  • Education
  • Financial services
  • Government
  • Life sciences

Partners

  • Amazon Bedrock
  • Google Cloud's Vertex AI

Learn

  • Blog
  • Courses
  • Use cases
  • Connectors
  • Customer stories
  • Engineering at Anthropic
  • Events
  • Powered by Claude
  • Service partners
  • Startups program

Company

  • Anthropic
  • Careers
  • Economic Futures
  • Research
  • News
  • Responsible Scaling Policy
  • Security and compliance
  • Transparency

Learn

  • Blog
  • Courses
  • Use cases
  • Connectors
  • Customer stories
  • Engineering at Anthropic
  • Events
  • Powered by Claude
  • Service partners
  • Startups program

Help and security

  • Availability
  • Status
  • Support
  • Discord

Terms and policies

  • Privacy policy
  • Responsible disclosure policy
  • Terms of service: Commercial
  • Terms of service: Consumer
  • Usage policy
ビルド/ツール

テキストエディタツール

Claudeがテキストファイルを表示・編集できるAnthropicスキーマのテキストエディタツールについて学びます。

Was this page helpful?

This feature is eligible for Zero Data Retention (ZDR). When your organization has a ZDR arrangement, data sent through this feature is not stored after the API response is returned.

Claudeは、Anthropicスキーマのテキストエディタツールを使用してテキストファイルを表示・修正でき、コードやその他のテキストドキュメントのデバッグ、修正、改善を支援します。これにより、Claudeはファイルと直接対話でき、変更を提案するだけでなく、実践的な支援を提供できます。

モデルサポートについては、ツールリファレンスを参照してください。

テキストエディタツールを使用する場合

テキストエディタツールを使用する場合の例は以下の通りです:

  • コードデバッグ: Claudeにコード内のバグを特定・修正させます。構文エラーからロジックの問題まで対応します。
  • コードリファクタリング: Claudeに対象を絞った編集を通じてコード構造、可読性、パフォーマンスを改善させます。
  • ドキュメント生成: Claudeにdocstring、コメント、READMEファイルをコードベースに追加させます。
  • テスト作成: Claudeに実装の理解に基づいてコードのユニットテストを作成させます。

テキストエディタツールを使用する

Messages APIを使用してテキストエディタツール(str_replace_based_edit_toolという名前)をClaudeに提供します。

大きなファイルを表示する際の切り詰めを制御するために、オプションでmax_charactersパラメータを指定できます。

max_charactersはtext_editor_20250728以降のバージョンのテキストエディタツールとのみ互換性があります。

テキストエディタツールは以下のように使用できます:

テキストエディタツールコマンド

テキストエディタツールは、ファイルの表示と修正のための複数のコマンドをサポートしています:

view

viewコマンドを使用すると、Claudeはファイルの内容を確認するか、ディレクトリの内容をリストできます。ファイル全体または特定の行の範囲を読み込むことができます。

パラメータ:

  • command:「view」である必要があります
  • path:表示するファイルまたはディレクトリへのパス
  • view_range(オプション):表示する開始行番号と終了行番号を指定する2つの整数の配列。行番号は1から始まり、終了行に-1を指定するとファイルの終わりまで読み込みます。このパラメータはファイルを表示する場合にのみ適用され、ディレクトリには適用されません。

str_replace

str_replaceコマンドを使用すると、Claudeはファイル内の特定の文字列を新しい文字列に置換できます。これは正確な編集を行うために使用されます。

パラメータ:

  • command:「str_replace」である必要があります
  • path:修正するファイルへのパス
  • old_str:置換するテキスト(空白とインデントを含めて完全に一致する必要があります)
  • new_str:古いテキストの代わりに挿入する新しいテキスト

create

createコマンドを使用すると、Claudeは指定されたコンテンツで新しいファイルを作成できます。

パラメータ:

  • command:「create」である必要があります
  • path:新しいファイルを作成するパス
  • file_text:新しいファイルに書き込むコンテンツ

insert

insertコマンドを使用すると、Claudeはファイル内の特定の場所にテキストを挿入できます。

パラメータ:

  • command:「insert」である必要があります
  • path:修正するファイルへのパス
  • insert_line:テキストを挿入する行番号の後(ファイルの開始の場合は0)
  • insert_text:挿入するテキスト

例:テキストエディタツールで構文エラーを修正する

この例では、Claude がテキストエディタツールを使用して Python ファイルの構文エラーを修正する方法を示します。

まず、アプリケーションは Claude にテキストエディタツールと構文エラーを修正するためのプロンプトを提供します:

Claude はまずテキストエディタツールを使用してファイルを表示します:

Output
{
  "id": "msg_01XAbCDeFgHiJkLmNoPQrStU",
  "model": "claude-opus-4-7",
  "stop_reason": "tool_use",
  "role": "assistant",
  "content": [
    {
      "type": "text",
      "text": "I'll help you fix the syntax error in your primes.py file. First, let me take a look at the file to identify the issue."
    },
    {
      "type": "tool_use",
      "id": "toolu_01AbCdEfGhIjKlMnOpQrStU",
      "name": "str_replace_based_edit_tool",
      "input": {
        "command": "view",
        "path": "primes.py"
      }
    }
  ]
}

その後、アプリケーションはファイルを読み込み、その内容を Claude に返す必要があります:

行番号

上記の例では、view ツール結果には各行の前に行番号が付いたファイル内容が含まれています(例:「1: def is_prime(n):」)。行番号は必須ではありませんが、view_range パラメータを使用してファイルの特定のセクションを検査し、insert_line パラメータを使用して正確な場所にコンテンツを追加するために必須です。

Claude は構文エラーを特定し、str_replace コマンドを使用してそれを修正します:

Output
{
  "id": "msg_01VwXyZAbCdEfGhIjKlMnO",
  "model": "claude-opus-4-7",
  "stop_reason": "tool_use",
  "role": "assistant",
  "content": [
    {
      "type": "text",
      "text": "I found the syntax error in your primes.py file. In the `get_primes` function, there is a missing colon (:) at the end of the for loop line. Let me fix that for you."
    },
    {
      "type": "tool_use",
      "id": "toolu_01PqRsTuVwXyZAbCdEfGh",
      "name": "str_replace_based_edit_tool",
      "input": {
        "command": "str_replace",
        "path": "primes.py",
        "old_str": "    for num in range(2, limit + 1)",
        "new_str": "    for num in range(2, limit + 1):"
      }
    }
  ]
}

その後、アプリケーションは編集を行い、結果を返す必要があります:

最後に、Claude は修正の完全な説明を提供します:

Output
{
  "id": "msg_01IjKlMnOpQrStUvWxYzAb",
  "model": "claude-opus-4-7",
  "stop_reason": "end_turn",
  "role": "assistant",
  "content": [
    {
      "type": "text",
      "text": "I've fixed the syntax error in your primes.py file. The issue was in the `get_primes` function at line 19. There was a missing colon (:) at the end of the for loop line.\n\nHere's what I changed:\n\nFrom:\n```python\nfor num in range(2, limit + 1)\n```\n\nTo:\n```python\nfor num in range(2, limit + 1):\n```\n\nIn Python, a colon is required at the end of statements that start a new code block, such as for loops, if statements, function definitions, etc. Without the colon, Python raises a syntax error because it doesn't know where the code block begins.\n\nYour code should now run correctly. The function will properly generate a list of prime numbers up to the specified limit."
    }
  ]
}

テキストエディタツールを実装する

テキストエディタツールはスキーマレスツールとして実装されています。このツールを使用する場合、他のツールのような入力スキーマを提供する必要はありません。スキーマは Claude のモデルに組み込まれており、変更することはできません。

ツールタイプは Claude 4 モデルの場合 type: "text_editor_20250728" です。

テキストエディタツールを実装する際は、以下の点に注意してください:

  1. セキュリティ: このツールはローカルファイルシステムにアクセスできるため、適切なセキュリティ対策を実装してください。
  2. バックアップ: 重要なファイルへの編集を許可する前に、常にバックアップを作成してください。
  3. 検証: 意図しない変更を防ぐために、すべての入力を検証してください。
  4. 一意のマッチング: 置換が正確に 1 つの場所にマッチすることを確認して、意図しない編集を避けてください。

エラーを処理する

テキストエディタツールを使用する場合、さまざまなエラーが発生する可能性があります。以下は、それらを処理する方法に関するガイダンスです:

実装のベストプラクティスに従う


価格とトークン使用量

The text editor tool uses the same pricing structure as other tools used with Claude. It follows the standard input and output token pricing based on the Claude model you're using.

In addition to the base tokens, the following additional input tokens are needed for the text editor tool:

ToolAdditional input tokens
text_editor_20250429 (Claude 4.x)700 tokens
text_editor_20250124 (Claude Sonnet 3.7 (deprecated))700 tokens

ツール価格の詳細については、ツール使用価格を参照してください。

テキストエディタツールを他のツールと統合する

テキストエディタツールは他の Claude ツールと一緒に使用できます。ツールを組み合わせる場合は、以下を確認してください:

  • ツールバージョンが使用しているモデルと一致していること
  • リクエストに含まれるすべてのツールの追加トークン使用量を考慮すること

変更ログ

日付バージョン変更内容
2025 年 7 月 28 日text_editor_20250728いくつかの問題を修正し、オプションの max_characters パラメータを追加した更新テキストエディタツールのリリース。text_editor_20250429 と同一です。
2025 年 4 月 29 日text_editor_20250429Claude 4 用テキストエディタツールのリリース。このバージョンは undo_edit コマンドを削除しますが、他のすべての機能は保持されます。ツール名は str_replace ベースのアーキテクチャを反映するように更新されました。
2025 年 3 月 13 日text_editor_20250124スタンドアロンテキストエディタツールドキュメントの導入。このバージョンは Claude Sonnet 3.7 に最適化されていますが、前のバージョンと同一の機能を持っています。
2024 年 10 月 22 日text_editor_20241022Claude Sonnet 3.5(廃止)を使用したテキストエディタツールの初期リリース。view、create、、、および コマンドを通じてファイルを表示、作成、編集する機能を提供します。

次のステップ

テキストエディタツールをより便利で強力な方法で使用するためのアイデアをいくつか紹介します:

  • 開発ワークフローと統合する:テキストエディタツールを開発ツールまたは IDE に組み込む
  • コードレビューシステムを作成する:Claude にコードをレビューして改善させる
  • デバッグアシスタントを構築する:Claude がコード内の問題を診断して修正するのを支援するシステムを作成する
  • ファイル形式変換を実装する:Claude がファイルをある形式から別の形式に変換するのを支援する
  • ドキュメント自動化:Claude がコードを自動的にドキュメント化するワークフローを設定する

テキストエディタツールにより、Claude はコードベースで直接作業でき、デバッグから自動ドキュメント化までのワークフローをサポートします。

ツール使用の概要

Claude で使用するツールワークフローを実装する方法を学びます。

Bash ツール

Claude でシェルコマンドを実行します。

client = anthropic.Anthropic()

response = client.messages.create(
    model="claude-opus-4-7",
    max_tokens=1024,
    tools=[
        {
            "type": "text_editor_20250728",
            "name": "str_replace_based_edit_tool",
            "max_characters": 10000,
        }
    ],
    messages=[
        {
            "role": "user",
            "content": "There's a syntax error in my primes.py file. Can you help me fix it?",
        }
    ],
)

print(response)
  1. 1

    Claudeにテキストエディタツールとユーザープロンプトを提供する

    • APIリクエストにテキストエディタツールを含める
    • ファイルの確認または修正が必要なユーザープロンプトを提供します。例えば「コードの構文エラーを修正してくれませんか?」など
  2. 2

    Claudeがツールを使用してファイルまたはディレクトリを確認する

    • Claudeは何を確認する必要があるかを評価し、viewコマンドを使用してファイルの内容を確認するか、ディレクトリの内容をリストします
    • APIレスポンスにはviewコマンドを含むtool_useコンテンツブロックが含まれます
  3. 3

    viewコマンドを実行して結果を返す

    • Claudeのツール使用リクエストからファイルまたはディレクトリパスを抽出する
    • ファイルの内容を読み込むか、ディレクトリの内容をリストする
    • ツール設定でmax_charactersパラメータが指定されていた場合、ファイルの内容をその長さに切り詰める
    • tool_resultコンテンツブロックを含む新しいuserメッセージで会話を続けることで、結果をClaudeに返す
  4. 4

    Claudeがツールを使用してファイルを修正する

    • ファイルまたはディレクトリを確認した後、Claudeはstr_replaceなどのコマンドを使用して変更を加えるか、insertを使用して特定の行番号にテキストを追加する場合があります
    • Claudeがstr_replaceコマンドを使用する場合、古いテキストと新しいテキストを含む適切にフォーマットされたツール使用リクエストを構築します
  5. 5

    編集を実行して結果を返す

    • Claudeのツール使用リクエストからファイルパス、古いテキスト、新しいテキストを抽出する
    • ファイル内のテキスト置換を実行する
    • 結果をClaudeに返す
  6. 6

    Claudeが分析と説明を提供する

    • ファイルを確認し、場合によっては編集した後、Claudeは見つけたもの、加えた変更の完全な説明を提供します
client = anthropic.Anthropic()

response = client.messages.create(
    model="claude-opus-4-7",
    max_tokens=1024,
    tools=[{"type": "text_editor_20250728", "name": "str_replace_based_edit_tool"}],
    messages=[
        {
            "role": "user",
            "content": "There's a syntax error in my primes.py file. Can you help me fix it?",
        }
    ],
)

print(response)
response = client.messages.create(
    model="claude-opus-4-7",
    max_tokens=1024,
    tools=[{"type": "text_editor_20250728", "name": "str_replace_based_edit_tool"}],
    messages=[
        {
            "role": "user",
            "content": "There's a syntax error in my primes.py file. Can you help me fix it?",
        },
        {
            "role": "assistant",
            "content": [
                {
                    "type": "text",
                    "text": "I'll help you fix the syntax error in your primes.py file. First, let me take a look at the file to identify the issue.",
                },
                {
                    "type": "tool_use",
                    "id": "toolu_01AbCdEfGhIjKlMnOpQrStU",
                    "name": "str_replace_based_edit_tool",
                    "input": {"command": "view", "path": "primes.py"},
                },
            ],
        },
        {
            "role": "user",
            "content": [
                {
                    "type": "tool_result",
                    "tool_use_id": "toolu_01AbCdEfGhIjKlMnOpQrStU",
                    "content": '1: def is_prime(n):\n2:     """Check if a number is prime."""\n3:     if n <= 1:\n4:         return False\n5:     if n <= 3:\n6:         return True\n7:     if n % 2 == 0 or n % 3 == 0:\n8:         return False\n9:     i = 5\n10:     while i * i <= n:\n11:         if n % i == 0 or n % (i + 2) == 0:\n12:             return False\n13:         i += 6\n14:     return True\n15: \n16: def get_primes(limit):\n17:     """Generate a list of prime numbers up to the given limit."""\n18:     primes = []\n19:     for num in range(2, limit + 1)\n20:         if is_prime(num):\n21:             primes.append(num)\n22:     return primes\n23: \n24: def main():\n25:     """Main function to demonstrate prime number generation."""\n26:     limit = 100\n27:     prime_list = get_primes(limit)\n28:     print(f"Prime numbers up to {limit}:")\n29:     print(prime_list)\n30:     print(f"Found {len(prime_list)} prime numbers.")\n31: \n32: if __name__ == "__main__":\n33:     main()',
                }
            ],
        },
    ],
)

print(response)
response = client.messages.create(
    model="claude-opus-4-7",
    max_tokens=1024,
    tools=[{"type": "text_editor_20250728", "name": "str_replace_based_edit_tool"}],
    messages=[
        # Previous messages...
        {
            "role": "assistant",
            "content": [
                {
                    "type": "text",
                    "text": "I found the syntax error in your primes.py file. In the `get_primes` function, there is a missing colon (:) at the end of the for loop line. Let me fix that for you.",
                },
                {
                    "type": "tool_use",
                    "id": "toolu_01PqRsTuVwXyZAbCdEfGh",
                    "name": "str_replace_based_edit_tool",
                    "input": {
                        "command": "str_replace",
                        "path": "primes.py",
                        "old_str": "    for num in range(2, limit + 1)",
                        "new_str": "    for num in range(2, limit + 1):",
                    },
                },
            ],
        },
        {
            "role": "user",
            "content": [
                {
                    "type": "tool_result",
                    "tool_use_id": "toolu_01PqRsTuVwXyZAbCdEfGh",
                    "content": "Successfully replaced text at exactly one location.",
                }
            ],
        },
    ],
)

print(response)
  1. 1

    エディタ実装を初期化する

    ファイルの読み込み、書き込み、変更などのファイル操作を処理するヘルパー関数を作成します。ミスから回復するためのバックアップ機能の実装を検討してください。

  2. 2

    エディタツール呼び出しを処理する

    コマンドタイプに基づいて Claude からのツール呼び出しを処理する関数を作成します:

    def handle_editor_tool(tool_call):
        input_params = tool_call.input
        command = input_params.get("command", "")
        file_path = input_params.get("path", "")
    
        if command == "view":
            # Read and return file contents
            pass
        elif command == "str_replace":
            # Replace text in file
            pass
        elif command == "create":
            # Create new file
            pass
        elif command == "insert":
            # Insert text at location
            pass
  3. 3

    セキュリティ対策を実装する

    検証とセキュリティチェックを追加します:

    • ディレクトリトラバーサルを防ぐためにファイルパスを検証する
    • 変更前にバックアップを作成する
    • エラーを適切に処理する
    • パーミッションチェックを実装する
  4. 4

    Claude の応答を処理する

    Claude の応答からツール呼び出しを抽出して処理します:

    # Process tool use in Claude's response
    for content in response.content:
        if content.type == "tool_use":
            # Execute the tool based on command
            result = handle_editor_tool(content)
    
            # Return result to Claude
            tool_result = {
                "type": "tool_result",
                "tool_use_id": content.id,
                "content": result,
            }

str_replace
insert
undo_edit