translate_text
Translate text between languages using source and target language codes to convert content for multilingual communication and understanding.
Instructions
Translate text from one language to another
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| sourceLang | Yes | Source language code | |
| targetLang | Yes | Target language code | |
| text | Yes | Text to translate |
Implementation Reference
- src/tools/translateText.ts:15-18 (handler)The asynchronous handler function that executes the core logic of the 'translate_text' tool by invoking the translateText utility and formatting the MCP response.async({ text, sourceLang, targetLang }) => { const translated = await translateText(text, sourceLang, targetLang) return { content: [{ type: 'text', text: translated }] } },
- src/tools/translateText.ts:10-14 (schema)Zod schema defining the input parameters for the 'translate_text' tool: text, sourceLang, and targetLang.{ text: z.string().describe('Text to translate'), sourceLang: z.string().length(2).describe('Source language code'), targetLang: z.string().length(2).describe('Target language code'), },
- src/index.ts:22-22 (registration)Registers the 'translate_text' tool on the MCP server by calling the toolTranslateText function.toolTranslateText(server)
- src/utils/translateClient.ts:3-32 (helper)Supporting utility function that performs the actual translation via API call, used by the tool handler.export async function translateText( text: string, source: string, target: string, ): Promise<string> { const response = await fetch(`${TRANSLATE_API_URL}/translate`, { method: 'POST', headers: { accept: 'application/json', 'Content-Type': 'application/json', Authorization: TRANSLATE_API_KEY, }, body: JSON.stringify( { platform: 'api', data: text, from: source, to: target, }, ), }) if (!response.ok) { throw new Error(`Translation API error: ${response.status} ${response.statusText}`) } const data = await response.json() as { error: boolean, result: string } return data.result }