import { z } from 'zod';
import { stopWda, getRunningWda } from '../executor/wda-runner.js';
export const wdaStopSchema = z.object({
port: z
.number()
.optional()
.describe('WDA server port to stop (default: 8100)'),
});
export type WdaStopInput = z.infer<typeof wdaStopSchema>;
export const wdaStopTool = {
name: 'wda_stop',
description:
'Stop a running WebDriverAgent server. Gracefully terminates the WDA process and releases the port.',
inputSchema: wdaStopSchema,
handler: async (input: WdaStopInput) => {
const port = input.port ?? 8100;
const running = getRunningWda();
const wdaProcess = running.get(port);
if (!wdaProcess) {
return {
content: [
{
type: 'text' as const,
text: `No WDA process found on port ${port}.\n\nCurrently running WDA processes: ${
running.size === 0 ? 'none' : Array.from(running.keys()).join(', ')
}`,
},
],
};
}
const deviceName = wdaProcess.deviceName;
const stopped = await stopWda(port);
if (stopped) {
return {
content: [
{
type: 'text' as const,
text: `WebDriverAgent stopped successfully\n\nPort: ${port}\nDevice: ${deviceName}`,
},
],
};
}
return {
content: [
{
type: 'text' as const,
text: `Failed to stop WDA on port ${port}`,
},
],
isError: true,
};
},
};