translate
Translate text between multiple languages with language detection and context-aware adjustments. Specify target language, provide contextual hints, and include instructions for precise translations.
Instructions
Translate text between languages with support for language detection and context-aware translations.
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| context | No | Additional context string to improve translation quality (e.g., 'This is a legal document' or 'Im talking with a doctor'). This helps the translation system better understand the domain. | |
| instructions | No | A list of instructions to adjust the network’s behavior regarding the output (e.g., 'Use a formal tone'). | |
| source | No | The source language code (e.g., 'en-EN' for English). If not specified, the system will attempt to detect it automatically. If you have a hint about the source language, you should specify it in the source_hint field. | |
| source_hint | No | Used to guide language detection. Specify this when the source language is uncertain to improve detection accuracy. | |
| target | Yes | The target language code (e.g., 'it-IT' for Italian). This specifies the language you want the text translated into. | |
| text | Yes | An array of text blocks to translate. Each block contains a text string and a boolean indicating whether it should be translated. This allows for selective translation where some text blocks can be preserved in their original form while others are translated. |
Input Schema (JSON Schema)
{
"$schema": "http://json-schema.org/draft-07/schema#",
"additionalProperties": false,
"properties": {
"context": {
"description": "Additional context string to improve translation quality (e.g., 'This is a legal document' or 'Im talking with a doctor'). This helps the translation system better understand the domain.",
"type": "string"
},
"instructions": {
"description": "A list of instructions to adjust the network’s behavior regarding the output (e.g., 'Use a formal tone').",
"items": {
"type": "string"
},
"type": "array"
},
"source": {
"description": "The source language code (e.g., 'en-EN' for English). If not specified, the system will attempt to detect it automatically. If you have a hint about the source language, you should specify it in the source_hint field.",
"type": "string"
},
"source_hint": {
"description": "Used to guide language detection. Specify this when the source language is uncertain to improve detection accuracy.",
"type": "string"
},
"target": {
"description": "The target language code (e.g., 'it-IT' for Italian). This specifies the language you want the text translated into.",
"type": "string"
},
"text": {
"description": "An array of text blocks to translate. Each block contains a text string and a boolean indicating whether it should be translated. This allows for selective translation where some text blocks can be preserved in their original form while others are translated.",
"items": {
"additionalProperties": false,
"properties": {
"text": {
"type": "string"
},
"translatable": {
"type": "boolean"
}
},
"required": [
"text",
"translatable"
],
"type": "object"
},
"type": "array"
}
},
"required": [
"text",
"target"
],
"type": "object"
}
Implementation Reference
- src/mcp/tools/translate.ts:55-69 (handler)The core handler function for the 'translate' tool. It validates input using translateSchema, prepares instructions from context, and invokes the Lara Translator's translate method.export async function translateHandler(args: unknown, lara: Translator) { const validatedArgs = translateSchema.parse(args); const { text, source, target, context, instructions, adapt_to } = validatedArgs; let instructionsList = [...(instructions ?? [])]; if (context) { instructionsList.push(makeInstructions(context)); } const result = await lara.translate(text, source ?? null, target, { instructions: instructionsList, adaptTo: adapt_to, }); return result.translation; }
- src/mcp/tools/translate.ts:4-50 (schema)Zod schemas defining the input structure for the translate tool: textBlockSchema for individual blocks and translateSchema for the full input including languages, context, instructions, etc.export const textBlockSchema = z.object({ text: z.string(), translatable: z.boolean(), }); export const translateSchema = z.object({ text: z .array(textBlockSchema) .describe( "An array of text blocks to translate. Each block contains a text string and a boolean indicating whether it should be translated. This allows for selective translation where some text blocks can be preserved in their original form while others are translated." ), source: z .string() .optional() .describe( "The source language code (e.g., 'en-EN' for English). If not specified, the system will attempt to detect it automatically. If you have a hint about the source language, you should specify it in the source_hint field." ), target: z .string() .describe( "The target language code (e.g., 'it-IT' for Italian). This specifies the language you want the text translated into." ), context: z .string() .optional() .describe( "Additional context string to improve translation quality (e.g., 'This is a legal document' or 'Im talking with a doctor'). This helps the translation system better understand the domain." ), instructions: z .array(z.string()) .optional() .describe( "A list of instructions to adjust the network’s behavior regarding the output (e.g., 'Use a formal tone')." ), source_hint: z .string() .optional() .describe( "Used to guide language detection. Specify this when the source language is uncertain to improve detection accuracy." ), adapt_to: z .array(z.string()) .optional() .describe( "A list of translation memory IDs for adapting the translation." ), });
- src/mcp/tools.ts:36-45 (registration)Registration of the translateHandler in the handlers object, which is used by the CallTool function to dispatch tool calls.const handlers: Record<string, Handler> = { translate: translateHandler, create_memory: createMemory, delete_memory: deleteMemory, update_memory: updateMemory, add_translation: addTranslation, delete_translation: deleteTranslation, import_tmx: importTmx, check_import_status: checkImportStatus, };
- src/mcp/tools.ts:90-95 (registration)Tool specification in ListTools, including name, description, and inputSchema for MCP tool discovery.{ name: "translate", description: "Translate text between languages with support for language detection, context-aware translations and translation memories using Lara Translate.", inputSchema: z.toJSONSchema(translateSchema), },
- src/mcp/tools/translate.ts:52-53 (helper)Helper function to create instruction strings from context, used in the translateHandler.const makeInstructions = (text: string) => `Always consider the following contextual information: ${text}`;