list_running_processes
Retrieve and filter currently active processes on a Windows system using a command-line interface. Specify an optional filter to match process names for targeted results.
Instructions
List all running processes on the system. Can be filtered by providing an optional filter string that will match against process names.
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| filter | No | Optional filter string to match against process names |
Implementation Reference
- index.ts:56-99 (handler)The handler function that implements the core logic for listing running processes. It uses platform-specific commands (PowerShell on Windows, ps on Unix) with optional filtering and executes them via the executeCommand helper.async ({ filter }) => { try { let cmd; if (isWindows) { cmd = "powershell.exe -Command \"Get-Process"; if (filter) { // Add filter if provided cmd += ` | Where-Object { $_.ProcessName -like '*${filter}*' }`; } cmd += " | Select-Object Id, ProcessName, CPU, WorkingSet, Description | Format-Table -AutoSize | Out-String\""; } else { // Fallback for Unix systems cmd = "ps aux"; if (filter) { cmd += ` | grep -i ${filter}`; } } const stdout = executeCommand(cmd); return { content: [ { type: "text", text: stdout.toString(), }, ], }; } catch (error) { return { isError: true, content: [ { type: "text", text: `Error listing processes: ${error}`, }, ], }; } }
- index.ts:53-55 (schema)Zod schema defining the input parameters for the tool, including an optional filter string.{ filter: z.string().optional().describe("Optional filter string to match against process names"), },
- index.ts:50-100 (registration)The server.tool registration call that registers the list_running_processes tool with its description, input schema, and handler function.server.tool( "list_running_processes", "List all running processes on the system. Can be filtered by providing an optional filter string that will match against process names.", { filter: z.string().optional().describe("Optional filter string to match against process names"), }, async ({ filter }) => { try { let cmd; if (isWindows) { cmd = "powershell.exe -Command \"Get-Process"; if (filter) { // Add filter if provided cmd += ` | Where-Object { $_.ProcessName -like '*${filter}*' }`; } cmd += " | Select-Object Id, ProcessName, CPU, WorkingSet, Description | Format-Table -AutoSize | Out-String\""; } else { // Fallback for Unix systems cmd = "ps aux"; if (filter) { cmd += ` | grep -i ${filter}`; } } const stdout = executeCommand(cmd); return { content: [ { type: "text", text: stdout.toString(), }, ], }; } catch (error) { return { isError: true, content: [ { type: "text", text: `Error listing processes: ${error}`, }, ], }; } } );
- index.ts:17-47 (helper)Cross-platform helper function to execute shell commands, adapting Windows commands for non-Windows environments where possible.function executeCommand(command: string, options: any = {}) { if (isWindows) { return execSync(command, options); } else { // Log warning for non-Windows environments console.error(`Warning: Running in a non-Windows environment (${platform()}). Windows commands may not work.`); // For testing purposes on non-Windows platforms try { // For Linux/MacOS, we'll strip cmd.exe and powershell.exe references let modifiedCmd = command; // Replace cmd.exe /c with empty string modifiedCmd = modifiedCmd.replace(/cmd\.exe\s+\/c\s+/i, ''); // Replace powershell.exe -Command with empty string or a compatible command modifiedCmd = modifiedCmd.replace(/powershell\.exe\s+-Command\s+("|')/i, ''); // Remove trailing quotes if we removed powershell -Command if (modifiedCmd !== command) { modifiedCmd = modifiedCmd.replace(/("|')$/, ''); } console.error(`Attempting to execute modified command: ${modifiedCmd}`); return execSync(modifiedCmd, options); } catch (error) { console.error(`Error executing modified command: ${error}`); return Buffer.from(`This tool requires a Windows environment. Current platform: ${platform()}`); } } }