UNPKG

claude-playwright

Version:

Seamless integration between Claude Code and Playwright MCP for efficient browser automation and testing

105 lines • 4.9 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.setupMCPForClaude = setupMCPForClaude; exports.setupBrowserProfiles = setupBrowserProfiles; exports.createSessionConfig = createSessionConfig; exports.setupSessionMCPIntegration = setupSessionMCPIntegration; exports.checkClaudeInstallation = checkClaudeInstallation; const fs_extra_1 = __importDefault(require("fs-extra")); const path_1 = __importDefault(require("path")); const chalk_1 = __importDefault(require("chalk")); async function setupMCPForClaude(projectPath) { // Create local .mcp.json in project directory const mcpConfigPath = path_1.default.join(projectPath, '.mcp.json'); try { // No longer need to copy scripts - MCP server is in the package // The MCP server is built and available in node_modules after installation // Create MCP configuration pointing to built MCP server const mcpConfig = { mcpServers: { playwright: { command: 'node', args: [ path_1.default.join('node_modules', 'claude-playwright', 'dist', 'mcp', 'server.cjs') ], env: { // BASE_URL can be configured per project BASE_URL: process.env.BASE_URL || 'http://localhost:3000' } } } }; // Write MCP configuration to project await fs_extra_1.default.writeJSON(mcpConfigPath, mcpConfig, { spaces: 2 }); console.log(chalk_1.default.green('āœ“ MCP configured for Claude Code')); console.log(chalk_1.default.gray(`šŸ“‹ Configuration written to: ${mcpConfigPath}`)); console.log(chalk_1.default.yellow('\nšŸ“Œ Important: When starting Claude Code in this project:')); console.log(chalk_1.default.yellow(' 1. Run: claude')); console.log(chalk_1.default.yellow(' 2. Accept the MCP server when prompted')); console.log(chalk_1.default.yellow(' 3. Check connection with /mcp command')); return true; } catch (error) { console.error(chalk_1.default.red('āŒ Failed to setup MCP:'), error); return false; } } async function setupBrowserProfiles(projectPath) { const profilesPath = path_1.default.join(projectPath, 'browser-profiles', 'default'); const authStatesPath = path_1.default.join(projectPath, 'auth-states'); const sessionsPath = path_1.default.join(projectPath, 'playwright-sessions'); await fs_extra_1.default.ensureDir(profilesPath); await fs_extra_1.default.ensureDir(authStatesPath); await fs_extra_1.default.ensureDir(sessionsPath); // Create session configuration file await createSessionConfig(projectPath); console.log(chalk_1.default.green('āœ“ Browser profiles and sessions configured')); console.log(chalk_1.default.gray(`šŸ“ Default profile: ${profilesPath}`)); console.log(chalk_1.default.gray(`šŸ” Auth states: ${authStatesPath}`)); console.log(chalk_1.default.gray(`šŸ’¾ Sessions: ${sessionsPath}`)); } async function createSessionConfig(projectPath) { const sessionConfigPath = path_1.default.join(projectPath, 'playwright-sessions', '.config.json'); const sessionConfig = { defaultSessionTimeout: 28800000, // 8 hours autoCleanupExpired: true, autoLoadLatest: true, maxConcurrentSessions: 5, sessionBackupEnabled: true, profileIntegration: { enabled: true, autoSelectProfile: true, fallbackProfile: 'default' }, logging: { enabled: true, level: 'info' } }; await fs_extra_1.default.writeJSON(sessionConfigPath, sessionConfig, { spaces: 2 }); console.log(chalk_1.default.green('āœ“ Session configuration created')); } async function setupSessionMCPIntegration(projectPath) { // Session management is now built into the TypeScript MCP server // No need to create additional scripts console.log(chalk_1.default.green('āœ“ Session management integrated in MCP server')); return true; } async function checkClaudeInstallation() { // Check if claude command is available try { const { execSync } = require('child_process'); execSync('which claude', { stdio: 'ignore' }); console.log(chalk_1.default.green('āœ“ Claude Code CLI detected')); return true; } catch (error) { console.log(chalk_1.default.yellow('āš ļø Claude Code CLI not found in PATH')); console.log(chalk_1.default.gray(' Install with: npm install -g @anthropic-ai/claude-code')); return false; } } //# sourceMappingURL=mcp-setup.js.map