Skip to main content
Glama

code_review

Analyze code for bugs, style issues, performance problems, and security vulnerabilities to improve code quality and reliability.

Instructions

Provides a code review for a given file or code snippet, focusing on potential bugs, style issues, performance bottlenecks, and security vulnerabilities.

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
file_pathNoThe full path to the local file containing the code to review
languageNoThe programming language of the code
code_snippetNoOptional small code snippet for quick reviews (alternative to file_path)

Implementation Reference

  • Main handler function for the code_review tool. Validates arguments, reads file or code snippet, sanitizes inputs, generates prompt using template and system prompt, calls Deepseek API for review, handles errors, and returns ToolResponse.
    export async function handler(args: unknown): Promise<ToolResponse> { // Validate arguments if (!checkRateLimit()) { return { content: [ { type: 'text', text: 'Rate limit exceeded. Please try again later.', }, ], isError: true, }; } if (!args || typeof args !== 'object') { return { content: [ { type: 'text', text: 'Invalid arguments provided.', }, ], isError: true, }; } // Type guard for CodeReviewArgs if (!('language' in args) || typeof args.language !== 'string') { return { content: [ { type: 'text', text: 'Language parameter is required and must be a string.', }, ], isError: true, }; } try { let codeToReview: string; const typedArgs = args as CodeReviewArgs; // Get code from either file or snippet if (typedArgs.file_path) { try { codeToReview = await readFileContent(typedArgs.file_path); } catch (error) { return { content: [ { type: 'text', text: `Error reading file: ${error instanceof Error ? error.message : 'Unknown error'}`, }, ], isError: true, }; } } else if (typedArgs.code_snippet) { codeToReview = typedArgs.code_snippet; } else { return { content: [ { type: 'text', text: 'Either file_path or code_snippet must be provided.', }, ], isError: true, }; } // Sanitize inputs const sanitizedCode = sanitizeInput(codeToReview); const sanitizedLanguage = sanitizeInput(typedArgs.language); // Create the complete prompt const prompt = createPrompt(PROMPT_TEMPLATE, { language: sanitizedLanguage, code: sanitizedCode, }); // Make the API call const response = await makeDeepseekAPICall(prompt, SYSTEM_PROMPT); if (response.isError) { return { content: [ { type: 'text', text: `Error generating code review: ${response.errorMessage || 'Unknown error'}`, }, ], isError: true, }; } // Return the formatted response return { content: [ { type: 'text', text: response.text, }, ], }; } catch (error) { console.error('Code review tool error:', error); return { content: [ { type: 'text', text: `Error processing code review: ${error instanceof Error ? error.message : 'Unknown error'}`, }, ], isError: true, }; } }
  • ToolDefinition object defining the name, description, and inputSchema (with properties file_path, language, code_snippet and oneOf requirements for either file or snippet with language).
    export const definition: ToolDefinition = { name: 'code_review', description: 'Provides a code review for a given file or code snippet, focusing on potential bugs, style issues, performance bottlenecks, and security vulnerabilities.', inputSchema: { type: 'object', properties: { file_path: { type: 'string', description: 'The full path to the local file containing the code to review', }, language: { type: 'string', description: 'The programming language of the code', }, code_snippet: { type: 'string', description: 'Optional small code snippet for quick reviews (alternative to file_path)', }, }, oneOf: [ { required: ['file_path', 'language'] }, { required: ['code_snippet', 'language'] }, ], }, };
  • src/server.ts:56-64 (registration)
    Registration of code_review tool in the ListToolsRequestSchema handler by including codeReview.definition in the tools array.
    this.server.setRequestHandler(ListToolsRequestSchema, async () => ({ tools: [ secondOpinion.definition, codeReview.definition, designCritique.definition, writingFeedback.definition, brainstormEnhancements.definition, ], }));
  • src/server.ts:92-100 (registration)
    Registration of code_review tool in the CallToolRequestSchema switch case: validates args with isCodeReviewArgs and calls codeReview.handler(args).
    case "code_review": { if (!isCodeReviewArgs(args)) { throw new McpError( ErrorCode.InvalidParams, "Invalid parameters for code review" ); } response = await codeReview.handler(args); break;
  • Type guard function isCodeReviewArgs used to validate arguments before calling the handler.
    export function isCodeReviewArgs(args: unknown): args is CodeReviewArgs { if (!args || typeof args !== 'object') return false; const a = args as Record<string, unknown>; // Must have either file_path or code_snippet const hasFilePath = 'file_path' in a && (typeof a.file_path === 'string' || a.file_path === undefined); const hasCodeSnippet = 'code_snippet' in a && (typeof a.code_snippet === 'string' || a.code_snippet === undefined); const hasLanguage = 'language' in a && typeof a.language === 'string'; return hasLanguage && (hasFilePath || hasCodeSnippet); }
  • TypeScript interface defining the shape of CodeReviewArgs.
    export interface CodeReviewArgs { file_path?: string; language: string; code_snippet?: string; }

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/cyanheads/mentor-mcp-server'

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