ts-refactor-mcp

ts-refactor-mcp

An MCP server that enables AI agents to move TypeScript files and directories while automatically updating all affected imports using a persistent tsserver instance. This ensures atomic, error-free refactoring that maintains project integrity without manual intervention or wasted tokens.

Category
访问服务器

README

ts-refactor-mcp

TypeScript-aware file refactoring for AI agents via the Model Context Protocol (MCP).

What is this?

An MCP server that enables AI coding agents to move TypeScript files while automatically updating all imports. When you move a file in VS Code, TypeScript's language server updates every import automatically. This tool exposes that same capability to AI agents through MCP.

The problem it solves: AI agents can move files, but they break imports. They either miss updates or waste tokens fixing them manually. This server does it correctly in one atomic operation.

Installation

npm install ts-refactor-mcp

Or install from source:

git clone https://github.com/schicks/ts-refactor-mcp.git
cd ts-refactor-mcp
npm install
npm run build

MCP Configuration

Add to your MCP client configuration (e.g., Claude Desktop):

{
  "mcpServers": {
    "ts-refactor": {
      "command": "npx",
      "args": ["ts-refactor-mcp"]
    }
  }
}

Or use the built package:

{
  "mcpServers": {
    "ts-refactor": {
      "command": "node",
      "args": ["/path/to/ts-refactor-mcp/dist/index.js"]
    }
  }
}

Available Tools

moveFile

Move a TypeScript file and update all imports automatically.

Input:

{
  projectRoot: string;  // Path to project root (where tsconfig.json is)
  oldPath: string;      // Current file path
  newPath: string;      // New file path
  dryRun?: boolean;     // If true, preview changes without applying
}

Output (when applied):

{
  applied: true;
  filesModified: number;
  moved: { from: string; to: string };
  durationMs: number;
}

Output (dry-run):

{
  applied: false;
  edits: Array<{
    filePath: string;
    textEdits: Array<{
      start: { line: number; offset: number };
      end: { line: number; offset: number };
      newText: string;
    }>;
  }>;
  wouldMove: { from: string; to: string };
  filesModified: number;
}

Example:

// Move a file and update all imports
{
  "projectRoot": "/home/user/my-project",
  "oldPath": "/home/user/my-project/src/utils/helper.ts",
  "newPath": "/home/user/my-project/src/lib/helper.ts",
  "dryRun": false
}

// Preview changes first
{
  "projectRoot": "/home/user/my-project",
  "oldPath": "/home/user/my-project/src/utils/helper.ts",
  "newPath": "/home/user/my-project/src/lib/helper.ts",
  "dryRun": true
}

warmup

Pre-load a TypeScript project to speed up subsequent operations.

Input:

{
  projectRoot: string;  // Path to project root (where tsconfig.json is)
}

Output:

{
  status: 'ready';
  durationMs: number;
}

Why use this: First operation on a project takes 5-30 seconds while TypeScript loads. Call warmup at session start to pay this cost upfront. Subsequent operations complete in 10-100ms.

How it Works

  1. Persistent tsserver: Keeps TypeScript's language server running between requests
  2. Atomic operations: All import updates succeed or none do—no partial failures
  3. Uses project's TypeScript: Spawns tsserver from your node_modules/typescript
  4. Battle-tested: Uses the same getEditsForFileRename API that VS Code uses
Agent calls moveFile
    ↓
MCP Server
    ↓
tsserver.getEditsForFileRename() ← Same API VS Code uses
    ↓
Apply all edits atomically
    ↓
Move the file
    ↓
Return success

Performance

  • Initial warmup: 5-30 seconds (large projects)
  • Subsequent moves: 10-100ms
  • Memory: Persistent tsserver process (~100-500MB depending on project size)

Requirements

  • Node.js >= 18.0.0
  • TypeScript project with tsconfig.json
  • TypeScript installed in project's node_modules

Development

Setup

git clone https://github.com/schicks/ts-refactor-mcp.git
cd ts-refactor-mcp
npm install

Run Tests

npm test                 # Run all tests
npm run test:watch      # Watch mode

Build

npm run build           # Compile TypeScript
npm run watch           # Watch mode

Project Structure

src/
├── tsserver-client/    # Wrapper around tsserver process
├── edit-applier/       # Applies text edits to filesystem
├── mcp-server/         # MCP protocol implementation
└── types/              # Shared TypeScript types

__tests__/
├── tsserver-client/    # Unit tests for tsserver wrapper
├── edit-applier/       # Unit tests for edit applier
├── mcp-server/         # Integration tests for MCP server
├── acceptance.test.ts  # End-to-end acceptance test
└── fixtures/           # Test fixtures

Architecture Decisions

Persistent tsserver Process

We keep tsserver running between requests. First request pays startup cost (5-30s), subsequent requests are fast (10-100ms). Without persistence, every move would reload the entire project.

Atomic Operations

All edits and the file move happen atomically. Either everything succeeds or nothing changes. This prevents broken intermediate states.

Project's Own TypeScript

We use the TypeScript version from your project's node_modules, not a global install. This ensures refactoring behavior matches your project's TypeScript version.

No State Management

When files change outside our server, we don't track it. If tsserver gets out of sync, call warmup again. Trying to maintain perfect sync is complex and unnecessary—tsserver handles file watching internally.

Limitations

  • TypeScript only: Requires tsconfig.json (JavaScript-only projects not supported)
  • One project at a time: One tsserver per tsconfig
  • No directory moves: Currently only supports single file moves
  • Cold starts: MCP server restart requires project warmup again

Future Enhancements

Potential future additions (not currently implemented):

  • moveDirectory: Move entire directories with all files
  • renameSymbol: Rename a function/class across files
  • extractToFile: Move a function to a new file
  • Multi-root workspace support
  • JavaScript-only project support

Contributing

Pull requests welcome! Please:

  1. Add tests for new functionality
  2. Ensure all tests pass (npm test)
  3. Follow existing code style
  4. Update documentation

License

MIT

Credits

Built using:

推荐服务器

Baidu Map

Baidu Map

百度地图核心API现已全面兼容MCP协议,是国内首家兼容MCP协议的地图服务商。

官方
精选
JavaScript
Playwright MCP Server

Playwright MCP Server

一个模型上下文协议服务器,它使大型语言模型能够通过结构化的可访问性快照与网页进行交互,而无需视觉模型或屏幕截图。

官方
精选
TypeScript
Magic Component Platform (MCP)

Magic Component Platform (MCP)

一个由人工智能驱动的工具,可以从自然语言描述生成现代化的用户界面组件,并与流行的集成开发环境(IDE)集成,从而简化用户界面开发流程。

官方
精选
本地
TypeScript
Audiense Insights MCP Server

Audiense Insights MCP Server

通过模型上下文协议启用与 Audiense Insights 账户的交互,从而促进营销洞察和受众数据的提取和分析,包括人口统计信息、行为和影响者互动。

官方
精选
本地
TypeScript
VeyraX

VeyraX

一个单一的 MCP 工具,连接你所有喜爱的工具:Gmail、日历以及其他 40 多个工具。

官方
精选
本地
graphlit-mcp-server

graphlit-mcp-server

模型上下文协议 (MCP) 服务器实现了 MCP 客户端与 Graphlit 服务之间的集成。 除了网络爬取之外,还可以将任何内容(从 Slack 到 Gmail 再到播客订阅源)导入到 Graphlit 项目中,然后从 MCP 客户端检索相关内容。

官方
精选
TypeScript
Kagi MCP Server

Kagi MCP Server

一个 MCP 服务器,集成了 Kagi 搜索功能和 Claude AI,使 Claude 能够在回答需要最新信息的问题时执行实时网络搜索。

官方
精选
Python
e2b-mcp-server

e2b-mcp-server

使用 MCP 通过 e2b 运行代码。

官方
精选
Neon MCP Server

Neon MCP Server

用于与 Neon 管理 API 和数据库交互的 MCP 服务器

官方
精选
Exa MCP Server

Exa MCP Server

模型上下文协议(MCP)服务器允许像 Claude 这样的 AI 助手使用 Exa AI 搜索 API 进行网络搜索。这种设置允许 AI 模型以安全和受控的方式获取实时的网络信息。

官方
精选