MCP ts-morph Refactoring Tools

Integrations

  • Enables AST-based code refactoring operations for JavaScript files including symbol renaming, file/folder renaming with automatic import path updates, and reference finding.

  • Provides a Node.js-based refactoring server that can be integrated with editor extensions like Cursor to perform code transformations.

  • Provides refactoring capabilities for TypeScript codebases including symbol renaming, finding references, and updating import paths, all performed using AST-based analysis.

MCP ts-morph Refactoring Tools

overview

The MCP server leverages ts-morph to provide refactoring operations for TypeScript and JavaScript codebases, and works with editor extensions such as Cursor to allow AST-based (Abstract Syntax Tree)-based symbol renaming, file/folder renaming, find references, and more.

Environment construction

Add the following settings to mcp.json .

{ "mcpServers": { "mcp-tsmorph-refactor": { // 任意のサーバー名 "command": "node", // TODO: ビルド後のエントリポイントへのパスを指定してください "args": ["/path/to/this/repo/dist/index.js"], "env": {} } } }

Logging Settings (Environment Variables)

The output level and destination of the server operation log can be controlled by the following environment variables. Set them in env block of mcp.json .

  • LOG_LEVEL : Sets the log verbosity.
    • Available levels: fatal , error , warn , info (default), debug , trace , silent
    • Example: "LOG_LEVEL": "debug"
  • LOG_OUTPUT : Specifies the log output destination.
    • console (default): Logs to standard output. If you are in a development environment ( NODE_ENV !== 'production' ) and have pino-pretty installed, the output will be formatted in a pretty way.
    • file : Outputs the log to the specified file. Set this to avoid impacting MCP clients.
    • Example: "LOG_OUTPUT": "file"
  • LOG_FILE_PATH : If LOG_OUTPUT is set to file , this specifies the absolute path of the log file.
    • Default: [プロジェクトルート]/app.log
    • Example: "LOG_FILE_PATH": "/var/log/mcp-tsmorph.log"

Example config (in mcp.json ):

// ... (mcp.json の他の設定) "env": { "LOG_LEVEL": "debug", // デバッグレベルのログを "LOG_OUTPUT": "file", // ファイルに出力 "LOG_FILE_PATH": "/Users/yourname/logs/mcp-tsmorph.log" // ログファイルのパス指定 } // ...

Features provided

The MCP server provides the following refactoring features, each of which uses ts-morph to analyze the AST and make changes while maintaining consistency across the project:

Renaming symbols ( rename_symbol_by_tsmorph )

  • What it does : Globally rename a symbol (function, variable, class, interface, etc.) at a specific position in a specified file across the entire project.
  • Use case : You want to change the name of a function or variable, but there are many references to it and it would be difficult to change it manually.
  • Required information : tsconfig.json path of the project, path of the target file, position of the symbol (line and column), current symbol name, new symbol name

Renaming a file/folder ( rename_filesystem_entry_by_tsmorph )

  • Feature : Renames multiple specified files and/or folders and automatically updates the paths in all import / export statements in the project.
  • Use cases : When you change file structure and want to modify import paths accordingly. When you want to rename/move multiple files/folders at once.
  • Required information : project's tsconfig.json path, array of rename operations ( renames: { oldPath: string, newPath: string }[] ).
  • Note : It will update references while preserving the original path format, including path aliases (such as @/ ) and relative index imports (paths containing . or .. ). It will also check for path collisions (duplicates in existing paths and within the operation) before the rename operation.
  • Note (Execution time): When working with many files and folders at once, or for very large projects, parsing and updating references can take some time.

Finding references ( find_references_by_tsmorph )

  • What it does : Finds and lists the definition of a symbol at a particular location in a specified file, as well as all its references throughout the project.
  • Use case : You want to understand where a function or variable is used. You want to investigate the impact of a refactoring.
  • Required information : project's tsconfig.json path, target file path, symbol position (line, column).

Remove a path alias ( remove_path_alias_by_tsmorph )

  • Function : Replaces path aliases (such as @/components ) in import / export statements in the specified file or directory with relative paths (such as ../../components ).
  • Use case : You want to make your project more portable or to conform to specific coding standards.
  • Required information : tsconfig.json path of the project, path of the file or directory to process.

(Add any other features)

Developer Information

Prerequisites

  • Node.js (for version, see .node-version or volta field in package.json )
  • pnpm (see packageManager field in package.json for version)

set up

Clone the repository and install the dependencies:

git clone https://github.com/sirosuzume/mcp-tsmorph-refactor.git cd mcp-tsmorph-refactor pnpm install

Build

Compiles TypeScript code into JavaScript.

pnpm build

The build artifacts are output to dist directory.

test

Run the unit tests.

pnpm test

Linting and formatting

It statically analyzes and formats your code.

# Lintチェック pnpm lint # Lint修正 pnpm lint:fix # フォーマット pnpm format

license

This project is released under the MIT license, see the LICENSE file for details.

You must be authenticated.

A
security – no known vulnerabilities
A
license - permissive license
A
quality - confirmed to work

hybrid server

The server is able to function both locally and remotely, depending on the configuration or use case.

Provides TypeScript and JavaScript code refactoring operations using ts-morph, allowing AST-based symbol renaming, file/folder renaming, reference searching, and path alias removal when integrated with editor extensions like Cursor.

  1. 概要
    1. 環境構築
      1. ロギング設定 (環境変数)
    2. 提供される機能
      1. シンボル名の変更 (rename_symbol_by_tsmorph)
      2. ファイル/フォルダ名の変更 (rename_filesystem_entry_by_tsmorph)
      3. 参照箇所の検索 (find_references_by_tsmorph)
      4. パスエイリアスの削除 (remove_path_alias_by_tsmorph)
    3. 開発者向け情報
      1. 前提条件
      2. セットアップ
      3. ビルド
      4. テスト
      5. リンティングとフォーマット
    4. ライセンス

      Related MCP Servers

      • A
        security
        A
        license
        A
        quality
        TypeScript-based MCP server designed to enhance code editing experiences by providing features such as hover information, code completion, and diagnostics.
        Last updated -
        3
        13
        MIT License
        • Apple
      • A
        security
        A
        license
        A
        quality
        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.
        Last updated -
        5
        5
        TypeScript
        MIT License
      • -
        security
        A
        license
        -
        quality
        A TypeScript-based MCP server that provides tools for making precise line-based edits to text files within allowed directories.
        Last updated -
        18
        TypeScript
        MIT License
        • Apple
      • A
        security
        F
        license
        A
        quality
        A TypeScript-based server that visualizes project directory structures in Markdown format, automatically documenting file contents with syntax highlighting and supporting customizable exclusion patterns.
        Last updated -
        1
        3
        2
        TypeScript
        • Linux
        • Apple

      View all related MCP servers

      ID: byunmqla3h