Files
mcp-windows/src/tools/process_kill.ts
T

58 lines
1.6 KiB
TypeScript
Raw Normal View History

/* Copyright (C) 2026 Moko Consulting <hello@mokoconsulting.tech>
* SPDX-License-Identifier: GPL-3.0-or-later
*
* Tool: windows_process_kill (#3)
*/
import { McpServer } from '@modelcontextprotocol/sdk/server/mcp.js';
import { z } from 'zod';
import { runPowerShell } from '../shell.js';
export function registerProcessKillTools(server: McpServer): void {
server.tool(
'windows_process_kill',
'Terminate running processes by PID or name.',
{
pid: z.number().optional().describe('Process ID to kill'),
name: z.string().optional().describe('Process name to kill (all matching)'),
force: z.boolean().default(false).describe('Force termination'),
},
async ({ pid, name, force }) => {
if (!pid && !name) {
return { content: [{ type: 'text', text: 'Provide either pid or name.' }], isError: true };
}
const forceFlag = force ? ' -Force' : '';
let ps: string;
if (pid) {
ps = `
$p = Get-Process -Id ${pid} -ErrorAction SilentlyContinue
if ($p) {
$n = $p.ProcessName
Stop-Process -Id ${pid}${forceFlag} -ErrorAction Stop
"Killed PID ${pid} ($n)"
} else {
"No process with PID ${pid}"
}`;
} else {
ps = `
$procs = Get-Process -Name '${name!.replace(/'/g, "''")}' -ErrorAction SilentlyContinue
if ($procs) {
$count = @($procs).Count
$procs | Stop-Process${forceFlag} -ErrorAction Stop
"Killed $count process(es) named '${name}'"
} else {
"No processes named '${name}'"
}`;
}
const result = await runPowerShell(ps);
return {
content: [{ type: 'text', text: result.stdout || result.stderr }],
isError: result.exitCode !== 0,
};
},
);
}