#!/usr/bin/env node
import type { Server } from '@modelcontextprotocol/sdk/server/index.js';
import type { Config } from '../config';
import type { BrowserContext } from 'playwright';
import { healthCheck } from './tools/healthCheck.js';
const { createConnection: originalCreateConnection } = require('playwright/lib/mcp/index');
const healthCheckToolSchema = {
name: 'health_check',
title: 'Health check',
description: 'Perform a local system health check to verify Playwright MCP server configuration and connectivity. Returns system information and configuration status.',
inputSchema: {
type: 'object',
properties: {
browserType: {
type: 'string',
description: 'Optional: Specify the browser type being used (chromium, firefox, webkit)'
}
}
}
};
export async function createConnection(config?: Config, contextGetter?: () => Promise<BrowserContext>): Promise<Server> {
healthCheck.initialize(contextGetter);
const server = await originalCreateConnection(config, contextGetter);
const originalHandlers = (server as any)._requestHandlers;
if (originalHandlers) {
const originalListHandler = originalHandlers.get('tools/list');
const originalCallHandler = originalHandlers.get('tools/call');
if (originalListHandler) {
(server as any)._requestHandlers.set('tools/list', async (request: any, extra: any) => {
const result = await originalListHandler(request, extra);
if (result && result.tools) {
result.tools.push({
name: healthCheckToolSchema.name,
description: healthCheckToolSchema.description,
inputSchema: healthCheckToolSchema.inputSchema
});
}
return result;
});
}
if (originalCallHandler) {
(server as any)._requestHandlers.set('tools/call', async (request: any, extra: any) => {
if (request.params?.name === healthCheckToolSchema.name) {
try {
const result = await healthCheck.execute(request.params.arguments || {});
// Return a simple generic message if health check is successful
if (result.success) {
return {
content: [{ type: 'text', text: 'Playwright MCP is healthy' }]
};
} else {
return {
content: [{ type: 'text', text: JSON.stringify(result) }]
};
}
} catch (error: any) {
return {
content: [{ type: 'text', text: JSON.stringify({ error: error.message }) }],
isError: true
};
}
}
return originalCallHandler(request, extra);
});
}
}
return server;
}
export { healthCheck };