Skip to main content
Glama

Get Last Error

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
NameRequiredDescriptionDefault

No arguments

Implementation Reference

  • 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}`,
        }],
      };
    }
  • 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}`,
          }],
        };
      }
    );
  • 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;
    }

Tool Definition Quality

Score is being calculated. Check back soon.

Install Server

Other Tools

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/chrisvin-jabamani/terminal-reader-mcp'

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