MCP Server Semgrep

import { executeSemgrepCommand } from '../utils/index.js'; import { ErrorCode, McpError } from '../sdk.js'; interface ListRulesParams { language?: string; registry?: string; timeout?: number; } /** * Handles a request to list available Semgrep rules * @param {ListRulesParams} params Request parameters * @returns {Promise<object>} List of available rules */ export async function handleListRules(params: ListRulesParams): Promise<object> { // Build command arguments for listing supported languages const args = ['--help']; try { // Execute semgrep command const { stdout, stderr } = await executeSemgrepCommand(args, params.timeout); // Since semgrep's output format can be inconsistent, we'll hardcode a set of common languages // This is more reliable than trying to parse the output const commonLanguages = [ "apex", "bash", "c", "c++", "c#", "dart", "dockerfile", "elixir", "go", "html", "java", "javascript", "json", "kotlin", "lua", "ocaml", "php", "python", "r", "ruby", "rust", "scala", "swift", "typescript", "yaml" ]; // Filter by language if specified let filteredLanguages = commonLanguages; if (params.language) { filteredLanguages = commonLanguages.filter(lang => lang.toLowerCase().includes(params.language!.toLowerCase()) ); } return { status: 'success', languages: filteredLanguages, count: filteredLanguages.length }; } catch (error: any) { console.error(`Error in listRules handler: ${error.message}`); if (error instanceof McpError) { throw error; } // Return hardcoded list instead of failing const commonLanguages = [ "apex", "bash", "c", "c++", "c#", "dart", "dockerfile", "elixir", "go", "html", "java", "javascript", "json", "kotlin", "lua", "ocaml", "php", "python", "r", "ruby", "rust", "scala", "swift", "typescript", "yaml" ]; return { status: 'success', languages: commonLanguages, count: commonLanguages.length, note: "Error occurred, but returning default language list" }; } }