swift_package_stop
Stop a running Swift Package executable by providing its process ID (PID) to ensure proper termination on the sl-test server.
Instructions
Stops a running Swift Package executable started with swift_package_run
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| pid | Yes | Process ID (PID) of the running executable |
Implementation Reference
- src/tools/run-swift-package.ts:266-313 (handler)The core handler function for 'swift_package_stop' that terminates the process by PID using SIGTERM followed by SIGKILL if necessary, and cleans up the activeProcesses map.async (params: { pid: number }): Promise<ToolResponse> => { const processInfo = activeProcesses.get(params.pid); if (!processInfo) { return createTextResponse( `⚠️ No running process found with PID ${params.pid}. Use swift_package_run to check active processes.`, true, ); } try { processInfo.process.kill('SIGTERM'); // Give it 5 seconds to terminate gracefully await new Promise((resolve) => { let terminated = false; processInfo.process.on('exit', () => { terminated = true; resolve(true); }); setTimeout(() => { if (!terminated) { processInfo.process.kill('SIGKILL'); } resolve(true); }, 5000); }); activeProcesses.delete(params.pid); return { content: [ { type: 'text' as const, text: `✅ Stopped executable (was running since ${processInfo.startedAt.toISOString()})`, }, { type: 'text' as const, text: `💡 Process terminated. You can now run swift_package_run again if needed.`, }, ], }; } catch (error) { const message = error instanceof Error ? error.message : String(error); return createErrorResponse('Failed to stop process', message, 'SystemError'); } },
- Zod input schema defining the required 'pid' parameter as a number.{ pid: z.number().describe('Process ID (PID) of the running executable'), },
- src/tools/run-swift-package.ts:258-315 (registration)Local registration function registerStopSwiftPackageTool that sets up the tool 'swift_package_stop' with schema and handler using registerTool.export function registerStopSwiftPackageTool(server: McpServer): void { registerTool( server, 'swift_package_stop', 'Stops a running Swift Package executable started with swift_package_run', { pid: z.number().describe('Process ID (PID) of the running executable'), }, async (params: { pid: number }): Promise<ToolResponse> => { const processInfo = activeProcesses.get(params.pid); if (!processInfo) { return createTextResponse( `⚠️ No running process found with PID ${params.pid}. Use swift_package_run to check active processes.`, true, ); } try { processInfo.process.kill('SIGTERM'); // Give it 5 seconds to terminate gracefully await new Promise((resolve) => { let terminated = false; processInfo.process.on('exit', () => { terminated = true; resolve(true); }); setTimeout(() => { if (!terminated) { processInfo.process.kill('SIGKILL'); } resolve(true); }, 5000); }); activeProcesses.delete(params.pid); return { content: [ { type: 'text' as const, text: `✅ Stopped executable (was running since ${processInfo.startedAt.toISOString()})`, }, { type: 'text' as const, text: `💡 Process terminated. You can now run swift_package_run again if needed.`, }, ], }; } catch (error) { const message = error instanceof Error ? error.message : String(error); return createErrorResponse('Failed to stop process', message, 'SystemError'); } }, ); }
- src/utils/register-tools.ts:177-181 (registration)Top-level registration entry that conditionally calls registerStopSwiftPackageTool based on environment variable.register: registerStopSwiftPackageTool, groups: [ToolGroup.SWIFT_PACKAGE_WORKFLOW], envVar: 'XCODEBUILDMCP_TOOL_SWIFT_PACKAGE_STOP', isWriteTool: true, },
- src/tools/run-swift-package.ts:12-16 (helper)Global Map tracking active Swift package processes by PID, used by run, list, and stop tools.// Store active processes so we can manage them - keyed by PID for uniqueness const activeProcesses = new Map< number, { process: ChildProcess; startedAt: Date; packagePath: string; executableName?: string } >();