Skip to main content
Glama
rodhayl
by rodhayl

mcp_translate_code

Translate code between programming languages while preserving structure and comments. This tool converts source code from one language to another, maintaining the original logic and formatting for local development workflows.

Instructions

Translate code between languages with structure preservation.

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
codeYesSource code to translate
sourceLanguageYesSource programming language
targetLanguageYesTarget programming language
preserveCommentsNoKeep comments in output (default: true)

Implementation Reference

  • The `translateCode` method within `CodeAssistanceTools` class acts as the handler for the `mcp_translate_code` tool, coordinating the LLM call for translating code between programming languages.
      async translateCode(
        code: string,
        targetLanguage: string,
        options?: {
          sourceLanguage?: string;
          preserveComments?: boolean;
        }
      ): Promise<TranslateCodeResult> {
        const sourceLanguage = options?.sourceLanguage ?? 'auto-detect';
    
        const prompt = `You are an expert polyglot programmer. Translate this code from ${sourceLanguage} to ${targetLanguage}.
    
    Guidelines:
    - Use idiomatic patterns in the target language
    - Include equivalent library imports where needed
    - ${options?.preserveComments ? 'Preserve and translate comments' : 'Add comments explaining key differences'}
    
    Provide your response as JSON:
    {
      "sourceLanguage": "Detected or specified source language",
      "targetLanguage": "${targetLanguage}",
      "translatedCode": "The translated code",
      "notes": ["Important translation notes"],
      "warnings": ["Any caveats or things that couldn't be directly translated"]
    }`;
    
        try {
          const responseText = await this.llmWrapper.callToolLlm(
            'mcp_translate_code',
            [
              { role: 'system', content: prompt },
              { role: 'user', content: `\`\`\`${sourceLanguage}\n${code}\n\`\`\`` },
            ],
            { type: 'translate_code', sourceLanguage, targetLanguage }
          );
    
          const parsed = this.parseJsonResponse(responseText, {
            sourceLanguage: sourceLanguage,
            targetLanguage: targetLanguage,
            translatedCode: '',
            notes: [],
          });
    
          return {
            success: true,
            sourceLanguage: parsed.sourceLanguage || sourceLanguage,
            targetLanguage: parsed.targetLanguage || targetLanguage,
            translatedCode: parsed.translatedCode || '',
            notes: parsed.notes || [],
            warnings: parsed.warnings,
          };
        } catch (error) {
          return {
            success: false,
            sourceLanguage: sourceLanguage,
            targetLanguage: targetLanguage,
            translatedCode: '',
            notes: [],
            error: error instanceof Error ? error.message : 'Unknown error',
          };
        }
      }
Behavior3/5

Does the description disclose side effects, auth requirements, rate limits, or destructive behavior?

With no annotations provided, the description must carry the full behavioral disclosure burden. It mentions 'structure preservation' as a key trait, but lacks critical details: it does not explain what 'structure' encompasses (AST, control flow, comments), error handling behavior for unsupported languages, output format, or whether the translation attempts functional equivalence.

Agents need to know what a tool does to the world before calling it. Descriptions should go beyond structured annotations to explain consequences.

Conciseness5/5

Is the description appropriately sized, front-loaded, and free of redundancy?

Single sentence with zero waste. Front-loaded with the primary verb ('Translate'), specifies the domain ('code between languages'), and appends the key differentiator ('structure preservation'). No filler words or redundant phrases.

Shorter descriptions cost fewer tokens and are easier for agents to parse. Every sentence should earn its place.

Completeness3/5

Given the tool's complexity, does the description cover enough for an agent to succeed on first attempt?

With no output schema and no annotations, the description should ideally disclose the return value format and success/failure behavior. The 100% input schema coverage handles parameter documentation, but for a complex AI translation operation, the description remains thin regarding output guarantees and supported language constraints.

Complex tools with many parameters or behaviors need more documentation. Simple tools need less. This dimension scales expectations accordingly.

Parameters3/5

Does the description clarify parameter syntax, constraints, interactions, or defaults beyond what the schema provides?

Input schema has 100% description coverage, establishing a baseline of 3. The description adds minimal semantic value beyond the schema—it implies the nature of translation is structural, which contextualizes the `preserveComments` parameter, but provides no guidance on language identifier formats (e.g., 'python' vs 'py') or expected code length limits.

Input schemas describe structure but not intent. Descriptions should explain non-obvious parameter relationships and valid value ranges.

Purpose4/5

Does the description clearly state what the tool does and how it differs from similar tools?

Description clearly states the core action (translate) and domain (code between languages) and adds a specific quality attribute (structure preservation). However, it fails to differentiate from sibling tools like `refactor_helper` or `code_helper` that also manipulate code structure.

Agents choose between tools based on descriptions. A clear purpose with a specific verb and resource helps agents select the right tool.

Usage Guidelines2/5

Does the description explain when to use this tool, when not to, or what alternatives exist?

No guidance provided on when to select this tool versus the numerous sibling code manipulation tools (e.g., `refactor_helper`, `formatter`, `suggest_refactoring`). No prerequisites, limitations, or exclusion criteria are mentioned.

Agents often have multiple tools that could apply. Explicit usage guidance like "use X instead of Y when Z" prevents misuse.

Install Server

Other Tools

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/rodhayl/mcpLocalHelper'

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