代码重构服务

一个用于自动化重构Java和TypeScript/JavaScript代码库的模型上下文协议(MCP)服务器,支持类、方法、字段的重命名和移动,以及参数修改等功能。

  • by egn88
  • TypeScript
  • 本地部署
  • 4 星标
  • 更新于
在 GitHub 查看 egn88/refactor-mcp

使用说明

重构MCP

用于自动重构Java和TypeScript/JavaScript代码库的模型上下文协议(MCP)服务器。用途 OpenRewrite 用于Java重构和 ts变形 对于TypeScript。

特性

Java重构(通过OpenRewrite)

  • 重命名类 -重命名一个类并更新整个代码库中的所有引用
  • 重命名方法 -重命名方法并更新所有调用站点
  • 重命名字段 -重命名字段并更新所有用法
  • 移动类 -将类移动到其他包并更新导入
  • 添加/删除/重新排序参数 -修改方法签名并更新所有调用站点
  • Java记录支持 -完全支持修改Java记录组件(添加 null 呼叫网站)

Types/JavaScript重构(通过ts变形)

  • 重命名符号 -重命名任何符号(类、函数、变量、接口、类型)并更新所有引用
  • 移动符号 -将符号移动到其他文件并更新所有导入
  • 重命名文件 -重命名/移动文件并更新所有导入
  • 提取功能 -将代码块提取到具有自动参数检测功能的新函数中
  • 添加/删除参数 -修改函数签名并更新所有调用站点

公用事业

  • 检测项目类型 -自动检测Java/TypeScript项目及其构建系统

需求

  • Node.js 18+
  • 对于Java重构:Maven或Gradle项目,提供OpenRewrite插件

安装

# Clone the repository
git clone https://github.com/YOUR_USERNAME/refactor-mcp.git
cd refactor-mcp

# Install dependencies
npm install

# Build
npm run build

配置

克劳德代码/克劳德桌面

添加到您的Claude配置文件中:

克劳德代码 (~/.claude/settings.json):

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

克劳德桌面 (~/Library/Application Support/Claude/claude_desktop_config.json 在macOS上):

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

Java项目

为了使Java重构工作,您的项目需要OpenRewrite Maven插件 pom.xml:

<build>
    <plugins>
        <plugin>
            <groupId>org.openrewrite.maven</groupId>
            <artifactId>rewrite-maven-plugin</artifactId>
            <version>5.44.0</version>
        </plugin>
    </plugins>
</build>

或者,对于Gradle,请添加 build.gradle:

plugins {
    id 'org.openrewrite.rewrite' version '6.25.0'
}

用法

所有工具均支持 dryRun 参数(默认值: true)预览更改而不应用它们。

Java示例

重命名类:

Rename com.example.UserService to com.example.CustomerService

重命名方法:

Rename method getUser to fetchUser in com.example.UserService

将类移动到新包:

Move all classes from com.example.old to com.example.new

TypeScript示例

重命名符号:

Rename UserService to CustomerService in the TypeScript project

将符号移动到其他文件:

Move the UserService class from user-service.ts to services/customer-service.ts

提取函数:

Extract lines 10-20 from utils.ts into a new function called processData

可用工具

工具 说明
detect_project_type 检测项目类型并构建系统
java_rename_class 重命名Java类
java_rename_method 重命名Java方法
java_rename_field 重命名Java字段
java_move_class 将Java类移动到其他包中
java_add_parameter 向Java方法添加参数
java_remove_parameter 从Java方法中删除参数
java_reorder_parameters 重新排序Java方法中的参数
typescript_rename_symbol 重命名任何TypeScript符号
typescript_move_symbol 将符号移动到其他文件
typescript_rename_file 重命名/移动TypeScript文件
typescript_extract_function 将代码提取到新函数中
typescript_add_parameter 向函数添加参数
typescript_remove_parameter 从函数中删除参数

发展

# Run in development mode
npm run dev

# Build
npm run build

# Run tests
npm test

运作原理

Java重构

服务器动态生成OpenRewrite配方YAML文件,并通过Maven/Gradle插件执行它们。这确保了通过正确处理导入、引用和边缘情况进行类型安全的重构。

TypeScript重构

使用ts-morph直接解析和操作TypeScript AST。更改会被跟踪,在应用之前可以预览为差异。

Java记录支持

Java记录需要特殊处理,因为它们具有隐式规范构造函数。当您修改记录的组件时, 记录声明和所有呼叫站点 (new RecordName(...))必须更新。

记录修改的工作原理

当你使用 java_add_parameter, java_remove_parameter,或 java_change_method_signature 在Java记录上(与 methodName: "<init>" 或类名),工具:

  1. 检测记录 -自动识别目标类是否为Java记录
  2. 修改记录声明 -直接在源文件中更新记录组件列表
  3. 更新所有呼叫站点 -使用OpenRewrite的 AddNullMethodArgument 要添加的食谱 null 致所有人 new RecordName(...) 表达

示例:向记录中添加组件

// Before: public record Person(String name, int age) {}
// After:  public record Person(String name, int age, String email) {}

// All call sites are updated:
// Before: new Person("John", 30)
// After:  new Person("John", 30, null)

重要记录注意事项

  • 新参数添加为 null -添加组件时,所有现有呼叫站点都会收到 null 作为新的参数值。如果需要不同的默认值,您可能需要手动更新这些。
  • 使用 <init> 或 <constructor> 作为方法名 -两者都是针对记录构建者的
  • 先进行试运行 -始终使用 dryRun: true 在应用之前预览更改

技术细节:OpenRewrite食谱

了解使用哪些OpenRewrite配方有助于排除故障或扩展工具。

声明与呼叫站点

OpenRewrite有不同的修改配方 声明 (其中定义了方法/构造函数)vs 呼叫站点 (在调用它们的地方):

修改内容 配方 句柄
方法声明(在签名中添加参数) AddMethodParameter 仅声明
方法/构造函数调用(添加参数) AddNullMethodArgument 呼叫站点(包括 new)
方法/构造函数调用(添加文字) AddLiteralMethodArgument 呼叫站点(包括 new)
方法/构造函数调用(删除参数) DeleteMethodArgument 呼叫站点(包括 new)
方法/构造函数调用(重新排序参数) ReorderMethodArguments 呼叫站点(包括 new)

按操作选择配方

工具操作 常规方法 Java记录
添加参数 AddMethodParameter 记录实用程序+ AddNullMethodArgument
删除参数 DeleteMethodArgument 记录实用程序+ DeleteMethodArgument
重新排序参数 ReorderMethodArguments 记录实用程序+ ReorderMethodArguments
批量更改 ChangeMethodSignature composite 记录工具+ UpdateCallSites 复合材料

施工人员的方法模式

要在方法模式中定位构造函数,请使用以下任一方法:

  • com.example.MyClass <constructor>(..) -推荐
  • com.example.MyClass <init>(..) -也工作

两种模式匹配 new MyClass(...) 与调用站点食谱一起使用时的表达式。

许可证

麻省理工学院