Skip to main content
Glama

Get Last Command

get_last_command

Retrieve the most recent terminal command and its output to understand what the user just executed, eliminating manual copy-pasting of terminal data.

Instructions

Get the most recent terminal command and its output. Use this to see what the user just ran in their terminal.

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault

No arguments

Implementation Reference

  • index.js:86-107 (handler)
    The main handler function for the 'get_last_command' tool. It reads the terminal log file using the parseLogFile helper, extracts the last command entry, determines its success status based on exit code, and returns a formatted text response with the command, status, and output.
    async () => {
      const entries = parseLogFile();
      
      if (entries.length === 0) {
        return {
          content: [{
            type: 'text',
            text: 'No terminal history found. Make sure to use the `cap` command to capture terminal output.\n\nExample: cap npm run dev',
          }],
        };
      }
    
      const last = entries[entries.length - 1];
      const exitStatus = last.exitCode === 0 ? '✓ Success' : `✗ Failed (exit code: ${last.exitCode})`;
      
      return {
        content: [{
          type: 'text',
          text: `Command: ${last.command}\nStatus: ${exitStatus}\n\nOutput:\n${last.output}`,
        }],
      };
    }
  • index.js:79-108 (registration)
    The registration of the 'get_last_command' tool on the MCP server, specifying the tool name, metadata (title, description), empty input schema, and reference to the handler function.
    server.registerTool(
      'get_last_command',
      {
        title: 'Get Last Command',
        description: 'Get the most recent terminal command and its output. Use this to see what the user just ran in their terminal.',
        inputSchema: {},
      },
      async () => {
        const entries = parseLogFile();
        
        if (entries.length === 0) {
          return {
            content: [{
              type: 'text',
              text: 'No terminal history found. Make sure to use the `cap` command to capture terminal output.\n\nExample: cap npm run dev',
            }],
          };
        }
    
        const last = entries[entries.length - 1];
        const exitStatus = last.exitCode === 0 ? '✓ Success' : `✗ Failed (exit code: ${last.exitCode})`;
        
        return {
          content: [{
            type: 'text',
            text: `Command: ${last.command}\nStatus: ${exitStatus}\n\nOutput:\n${last.output}`,
          }],
        };
      }
    );
  • The parseLogFile helper function parses the terminal history log file (LOG_FILE) into an array of command entries, each containing command, output, exitCode, and optional timestamp. This is called by the get_last_command handler to retrieve history.
    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;
    }
Behavior2/5

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

No annotations are provided, so the description carries the full burden. It mentions retrieving command and output but lacks details on permissions, rate limits, or what happens if no recent command exists. It does not disclose behavioral traits beyond the basic operation.

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?

Two sentences, front-loaded with the core purpose and followed by usage context. Every sentence earns its place with no wasted words, making it efficient and well-structured.

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

Completeness3/5

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

Given no annotations and no output schema, the description is minimal but covers the basic operation. It lacks details on return format, error handling, or dependencies, which could be important for a tool interacting with terminal history. Adequate but with clear gaps.

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 does not add param info, which is appropriate, but baseline is 4 for zero parameters as it avoids redundancy.

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 specific verb ('get') and resource ('most recent terminal command and its output'), distinguishing it from siblings like 'get_recent_commands' (plural) and 'get_last_error' (error-specific). It explicitly identifies what the tool retrieves.

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

Usage Guidelines4/5

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

The description provides clear context for when to use it ('to see what the user just ran in their terminal'), but does not explicitly state when not to use it or name alternatives like 'get_recent_commands' for multiple commands. It implies usage based on recency.

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