Skip to main content
Glama
oofengoo

MCP-Serve 多语言翻译服务

by oofengoo
index.ts2.83 kB
import { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js"; import { StdioServerTransport } from "@modelcontextprotocol/sdk/server/stdio.js"; import { z } from "zod"; import { geti18n, TranslateToEn, downFile } from "./api"; import path from "path"; import fs from "fs"; const server = new McpServer({ name: "my-custom-mcp-server", version: "1.0.0", }); const port = process.env.PORT; // 多语言翻译工具 server.tool( "translationI18n", { input: z.string(), }, async ({ input }) => { // 匹配所有 ##...## 的内容 const regex = /##([\s\S]+?)##/g; let matches = input.matchAll(regex); let result = input; for (const match of matches) { const value = match[1]; if (!value) continue; let params: Record<string, any> = { language: 1, autoAlias: true, value: value, }; if (port === "5500") { params.valueEn = await TranslateToEn(value); } // 调用多语言接口 const alias = await geti18n(params); if (alias) { result = result.replace(match[0], alias); } } return { content: [ { type: "text", text: result, }, ], }; } ); // 多语言复制工具 server.tool( "copyI18n", { rootPath: z.string(), }, async ({ rootPath }) => { let langArr = [{ name: "ZhCn", value: 1, filePath: "ZhCn.js" }]; // 目前只有企业版需要英文文件 if (port === "5500") { langArr.push({ name: "EnUs", value: 2, filePath: "EnUs.js" }); } // 获取工作区根路径 // const rootPath = process.cwd(); let result = ""; try { for (let lang of langArr) { const langTargetPath = path.join( rootPath, "src/lang/source", lang.filePath ); const tempFilePath = path.join(rootPath, lang.filePath); // 创建一个写入流 const writer = fs.createWriteStream(tempFilePath); const res = await downFile(lang.value); res.data.pipe(writer); await new Promise<void>((resolve, reject) => { writer.on("finish", () => resolve()); writer.on("error", (err) => reject(err)); }); fs.copyFileSync(tempFilePath, langTargetPath); fs.unlinkSync(tempFilePath); console.log(`${lang.name}文件复制成功`); result = `${tempFilePath}文件复制成功`; } } catch (error) { console.log("文件复制失败", error); result = `文件复制失败${error},${rootPath},`; } return { content: [ { type: "text", text: result, }, ], }; } ); async function startServer() { const transport = new StdioServerTransport(); await server.connect(transport); } startServer();

Implementation Reference

Latest Blog Posts

MCP directory API

We provide all the information about MCP servers via our MCP API.

curl -X GET 'https://glama.ai/api/mcp/v1/servers/oofengoo/mcp-serve-learn'

If you have feedback or need assistance with the MCP directory API, please join our Discord server