Skip to main content
Glama

shell_netstat

Display network connection information to monitor active connections and troubleshoot network issues using command-line arguments.

Instructions

Network connection information

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
argsNoCommand arguments

Implementation Reference

  • Registration of the shell.netstat command in allowedCommands object. This config defines the tool's base command ('netstat'), description, allowed arguments for input validation, and execution timeout. The MCP tool name exposed is 'netstat' after stripping 'shell.' prefix.
    'shell.netstat': {
      command: 'netstat',
      description: 'Network connection information',
      allowedArgs: [
        '-a',     // all connections
        '-n',     // numeric addresses
        '-t',     // TCP
        '-u',     // UDP
        '-l',     // listening
        '-p',     // show process
        '--help'
      ],
      timeout: 3000
    },
  • Dynamically generates MCP tool definitions from allowedCommands. For 'shell.netstat', creates tool {name: 'netstat', description: ..., inputSchema: {args: array of strings}}
      const tools: Tool[] = [];
      const processedNames = new Set<string>();
    
      this.logger.debug('Starting to process tool list', { 
        commandCount: Object.keys(allowedCommands).length 
      });
    
      Object.entries(allowedCommands).forEach(([name, config]) => {
        const toolName = name.replace('shell.', '');
        this.logger.debug('Processing command', { 
          originalName: name,
          toolName,
          isProcessed: processedNames.has(toolName)
        });
    
        if (!processedNames.has(toolName)) {
          processedNames.add(toolName);
          tools.push({
            name: toolName,
            description: config.description,
            inputSchema: {
              type: "object",
              properties: {
                args: {
                  type: "array",
                  items: { type: "string" },
                  description: "Command arguments"
                }
              }
            }
          });
        }
      });
    
      this.logger.debug('Tool list processing completed', { 
        toolCount: tools.length,
        processedNames: Array.from(processedNames)
      });
    
      this.validateToolNames(tools);
      return tools;
    }
  • MCP CallTool request handler. For tool 'netstat', maps to fullCommand 'shell.netstat', retrieves config from allowedCommands, validates args against allowedArgs, executes via executor, collects stdout and returns as text content.
    this.server.setRequestHandler(CallToolRequestSchema, async (request, extra: unknown) => {
      const ext = extra as Extra;
      if (!request.params?.name) {
        throw new ToolError('MISSING_COMMAND', 'Command name is required');
      }
      
      const command = String(request.params.name);
      const fullCommand = command.startsWith('shell.') ? command : `shell.${command}`;
      
      if (!(fullCommand in allowedCommands)) {
        throw new ToolError('COMMAND_NOT_FOUND', 'Command not found', { command });
      }
      
      const config = allowedCommands[fullCommand];
      const args = Array.isArray(request.params.arguments?.args)
        ? request.params.arguments.args.map(String)
        : [];
    
      const context: CommandContext = {
        requestId: ext.id || 'unknown',
        command,
        args,
        timeout: config.timeout,
        workDir: config.workDir,
        env: config.env
      };
    
      this.logger.info('Starting command execution', context);
    
      try {
        this.validator.validateCommand(command, args);
        
        this.logger.debug('Command validation passed', {
          ...context,
          config
        });
    
        const stream = await this.executor.execute(command, args, {
          timeout: config.timeout,
          cwd: config.workDir,
          env: config.env
        });
    
        ext.onCancel?.(() => {
          this.logger.info('Received cancel request', context);
          this.executor.interrupt();
        });
    
        const output = await this.collectOutput(stream);
    
        this.logger.info('Command execution completed', {
          ...context,
          outputLength: output.length
        });
    
        return {
          content: [{
            type: "text",
            text: output
          }]
        };
    
      } catch (error) {
        this.logger.error('Command execution failed', {
          ...context,
          error: error instanceof Error ? error.message : String(error),
          stack: error instanceof Error ? error.stack : undefined
        });
        
        throw new ToolError(
          'EXECUTION_FAILED',
          `Command execution failed: ${error instanceof Error ? error.message : String(error)}`,
          context
        );
      }
    });
  • Executes the shell command. For 'shell.netstat', strips to 'netstat' and spawns child_process.spawn('netstat', args, options), returning stdout stream.
    async execute(
      command: string,
      args: string[] = [],
      options: ExecuteOptions = {}
    ): Promise<{ stdout: Readable }> {
      const commandKey = `${command} ${args.join(' ')}`;
      
      try {
        // Check security
        await this.securityChecker.validateCommand(command, args, options);
    
        // Check cache
        const cached = this.cache.get(commandKey);
        if (cached) {
          this.logger.debug('Using cached command result', { command, args });
          return this.createStreamFromCache(cached);
        }
    
        // Remove 'shell.' prefix for execution
        const baseCommand = command.replace('shell.', '');
    
        // Execute command
        this.logger.debug('Starting command execution', { command, args, options });
        const childProcess = spawn(baseCommand, args, {
          stdio: ['ignore', 'pipe', 'pipe'],
          timeout: options.timeout,
          cwd: options.cwd,
          env: {
            ...process.env,
            ...options.env
          },
          signal: options.signal
        });
    
        this.currentProcess = childProcess;
    
        // Error handling
        childProcess.on('error', (error: Error) => {
          this.logger.error('Command execution error', {
            command,
            args,
            error: error.message
          });
          throw new ToolError(
            'PROCESS_ERROR',
            'Command execution error',
            { command, args, error: error.message }
          );
        });
    
        // Timeout handling
        if (options.timeout) {
          setTimeout(() => {
            if (childProcess.exitCode === null) {
              this.logger.warn('Command execution timeout', {
                command,
                args,
                timeout: options.timeout
              });
              childProcess.kill();
              throw new ToolError(
                'TIMEOUT',
                'Command execution timeout',
                { command, args, timeout: options.timeout }
              );
            }
          }, options.timeout);
        }
    
        if (!childProcess.stdout) {
          throw new ToolError(
            'STREAM_ERROR',
            'Unable to get command output stream',
            { command, args }
          );
        }
    
        // Monitor process status
        childProcess.on('exit', (code, signal) => {
          this.logger.debug('Command execution completed', {
            command,
            args,
            exitCode: code,
            signal
          });
        });
    
        return {
          stdout: childProcess.stdout
        };
  • Validates input arguments for shell commands against the allowedArgs defined in the config for 'shell.netstat'. Ensures only permitted flags and safe paths are used.
    validateCommand(
      command: string, 
      args: string[] = [], 
      options: CommandOptions = {}
    ): void {
      console.log('Validating command:', {
        command,
        args,
        baseCommand: command.replace('shell.', ''),
        fullCommand: `shell.${command.replace('shell.', '')}`,
        config: allowedCommands[`shell.${command.replace('shell.', '')}`]
      });
    
      const baseCommand = command.replace('shell.', '');
      
      if (!(`shell.${baseCommand}` in allowedCommands)) {
        throw new Error(`Command not allowed: ${command}`);
      }
      
      const config = allowedCommands[`shell.${baseCommand}`];
      
      const allowedArgs = config.allowedArgs || [];
      
      console.log('Checking args:', {
        allowedArgs,
        hasWildcard: allowedArgs.includes('*')
      });
    
      args.forEach(arg => {
        if (arg.startsWith('-')) {
          if (!allowedArgs.includes(arg)) {
            console.log('Invalid option:', arg);
            throw new Error(`Invalid argument: ${arg}`);
          }
        }
        else if (!allowedArgs.includes('*')) {
          console.log('Path not allowed:', arg);
          throw new Error(`Invalid argument: ${arg}`);
        } else {
          // 檢查路徑參數
          this.validatePath(arg);
        }
      });
Behavior2/5

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

With no annotations provided, the description carries the full burden of behavioral disclosure but offers minimal information. It doesn't specify if this is a read-only operation, what permissions are required, how output is formatted, or potential side effects. The description is too brief to adequately inform the agent about behavioral traits.

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 extremely concise at just three words, with zero wasted text. It is front-loaded and efficiently communicates the core purpose without unnecessary elaboration, making it easy for an agent to parse quickly.

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 the complexity of a shell command tool with no annotations, no output schema, and minimal description, the description is incomplete. It doesn't explain what 'netstat' does specifically, what information is returned, or how to interpret results, leaving significant gaps for an agent to use the tool effectively.

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 single parameter 'args' documented as 'Command arguments'. The description adds no additional meaning about parameters, such as typical argument values or examples. Since the schema does the heavy lifting, the baseline score of 3 is appropriate.

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

Purpose3/5

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

The description 'Network connection information' states what the tool does at a high level but lacks specificity. It doesn't mention the verb (e.g., 'display', 'list', 'show') or differentiate from sibling tools like 'shell_ip' or 'shell_nslookup' that also provide network-related information. The purpose is clear but vague.

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. It doesn't mention context, prerequisites, or exclusions, leaving the agent to infer usage from the tool name alone among many shell siblings.

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/kevinwatt/shell-mcp'

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