Skip to content

Conversation

@christian-bromann
Copy link
Member

This PR adds support for Anthropic's Text Editor tool to @langchain/anthropic, enabling Claude to view and modify text files directly. This allows Claude to debug, fix, and improve code or other text documents with hands-on assistance.

Changes

  • New textEditor_20250728 function - For Claude 4.x models (str_replace_based_edit_tool)

    • Commands: view, str_replace, create, insert
    • Supports optional maxCharacters parameter for truncating large files
  • New textEditor_20250124 function - For Claude 3.7 models (str_replace_editor)

    • Commands: view, str_replace, create, insert, undo_edit
    • Includes undo_edit command (not available in Claude 4.x)
  • Added strongly-typed command interfaces in types.ts:

    • TextEditor20250728Command union type with ViewCommand, StrReplaceCommand, CreateCommand, InsertCommand
    • TextEditor20250124Command union type (includes UndoEditCommand)
  • Fixed provider tool definition handling in chat_models.ts to use tool.extras instead of tool.metadata

  • Updated TypeScript target to ES2022 for better language feature support

Usage

import { ChatAnthropic, tools } from "@langchain/anthropic";
import * as fs from "fs";

const model = new ChatAnthropic({ model: "claude-sonnet-4-5" });

const textEditor = tools.textEditor_20250728({
  execute: async (args) => {
    if (args.command === "view") {
      const content = fs.readFileSync(args.path, "utf-8");
      return content.split("\n").map((line, i) => `${i + 1}: ${line}`).join("\n");
    }
    if (args.command === "str_replace") {
      let content = fs.readFileSync(args.path, "utf-8");
      content = content.replace(args.old_str, args.new_str);
      fs.writeFileSync(args.path, content);
      return "Successfully replaced text.";
    }
    // Handle create, insert...
  },
  maxCharacters: 10000,
});

const llmWithEditor = model.bindTools([textEditor]);
const response = await llmWithEditor.invoke(
  "There's a syntax error in my primes.py file. Can you fix it?"
);

@changeset-bot
Copy link

changeset-bot bot commented Dec 3, 2025

🦋 Changeset detected

Latest commit: fc7209f

The changes in this PR will be included in the next version bump.

This PR includes no changesets

When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

});

const textEditor = tools.textEditor_20250728({
execute: async (args: TextEditor20250728Command) => {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

wont this args type be inferred?

Suggested change
execute: async (args: TextEditor20250728Command) => {
async execute(args) {

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants