MCP ts-morph Refactoring Tools

MCP ts-morph Refactoring Tools

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.

Category
Visit Server

Tools

test_tool

MCPサーバーへの接続のテストに用いる ## Usage このツールはMCPサーバーへの接続が正常に行われているかを確認するためのツールです。 ## Key Features ## Filtering Options ## Result Interpretation ## When to Use

rename_symbol_by_tsmorph

[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.

rename_filesystem_entry_by_tsmorph

[Uses ts-morph] Renames **a single** TypeScript/JavaScript file **OR FOLDER** and updates all import/export paths referencing it throughout the project. Analyzes the project based on `tsconfig.json` to find all references to the file/folder being renamed and automatically corrects its paths. **Includes a remark about potential issues with path aliases and relative index imports.** ## Usage Use this tool when you want to rename a file (e.g., `utils.ts` -> `helpers.ts`) or a folder (e.g., `src/data` -> `src/coreData`) and need all the `import` statements in other files that point to it to be automatically updated. 1. Specify the path to the project's `tsconfig.json` file. **Must be an absolute path.** 2. Specify the current **absolute path** of the file or folder to rename. 3. Specify the new desired **absolute path** for the file or folder. 4. It's recommended to first run with `dryRun: true` to check which files will be affected. 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): Absolute path to the project's root `tsconfig.json` file. Essential for ts-morph to parse the project. **Must be an absolute path.** - oldPath (string, required): The current absolute path of the file or folder to rename. **Must be an absolute path.** - newPath (string, required): The new desired absolute path for the file or folder. **Must be an absolute path.** - 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 (the renamed file/folder and files with updated imports) or scheduled to be modified if dryRun. - On failure: Returns a message indicating the error. ## Remarks (Added) - **Caution:** Updating import/export statements containing path aliases (like `@/`) or relative paths referencing a directory's `index.ts` (like `import from '.' `) might be incomplete in the current `ts-morph` implementation. Manual verification and correction might be necessary after renaming.

find_references_by_tsmorph

[Uses ts-morph] Finds the definition and all references to a symbol at a given position throughout the project. Analyzes the project based on `tsconfig.json` to locate the definition and all usages of the symbol (function, variable, class, etc.) specified by its position. ## Usage Use this tool before refactoring to understand the impact of changing a specific symbol. It helps identify where a function is called, where a variable is used, etc. 1. Specify the **absolute path** to the project's `tsconfig.json`. 2. Specify the **absolute path** to the file containing the symbol you want to investigate. 3. Specify the exact **position** (line and column) of the symbol within the file. ## Parameters - tsconfigPath (string, required): Absolute path to the project's root `tsconfig.json` file. Essential for ts-morph to parse the project. **Must be an absolute path.** - targetFilePath (string, required): The absolute path to the file containing the symbol to find references for. **Must be an absolute path.** - position (object, required): The exact position of the symbol to find references for. - line (number, required): 1-based line number. - column (number, required): 1-based column number. ## Result - On success: Returns a message containing the definition location (if found) and a list of reference locations (file path, line number, column number, and line text). - On failure: Returns a message indicating the error.

remove_path_alias_by_tsmorph

[Uses ts-morph] Replaces path aliases (e.g., '@/') with relative paths in import/export statements within the specified target path. Analyzes the project based on `tsconfig.json` to resolve aliases and calculate relative paths. ## Usage Use this tool to convert alias paths like `import Button from '@/components/Button'` to relative paths like `import Button from '../../components/Button'`. This can be useful for improving portability or adhering to specific project conventions. 1. Specify the **absolute path** to the project`tsconfig.json`. 2. Specify the **absolute path** to the target file or directory where path aliases should be removed. 3. Optionally, run with `dryRun: true` to preview the changes without modifying files. ## Parameters - tsconfigPath (string, required): Absolute path to the project`tsconfig.json` file. **Must be an absolute path.** - targetPath (string, required): The absolute path to the file or directory to process. **Must be an absolute path.** - dryRun (boolean, optional): If true, only show intended changes without modifying files. Defaults to false. ## Result - On success: Returns a message containing the list of file paths modified (or scheduled to be modified if dryRun). - On failure: Returns a message indicating the error.

README

MCP ts-morph Refactoring Tools

概要

この MCP サーバーは、ts-morph を利用して TypeScript および JavaScript のコードベースに対するリファクタリング操作を提供します。 Cursor などのエディタ拡張機能と連携し、シンボル名の変更、ファイル/フォルダ名の変更、参照箇所の検索などを AST (Abstract Syntax Tree) ベースで行うことができます。

環境構築

mcp.json に以下のように設定を追加します。

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

提供される機能

この MCP サーバーは以下のリファクタリング機能を提供します。各機能は ts-morph を利用して AST を解析し、プロジェクト全体の整合性を保ちながら変更を行います。

シンボル名の変更 (rename_symbol_by_tsmorph)

  • 機能: 指定されたファイル内の特定の位置にあるシンボル (関数、変数、クラス、インターフェースなど) の名前を、プロジェクト全体で一括変更します。
  • ユースケース: 関数名や変数名を変更したいが、参照箇所が多く手作業での変更が困難な場合。
  • 必要な情報: プロジェクトの tsconfig.json パス、対象ファイルのパス、シンボルの位置 (行・列)、現在のシンボル名、新しいシンボル名

ファイル/フォルダ名の変更 (rename_filesystem_entry_by_tsmorph)

  • 機能: 指定されたファイルまたはフォルダの名前を変更し、プロジェクト内のすべての import/export 文のパスを自動的に更新します。
  • ユースケース: ファイル構成を変更し、それに伴って import パスを修正したい場合。
  • 必要な情報: プロジェクトの tsconfig.json パス、変更前のパス、変更後のパス。
  • 注意: パスエイリアスや相対的なインデックスインポートの更新は不完全な場合があります。変更後に手動確認が必要な場合があります。"."".."@/*等のパスで import している場合、更新されないことがあります。

参照箇所の検索 (find_references_by_tsmorph)

  • 機能: 指定されたファイル内の特定の位置にあるシンボルの定義箇所と、プロジェクト全体でのすべての参照箇所を検索して一覧表示します。
  • ユースケース: ある関数や変数がどこで使われているかを把握したい場合。リファクタリングの影響範囲を調査したい場合。
  • 必要な情報: プロジェクトの tsconfig.json パス、対象ファイルのパス、シンボルの位置 (行・列)。

パスエイリアスの削除 (remove_path_alias_by_tsmorph)

  • 機能: 指定されたファイルまたはディレクトリ内の import/export 文に含まれるパスエイリアス (@/components など) を、相対パス (../../components など) に置換します。
  • ユースケース: プロジェクトの移植性を高めたい場合や、特定のコーディング規約に合わせたい場合。
  • 必要な情報: プロジェクトの tsconfig.json パス、処理対象のファイルまたはディレクトリのパス。

(その他の機能があれば追記)

開発者向け情報

前提条件

  • Node.js (バージョンは .node-version または package.jsonvolta フィールドを参照)
  • pnpm (バージョンは package.jsonpackageManager フィールドを参照)

セットアップ

リポジトリをクローンし、依存関係をインストールします。

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

ビルド

TypeScript コードを JavaScript にコンパイルします。

pnpm build

ビルド成果物は dist ディレクトリに出力されます。

テスト

ユニットテストを実行します。

pnpm test

リンティングとフォーマット

コードの静的解析とフォーマットを行います。

# Lintチェック
pnpm lint

# Lint修正
pnpm lint:fix

# フォーマット
pnpm format

ライセンス

このプロジェクトは MIT ライセンスの下で公開されています。詳細は LICENSE ファイルをご覧ください。

Recommended Servers

playwright-mcp

playwright-mcp

A Model Context Protocol server that enables LLMs to interact with web pages through structured accessibility snapshots without requiring vision models or screenshots.

Official
Featured
TypeScript
Magic Component Platform (MCP)

Magic Component Platform (MCP)

An AI-powered tool that generates modern UI components from natural language descriptions, integrating with popular IDEs to streamline UI development workflow.

Official
Featured
Local
TypeScript
Audiense Insights MCP Server

Audiense Insights MCP Server

Enables interaction with Audiense Insights accounts via the Model Context Protocol, facilitating the extraction and analysis of marketing insights and audience data including demographics, behavior, and influencer engagement.

Official
Featured
Local
TypeScript
VeyraX MCP

VeyraX MCP

Single MCP tool to connect all your favorite tools: Gmail, Calendar and 40 more.

Official
Featured
Local
graphlit-mcp-server

graphlit-mcp-server

The Model Context Protocol (MCP) Server enables integration between MCP clients and the Graphlit service. Ingest anything from Slack to Gmail to podcast feeds, in addition to web crawling, into a Graphlit project - and then retrieve relevant contents from the MCP client.

Official
Featured
TypeScript
Kagi MCP Server

Kagi MCP Server

An MCP server that integrates Kagi search capabilities with Claude AI, enabling Claude to perform real-time web searches when answering questions that require up-to-date information.

Official
Featured
Python
E2B

E2B

Using MCP to run code via e2b.

Official
Featured
Neon Database

Neon Database

MCP server for interacting with Neon Management API and databases

Official
Featured
Exa Search

Exa Search

A Model Context Protocol (MCP) server lets AI assistants like Claude use the Exa AI Search API for web searches. This setup allows AI models to get real-time web information in a safe and controlled way.

Official
Featured
Qdrant Server

Qdrant Server

This repository is an example of how to create a MCP server for Qdrant, a vector search engine.

Official
Featured