Code Refactor MCP server
Commit Oct 18, 2026Refactors legacy code into modern, idiomatic code using local Ollama or OpenAI-compatible cloud backends.
Also known as zhouhengchang/code-refactor, code-refactor, workbuddy-code-refactor-connector
- Stars
- 0
- Tools
- —
- Downloads, 30 days
- —
- Latest version
- 1.0.4
- Last commit
- Oct 18, 2026
- License
- MIT
What is Code Refactor MCP?
Code Refactor refactors legacy code into modern, idiomatic code using local Ollama or OpenAI-compatible cloud backends.
Summary generated from the project's README
About Code Refactor MCP server
Transforming legacy source code into modern, idiomatic versions is the primary function of this MCP server. It provides developers with specialized tools to automate the refactoring process through an AI client. Users can choose between running inference locally using Ollama to keep code on their own machine or connecting to cloud-based OpenAI-compatible interfaces for higher quality results.
The server exposes the refactor_code tool to process source code based on specific task descriptions and language hints, alongside a check_status tool to verify backend availability. Configuration is handled entirely through environment variables, requiring settings like OLLAMA_HOST for local setups or OPENAI_API_KEY and OPENAI_BASE_URL for cloud connections. It includes a default timeout of 180,000 milliseconds to accommodate slower local CPU processing during heavy refactoring tasks.
What you can do with it
- 1Maintain data privacyRun refactoring tasks locally so that your proprietary code never leaves your own hardware.
Written from the project's README. Every use case is checked against a quote from it.
How to install Code Refactor MCP
Formats verified 10 Nov 2026Install Code Refactor MCP in Claude Code
$ claude mcp add --env OPENAI_API_KEY=<YOUR_OPENAI_API_KEY> code-refactor -- npx -y workbuddy-code-refactor-connectorInstall Code Refactor MCP in Cursor
~/.cursor/mcp.json
{
"mcpServers": {
"code-refactor": {
"command": "npx",
"args": [
"-y",
"workbuddy-code-refactor-connector"
],
"env": {
"OPENAI_API_KEY": "<YOUR_OPENAI_API_KEY>"
}
}
}
}Install Code Refactor MCP in VS Code
.vscode/mcp.json
{
"servers": {
"code-refactor": {
"type": "stdio",
"command": "npx",
"args": [
"-y",
"workbuddy-code-refactor-connector"
],
"env": {
"OPENAI_API_KEY": "<YOUR_OPENAI_API_KEY>"
}
}
}
}Install Code Refactor MCP in Claude Desktop
claude_desktop_config.json
{
"mcpServers": {
"code-refactor": {
"command": "npx",
"args": [
"-y",
"workbuddy-code-refactor-connector"
],
"env": {
"OPENAI_API_KEY": "<YOUR_OPENAI_API_KEY>"
}
}
}
}Install Code Refactor MCP in Codex
~/.codex/config.toml
[mcp_servers.code-refactor] command = "npx" args = ["-y", "workbuddy-code-refactor-connector"] [mcp_servers.code-refactor.env] OPENAI_API_KEY = "<YOUR_OPENAI_API_KEY>"
Install Code Refactor MCP in Gemini CLI
~/.gemini/settings.json
{
"mcpServers": {
"code-refactor": {
"command": "npx",
"args": [
"-y",
"workbuddy-code-refactor-connector"
],
"env": {
"OPENAI_API_KEY": "<YOUR_OPENAI_API_KEY>"
}
}
}
}Configuration
OPENAI_API_KEYSecretAPI key for the cloud endpoint, e.g. a DeepSeek key. Only needed when REFACTOR_BACKEND=openai.
OLLAMA_HOSTOllama server address used when REFACTOR_BACKEND=ollama.
OLLAMA_MODELOllama model name used when REFACTOR_BACKEND=ollama.
OPENAI_BASE_URLBase URL of the OpenAI-compatible endpoint, e.g. https://api.deepseek.com/v1. Required when REFACTOR_BACKEND=openai.
OPENAI_MODELCloud model name, e.g. deepseek-chat.
REFACTOR_BACKENDInference backend: 'ollama' (local, code stays on your machine, default) or 'openai' (any OpenAI-compatible cloud endpoint, e.g. DeepSeek).
REFACTOR_TIMEOUT_MSPer-call timeout in milliseconds. Raise it for slow local CPU inference.
| Name | Description | |
|---|---|---|
| OPENAI_API_KEY | API key for the cloud endpoint, e.g. a DeepSeek key. Only needed when REFACTOR_BACKEND=openai. | Secret |
| OLLAMA_HOST | Ollama server address used when REFACTOR_BACKEND=ollama. | |
| OLLAMA_MODEL | Ollama model name used when REFACTOR_BACKEND=ollama. | |
| OPENAI_BASE_URL | Base URL of the OpenAI-compatible endpoint, e.g. https://api.deepseek.com/v1. Required when REFACTOR_BACKEND=openai. | |
| OPENAI_MODEL | Cloud model name, e.g. deepseek-chat. | |
| REFACTOR_BACKEND | Inference backend: 'ollama' (local, code stays on your machine, default) or 'openai' (any OpenAI-compatible cloud endpoint, e.g. DeepSeek). | |
| REFACTOR_TIMEOUT_MS | Per-call timeout in milliseconds. Raise it for slow local CPU inference. |
README
View on GitHubworkbuddy-code-refactor-connector
WorkBuddy 代码重构连接器 —— 一个基于 Model Context Protocol 的 MCP Server,把「代码重构」能力封装成 AI 可直接调用的工具。
支持本地 Ollama 与任意 OpenAI 兼容的云端接口两种推理后端,通过环境变量切换。
功能
暴露两个 MCP 工具:
| 工具 | 参数 | 说明 |
|---|---|---|
refactor_code |
code(源代码)、task(重构任务描述)、language(可选,语言提示) |
执行代码重构,返回重构后的代码与说明 |
check_status |
无 | 检查当前推理后端是否可用,并返回后端/模型信息 |
安装
npm install -g workbuddy-code-refactor-connector
或直接以 npx 运行(无需全局安装):
npx workbuddy-code-refactor-connector
配置
所有配置均通过环境变量读取,包内不硬编码任何密钥。
方式一:本地 Ollama(默认,代码不出本机)
REFACTOR_BACKEND=ollama # 默认值,可省略
OLLAMA_HOST=http://127.0.0.1:11434
OLLAMA_MODEL=deepseek-coder
方式二:云端 OpenAI 兼容接口
REFACTOR_BACKEND=openai
OPENAI_BASE_URL=https://api.deepseek.com/v1
OPENAI_MODEL=deepseek-flash
OPENAI_API_KEY=sk-xxxxxxxxxxxx
全部环境变量
| 变量 | 默认值 | 说明 |
|---|---|---|
REFACTOR_BACKEND |
ollama |
推理后端:ollama | openai |
OLLAMA_HOST |
http://127.0.0.1:11434 |
Ollama 服务地址 |
OLLAMA_MODEL |
deepseek-coder |
本地模型名 |
OPENAI_BASE_URL |
https://api.deepseek.com/v1 |
OpenAI 兼容接口地址 |
OPENAI_MODEL |
deepseek-flash |
云端模型名 |
OPENAI_API_KEY |
无 | 云端接口密钥(openai 后端必填) |
REFACTOR_TIMEOUT_MS |
180000 |
单次推理超时毫秒数(纯 CPU 本地推理较慢,默认放宽到 3 分钟) |
接入 WorkBuddy
在 WorkBuddy 的连接器配置(mcp.json)中登记本服务:
{
"mcpServers": {
"code-refactor": {
"command": "npx",
"args": ["-y", "workbuddy-code-refactor-connector"],
"env": {
"REFACTOR_BACKEND": "ollama",
"OLLAMA_MODEL": "deepseek-coder"
}
}
}
}
若使用云端后端,在 env 中补充 REFACTOR_BACKEND、OPENAI_BASE_URL、OPENAI_MODEL、OPENAI_API_KEY。
设计要点
- 密钥零硬编码:地址、模型名、密钥全部来自环境变量。
- 超时保护:所有工具调用都有超时上限,默认 180 秒。
- 错误可读:任何异常都转换为可读的中文提示,不向调用方抛出原始堆栈。
- 双后端:本地推理保障代码不出本机;云端接口用于追求更高重构质量。
许可
Code Refactor MCP questions
How do I add Code Refactor MCP to Claude Code?
Run claude mcp add --env OPENAI_API_KEY=<YOUR_OPENAI_API_KEY> code-refactor -- npx -y workbuddy-code-refactor-connector in your terminal. Add --scope user to make it available in every project.
How do I add Code Refactor MCP to Cursor?
Open ~/.cursor/mcp.json and add the server under mcpServers, using the Cursor config shown in the install section above.
How can I ensure my code stays local?
You can use the local Ollama backend to ensure code does not leave your machine.
Can I use cloud-based models?
Yes, you can use any OpenAI-compatible cloud interface by setting the REFACTOR_BACKEND to openai.