a11y-test-mcp 1.0.5 → 1.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -10,7 +10,7 @@ An MCP (Model Context Protocol) server for performing a11y test on webpages usin
10
10
  * Passes
11
11
  * Incomplete
12
12
  * Inapplicable
13
- * Can specify specific WCAG criteria
13
+ * Can specify specific WCAG criteria(Default WCAG 2.0 level A, WCAG 2.0 level AA, WCAG 2.1 level A, WCAG 2.1 level AA)
14
14
 
15
15
  ## Installation
16
16
 
@@ -0,0 +1,6 @@
1
+ /** WCAG tag map */
2
+ export declare const WCAG_TAG_MAP: Record<string, string>;
3
+ /** Allow prefixes or tags */
4
+ export declare const ALLOWED_PREFIXES_OR_TAGS: string[];
5
+ /** default WCAG tags */
6
+ export declare const DEFAULT_WCAG_TAGS: string[];
@@ -0,0 +1,21 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.DEFAULT_WCAG_TAGS = exports.ALLOWED_PREFIXES_OR_TAGS = exports.WCAG_TAG_MAP = void 0;
4
+ /** WCAG tag map */
5
+ exports.WCAG_TAG_MAP = {
6
+ 'a': 'wcag2a',
7
+ 'wcag20a': 'wcag2a',
8
+ 'wcag2a': 'wcag2a',
9
+ 'aa': 'wcag2aa',
10
+ 'wcag20aa': 'wcag2aa',
11
+ 'wcag2aa': 'wcag2aa',
12
+ 'wcag21a': 'wcag21a',
13
+ 'wcag21aa': 'wcag21aa',
14
+ 'wcag22a': 'wcag22a',
15
+ 'wcag22aa': 'wcag22aa',
16
+ // Add other known tags or aliases here
17
+ };
18
+ /** Allow prefixes or tags */
19
+ exports.ALLOWED_PREFIXES_OR_TAGS = ['wcag', 'best-practice', 'section508'];
20
+ /** default WCAG tags */
21
+ exports.DEFAULT_WCAG_TAGS = ["wcag2a", "wcag2aa", "wcag21a", "wcag21aa"];
@@ -6,6 +6,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
6
6
  exports.convertTestResultToText = exports.execTest = void 0;
7
7
  const playwright_1 = __importDefault(require("playwright"));
8
8
  const playwright_2 = __importDefault(require("@axe-core/playwright"));
9
+ const constants_1 = require("./constants");
9
10
  /**
10
11
  * Enhance WCAG tag conversion
11
12
  * @param {string[]} tags - Array of WCAG tags
@@ -14,33 +15,28 @@ const playwright_2 = __importDefault(require("@axe-core/playwright"));
14
15
  const convertWcagTag = (tags) => {
15
16
  return tags.map(tag => {
16
17
  const lowerTag = tag.toLowerCase().replace(/[\s.]/g, '');
17
- switch (lowerTag) {
18
- case 'wcag2a':
19
- case 'a':
20
- case 'wcag20a':
21
- return 'wcag2a';
22
- case 'wcag2aa':
23
- case 'aa':
24
- case 'wcag20aa':
25
- return 'wcag2aa';
26
- case 'wcag21a':
27
- return 'wcag21a';
28
- case 'wcag21aa':
29
- return 'wcag21aa';
30
- case 'wcag22a':
31
- return 'wcag22a';
32
- case 'wcag22aa':
33
- return 'wcag22aa';
34
- default:
35
- if (lowerTag.startsWith('wcag') || ['best-practice', 'section508'].includes(lowerTag)) {
36
- return lowerTag;
37
- }
38
- console.warn(`Unrecognized WCAG tag: ${tag}`);
39
- return '';
18
+ if (lowerTag in constants_1.WCAG_TAG_MAP) {
19
+ return constants_1.WCAG_TAG_MAP[lowerTag];
20
+ }
21
+ if (constants_1.ALLOWED_PREFIXES_OR_TAGS.some(prefixOrTag => lowerTag.startsWith(prefixOrTag) || lowerTag === prefixOrTag)) {
22
+ return lowerTag;
40
23
  }
41
- }).filter(tag => {
42
- return tag !== '';
43
- });
24
+ console.warn(`Unrecognized WCAG tag: ${tag}`);
25
+ return '';
26
+ }).filter(tag => tag !== '');
27
+ };
28
+ /**
29
+ * Formats a single accessibility violation into a human-readable string.
30
+ * Includes impact level, ID, description, node count, help URL, and details of each affected node.
31
+ * @param {ViolationSummary} v - The violation summary object containing details about the violation.
32
+ * @returns {string} A formatted string representing the violation, suitable for display in reports or logs.
33
+ */
34
+ const formatViolation = (v) => {
35
+ const violationHeader = ` - [${String(v.impact?.toUpperCase() ?? 'N/A')}] ${v.id}: ${v.description} (Nodes: ${String(v.nodes.length)}, Help: ${v.helpUrl})`;
36
+ const violationNodes = v.nodes
37
+ .map((node, index) => ` Node ${String(index + 1)}: ${node.html}`)
38
+ .join('\n');
39
+ return `${violationHeader}\n${violationNodes}`;
44
40
  };
45
41
  /**
46
42
  * Execute a11y test
@@ -49,60 +45,52 @@ const convertWcagTag = (tags) => {
49
45
  * @returns {AccessibilityTestOutput[]} - Results of the accessibility tests
50
46
  */
51
47
  const execTest = async (urls, wcagStandards) => {
52
- const results = [];
53
48
  const browser = await playwright_1.default.chromium.launch();
54
49
  const context = await browser.newContext();
50
+ const tagsToUse = (wcagStandards && wcagStandards.length > 0)
51
+ ? convertWcagTag(wcagStandards)
52
+ : constants_1.DEFAULT_WCAG_TAGS;
55
53
  try {
56
- for (const url of urls) {
57
- let page;
54
+ const results = await Promise.all(urls.map(async (url) => {
55
+ let page = null;
58
56
  try {
59
57
  page = await context.newPage();
60
58
  await page.goto(url, { waitUntil: 'networkidle' });
61
59
  const axeBuilder = new playwright_2.default({ page });
62
- const tagsToUse = (wcagStandards && wcagStandards.length > 0)
63
- ? convertWcagTag(wcagStandards)
64
- : ["wcag2a", "wcag2aa", "wcag21a", "wcag21aa"];
65
- if (tagsToUse.length > 0) {
66
- axeBuilder.withTags(tagsToUse);
67
- }
68
- else {
69
- console.warn("No valid WCAG tags specified, running Axe with default rules.");
70
- }
60
+ axeBuilder.withTags(tagsToUse);
71
61
  const axeResults = await axeBuilder.analyze();
72
- // Summarize results, handling null impact
73
62
  const summarizedViolations = axeResults.violations.map(v => ({
74
63
  id: v.id,
75
- // Handle null impact from axe-core
76
64
  impact: v.impact === null ? undefined : v.impact,
77
65
  description: v.description,
78
66
  helpUrl: v.helpUrl,
79
67
  nodes: v.nodes
80
68
  }));
81
- results.push({
69
+ return {
82
70
  url: url,
83
71
  violations: summarizedViolations,
84
72
  passesCount: axeResults.passes.length,
85
73
  incompleteCount: axeResults.incomplete.length,
86
74
  inapplicableCount: axeResults.inapplicable.length,
87
- });
75
+ };
88
76
  }
89
77
  catch (error) {
90
- results.push({
78
+ return {
91
79
  url: url,
92
80
  error: `Failed to test: ${error instanceof Error ? error.message : String(error)}`,
93
- });
81
+ };
94
82
  }
95
83
  finally {
96
- if (page) {
84
+ if (page !== null) {
97
85
  await page.close();
98
86
  }
99
87
  }
100
- }
88
+ }));
89
+ return results;
101
90
  }
102
91
  finally {
103
92
  await browser.close();
104
93
  }
105
- return results;
106
94
  };
107
95
  exports.execTest = execTest;
108
96
  /**
@@ -111,34 +99,25 @@ exports.execTest = execTest;
111
99
  * @returns {string} - Text representation of the results
112
100
  */
113
101
  const convertTestResultToText = (structuredResults) => {
114
- return structuredResults
115
- .map((result) => {
116
- const resultTextList = [`URL: ${result.url}`];
102
+ let outputText = '';
103
+ for (const result of structuredResults) {
104
+ let resultText = `URL: ${result.url}\n`;
117
105
  if (result.error) {
118
- resultTextList.push(` Error: ${result.error}`);
106
+ resultText += ` Error: ${result.error}\n`;
119
107
  }
120
108
  else {
121
- resultTextList.push(` Violations: ${String(result.violations?.length ?? 0)}`);
122
- const resultViolationText = result.violations?.map((v) => {
123
- return [
124
- ` - [${String(v.impact?.toUpperCase() ?? 'N/A')}] ${v.id}: ${v.description} (Nodes: ${String(v.nodes.length)}, Help: ${v.helpUrl})`,
125
- v.nodes
126
- .map((node, index) => {
127
- return ` Node ${String(index + 1)}: ${node.html}`;
128
- })
129
- .join('\n')
130
- ].join('\n');
131
- });
132
- if (resultViolationText !== undefined) {
133
- resultTextList.push(...resultViolationText);
109
+ resultText += ` Violations: ${String(result.violations?.length ?? 0)}\n`;
110
+ if (result.violations && result.violations.length > 0) {
111
+ for (const violation of result.violations) {
112
+ resultText += `${formatViolation(violation)}\n`;
113
+ }
134
114
  }
135
- resultTextList.push(` Passes: ${String(result.passesCount ?? 0)}`);
136
- resultTextList.push(` Incomplete: ${String(result.incompleteCount ?? 0)}`);
137
- resultTextList.push(` Inapplicable: ${String(result.inapplicableCount ?? 0)}`);
115
+ resultText += ` Passes: ${String(result.passesCount ?? 0)}\n`;
116
+ resultText += ` Incomplete: ${String(result.incompleteCount ?? 0)}\n`;
117
+ resultText += ` Inapplicable: ${String(result.inapplicableCount ?? 0)}\n`;
138
118
  }
139
- return resultTextList.join('\n');
140
- })
141
- .join('\n')
142
- .trim();
119
+ outputText += resultText + '\n';
120
+ }
121
+ return outputText.trim();
143
122
  };
144
123
  exports.convertTestResultToText = convertTestResultToText;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "a11y-test-mcp",
3
- "version": "1.0.5",
3
+ "version": "1.1.0",
4
4
  "main": "build/index.js",
5
5
  "scripts": {
6
6
  "postinstall": "npx -y playwright install",