Skip to main content
Glama
malaksedarous

Context Optimizer MCP Server

runAndExtract

Execute terminal commands and extract specific information from output using natural language prompts. Optimizes token usage by retrieving only relevant data.

Instructions

Execute terminal commands and intelligently extract specific information from their output. Supports cross-platform command execution with security controls.

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
terminalCommandYesShell command to execute. Must be non-interactive (no user input prompts). Navigation commands (cd, pushd, etc.) are not allowed - use workingDirectory instead.
extractionPromptYesNatural language description of what information to extract from the command output. Examples: "Show me the raw output", "Summarize the results", "Extract all error messages", "Find version numbers", "List all files", "Did the command succeed?", "Are there any warnings?"
workingDirectoryYesFull absolute path where command should be executed (e.g., "C:\Users\username\project", "/home/user/project"). Must be within configured security boundaries.

Implementation Reference

  • Main execution handler for the runAndExtract tool. Validates inputs (terminalCommand, extractionPrompt, workingDirectory), validates directory path and command security, executes the shell command via execa, processes output through an LLM using the extraction prompt, saves the session for follow-up questions, and returns the extracted information.
    async execute(args: any): Promise<MCPToolResponse> {
      try {
        this.logOperation('Terminal execution started', {
          command: args.terminalCommand,
          workingDirectory: args.workingDirectory
        });
        
        // Validate required fields
        const fieldError = this.validateRequiredFields(args, ['terminalCommand', 'extractionPrompt', 'workingDirectory']);
        if (fieldError) {
          return this.createErrorResponse(fieldError);
        }
        
        // Validate working directory
        const dirValidation = await PathValidator.validateWorkingDirectory(args.workingDirectory);
        if (!dirValidation.valid) {
          return this.createErrorResponse(dirValidation.error!);
        }
        
        // Validate command security
        const commandValidation = CommandValidator.validateCommand(args.terminalCommand);
        if (!commandValidation.valid) {
          return this.createErrorResponse(commandValidation.error!);
        }
        
        // Log warnings if any
        if (commandValidation.warnings) {
          for (const warning of commandValidation.warnings) {
            Logger.warn(`${warning}`);
          }
        }
        
        // Execute command
        const executionResult = await this.executeCommand(
          args.terminalCommand,
          dirValidation.resolvedPath!
        );
        
        // Process output with LLM
        const extractedInfo = await this.processOutputWithLLM(
          args.terminalCommand,
          executionResult.output,
          executionResult.exitCode,
          args.extractionPrompt
        );
        
        // Save session for follow-up questions
        await SessionManager.saveTerminalSession({
          command: args.terminalCommand,
          output: executionResult.output,
          exitCode: executionResult.exitCode,
          extractionPrompt: args.extractionPrompt,
          extractedInfo,
          timestamp: new Date().toISOString(),
          workingDirectory: dirValidation.resolvedPath!
        });
        
        this.logOperation('Terminal execution completed successfully');
        return this.createSuccessResponse(extractedInfo);
        
      } catch (error) {
        this.logOperation('Terminal execution failed', { error });
        return this.createErrorResponse(
          `Terminal execution failed: ${error instanceof Error ? error.message : String(error)}`
        );
      }
    }
  • Input schema for runAndExtract tool. Requires three string fields: terminalCommand (shell command to execute), extractionPrompt (natural language description of what to extract), and workingDirectory (absolute path for execution).
    readonly inputSchema = {
      type: 'object',
      properties: {
        terminalCommand: {
          type: 'string',
          description: 'Shell command to execute. Must be non-interactive (no user input prompts). Navigation commands (cd, pushd, etc.) are not allowed - use workingDirectory instead.'
        },
        extractionPrompt: {
          type: 'string',
          description: 'Natural language description of what information to extract from the command output. Examples: "Show me the raw output", "Summarize the results", "Extract all error messages", "Find version numbers", "List all files", "Did the command succeed?", "Are there any warnings?"'
        },
        workingDirectory: {
          type: 'string',
          description: 'Full absolute path where command should be executed (e.g., "C:\\Users\\username\\project", "/home/user/project"). Must be within configured security boundaries.'
        }
      },
      required: ['terminalCommand', 'extractionPrompt', 'workingDirectory']
    };
  • src/server.ts:60-63 (registration)
    Registration of RunAndExtractTool in the server's setupTools() method. The tool is instantiated as new RunAndExtractTool() and added to the tools map, making it available to MCP clients.
    private setupTools(): void {
      const toolInstances = [
        new AskAboutFileTool(),
        new RunAndExtractTool(),
  • Private helper executeCommand that runs the shell command using execa with shell:true, cross-platform support, configurable timeout, and captures both stdout and stderr. Handles timeout errors and returns partial results on failure.
    private async executeCommand(command: string, workingDirectory: string): Promise<{
      output: string;
      exitCode: number;
      success: boolean;
    }> {
      const config = ConfigurationManager.getConfig();
      
      try {
        const result = await execa(command, {
          shell: true,
          cwd: workingDirectory,
          timeout: config.security.commandTimeout,
          all: true,                           // Capture both stdout and stderr
          reject: false                        // Don't throw on non-zero exit codes
        });
        
        return {
          output: result.all || result.stdout || result.stderr || '',
          exitCode: result.exitCode || 0,
          success: result.exitCode === 0
        };
      } catch (error: any) {
        if (error.timedOut) {
          throw new Error(`Command timed out after ${config.security.commandTimeout}ms. This may indicate an interactive command or infinite loop.`);
        }
        
        // Return partial results if available
        return {
          output: error.all || error.stdout || error.stderr || `Command failed: ${error.message}`,
          exitCode: error.exitCode || 1,
          success: false
        };
      }
    }
  • Private helper processOutputWithLLM that sends the command output to an LLM provider for intelligent extraction. Uses createTerminalExtractionPrompt to build the prompt with instructions for summarizing/extracting based on the user's natural language extractionPrompt.
      private async processOutputWithLLM(
        command: string,
        output: string,
        exitCode: number,
        extractionPrompt: string
      ): Promise<string> {
        const config = ConfigurationManager.getConfig();
        const provider = LLMProviderFactory.createProvider(config.llm.provider);
        const apiKey = this.getApiKey(config.llm.provider, config.llm);
        
        const prompt = this.createTerminalExtractionPrompt(output, extractionPrompt, command, exitCode);
        const response = await provider.processRequest(prompt, config.llm.model, apiKey);
        
        if (!response.success) {
          throw new Error(`LLM processing failed: ${response.error}`);
        }
        
        return response.content;
      }
      
      private createTerminalExtractionPrompt(
        commandOutput: string,
        extractionPrompt: string,
        terminalCommand: string,
        exitCode: number
      ): string {
        return `You are an expert at summarizing terminal command output and extracting specific information.
    
    Command executed: ${terminalCommand}
    Exit code: ${exitCode}
    Extraction request: ${extractionPrompt}
    
    Instructions:
    - Focus only on what the user specifically requested
    - Be concise and well-formatted
    - Use markdown formatting for better readability
    - If the command failed (non-zero exit code), mention this clearly
    - If there's no relevant information, say so clearly
    
    Command output:
    ${commandOutput}`;
      }
Behavior2/5

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

No annotations provided, so description must fully disclose behavior. Mentions security controls and extraction, but doesn't explain output format, error handling, or limitations of the extraction process.

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, no redundant information, purpose is front-loaded and clear.

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?

Three required parameters, no output schema, no annotations. Description does not explain what the tool returns or how extraction results are structured, 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?

Schema coverage is 100% with descriptions for each parameter. The description adds little beyond restating purpose; no new parameter meaning.

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 specific verb 'Execute terminal commands' and resource 'output extraction', distinguishing it from sibling tools which are about asking questions or research.

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

Usage Guidelines3/5

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

Implies usage through security controls and cross-platform support, but lacks explicit when-to-use, when-not-to-use, or alternative tool references.

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/malaksedarous/context-optimizer-mcp-server'

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