#!/usr/bin/env node
/**
* Playwright Accessibility Testing MCP Server
*
* This server provides Model Context Protocol (MCP) tools for running
* accessibility tests using Playwright and axe-core.
*
* Features:
* - URL scanning with optional element targeting
* - Interactive element testing with auto-discovery
* - Screenshot capture
* - Comprehensive WCAG 2.0/2.1 Level A/AA compliance testing
* - Predefined prompt templates for common testing scenarios
*/
import { McpServer } from '@modelcontextprotocol/sdk/server/mcp.js';
import { StdioServerTransport } from '@modelcontextprotocol/sdk/server/stdio.js';
// Configuration
import { SERVER_CONFIG } from './constants/config.js';
// Utilities
import { closeBrowser } from './utils/browser.js';
import { ensureScreenshotsDirectory } from './utils/screenshot.js';
// Tools
import { scanUrlSchema, executeScanUrl } from './tools/scan-url.js';
import { scanInteractiveByTextSchema, executeScanInteractiveByText } from './tools/scan-interactive-by-text.js';
// Prompts
import {
comprehensiveA11yTestSchema,
generateComprehensiveTestPrompt
} from './prompts/comprehensive-a11y-test.js';
// Initialize MCP server
const server = new McpServer({
name: SERVER_CONFIG.name,
version: SERVER_CONFIG.version
});
// Ensure screenshots directory exists
ensureScreenshotsDirectory();
/**
* TOOL: a11y_scanUrl
* Run accessibility scan on a URL or specific element
*/
server.registerTool(
'a11y_scanUrl',
{
description:
'Run accessibility scan on a URL or specific element using Playwright + axe-core. ' +
'Supports full page or targeted section scanning with optional screenshots. ' +
'Uses comprehensive WCAG 2.0, 2.1 Level A/AA and best-practice rules by default.',
inputSchema: scanUrlSchema
},
async (params) => executeScanUrl(params)
);
/**
* TOOL: a11y_scanInteractiveByText
* Test accessibility of components by finding them using visible text
*/
server.registerTool(
'a11y_scanInteractiveByText',
{
description:
'Test accessibility of components by finding them using visible text/labels instead of CSS selectors. ' +
'Automatically discovers and tests interactive elements. ' +
"Perfect for natural language testing like 'test the Rewards section' or 'test the user menu'. " +
'Uses comprehensive WCAG 2.0, 2.1 Level A/AA and best-practice rules by default.',
inputSchema: scanInteractiveByTextSchema
},
async (params) => executeScanInteractiveByText(params)
);
/**
* PROMPT: comprehensive-a11y-test
* Predefined template for comprehensive accessibility testing
*/
server.registerPrompt(
'comprehensive-a11y-test',
{
title: 'Comprehensive Accessibility Test',
description:
'Run a comprehensive accessibility audit with detailed analysis. ' +
'Just provide URL, optional block name, and optional steps.',
argsSchema: comprehensiveA11yTestSchema
},
async (params) => generateComprehensiveTestPrompt(params)
);
/**
* Cleanup on shutdown
*/
async function cleanup() {
await closeBrowser();
process.exit(0);
}
process.on('SIGINT', cleanup);
process.on('SIGTERM', cleanup);
/**
* Start the MCP server
*/
const transport = new StdioServerTransport();
server.connect(transport).catch((error) => {
console.error('Failed to start server:', error);
process.exit(1);
});
console.log('Playwright Accessibility MCP Server started');