Get Last Command
get_last_commandRetrieve 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.
Tool call template
This tool declares no required parameters. Add optional parameters from the input schema as needed.
{
"id": 1,
"jsonrpc": "2.0",
"method": "tools/call",
"params": {
"arguments": {},
"name": "get_last_command"
}
}Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
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}`, }], }; } ); - index.js:29-76 (helper)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; }