Skip to main content
Glama

Get Recent Commands

get_recent_commands

Retrieve recent terminal commands and their outputs to analyze execution history without manual copy-pasting.

Instructions

Get the last N terminal commands and their outputs.

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
countNoNumber of recent commands to retrieve (1-20)

Implementation Reference

  • The handler function for the get_recent_commands tool. It parses the terminal log using parseLogFile, retrieves the last N (default 5) commands, formats each with exit status, command, and output, and returns them as a single text content block.
    async ({ count = 5 }) => {
      const entries = parseLogFile();
      
      if (entries.length === 0) {
        return {
          content: [{
            type: 'text',
            text: 'No terminal history found.',
          }],
        };
      }
    
      const recent = entries.slice(-count);
      const formatted = recent.map((entry, i) => {
        const exitStatus = entry.exitCode === 0 ? '✓' : `✗ (${entry.exitCode})`;
        return `[${i + 1}] ${exitStatus} $ ${entry.command}\n${entry.output}`;
      }).join('\n\n---\n\n');
      
      return {
        content: [{
          type: 'text',
          text: `Last ${recent.length} commands:\n\n${formatted}`,
        }],
      };
    }
  • index.js:151-185 (registration)
    Registration of the get_recent_commands tool using server.registerTool, including name, metadata (title, description), input schema, and handler function.
    server.registerTool(
      'get_recent_commands',
      {
        title: 'Get Recent Commands',
        description: 'Get the last N terminal commands and their outputs.',
        inputSchema: {
          count: z.number().min(1).max(20).default(5).describe('Number of recent commands to retrieve (1-20)'),
        },
      },
      async ({ count = 5 }) => {
        const entries = parseLogFile();
        
        if (entries.length === 0) {
          return {
            content: [{
              type: 'text',
              text: 'No terminal history found.',
            }],
          };
        }
    
        const recent = entries.slice(-count);
        const formatted = recent.map((entry, i) => {
          const exitStatus = entry.exitCode === 0 ? '✓' : `✗ (${entry.exitCode})`;
          return `[${i + 1}] ${exitStatus} $ ${entry.command}\n${entry.output}`;
        }).join('\n\n---\n\n');
        
        return {
          content: [{
            type: 'text',
            text: `Last ${recent.length} commands:\n\n${formatted}`,
          }],
        };
      }
    );
  • Input schema for the tool using Zod to validate the 'count' parameter (number between 1-20, default 5).
    inputSchema: {
      count: z.number().min(1).max(20).default(5).describe('Number of recent commands to retrieve (1-20)'),
    },
  • Helper function parseLogFile that reads and parses the terminal history log file into an array of command entries, each containing command, output, exitCode, and optional timestamp.
    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?

With no annotations provided, the description carries full burden but offers minimal behavioral context. It states what the tool retrieves but doesn't disclose important traits like whether it's read-only, how commands are ordered (e.g., chronological), if outputs are truncated, or any rate limits. This is inadequate for a tool with potential complexity in command history retrieval.

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, efficient sentence that front-loads the core purpose without unnecessary words. Every part earns its place, making it highly concise and well-structured for quick understanding.

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

Completeness2/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 incomplete for a tool that retrieves historical data. It lacks details on return format (e.g., list structure, timestamps), error handling, or how 'recent' is defined (e.g., time-based vs. count-based), leaving significant gaps for the agent.

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

Parameters3/5

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

The schema description coverage is 100%, with the parameter 'count' fully documented in the schema. The description adds no additional parameter semantics beyond implying 'N' corresponds to 'count', so it meets the baseline of 3 without compensating value.

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

Purpose4/5

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

The description clearly states the verb ('Get') and resource ('last N terminal commands and their outputs'), making the purpose immediately understandable. However, it doesn't explicitly differentiate from sibling tools like 'get_last_command' or 'search_output', which prevents a perfect score.

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

Usage Guidelines2/5

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

The description provides no guidance on when to use this tool versus alternatives like 'get_last_command' or 'search_output'. There's no mention of context, prerequisites, or exclusions, leaving the agent to infer usage from tool names alone.

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