|
| 1 | +/** |
| 2 | + * Log Tools - Functions for capturing and managing iOS simulator logs |
| 3 | + * |
| 4 | + * This module provides tools for capturing and managing logs from iOS simulators. |
| 5 | + * It supports starting and stopping log capture sessions, and retrieving captured logs. |
| 6 | + * |
| 7 | + * Responsibilities: |
| 8 | + * - Starting and stopping log capture sessions |
| 9 | + * - Managing in-memory log sessions |
| 10 | + * - Retrieving captured logs |
| 11 | + */ |
| 12 | + |
| 13 | +import { startLogCapture, stopLogCapture } from '../utils/log_capture.js'; |
| 14 | +import { z } from 'zod'; |
| 15 | +import { McpServer } from '@modelcontextprotocol/sdk/server/mcp.js'; |
| 16 | +import { ToolResponse } from '../types/common.js'; |
| 17 | +import { validateRequiredParam } from '../utils/validation.js'; |
| 18 | +import { registerTool, createTextContent } from './common.js'; |
| 19 | + |
| 20 | +/** |
| 21 | + * Registers the tool to start capturing logs from an iOS simulator. |
| 22 | + * |
| 23 | + * @param server The MCP Server instance. |
| 24 | + */ |
| 25 | +export function registerStartSimulatorLogCaptureTool(server: McpServer): void { |
| 26 | + const schema = { |
| 27 | + simulatorUuid: z |
| 28 | + .string() |
| 29 | + .describe('UUID of the simulator to capture logs from (obtained from list_simulators).'), |
| 30 | + bundleId: z.string().describe('Bundle identifier of the app to capture logs for.'), |
| 31 | + captureConsole: z |
| 32 | + .boolean() |
| 33 | + .optional() |
| 34 | + .default(false) |
| 35 | + .describe('Whether to capture console output (requires app relaunch).'), |
| 36 | + }; |
| 37 | + |
| 38 | + async function handler(params: { |
| 39 | + simulatorUuid: string; |
| 40 | + bundleId: string; |
| 41 | + captureConsole?: boolean; |
| 42 | + }): Promise<ToolResponse> { |
| 43 | + const validationResult = validateRequiredParam('simulatorUuid', params.simulatorUuid); |
| 44 | + if (!validationResult.isValid) { |
| 45 | + return validationResult.errorResponse!; |
| 46 | + } |
| 47 | + |
| 48 | + const { sessionId, error } = await startLogCapture(params); |
| 49 | + if (error) { |
| 50 | + return { |
| 51 | + content: [createTextContent(`Error starting log capture: ${error}`)], |
| 52 | + isError: true, |
| 53 | + }; |
| 54 | + } |
| 55 | + return { |
| 56 | + content: [ |
| 57 | + createTextContent( |
| 58 | + `Log capture started successfully. Session ID: ${sessionId}.\n\n${params.captureConsole ? 'Note: Your app was relaunched to capture console output.' : 'Note: Only structured logs are being captured.'}\n\nNext Steps:\n1. Interact with your simulator and app.\n2. Use 'stop_and_get_simulator_log' with session ID '${sessionId}' to stop capture and retrieve logs.`, |
| 59 | + ), |
| 60 | + ], |
| 61 | + }; |
| 62 | + } |
| 63 | + |
| 64 | + registerTool( |
| 65 | + server, |
| 66 | + 'start_simulator_log_capture', |
| 67 | + 'Starts capturing logs from a specified simulator. Returns a session ID. By default, captures only structured logs. Use captureConsole:true to also capture console output (will relaunch the app).', |
| 68 | + schema, |
| 69 | + handler, |
| 70 | + ); |
| 71 | +} |
| 72 | + |
| 73 | +/** |
| 74 | + * Registers the tool to stop log capture and retrieve the content in one operation. |
| 75 | + * |
| 76 | + * @param server The MCP Server instance. |
| 77 | + */ |
| 78 | +export function registerStopAndGetSimulatorLogTool(server: McpServer): void { |
| 79 | + const schema = { |
| 80 | + logSessionId: z.string().describe('The session ID returned by start_simulator_log_capture.'), |
| 81 | + }; |
| 82 | + |
| 83 | + async function handler(params: { logSessionId: string }): Promise<ToolResponse> { |
| 84 | + const validationResult = validateRequiredParam('logSessionId', params.logSessionId); |
| 85 | + if (!validationResult.isValid) { |
| 86 | + return validationResult.errorResponse!; |
| 87 | + } |
| 88 | + const { logContent, error } = await stopLogCapture(params.logSessionId); |
| 89 | + if (error) { |
| 90 | + return { |
| 91 | + content: [ |
| 92 | + createTextContent(`Error stopping log capture session ${params.logSessionId}: ${error}`), |
| 93 | + ], |
| 94 | + isError: true, |
| 95 | + }; |
| 96 | + } |
| 97 | + return { |
| 98 | + content: [ |
| 99 | + createTextContent( |
| 100 | + `Log capture session ${params.logSessionId} stopped successfully. Log content follows:\n\n${logContent}`, |
| 101 | + ), |
| 102 | + ], |
| 103 | + }; |
| 104 | + } |
| 105 | + |
| 106 | + registerTool( |
| 107 | + server, |
| 108 | + 'stop_and_get_simulator_log', |
| 109 | + 'Stops an active simulator log capture session and returns the captured logs.', |
| 110 | + schema, |
| 111 | + handler, |
| 112 | + ); |
| 113 | +} |
0 commit comments