get_service_info
Retrieve Windows service information, including details or status of specific services, using secure command-line interaction on the Windows Command Line MCP Server.
Instructions
Retrieve information about Windows services. Can query all services or get detailed status of a specific service.
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| action | No | Action to perform | query |
| serviceName | No | Service name to get info about (optional) |
Implementation Reference
- index.ts:333-392 (handler)The main handler function for the 'get_service_info' tool. It constructs and executes PowerShell commands to query Windows services based on the 'action' ('query' or 'status') and optional 'serviceName'. Returns service information or error details.async ({ action, serviceName }) => { if (!isWindows) { return { content: [ { type: "text", text: "The service info tool is only available on Windows. Current platform: " + platform(), }, ], }; } try { let cmd = "powershell.exe -Command \""; if (action === "query") { if (serviceName) { cmd += "Get-Service -Name '" + serviceName + "' | Format-List Name, DisplayName, Status, StartType, Description"; } else { cmd += "Get-Service | Select-Object Name, DisplayName, Status, StartType | Format-Table -AutoSize | Out-String"; } } else if (action === "status" && serviceName) { cmd += "Get-Service -Name '" + serviceName + "' | Format-List *; " + "Get-CimInstance -ClassName Win32_Service -Filter \"Name='" + serviceName + "'\" | Format-List *"; } else { return { isError: true, content: [ { type: "text", text: "For 'status' action, serviceName parameter is required", }, ], }; } cmd += "\""; const stdout = executeCommand(cmd); return { content: [ { type: "text", text: stdout.toString(), }, ], }; } catch (error) { return { isError: true, content: [ { type: "text", text: `Error retrieving service info: ${error}`, }, ], }; } }
- index.ts:329-332 (schema)Input schema for the 'get_service_info' tool using Zod validation. Defines 'action' as enum query/status (default query) and optional 'serviceName' string.{ action: z.enum(["query", "status"]).default("query").describe("Action to perform"), serviceName: z.string().optional().describe("Service name to get info about (optional)"), },
- index.ts:326-393 (registration)Registration of the 'get_service_info' tool with MCP server using server.tool(). Includes name, description, input schema, and handler function.server.tool( "get_service_info", "Retrieve information about Windows services. Can query all services or get detailed status of a specific service.", { action: z.enum(["query", "status"]).default("query").describe("Action to perform"), serviceName: z.string().optional().describe("Service name to get info about (optional)"), }, async ({ action, serviceName }) => { if (!isWindows) { return { content: [ { type: "text", text: "The service info tool is only available on Windows. Current platform: " + platform(), }, ], }; } try { let cmd = "powershell.exe -Command \""; if (action === "query") { if (serviceName) { cmd += "Get-Service -Name '" + serviceName + "' | Format-List Name, DisplayName, Status, StartType, Description"; } else { cmd += "Get-Service | Select-Object Name, DisplayName, Status, StartType | Format-Table -AutoSize | Out-String"; } } else if (action === "status" && serviceName) { cmd += "Get-Service -Name '" + serviceName + "' | Format-List *; " + "Get-CimInstance -ClassName Win32_Service -Filter \"Name='" + serviceName + "'\" | Format-List *"; } else { return { isError: true, content: [ { type: "text", text: "For 'status' action, serviceName parameter is required", }, ], }; } cmd += "\""; const stdout = executeCommand(cmd); return { content: [ { type: "text", text: stdout.toString(), }, ], }; } catch (error) { return { isError: true, content: [ { type: "text", text: `Error retrieving service info: ${error}`, }, ], }; } } );
- index.ts:17-47 (helper)Shared helper function 'executeCommand' used by get_service_info to run the constructed PowerShell commands cross-platform.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()}`); } } }