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;
    }
Behavior4/5

Does the description disclose side effects, auth requirements, rate limits, or destructive behavior?

With no annotations provided, the description carries the full burden of behavioral disclosure. It clearly describes the tool's behavior: it returns the last terminal command only if it failed (non-zero exit code), and returns nothing if the last command succeeded. This covers the core behavioral trait effectively, though it doesn't mention potential edge cases like what happens if no commands have been run.

Agents need to know what a tool does to the world before calling it. Descriptions should go beyond structured annotations to explain consequences.

Conciseness5/5

Is the description appropriately sized, front-loaded, and free of redundancy?

The description is a single, well-structured sentence that efficiently conveys the tool's purpose, condition for use, and return behavior. Every word earns its place, with no redundancy or unnecessary elaboration.

Shorter descriptions cost fewer tokens and are easier for agents to parse. Every sentence should earn its place.

Completeness4/5

Given the tool's complexity, does the description cover enough for an agent to succeed on first attempt?

Given the tool's simplicity (0 parameters, no annotations, no output schema), the description is nearly complete. It explains what the tool does, when to use it, and what it returns. The only minor gap is the lack of output format details (e.g., whether it returns a string, object, or error message), but for a simple tool like this, the description is sufficient for an agent to use it correctly.

Complex tools with many parameters or behaviors need more documentation. Simple tools need less. This dimension scales expectations accordingly.

Parameters4/5

Does the description clarify parameter syntax, constraints, interactions, or defaults beyond what the schema provides?

The input schema has 0 parameters with 100% coverage, so no parameter documentation is needed. The description appropriately doesn't discuss parameters, focusing instead on the tool's behavior. A baseline of 4 is applied for tools with 0 parameters, as no additional semantic information is required.

Input schemas describe structure but not intent. Descriptions should explain non-obvious parameter relationships and valid value ranges.

Purpose5/5

Does the description clearly state what the tool does and how it differs from similar tools?

The description clearly states the tool's purpose with a specific verb ('Get') and resource ('last terminal command'), and distinguishes it from siblings by specifying 'only if it failed (non-zero exit code)'. This differentiates it from tools like 'get_last_command' or 'get_recent_commands' that might return commands regardless of exit status.

Agents choose between tools based on descriptions. A clear purpose with a specific verb and resource helps agents select the right tool.

Usage Guidelines5/5

Does the description explain when to use this tool, when not to, or what alternatives exist?

The description explicitly states when to use this tool ('only if it failed') and implies when not to use it (if the last command succeeded, it 'Returns nothing'). It also distinguishes from siblings by focusing on failed commands only, unlike 'get_last_command' which presumably returns the last command regardless of exit code.

Agents often have multiple tools that could apply. Explicit usage guidance like "use X instead of Y when Z" prevents misuse.

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