run_powershell
Execute PowerShell commands securely with configurable timeouts for enterprise automation, system monitoring, and management platform script generation.
Instructions
Execute PowerShell commands securely.
Args:
code: PowerShell code to execute
timeout: Command timeout in seconds (1-300, default 60)
ctx: MCP context for logging and progress reporting
Returns:
Command output as string
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| code | Yes | ||
| timeout | No | ||
| ctx | No |
Implementation Reference
- run.py:45-72 (registration)Registers the run_powershell tool including its input schema in the ListTools response.@server.setRequestHandler(ListToolsRequestSchema) async def handle_list_tools(): return { "tools": [ { "name": "run_powershell", "description": "Execute PowerShell commands securely", "inputSchema": { "type": "object", "properties": { "code": { "type": "string", "description": "PowerShell code to execute" }, "timeout": { "type": "integer", "description": "Command timeout in seconds", "minimum": 1, "maximum": 300, "default": 60 } }, "required": ["code"] } } ] }
- run.py:53-69 (schema)Input schema definition for the run_powershell tool."type": "object", "properties": { "code": { "type": "string", "description": "PowerShell code to execute" }, "timeout": { "type": "integer", "description": "Command timeout in seconds", "minimum": 1, "maximum": 300, "default": 60 } }, "required": ["code"] } }
- run.py:74-139 (handler)Main handler for run_powershell tool: validates input, executes PowerShell command with timeout handling, returns stdout or errors.@server.setRequestHandler(CallToolRequestSchema) async def handle_tool_call(request): if request.params.name != "run_powershell": raise McpError( ErrorCode.MethodNotFound, f"Unknown tool: {request.params.name}" ) code = request.params.arguments.get("code") if not isinstance(code, str): raise McpError( ErrorCode.InvalidParams, "code parameter must be a string" ) timeout = request.params.arguments.get("timeout", 60) if not isinstance(timeout, int) or timeout < 1 or timeout > 300: raise McpError( ErrorCode.InvalidParams, "timeout must be between 1 and 300 seconds" ) try: process = await asyncio.create_subprocess_exec( "powershell", "-NoProfile", "-NonInteractive", "-Command", code, stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True ) stdout, stderr = await asyncio.wait_for( process.communicate(), timeout=timeout ) if process.returncode != 0: raise McpError( ErrorCode.InternalError, stderr or "Command failed with no error output" ) return { "content": [ { "type": "text", "text": stdout } ] } except asyncio.TimeoutError: process.kill() raise McpError( ErrorCode.Timeout, f"Command timed out after {timeout} seconds" ) except Exception as e: raise McpError( ErrorCode.InternalError, str(e) )