Skip to main content
Glama

Execute a command in WSL (use read-only tools when possible)

execute_command
Destructive

Execute commands in Windows Subsystem for Linux environments with security validation to prevent shell injection and dangerous operations.

Instructions

Execute a command in WSL (use read-only tools when possible)

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
commandYesCommand to execute
working_dirNoWorking directory
timeoutNoTimeout (ms)

Implementation Reference

  • src/index.ts:324-395 (registration)
    Complete MCP tool registration for 'execute_command', defining name, description, input schema, annotations, and the handler function that manages execution with safety checks.
    // execute_command tool - potentially destructive
    this.server.tool(
    	{
    		name: 'execute_command',
    		description: 'Execute a command in WSL (use read-only tools when possible)',
    		schema: v.object({
    			command: v.pipe(
    				v.string(),
    				v.description('Command to execute'),
    			),
    			working_dir: v.optional(
    				v.pipe(
    					v.string(),
    					v.description('Working directory'),
    				),
    			),
    			timeout: v.optional(
    				v.pipe(
    					v.number(),
    					v.description('Timeout (ms)'),
    				),
    			),
    		}),
    		annotations: {
    			readOnlyHint: false,
    			destructiveHint: true,
    		},
    	},
    	async ({ command, working_dir, timeout }) => {
    		try {
    			const result = await this.execute_wsl_command(
    				command,
    				working_dir,
    				timeout,
    			);
    
    			if (result.requires_confirmation) {
    				return {
    					content: [
    						{
    							type: 'text' as const,
    							text: result.stderr,
    						},
    					],
    				};
    			}
    
    			return {
    				content: [
    					{
    						type: 'text' as const,
    						text: this.format_output(result),
    					},
    				],
    			};
    		} catch (error) {
    			return {
    				content: [
    					{
    						type: 'text' as const,
    						text: `Error executing command: ${
    							error instanceof Error
    								? error.message
    								: String(error)
    						}`,
    					},
    				],
    				isError: true,
    			};
    		}
    	},
    );
  • Low-level handler in CommandExecutor class that performs the actual command execution: sanitizes inputs, spawns WSL process, captures output, handles timeout and errors.
    public async execute_command(
    	command: string,
    	working_dir?: string,
    	timeout?: number,
    ): Promise<CommandResponse> {
    	return new Promise((resolve, reject) => {
    		const sanitized_command = this.sanitize_command(command);
    		const validated_dir = this.validate_working_dir(working_dir);
    		const validated_timeout = this.validate_timeout(timeout);
    
    		const cd_command = validated_dir ? `cd "${validated_dir}" && ` : '';
    		const full_command = `${cd_command}${sanitized_command}`;
    
    		const wsl_process = spawn(wsl_config.executable, [
    			'--exec',
    			wsl_config.shell,
    			'-c',
    			full_command,
    		]);
    
    		let stdout = '';
    		let stderr = '';
    
    		wsl_process.stdout.on('data', (data) => {
    			stdout += data.toString();
    		});
    
    		wsl_process.stderr.on('data', (data) => {
    			stderr += data.toString();
    		});
    
    		let timeout_id: NodeJS.Timeout | undefined;
    		if (validated_timeout) {
    			timeout_id = setTimeout(() => {
    				wsl_process.kill();
    				reject(new CommandTimeoutError(validated_timeout));
    			}, validated_timeout);
    		}
    
    		wsl_process.on('close', (code) => {
    			if (timeout_id) {
    				clearTimeout(timeout_id);
    			}
    			resolve({
    				stdout,
    				stderr,
    				exit_code: code,
    				command: sanitized_command,
    				working_dir: validated_dir,
    			});
    		});
    
    		wsl_process.on('error', (error) => {
    			if (timeout_id) {
    				clearTimeout(timeout_id);
    			}
    			reject(error);
    		});
    	});
    }
  • Type definition for CommandResponse, used as the return type for command execution results.
    export interface CommandResponse {
    	stdout: string;
    	stderr: string;
    	exit_code: number | null;
    	command: string;
    	requires_confirmation?: boolean;
    	error?: string;
    	working_dir?: string;
  • Helper method to detect potentially dangerous commands requiring confirmation.
    public is_dangerous_command(command: string): boolean {
    	return dangerous_commands.some(
    		(dangerous) =>
    			command.toLowerCase().includes(dangerous.toLowerCase()) ||
    			command.match(new RegExp(`\\b${dangerous}\\b`, 'i')),
    	);
    }
  • Valibot input schema defining parameters for the execute_command tool.
    schema: v.object({
    	command: v.pipe(
    		v.string(),
    		v.description('Command to execute'),
    	),
    	working_dir: v.optional(
    		v.pipe(
    			v.string(),
    			v.description('Working directory'),
    		),
    	),
    	timeout: v.optional(
    		v.pipe(
    			v.number(),
    			v.description('Timeout (ms)'),
    		),
    	),
    }),
Behavior3/5

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

Annotations indicate destructiveHint=true and readOnlyHint=false, which the description doesn't contradict. However, the description adds minimal behavioral context beyond annotations—it doesn't explain what makes commands destructive, potential side effects, or execution constraints like permissions or rate limits. The 'use read-only tools when possible' hint adds some caution but lacks specificity.

Agents need to know what a tool does to the world before calling it. Descriptions should go beyond structured annotations to explain consequences.

Conciseness4/5

Is the description appropriately sized, front-loaded, and free of redundancy?

The description is very brief—a single sentence with a parenthetical note. It's front-loaded with the core purpose, but the parenthetical feels tacked on and doesn't integrate smoothly. While concise, it could be more structured to separate purpose from guidance.

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 destructive nature (per annotations) and lack of output schema, the description is insufficient. It doesn't explain what the tool returns, error conditions, or safety considerations, leaving gaps for a mutation tool with potential side effects. The context signals don't compensate for these omissions.

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?

With 100% schema description coverage, the input schema fully documents all three parameters. The description adds no parameter-specific information beyond what's in the schema, so it meets the baseline of 3 without compensating or adding value.

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

Purpose2/5

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

Tautological: description restates name/title.

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. While it mentions 'use read-only tools when possible,' this is vague advice that doesn't specify concrete alternatives or scenarios where this tool should be preferred or avoided compared to siblings like get_directory_info or list_processes.

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/spences10/mcp-wsl-exec'

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