get_last_error
Retrieve the last terminal command that failed with a non-zero exit code to help diagnose errors without manual copy-pasting.
Instructions
Get the last terminal command only if it failed (non-zero exit code). Returns nothing if the last command succeeded.
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
No arguments | |||
Implementation Reference
- index.js:118-147 (handler)Handler function for the 'get_last_error' tool. It parses the log, checks if the last command failed (non-zero exit code), and returns the command, exit code, and output if there was an error; otherwise, indicates success or no history.async () => { const entries = parseLogFile(); if (entries.length === 0) { return { content: [{ type: 'text', text: 'No terminal history found.', }], }; } const last = entries[entries.length - 1]; if (last.exitCode === 0 || last.exitCode === null) { return { content: [{ type: 'text', text: 'Last command succeeded - no error to report.', }], }; } return { content: [{ type: 'text', text: `Command: ${last.command}\nExit Code: ${last.exitCode}\n\nError Output:\n${last.output}`, }], }; }
- index.js:113-117 (schema)Schema and metadata definition for the 'get_last_error' tool, including title, description, and empty input schema.{ title: 'Get Last Error', description: 'Get the last terminal command only if it failed (non-zero exit code). Returns nothing if the last command succeeded.', inputSchema: {}, },
- index.js:111-148 (registration)Registration of the 'get_last_error' tool using server.registerTool, including the tool name, schema, and inline handler function.server.registerTool( 'get_last_error', { title: 'Get Last Error', description: 'Get the last terminal command only if it failed (non-zero exit code). Returns nothing if the last command succeeded.', inputSchema: {}, }, async () => { const entries = parseLogFile(); if (entries.length === 0) { return { content: [{ type: 'text', text: 'No terminal history found.', }], }; } const last = entries[entries.length - 1]; if (last.exitCode === 0 || last.exitCode === null) { return { content: [{ type: 'text', text: 'Last command succeeded - no error to report.', }], }; } return { content: [{ type: 'text', text: `Command: ${last.command}\nExit Code: ${last.exitCode}\n\nError Output:\n${last.output}`, }], }; } );
- index.js:29-76 (helper)Helper function parseLogFile() that parses the terminal log file into structured command entries (command, output, exitCode), used by the 'get_last_error' handler and other tools.function parseLogFile() { if (!existsSync(LOG_FILE)) { return []; } const content = readFileSync(LOG_FILE, 'utf8'); const entries = []; const blocks = content.split('---CMD---').filter(block => block.trim()); for (const block of blocks) { const entry = { command: '', output: '', exitCode: null, timestamp: null, }; // Extract command (line starting with $) const cmdMatch = block.match(/^\s*\$\s*(.+?)(?:\n|---)/m); if (cmdMatch) { entry.command = cmdMatch[1].trim(); } // Extract output const outputMatch = block.match(/---OUTPUT---\n([\s\S]*?)(?:---EXIT|---END|$)/); if (outputMatch) { entry.output = outputMatch[1].trim(); } // Extract exit code const exitMatch = block.match(/---EXIT:(\d+)---/); if (exitMatch) { entry.exitCode = parseInt(exitMatch[1], 10); } // Extract timestamp if present const timestampMatch = block.match(/---TIMESTAMP:(.+?)---/); if (timestampMatch) { entry.timestamp = timestampMatch[1]; } if (entry.command) { entries.push(entry); } } return entries; }