export_results
Export Semgrep scan results to JSON, SARIF, or text format for integration with other tools and reporting systems.
Instructions
Exports scan results in various formats
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| results_file | Yes | Absolute path to JSON results file | |
| output_file | Yes | Absolute path to output file | |
| format | No | Output format (json, sarif, text) | text |
Implementation Reference
- src/index.ts:339-355 (registration)Tool registration/definition for 'export_results' - declares the tool name, description, and input schema (results_file, output_file, format) in the ListToolsRequestSchema handler.
{ name: 'export_results', description: 'Exports scan results in various formats', inputSchema: { type: 'object', properties: { results_file: { type: 'string', description: 'Absolute path to JSON results file' }, output_file: { type: 'string', description: 'Absolute path to output file' }, format: { type: 'string', description: 'Output format (json, sarif, text)', default: 'text' } }, required: ['results_file', 'output_file'] } }, - src/index.ts:385-386 (handler)Case routing in CallToolRequestSchema switch statement that dispatches 'export_results' to handleExportResults.
case 'export_results': return await this.handleExportResults(request.params.arguments); - src/index.ts:605-687 (handler)The handleExportResults method - the core handler that reads a JSON results file, formats it (json/sarif/text), and writes the output to a specified file.
private async handleExportResults(args: any) { if (!args.results_file || !args.output_file) { throw new McpError( ErrorCode.InvalidParams, 'results_file and output_file are required' ); } const resultsFile = validateAbsolutePath(args.results_file, 'results_file'); const outputFile = validateAbsolutePath(args.output_file, 'output_file'); const format = args.format || 'text'; try { const fileContent = await readFile(resultsFile, 'utf-8'); const results = parseSemgrepResults(fileContent); const findings = getSemgrepFindings(results); let output = ''; switch (format) { case 'json': output = JSON.stringify(results, null, 2); break; case 'sarif': { const sarifOutput = { $schema: 'https://raw.githubusercontent.com/oasis-tcs/sarif-spec/master/Schemata/sarif-schema-2.1.0.json', version: '2.1.0', runs: [{ tool: { driver: { name: 'semgrep', rules: findings.map((r: any) => ({ id: r.check_id, name: r.check_id, shortDescription: { text: r.extra?.message ?? '' }, defaultConfiguration: { level: r.extra?.severity === 'ERROR' ? 'error' : 'warning' } })) } }, results: findings.map((r: any) => ({ ruleId: r.check_id, message: { text: r.extra?.message ?? '' }, locations: [{ physicalLocation: { artifactLocation: { uri: r.path }, region: { startLine: r.start?.line, startColumn: r.start?.col, endLine: r.end?.line, endColumn: r.end?.col } } }] })) }] }; output = JSON.stringify(sarifOutput, null, 2); break; } case 'text': default: output = findings.map((r: any) => `[${r.extra?.severity ?? 'unknown'}] ${r.check_id}\n` + `File: ${r.path}\n` + `Lines: ${r.start?.line}-${r.end?.line}\n` + `Message: ${r.extra?.message ?? ''}\n` + '-------------------' ).join('\n'); break; } await writeFile(outputFile, output, 'utf-8'); return { content: [{ type: 'text', text: `Results successfully exported to ${outputFile}` }] }; } catch (error: any) { return { content: [{ type: 'text', text: `Error exporting results: ${error.message}` }], isError: true }; } }