Skip to main content
Glama
devskido

Playwright MCP Server

by devskido

end_codegen_session

End a Playwright browser automation session and generate the corresponding test file from recorded interactions.

Instructions

End a code generation session and generate the test file

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
sessionIdYesID of the session to end

Implementation Reference

  • Full Tool definition including the handler function that ends the codegen session by generating and saving a Playwright test file from recorded actions, cleaning up browser resources, and returning the generated file path.
    export const endCodegenSession: Tool = {
      name: 'end_codegen_session',
      description: 'End the current code generation session and generate Playwright test',
      parameters: {
        type: 'object',
        properties: {
          sessionId: {
            type: 'string',
            description: 'ID of the session to end'
          }
        },
        required: ['sessionId']
      },
      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}`);
        }
      }
    };
  • MCP tool schema definition used for input validation and tool registration.
      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"]
      }
    },
  • Registration and dispatch logic in the main tool handler that routes 'end_codegen_session' calls to its handler function.
    // Handle codegen tools
    switch (name) {
      case 'start_codegen_session':
        return await handleCodegenResult(startCodegenSession.handler(args));
      case 'end_codegen_session':
        return await handleCodegenResult(endCodegenSession.handler(args));
      case 'get_codegen_session':
        return await handleCodegenResult(getCodegenSession.handler(args));
      case 'clear_codegen_session':
        return await handleCodegenResult(clearCodegenSession.handler(args));
    }
  • src/tools.ts:484-490 (registration)
    Array listing codegen tools including 'end_codegen_session' for categorization and tool list composition.
    // Codegen tools
    export const CODEGEN_TOOLS = [
      'start_codegen_session',
      'end_codegen_session',
      'get_codegen_session',
      'clear_codegen_session'
    ];

Latest Blog Posts

MCP directory API

We provide all the information about MCP servers via our MCP API.

curl -X GET 'https://glama.ai/api/mcp/v1/servers/devskido/customed-playwright'

If you have feedback or need assistance with the MCP directory API, please join our Discord server