Skip to main content
Glama
rodhayl
by rodhayl

code_helper

Explain, optimize, or simplify code snippets to improve understanding, performance, or readability using local AI processing.

Instructions

Explain, optimize, or simplify code snippets.

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
actionYesAction: explain (code explanation), optimize (performance suggestions), simplify (reduce complexity)
codeYesCode snippet to process
languageNoProgramming language (optional, auto-detected)
levelNoFor explain: detail level (default: intermediate)
focusNoFor optimize: focus area (default: all)
preserveNoFor simplify: features to preserve (optional)

Implementation Reference

  • One of the methods (explainCode) in CodeAssistanceTools that uses 'code_helper' as a tool identifier to the LLM.
      async explainCode(
        code: string,
        options?: {
          language?: string;
          depth?: 'brief' | 'detailed' | 'comprehensive';
          focusOn?: string;
        }
      ): Promise<ExplainCodeResult> {
        const depth = options?.depth ?? 'detailed';
        const language = options?.language ?? 'code';
    
        const prompt = `You are an expert code explainer. Explain the following ${language} code in plain English.
    
    Depth level: ${depth}
    ${options?.focusOn ? `Focus specifically on: ${options.focusOn}` : ''}
    
    Provide your response as JSON with these fields:
    {
      "explanation": "Clear explanation of what the code does",
      "keyPoints": ["Key point 1", "Key point 2"],
      "complexity": "Brief complexity assessment",
      "potentialIssues": ["Any potential issues or edge cases"]
    }`;
    
        try {
          const responseText = await this.llmWrapper.callToolLlm(
            'code_helper',
            [
              { role: 'system', content: prompt },
              { role: 'user', content: `\`\`\`${language}\n${code}\n\`\`\`` },
            ],
            { type: 'explain_code', language }
          );
    
          const parsed = this.parseJsonResponse(responseText, {
            explanation: responseText,
            keyPoints: [],
          });
    
          return {
            success: true,
            explanation: parsed.explanation || responseText,
            keyPoints: parsed.keyPoints || [],
            complexity: parsed.complexity,
            potentialIssues: parsed.potentialIssues,
          };
        } catch (error) {
          return {
            success: false,
            explanation: '',
            keyPoints: [],
            error: error instanceof Error ? error.message : 'Unknown error',
          };
        }
      }
  • Another method in CodeAssistanceTools (suggestOptimizations) that uses 'code_helper'.
      async suggestOptimizations(
        code: string,
        options?: {
          language?: string;
          focusAreas?: Array<'performance' | 'memory' | 'readability' | 'algorithm'>;
        }
      ): Promise<SuggestOptimizationsResult> {
        const language = options?.language ?? 'code';
        const focusAreas = options?.focusAreas ?? ['performance', 'memory', 'readability', 'algorithm'];
    
        const prompt = `You are an expert code optimizer. Analyze the following ${language} code and suggest optimizations.
    
    Focus on: ${focusAreas.join(', ')}
    
    Constraints:
    - Preserve semantics (same outputs for all inputs). Do NOT propose behavior-changing edits.
    - Call out edge cases (e.g., empty arrays/strings, null/undefined, error handling) and keep them correct.
    - If an optimization depends on assumptions, state them explicitly.
    
    Provide your response as JSON:
    {
      "suggestions": [
        {
          "type": "performance|memory|readability|algorithm",
          "description": "What to optimize",
          "impact": "high|medium|low",
          "before": "Original code snippet (optional)",
          "after": "Optimized code snippet (optional)"
        }
      ],
      "summary": "Overall assessment"
    }`;
    
        try {
          const responseText = await this.llmWrapper.callToolLlm(
            'code_helper',
            [
              { role: 'system', content: prompt },
              { role: 'user', content: `\`\`\`${language}\n${code}\n\`\`\`` },
            ],
            { type: 'optimize_code', language }
          );
    
          const parsed = this.parseJsonResponse(responseText, {
            suggestions: [],
            summary: responseText,
          });
    
          return {
            success: true,
            suggestions: parsed.suggestions || [],
            summary: parsed.summary || '',
          };
        } catch (error) {
          return {
            success: false,
            suggestions: [],
            summary: '',
            error: error instanceof Error ? error.message : 'Unknown error',
          };
        }
      }
  • Another method in CodeAssistanceTools (simplifyCode) that uses 'code_helper'.
      async simplifyCode(
        code: string,
        options?: {
          language?: string;
          preserveComments?: boolean;
        }
      ): Promise<SimplifyCodeResult> {
        const language = options?.language ?? 'code';
    
        const prompt = `You are an expert at simplifying code. Rewrite this ${language} code to be cleaner, more readable, and easier to maintain while preserving its functionality.
    
    ${options?.preserveComments ? 'Preserve existing comments.' : ''}
    
    Provide your response as JSON:
    {
      "originalComplexity": "Brief assessment of original complexity",
      "simplifiedCode": "The simplified code",
      "simplifiedComplexity": "Brief assessment of new complexity",
      "changes": ["Description of each simplification made"]
    }`;
    
        try {
          const responseText = await this.llmWrapper.callToolLlm(
            'code_helper',
            [
              { role: 'system', content: prompt },
              { role: 'user', content: `\`\`\`${language}\n${code}\n\`\`\`` },
            ],
            { type: 'simplify_code', language }
          );
    
          const parsed = this.parseJsonResponse(responseText, {
            originalComplexity: '',
            simplifiedCode: code,
            simplifiedComplexity: '',
            changes: [],
          });
    
          return {
            success: true,
            originalComplexity: parsed.originalComplexity || '',
            simplifiedCode: parsed.simplifiedCode || code,
            simplifiedComplexity: parsed.simplifiedComplexity || '',
            changes: parsed.changes || [],
          };
        } catch (error) {
          return {
            success: false,
            originalComplexity: '',
            simplifiedCode: '',
            simplifiedComplexity: '',
            changes: [],
            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