Skip to main content
Glama
HasanJahidul

Terminal History MCP

command_chains

Returns commands executed within ±5 minutes of a matching query, revealing multi-step sequences like cd, build, deploy.

Instructions

For each match of query, return commands run within ±5 min — reveals multi-step sequences (cd → build → deploy).

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
queryYes
window_msNo
limitNo

Implementation Reference

  • The core handler function `commandChains` that executes the tool logic: searches for matching commands (seeds), then for each seed fetches all commands within a ±windowMs time range, returning groups of related commands as chains.
    export function commandChains(db: Database.Database, query: string, windowMs = 5 * 60 * 1000, limit = 10): SearchRow[][] {
      const seeds = searchHistory(db, query, limit);
      const out: SearchRow[][] = [];
      const ctx = db.prepare(`
        SELECT id, cmd, ts, shell, cwd, exit_code, duration_ms FROM commands
        WHERE ts BETWEEN ? AND ? ORDER BY ts ASC
      `);
      for (const s of seeds) {
        if (s.ts == null) { out.push([s]); continue; }
        const rows = ctx.all(s.ts - windowMs, s.ts + windowMs) as SearchRow[];
        out.push(rows);
      }
      return out;
    }
  • src/index.ts:78-91 (registration)
    Registration of the 'command_chains' tool in the TOOLS array, defining its name, description, and input schema.
      {
        name: "command_chains",
        description: "For each match of query, return commands run within ±5 min — reveals multi-step sequences (cd → build → deploy).",
        inputSchema: {
          type: "object",
          properties: {
            query: { type: "string" },
            window_ms: { type: "number", default: 300000 },
            limit: { type: "number", default: 5 },
          },
          required: ["query"],
        },
      },
    ];
  • The request handler that parses arguments (query, window_ms, limit) with Zod, calls commandChains(), formats results, and returns them as tool content.
    if (name === "command_chains") {
      const { query, window_ms, limit } = z.object({
        query: z.string(), window_ms: z.number().optional(), limit: z.number().optional(),
      }).parse(args);
      const chains = commandChains(db, query, window_ms ?? 300000, limit ?? 5);
      const text = chains.map((c, i) => `--- chain ${i + 1} ---\n${fmt(c)}`).join("\n\n");
      return { content: [{ type: "text", text: text || "(no chains)" }] };
    }
  • Input schema definition for the command_chains tool: query (string, required), window_ms (number, default 300000), limit (number, default 5).
        type: "object",
        properties: {
          query: { type: "string" },
          window_ms: { type: "number", default: 300000 },
          limit: { type: "number", default: 5 },
        },
        required: ["query"],
      },
    },
  • The `searchHistory` helper function is used internally by `commandChains` to find seed commands matching the query.
    export function searchHistory(db: Database.Database, query: string, limit = 20): SearchRow[] {
      const fts = escapeFts(query);
      if (!fts) return [];
      const stmt = db.prepare(`
        SELECT c.id, c.cmd, c.ts, c.shell, c.cwd, c.exit_code, c.duration_ms, commands_fts.rank AS rank
        FROM commands_fts
        JOIN commands c ON c.id = commands_fts.rowid
        WHERE commands_fts MATCH ?
        ORDER BY rank LIMIT ?
      `);
      return stmt.all(fts, limit) as SearchRow[];
    }
Behavior3/5

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

With no annotations provided, the description must fully disclose behavior. It discloses the time-window retrieval and the intent to reveal sequences, implying a read-only operation. However, it does not mention the return format, pagination, sorting, or behavior when no matches are found. It also omits any details about authentication or resource impact, leaving some uncertainty about exact behavior.

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 communicates core functionality. It includes a clarifying example in parentheses without extra fluff. Every word contributes value, making it highly concise and easy to parse.

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 the tool has 3 parameters, no output schema, and no annotations, the description explains the main feature (time-window matching) but lacks details about the 'limit' parameter, output format, and edge cases (e.g., no matches). It also doesn't clarify whether the tool is purely read-only or has any side effects. While adequate for basic understanding, it leaves several aspects unexplained.

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 input schema has 0% description coverage, so the description must compensate. It explicitly explains the 'query' parameter ('for each match of query') and hints at 'window_ms' via '±5 min'. However, it does not explain the 'limit' parameter or the exact meaning of 'window_ms' beyond the default. This partial coverage meets the baseline but leaves gaps for two parameters.

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?

Description clearly states the tool's purpose: for each match of a query, return commands run within a ±5 minute window, revealing multi-step sequences. The example (cd → build → deploy) further clarifies the intent. It distinguishes itself from sibling tools like failed_commands, recent_in_dir, and search_history by focusing on contextual grouping around matches rather than just listing or filtering individual commands.

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?

No guidance is provided on when to use this tool versus alternatives like failed_commands, recent_in_dir, or search_history. There is no mention of prerequisites, preferred scenarios, or situations where this tool is not appropriate. The description only explains functionality without contextual usage advice.

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/HasanJahidul/terminal-history-mcp'

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