end_codegen_session
Terminate a code generation session and produce the final test file. Uses the session ID to ensure proper closure and file generation within the Playwright MCP Server environment.
Instructions
End a code generation session and generate the test file
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| sessionId | Yes | ID of the session to end |
Input Schema (JSON Schema)
{
"properties": {
"sessionId": {
"description": "ID of the session to end",
"type": "string"
}
},
"required": [
"sessionId"
],
"type": "object"
}
Implementation Reference
- src/tools/codegen/index.ts:98-160 (handler)The core handler function that terminates the codegen session: ends the session via ActionRecorder, generates Playwright test code using PlaywrightGenerator, writes the test file to disk, and cleans up global browser/page instances.handler: async ({ sessionId }: { sessionId: string }) => { try { const recorder = ActionRecorder.getInstance(); const session = recorder.endSession(sessionId); if (!session) { throw new Error(`Session ${sessionId} not found`); } if (!session.options) { throw new Error(`Session ${sessionId} has no options configured`); } const generator = new PlaywrightGenerator(session.options); const result = await generator.generateTest(session); // Double check output directory exists const outputDir = path.dirname(result.filePath); await fs.mkdir(outputDir, { recursive: true }); // Write test file try { await fs.writeFile(result.filePath, result.testCode, "utf-8"); } catch (writeError: any) { throw new Error(`Failed to write test file: ${writeError.message}`); } // Close Playwright browser and cleanup try { if (global.browser?.isConnected()) { await global.browser.close(); } } catch (browserError: any) { console.warn("Failed to close browser:", browserError.message); } finally { global.browser = undefined; global.page = undefined; } const absolutePath = path.resolve(result.filePath); return { filePath: absolutePath, outputDirectory: outputDir, testCode: result.testCode, message: `Generated test file at: ${absolutePath}\nOutput directory: ${outputDir}`, }; } catch (error: any) { // Ensure browser cleanup even on error try { if (global.browser?.isConnected()) { await global.browser.close(); } } catch { // Ignore cleanup errors } finally { global.browser = undefined; global.page = undefined; } throw new Error(`Failed to end codegen session: ${error.message}`); } },
- src/tools/codegen/index.ts:88-97 (schema)Input schema definition for the end_codegen_session tool, specifying the required sessionId parameter.parameters: { type: "object", properties: { sessionId: { type: "string", description: "ID of the session to end", }, }, required: ["sessionId"], },
- src/toolHandler.ts:334-335 (registration)Registration in the main tool handler switch statement that dispatches calls to the endCodegenSession.handler function.case "end_codegen_session": return await handleCodegenResult(endCodegenSession.handler(args));
- src/tools.ts:37-49 (registration)Tool registration schema in createToolDefinitions() for MCP tool list, including name, description, and inputSchema.name: "end_codegen_session", description: "End a code generation session and generate the test file", inputSchema: { type: "object", properties: { sessionId: { type: "string", description: "ID of the session to end" } }, required: ["sessionId"] } },
- src/toolHandler.ts:584-609 (helper)Helper function used to wrap and format the result of codegen tool handlers, including end_codegen_session.async function handleCodegenResult( resultPromise: Promise<any> ): Promise<CallToolResult> { try { const result = await resultPromise; return { content: [ { type: "text", text: JSON.stringify(result), }, ], isError: false, }; } catch (error) { return { content: [ { type: "text", text: error instanceof Error ? error.message : String(error), }, ], isError: true, }; } }