MCP ts-morph Refactoring Tools

rename_symbol_by_tsmorph

Renames TypeScript/JavaScript symbols across projects by analyzing the AST. Ensures consistent updates in all file references, ideal for cross-file refactoring. Includes dry-run mode for verification.

Instructions

[Uses ts-morph] Renames TypeScript/JavaScript symbols across the project.

Analyzes the AST (Abstract Syntax Tree) to track and update references throughout the project, not just the definition site. Useful for cross-file refactoring tasks during Vibe Coding.

Usage

Use this tool, for example, when you change a function name defined in one file and want to reflect that change in other files that import and use it. ts-morph parses the project based on tsconfig.json to resolve symbol references and perform the rename.

  1. Specify the exact location (file path, line, column) of the symbol (function name, variable name, class name, etc.) you want to rename. This is necessary for ts-morph to identify the target Identifier node in the AST.
  2. Specify the current symbol name and the new symbol name.
  3. Specify the symbol kind (function, variable, class). This allows additional validation to ensure the node identified by ts-morph is of the expected type (e.g., an Identifier within a FunctionDeclaration), preventing unintended renames.
  4. It's recommended to first run with dryRun: true to check which files ts-morph will modify.
  5. If the preview looks correct, run with dryRun: false (or omit it) to actually save the changes to the file system.

Parameters

  • tsconfigPath (string, required): Path to the project's root tsconfig.json file. Essential for ts-morph to correctly parse the project structure and file references. Must be an absolute path (relative paths can be misinterpreted).
  • targetFilePath (string, required): Path to the file where the symbol to be renamed is defined (or first appears). Must be an absolute path (relative paths can be misinterpreted).
  • position (object, required): The exact position on the symbol to be renamed. Serves as the starting point for ts-morph to locate the AST node.
    • line (number, required): 1-based line number, typically obtained from an editor.
    • column (number, required): 1-based column number (position of the first character of the symbol name), typically obtained from an editor.
  • symbolName (string, required): The current name of the symbol before renaming. Used to verify against the node name found at the specified position.
  • newName (string, required): The new name for the symbol after renaming.
  • symbolKind (string, required): The kind of the symbol ("function", "variable", "class", etc.). Used to verify the type of the target by checking the kind of the parent node identified by ts-morph (e.g., FunctionDeclaration).
  • dryRun (boolean, optional): If set to true, prevents ts-morph from making and saving file changes, returning only the list of files that would be affected. Useful for verification. Defaults to false.

Result

  • On success: Returns a message containing the list of file paths modified (or scheduled to be modified if dryRun) by the rename.
  • On failure: Returns a message indicating the error.

Input Schema

NameRequiredDescriptionDefault
dryRunNoIf true, only show intended changes without modifying files.
newNameYesThe new name for the symbol.
positionYesThe exact position of the symbol to rename.
symbolKindYesThe kind of the symbol (e.g., "function", "variable", "class").
symbolNameYesThe current name of the symbol.
targetFilePathYesPath to the file containing the symbol to rename.
tsconfigPathYesPath to the project's tsconfig.json file.

Input Schema (JSON Schema)

{ "$schema": "http://json-schema.org/draft-07/schema#", "additionalProperties": false, "properties": { "dryRun": { "default": false, "description": "If true, only show intended changes without modifying files.", "type": "boolean" }, "newName": { "description": "The new name for the symbol.", "type": "string" }, "position": { "additionalProperties": false, "description": "The exact position of the symbol to rename.", "properties": { "column": { "description": "1-based column number.", "type": "number" }, "line": { "description": "1-based line number.", "type": "number" } }, "required": [ "line", "column" ], "type": "object" }, "symbolKind": { "description": "The kind of the symbol (e.g., \"function\", \"variable\", \"class\").", "type": "string" }, "symbolName": { "description": "The current name of the symbol.", "type": "string" }, "targetFilePath": { "description": "Path to the file containing the symbol to rename.", "type": "string" }, "tsconfigPath": { "description": "Path to the project's tsconfig.json file.", "type": "string" } }, "required": [ "tsconfigPath", "targetFilePath", "position", "symbolName", "newName", "symbolKind" ], "type": "object" }

You must be authenticated.

Other Tools from MCP ts-morph Refactoring Tools

Related Tools

ID: byunmqla3h