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',
          };
        }
      }

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