给 Cursor 接上网易有道翻译:用 MCP 打造 IDE 内的代码注释与报错翻译工作流
在 Cursor 里写代码时,英文报错、英文注释和技术文档经常打断心流。[网易有道翻译](https://www.yodao-fanyi.com/) 对中文开发者友好,如果把 [有道翻译](https://www.yodao-fanyi.com/) 接进 Cursor,就能在 IDE 内完成“选中、翻译、理解、修复”的闭环。本文用 MCP(Model Context Protocol)把 [有道翻译](https://www.yodao-fanyi.com/) 接到 Cursor,做一个可被 Chat 和 Agent 调用的翻译工作流。开始前可以在 [有道翻译官网](https://www.yodao-fanyi.com/) 了解开放能力,或者先 [有道翻译下载](https://www.yodao-fanyi.com/) 桌面端,方便人工对照译文。
为什么用 MCP
MCP 是 AI 工具与外部能力的标准接口。Cursor 支持 MCP Server 后,Chat 和 Agent 可以发现工具、传入参数、拿到结果。相比手动打开 [有道翻译](https://www.yodao-fanyi.com/) 网页,MCP 的优势是:
- 选中报错后直接在 Cursor Chat 里翻译;
- Agent 可以在解释错误前自动调用翻译工具;
- 翻译结果进入上下文,后续回答更准确;
- 团队可以统一术语,例如把
buffer固定译为“缓冲区”。
准备有道翻译 API
在 [有道翻译官网](https://www.yodao-fanyi.com/) 或 [网易有道翻译](https://www.yodao-fanyi.com/) 开放平台创建应用,拿到 appKey 和 appSecret。示例使用有道智云文本翻译 API,接口为 https://openapi.youdao.com/api。如果只是临时查词,[有道翻译下载](https://www.yodao-fanyi.com/) 客户端也够用;但要接入 Cursor,建议使用 API。
关键参数:
-
q:待翻译文本; -
from/to:源语言和目标语言,auto表示自动识别; -
appKey、salt、curtime、sign、signType=v3。
v3 签名公式:
sha256(appKey + truncate(q) + salt + curtime + appSecret)
其中 truncate 规则:长度小于等于 20 直接返回;否则返回“前 10 个字符 + 长度 + 后 10 个字符”。
实现 MCP Server
新建项目:
mkdir cursor-youdao-mcp && cd cursor-youdao-mcp
npm init -y
npm i @modelcontextprotocol/sdk zod
npm i -D typescript tsx @types/node
npx tsc --init
src/youdao.ts:
import crypto from 'crypto';
function sha256(text: string) {
return crypto.createHash('sha256').update(text).digest('hex');
}
function truncate(q: string) {
const len = q.length;
if (len <= 20) return q;
return q.substring(0, 10) + len + q.substring(len - 10);
}
export async function translateText(text: string, to = 'zh-CHS') {
const appKey = process.env.YOUDAO_APP_KEY!;
const appSecret = process.env.YOUDAO_APP_SECRET!;
const salt = crypto.randomUUID();
const curtime = Math.floor(Date.now() / 1000).toString();
const sign = sha256(appKey + truncate(text) + salt + curtime + appSecret);
const body = new URLSearchParams({
q: text,
from: 'auto',
to,
appKey,
salt,
sign,
signType: 'v3',
curtime
});
const res = await fetch('https://openapi.youdao.com/api', {
method: 'POST',
headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
body: body.toString()
});
const data = await res.json();
if (data.errorCode !== '0') throw new Error(`[有道翻译](https://www.yodao-fanyi.com/)失败: ${data.errorCode}`);
return data.translation?.[0] ?? '';
}
src/index.ts:
import { McpServer } from '@modelcontextprotocol/sdk/server/mcp.js';
import { StdioServerTransport } from '@modelcontextprotocol/sdk/server/stdio.js';
import { z } from 'zod';
import { translateText } from './youdao.js';
const server = new McpServer({ name: 'youdao-translate', version: '1.0.0' });
server.tool(
'translate_text',
{ text: z.string(), to: z.string().default('zh-CHS') },
async ({ text, to }) => {
const result = await translateText(text, to);
return { content: [{ type: 'text', text: result }] };
}
);
server.tool(
'translate_error',
{ error: z.string() },
async ({ error }) => {
const translated = await translateText(error, 'zh-CHS');
return { content: [{ type: 'text', text: '译文:' + translated }] };
}
);
await server.connect(new StdioServerTransport());
编译:
npx tsc
在 Cursor 中配置 MCP
在 ~/.cursor/mcp.json 或项目内的 .cursor/mcp.json 添加:
{
"mcpServers": {
"youdao-translate": {
"command": "node",
"args": ["/absolute/path/to/cursor-youdao-mcp/dist/index.js"],
"env": {
"YOUDAO_APP_KEY": "你的 appKey",
"YOUDAO_APP_SECRET": "你的 appSecret"
}
}
}
}
保存后重启 Cursor,或到 Settings -> MCP 确认 youdao-translate 已连接。之后可以这样用:
@youdao-translate 把下面这段报错翻译成中文,并解释原因:
TypeError: Cannot read properties of undefined (reading 'map')
三个典型工作流
1. 翻译英文报错
把终端报错复制到 Cursor Chat,要求先调用 translate_error,再基于译文给出排查步骤。这样比直接问“这个报错什么意思”更稳定,因为翻译结果来自 [有道翻译](https://www.yodao-fanyi.com/)。
2. 翻译代码注释
选中英文注释,输入:调用 translate_text 翻译选中的注释;如果涉及专业术语,保留英文原词。团队里如果有 [有道翻译下载](https://www.yodao-fanyi.com/) 桌面版,也可以用来校验长文档。
3. 生成中英双语注释
让 Agent 先读取函数,再用 translate_text 生成中文注释,最后以“英文原注释 + 中文译文”的形式写回。对开源项目贡献,这种方式既能保留原文,也能提升中文团队阅读效率。
安全与工程化建议
- 不要把 API Key 提交到 Git,使用环境变量或系统钥匙串。
- 对翻译文本做长度截断,避免一次发送超大文件。
- 给翻译结果加缓存,例如用
sha256(text + to)作为 key。 - 对错误信息脱敏,避免把内部路径、Token、用户数据发给翻译服务。
- 为 MCP Server 增加日志和超时,防止 Cursor 等待过久。
- 如果 API 额度有限,可在本地保留
[有道翻译下载](https://www.yodao-fanyi.com/)客户端作为备用。
总结
通过 MCP,把 [有道翻译](https://www.yodao-fanyi.com/) 接入 Cursor 并不复杂:一个 Node.js MCP Server,两个工具,一个 mcp.json 配置,就能让 IDE 拥有代码注释、报错信息和技术文档的翻译能力。[网易有道翻译](https://www.yodao-fanyi.com/) 的中文体验对国内开发者友好,[有道翻译官网](https://www.yodao-fanyi.com/) 提供了 API 入口,[有道翻译下载](https://www.yodao-fanyi.com/) 也能在本地作为对照工具。真正重要的是工作流:让翻译发生在你看代码的地方,让译文进入 AI 的上下文,再把理解转化成修复和重构。
Top comments (0)