@react-spectrum/mcp 1.1.0 → 1.2.1

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
@@ -1,6 +1,6 @@
1
1
  # @react-spectrum/mcp
2
2
 
3
- The `@react-spectrum/mcp` package provides a [Model Context Protocol (MCP)](https://modelcontextprotocol.io/docs/getting-started/intro) server for React Spectrum (S2) documentation. It exposes a set of tools that MCP clients can discover and call to browse the docs, search for icons and illustrations, and more.
3
+ The `@react-spectrum/mcp` package provides a [Model Context Protocol (MCP)](https://modelcontextprotocol.io/docs/getting-started/intro) server for React Spectrum (S2) documentation. It exposes a set of tools that MCP clients can discover and call to browse the docs, search for icons and illustrations, and more. It also bundles the React Aria docs tools, so you can browse React Aria documentation from the same server.
4
4
 
5
5
  ## Installation
6
6
 
@@ -110,6 +110,14 @@ Follow Windsurf MCP [documentation](https://docs.windsurf.com/windsurf/cascade/m
110
110
  | `get_s2_page` | `{ page_name: string, section_name?: string }` | Return full page markdown, or only the specified section. |
111
111
  | `search_s2_icons` | `{ terms: string \| string[] }` | Search S2 workflow icon names. |
112
112
  | `search_s2_illustrations` | `{ terms: string \| string[] }` | Search S2 illustration names. |
113
+ | `get_style_macro_property_values` | `{ propertyName: string }` | Return allowed values for an S2 style macro property. |
114
+ | `list_react_aria_pages` | `{ includeDescription?: boolean }` | List available pages in the React Aria docs. |
115
+ | `get_react_aria_page_info` | `{ page_name: string }` | Return page description and list of section titles. |
116
+ | `get_react_aria_page` | `{ page_name: string, section_name?: string }` | Return full page markdown, or only the specified section. |
117
+
118
+ ## Privacy Policy
119
+
120
+ See [Adobe's privacy policy](https://www.adobe.com/privacy/policy.html).
113
121
 
114
122
  ## Development
115
123
 
@@ -137,4 +145,19 @@ Update your MCP client configuration to use the local MCP server:
137
145
  }
138
146
  }
139
147
  }
148
+ ```
149
+
150
+ ## Bundling
151
+
152
+ To build an [MCP Bundle (MCPB)](https://github.com/modelcontextprotocol/mcpb), first prepare the staging directory:
153
+
154
+ ```bash
155
+ yarn workspace @react-spectrum/s2-docs generate:mcpb
156
+ ```
157
+
158
+ Then validate and pack using the [`@anthropic-ai/mcpb`](https://www.npmjs.com/package/@anthropic-ai/mcpb) CLI (paths are printed by the script above):
159
+
160
+ ```bash
161
+ npx @anthropic-ai/mcpb validate <staging-dir>
162
+ npx @anthropic-ai/mcpb pack <staging-dir> <output-path>
140
163
  ```
@@ -7,6 +7,7 @@
7
7
  "Add",
8
8
  "AddCircle",
9
9
  "AddContent",
10
+ "AIMark",
10
11
  "AlertDiamond",
11
12
  "AlertTriangle",
12
13
  "AlignBottom",
@@ -3926,7 +3926,7 @@
3926
3926
  "auto",
3927
3927
  "square",
3928
3928
  "video",
3929
- "number / number"
3929
+ "${number}/${number}"
3930
3930
  ],
3931
3931
  "additionalTypes": []
3932
3932
  },
@@ -870,7 +870,7 @@
870
870
  "auto",
871
871
  "square",
872
872
  "video",
873
- "number / number"
873
+ "${number}/${number}"
874
874
  ],
875
875
  "additionalTypes": [],
876
876
  "links": {
@@ -1,6 +1,6 @@
1
1
  #!/usr/bin/env node
2
2
  /// <reference types="node" />
3
- import { errorToString } from '../../shared/src/utils.js';
3
+ import { errorToString, readPackageVersion } from '../../shared/src/utils.js';
4
4
  import { listIconNames, listIllustrationNames, loadIconAliases, loadIllustrationAliases, loadStyleMacroPropertyValues } from './s2-data.js';
5
5
  import { startServer } from '../../shared/src/server.js';
6
6
  import { z } from 'zod';
@@ -12,102 +12,130 @@ import { z } from 'zod';
12
12
  console.log('Usage: npx @react-spectrum/mcp@latest\n\nStarts the MCP server for React Spectrum (S2) documentation.');
13
13
  process.exit(0);
14
14
  }
15
- await startServer('s2', '0.1.0', (server) => {
16
- server.registerTool('search_s2_icons', {
17
- title: 'Search S2 icons',
18
- description: 'Searches the S2 workflow icon set by one or more terms; returns matching icon names.',
19
- inputSchema: { terms: z.union([z.string(), z.array(z.string())]) }
20
- }, async ({ terms }) => {
21
- const allNames = listIconNames();
22
- const nameSet = new Set(allNames);
23
- const aliases = await loadIconAliases();
24
- const rawTerms = Array.isArray(terms) ? terms : [terms];
25
- const normalized = Array.from(new Set(rawTerms.map(t => String(t ?? '').trim().toLowerCase()).filter(Boolean)));
26
- if (normalized.length === 0) {
27
- throw new Error('Provide at least one non-empty search term.');
28
- }
29
- // direct name matches
30
- const results = new Set(allNames.filter(name => {
31
- const nameLower = name.toLowerCase();
32
- return normalized.some(term => nameLower.includes(term));
33
- }));
34
- // alias matches
35
- for (const [aliasKey, targets] of Object.entries(aliases)) {
36
- if (!targets || targets.length === 0) {
37
- continue;
15
+ await startServer('s2', readPackageVersion(import.meta.url), {
16
+ additionalLibraries: ['react-aria'],
17
+ registerAdditionalTools: (server) => {
18
+ server.registerTool('search_s2_icons', {
19
+ title: 'Search S2 icons',
20
+ description: 'Searches the S2 workflow icon set by one or more terms; returns matching icon names.',
21
+ inputSchema: { terms: z.union([z.string(), z.array(z.string())]) },
22
+ annotations: { readOnlyHint: true, openWorldHint: true }
23
+ }, async ({ terms }) => {
24
+ const allNames = listIconNames();
25
+ const nameSet = new Set(allNames);
26
+ const aliases = await loadIconAliases();
27
+ const rawTerms = Array.isArray(terms) ? terms : [terms];
28
+ const normalized = Array.from(new Set(rawTerms
29
+ .map(t => String(t ?? '')
30
+ .trim()
31
+ .toLowerCase())
32
+ .filter(Boolean)));
33
+ if (normalized.length === 0) {
34
+ throw new Error('Provide at least one non-empty search term.');
38
35
  }
39
- const aliasLower = aliasKey.toLowerCase();
40
- if (normalized.some(term => aliasLower.includes(term) || term.includes(aliasLower))) {
41
- for (const t of targets) {
42
- const n = String(t);
43
- if (nameSet.has(n)) {
44
- results.add(n);
36
+ // direct name matches
37
+ const results = new Set(allNames.filter(name => {
38
+ const nameLower = name.toLowerCase();
39
+ return normalized.some(term => nameLower.includes(term));
40
+ }));
41
+ // alias matches
42
+ for (const [aliasKey, targets] of Object.entries(aliases)) {
43
+ if (!targets || targets.length === 0) {
44
+ continue;
45
+ }
46
+ const aliasLower = aliasKey.toLowerCase();
47
+ if (normalized.some(term => aliasLower.includes(term) || term.includes(aliasLower))) {
48
+ for (const t of targets) {
49
+ const n = String(t);
50
+ if (nameSet.has(n)) {
51
+ results.add(n);
52
+ }
45
53
  }
46
54
  }
47
55
  }
48
- }
49
- return { content: [{ type: 'text', text: JSON.stringify(Array.from(results).sort((a, b) => a.localeCompare(b)), null, 2) }] };
50
- });
51
- server.registerTool('search_s2_illustrations', {
52
- title: 'Search S2 illustrations',
53
- description: 'Searches the S2 illustrations set by one or more terms; returns matching illustration names.',
54
- inputSchema: { terms: z.union([z.string(), z.array(z.string())]) }
55
- }, async ({ terms }) => {
56
- const allNames = listIllustrationNames();
57
- const nameSet = new Set(allNames);
58
- const aliases = await loadIllustrationAliases();
59
- const rawTerms = Array.isArray(terms) ? terms : [terms];
60
- const normalized = Array.from(new Set(rawTerms.map(t => String(t ?? '').trim().toLowerCase()).filter(Boolean)));
61
- if (normalized.length === 0) {
62
- throw new Error('Provide at least one non-empty search term.');
63
- }
64
- // direct name matches
65
- const results = new Set(allNames.filter(name => {
66
- const nameLower = name.toLowerCase();
67
- return normalized.some(term => nameLower.includes(term));
68
- }));
69
- // alias matches
70
- for (const [aliasKey, targets] of Object.entries(aliases)) {
71
- if (!targets || targets.length === 0) {
72
- continue;
56
+ return {
57
+ content: [
58
+ {
59
+ type: 'text',
60
+ text: JSON.stringify(Array.from(results).sort((a, b) => a.localeCompare(b)), null, 2)
61
+ }
62
+ ]
63
+ };
64
+ });
65
+ server.registerTool('search_s2_illustrations', {
66
+ title: 'Search S2 illustrations',
67
+ description: 'Searches the S2 illustrations set by one or more terms; returns matching illustration names.',
68
+ inputSchema: { terms: z.union([z.string(), z.array(z.string())]) },
69
+ annotations: { readOnlyHint: true, openWorldHint: true }
70
+ }, async ({ terms }) => {
71
+ const allNames = listIllustrationNames();
72
+ const nameSet = new Set(allNames);
73
+ const aliases = await loadIllustrationAliases();
74
+ const rawTerms = Array.isArray(terms) ? terms : [terms];
75
+ const normalized = Array.from(new Set(rawTerms
76
+ .map(t => String(t ?? '')
77
+ .trim()
78
+ .toLowerCase())
79
+ .filter(Boolean)));
80
+ if (normalized.length === 0) {
81
+ throw new Error('Provide at least one non-empty search term.');
73
82
  }
74
- const aliasLower = aliasKey.toLowerCase();
75
- if (normalized.some(term => aliasLower.includes(term) || term.includes(aliasLower))) {
76
- for (const t of targets) {
77
- const n = String(t);
78
- if (nameSet.has(n)) {
79
- results.add(n);
83
+ // direct name matches
84
+ const results = new Set(allNames.filter(name => {
85
+ const nameLower = name.toLowerCase();
86
+ return normalized.some(term => nameLower.includes(term));
87
+ }));
88
+ // alias matches
89
+ for (const [aliasKey, targets] of Object.entries(aliases)) {
90
+ if (!targets || targets.length === 0) {
91
+ continue;
92
+ }
93
+ const aliasLower = aliasKey.toLowerCase();
94
+ if (normalized.some(term => aliasLower.includes(term) || term.includes(aliasLower))) {
95
+ for (const t of targets) {
96
+ const n = String(t);
97
+ if (nameSet.has(n)) {
98
+ results.add(n);
99
+ }
80
100
  }
81
101
  }
82
102
  }
83
- }
84
- return { content: [{ type: 'text', text: JSON.stringify(Array.from(results).sort((a, b) => a.localeCompare(b)), null, 2) }] };
85
- });
86
- server.registerTool('get_style_macro_property_values', {
87
- title: 'Get style macro property values',
88
- description: 'Returns the allowed values for a given S2 style macro property (including expanded color/spacing value lists where applicable).',
89
- inputSchema: { propertyName: z.string() }
90
- }, async ({ propertyName }) => {
91
- const name = String(propertyName ?? '').trim();
92
- if (!name) {
93
- throw new Error('Provide a non-empty propertyName.');
94
- }
95
- const all = loadStyleMacroPropertyValues();
96
- let def = all[name];
97
- if (!def) {
98
- // fallback to case-insensitive lookup
99
- const lower = name.toLowerCase();
100
- const matchKey = Object.keys(all).find(k => k.toLowerCase() === lower);
101
- if (matchKey) {
102
- def = all[matchKey];
103
+ return {
104
+ content: [
105
+ {
106
+ type: 'text',
107
+ text: JSON.stringify(Array.from(results).sort((a, b) => a.localeCompare(b)), null, 2)
108
+ }
109
+ ]
110
+ };
111
+ });
112
+ server.registerTool('get_style_macro_property_values', {
113
+ title: 'Get style macro property values',
114
+ description: 'Returns the allowed values for a given S2 style macro property (including expanded color/spacing value lists where applicable).',
115
+ inputSchema: { propertyName: z.string() },
116
+ annotations: { readOnlyHint: true, openWorldHint: true }
117
+ }, async ({ propertyName }) => {
118
+ const name = String(propertyName ?? '').trim();
119
+ if (!name) {
120
+ throw new Error('Provide a non-empty propertyName.');
121
+ }
122
+ const all = loadStyleMacroPropertyValues();
123
+ let def = all[name];
124
+ if (!def) {
125
+ // fallback to case-insensitive lookup
126
+ const lower = name.toLowerCase();
127
+ const matchKey = Object.keys(all).find(k => k.toLowerCase() === lower);
128
+ if (matchKey) {
129
+ def = all[matchKey];
130
+ }
131
+ }
132
+ if (!def) {
133
+ const available = Object.keys(all).sort((a, b) => a.localeCompare(b));
134
+ throw new Error(`Unknown style macro property '${name}'. Available properties: ${available.join(', ')}`);
103
135
  }
104
- }
105
- if (!def) {
106
- const available = Object.keys(all).sort((a, b) => a.localeCompare(b));
107
- throw new Error(`Unknown style macro property '${name}'. Available properties: ${available.join(', ')}`);
108
- }
109
- return { content: [{ type: 'text', text: JSON.stringify(def, null, 2) }] };
110
- });
136
+ return { content: [{ type: 'text', text: JSON.stringify(def, null, 2) }] };
137
+ });
138
+ }
111
139
  });
112
140
  }
113
141
  catch (err) {
@@ -27,28 +27,32 @@ export function listIconNames() {
27
27
  return iconIdCache;
28
28
  }
29
29
  const bundled = readBundledJson('icons.json');
30
- return (iconIdCache = Array.isArray(bundled) ? bundled.slice().sort((a, b) => a.localeCompare(b)) : []);
30
+ return (iconIdCache = Array.isArray(bundled)
31
+ ? bundled.slice().sort((a, b) => a.localeCompare(b))
32
+ : []);
31
33
  }
32
34
  export function listIllustrationNames() {
33
35
  if (illustrationIdCache) {
34
36
  return illustrationIdCache;
35
37
  }
36
38
  const bundled = readBundledJson('illustrations.json');
37
- return (illustrationIdCache = Array.isArray(bundled) ? bundled.slice().sort((a, b) => a.localeCompare(b)) : []);
39
+ return (illustrationIdCache = Array.isArray(bundled)
40
+ ? bundled.slice().sort((a, b) => a.localeCompare(b))
41
+ : []);
38
42
  }
39
43
  export async function loadIconAliases() {
40
44
  if (iconAliasesCache) {
41
45
  return iconAliasesCache;
42
46
  }
43
47
  const bundled = readBundledJson('iconAliases.json');
44
- return (iconAliasesCache = (bundled && typeof bundled === 'object') ? bundled : {});
48
+ return (iconAliasesCache = bundled && typeof bundled === 'object' ? bundled : {});
45
49
  }
46
50
  export async function loadIllustrationAliases() {
47
51
  if (illustrationAliasesCache) {
48
52
  return illustrationAliasesCache;
49
53
  }
50
54
  const bundled = readBundledJson('illustrationAliases.json');
51
- return (illustrationAliasesCache = (bundled && typeof bundled === 'object') ? bundled : {});
55
+ return (illustrationAliasesCache = bundled && typeof bundled === 'object' ? bundled : {});
52
56
  }
53
57
  export function loadStyleMacroPropertyValues() {
54
58
  if (styleMacroPropertyValuesCache) {
@@ -67,7 +67,13 @@ export async function resolvePageRef(library, pageName) {
67
67
  return maybe;
68
68
  }
69
69
  const filePath = `${baseUrl}/${normalized}.md`;
70
- const stub = { key: normalized, name: path.basename(normalized), description: undefined, filePath, sections: [] };
70
+ const stub = {
71
+ key: normalized,
72
+ name: path.basename(normalized),
73
+ description: undefined,
74
+ filePath,
75
+ sections: []
76
+ };
71
77
  pageCache.set(stub.key, stub);
72
78
  return stub;
73
79
  }
@@ -4,11 +4,22 @@ import { McpServer } from '@modelcontextprotocol/sdk/server/mcp.js';
4
4
  import { parseSectionsFromMarkdown } from './parser.js';
5
5
  import { StdioServerTransport } from '@modelcontextprotocol/sdk/server/stdio.js';
6
6
  import { z } from 'zod';
7
- export async function startServer(library, version, registerAdditionalTools) {
7
+ export async function startServer(library, version, options = {}) {
8
8
  const server = new McpServer({
9
9
  name: library === 's2' ? 's2-docs-server' : 'react-aria-docs-server',
10
10
  version
11
11
  });
12
+ const libraries = [library, ...(options.additionalLibraries ?? [])];
13
+ for (const lib of libraries) {
14
+ await registerLibraryDocsTools(server, lib);
15
+ }
16
+ if (options.registerAdditionalTools) {
17
+ await options.registerAdditionalTools(server);
18
+ }
19
+ const transport = new StdioServerTransport();
20
+ await server.connect(transport);
21
+ }
22
+ async function registerLibraryDocsTools(server, library) {
12
23
  // Build page index at startup.
13
24
  try {
14
25
  await buildPageIndex(library);
@@ -18,9 +29,12 @@ export async function startServer(library, version, registerAdditionalTools) {
18
29
  }
19
30
  const toolPrefix = library === 's2' ? 's2' : 'react_aria';
20
31
  server.registerTool(`list_${toolPrefix}_pages`, {
21
- title: library === 's2' ? 'List React Spectrum (@react-spectrum/s2) docs pages' : 'List React Aria docs pages',
32
+ title: library === 's2'
33
+ ? 'List React Spectrum (@react-spectrum/s2) docs pages'
34
+ : 'List React Aria docs pages',
22
35
  description: `Returns a list of available pages in the ${library} docs.`,
23
- inputSchema: { includeDescription: z.boolean().optional() }
36
+ inputSchema: { includeDescription: z.boolean().optional() },
37
+ annotations: { readOnlyHint: true, openWorldHint: true }
24
38
  }, async ({ includeDescription }) => {
25
39
  const pages = await buildPageIndex(library);
26
40
  const items = pages
@@ -33,7 +47,8 @@ export async function startServer(library, version, registerAdditionalTools) {
33
47
  server.registerTool(`get_${toolPrefix}_page_info`, {
34
48
  title: 'Get page info',
35
49
  description: 'Returns page description and list of sections for a given page.',
36
- inputSchema: { page_name: z.string() }
50
+ inputSchema: { page_name: z.string() },
51
+ annotations: { readOnlyHint: true, openWorldHint: true }
37
52
  }, async ({ page_name }) => {
38
53
  const ref = await resolvePageRef(library, page_name);
39
54
  const info = await ensureParsedPage(ref);
@@ -47,7 +62,8 @@ export async function startServer(library, version, registerAdditionalTools) {
47
62
  server.registerTool(`get_${toolPrefix}_page`, {
48
63
  title: 'Get page markdown',
49
64
  description: 'Returns the full markdown content for a page, or a specific section if provided.',
50
- inputSchema: { page_name: z.string(), section_name: z.string().optional() }
65
+ inputSchema: { page_name: z.string(), section_name: z.string().optional() },
66
+ annotations: { readOnlyHint: true, openWorldHint: true }
51
67
  }, async ({ page_name, section_name }) => {
52
68
  const ref = await resolvePageRef(library, page_name);
53
69
  let text;
@@ -68,9 +84,4 @@ export async function startServer(library, version, registerAdditionalTools) {
68
84
  const snippet = lines.slice(section.startLine, section.endLine).join('\n');
69
85
  return { content: [{ type: 'text', text: snippet }] };
70
86
  });
71
- if (registerAdditionalTools) {
72
- await registerAdditionalTools(server);
73
- }
74
- const transport = new StdioServerTransport();
75
- await server.connect(transport);
76
87
  }
@@ -1,8 +1,31 @@
1
+ import { dirname, join } from 'path';
2
+ import { fileURLToPath } from 'url';
3
+ import { readFileSync } from 'fs';
4
+ // Resolves the version from the nearest package.json above the CLI entry point.
5
+ // Both the npm-published layout (dist/<lib>/src/index.js) and the .mcpb bundle
6
+ // layout (server/<lib>/src/index.js) place package.json three levels up.
7
+ export function readPackageVersion(entryUrl) {
8
+ const entryDir = dirname(fileURLToPath(entryUrl));
9
+ const pkgPath = join(entryDir, '..', '..', '..', 'package.json');
10
+ try {
11
+ const pkg = JSON.parse(readFileSync(pkgPath, 'utf8'));
12
+ if (typeof pkg.version === 'string' && pkg.version.length > 0) {
13
+ return pkg.version;
14
+ }
15
+ }
16
+ catch {
17
+ // fall through
18
+ }
19
+ return '0.0.0';
20
+ }
1
21
  export function errorToString(err) {
2
22
  if (err && typeof err === 'object' && 'stack' in err && typeof err.stack === 'string') {
3
23
  return err.stack;
4
24
  }
5
- if (err && typeof err === 'object' && 'message' in err && typeof err.message === 'string') {
25
+ if (err &&
26
+ typeof err === 'object' &&
27
+ 'message' in err &&
28
+ typeof err.message === 'string') {
6
29
  return err.message;
7
30
  }
8
31
  try {
package/package.json CHANGED
@@ -1,9 +1,28 @@
1
1
  {
2
2
  "name": "@react-spectrum/mcp",
3
- "version": "1.1.0",
3
+ "version": "1.2.1",
4
4
  "description": "MCP server for React Spectrum (S2) documentation",
5
- "type": "module",
5
+ "license": "Apache-2.0",
6
+ "repository": {
7
+ "type": "git",
8
+ "url": "https://github.com/adobe/react-spectrum"
9
+ },
6
10
  "bin": "dist/s2/src/index.js",
11
+ "source": "src/index.ts",
12
+ "files": [
13
+ "dist",
14
+ "src"
15
+ ],
16
+ "type": "module",
17
+ "sideEffects": [
18
+ "*.css"
19
+ ],
20
+ "main": "dist/main.js",
21
+ "module": "dist/module.js",
22
+ "types": "dist/types.d.ts",
23
+ "publishConfig": {
24
+ "access": "public"
25
+ },
7
26
  "scripts": {
8
27
  "prepublishOnly": "yarn build",
9
28
  "build": "node ./scripts/build-data.mjs && tsc -p tsconfig.json",
@@ -17,29 +36,10 @@
17
36
  "zod": "^3.23.8"
18
37
  },
19
38
  "devDependencies": {
20
- "typescript": "^5.8.2"
39
+ "typescript": "^6.0.3"
21
40
  },
22
41
  "engines": {
23
42
  "node": ">=18"
24
43
  },
25
- "license": "Apache-2.0",
26
- "publishConfig": {
27
- "access": "public"
28
- },
29
- "repository": {
30
- "type": "git",
31
- "url": "https://github.com/adobe/react-spectrum"
32
- },
33
- "files": [
34
- "dist",
35
- "src"
36
- ],
37
- "sideEffects": [
38
- "*.css"
39
- ],
40
- "main": "dist/main.js",
41
- "module": "dist/module.js",
42
- "types": "dist/types.d.ts",
43
- "source": "src/index.ts",
44
- "gitHead": "a6999bdf494a2e9c0381a5881908328bdd22ddae"
44
+ "gitHead": "0a2a7e68e4d896b3ac46c3647fe48fac9ea3ae2e"
45
45
  }
package/src/index.ts CHANGED
@@ -1,7 +1,13 @@
1
1
  #!/usr/bin/env node
2
2
  /// <reference types="node" />
3
- import {errorToString} from '../../shared/src/utils.js';
4
- import {listIconNames, listIllustrationNames, loadIconAliases, loadIllustrationAliases, loadStyleMacroPropertyValues} from './s2-data.js';
3
+ import {errorToString, readPackageVersion} from '../../shared/src/utils.js';
4
+ import {
5
+ listIconNames,
6
+ listIllustrationNames,
7
+ loadIconAliases,
8
+ loadIllustrationAliases,
9
+ loadStyleMacroPropertyValues
10
+ } from './s2-data.js';
5
11
  import type {McpServer} from '@modelcontextprotocol/sdk/server/mcp.js';
6
12
  import {startServer} from '../../shared/src/server.js';
7
13
  import {z} from 'zod';
@@ -11,115 +17,182 @@ import {z} from 'zod';
11
17
  try {
12
18
  const arg = (process.argv[2] || '').trim();
13
19
  if (arg === '--help' || arg === '-h' || arg === 'help') {
14
- console.log('Usage: npx @react-spectrum/mcp@latest\n\nStarts the MCP server for React Spectrum (S2) documentation.');
20
+ console.log(
21
+ 'Usage: npx @react-spectrum/mcp@latest\n\nStarts the MCP server for React Spectrum (S2) documentation.'
22
+ );
15
23
  process.exit(0);
16
24
  }
17
25
 
18
- await startServer('s2', '0.1.0', (server: McpServer) => {
19
- server.registerTool(
20
- 'search_s2_icons',
21
- {
22
- title: 'Search S2 icons',
23
- description: 'Searches the S2 workflow icon set by one or more terms; returns matching icon names.',
24
- inputSchema: {terms: z.union([z.string(), z.array(z.string())])}
25
- },
26
- async ({terms}) => {
27
- const allNames = listIconNames();
28
- const nameSet = new Set(allNames);
29
- const aliases = await loadIconAliases();
30
- const rawTerms = Array.isArray(terms) ? terms : [terms];
31
- const normalized = Array.from(new Set(rawTerms.map(t => String(t ?? '').trim().toLowerCase()).filter(Boolean)));
32
- if (normalized.length === 0) {
33
- throw new Error('Provide at least one non-empty search term.');
34
- }
35
- // direct name matches
36
- const results = new Set(allNames.filter(name => {
37
- const nameLower = name.toLowerCase();
38
- return normalized.some(term => nameLower.includes(term));
39
- }));
40
- // alias matches
41
- for (const [aliasKey, targets] of Object.entries(aliases)) {
42
- if (!targets || targets.length === 0) {continue;}
43
- const aliasLower = aliasKey.toLowerCase();
44
- if (normalized.some(term => aliasLower.includes(term) || term.includes(aliasLower))) {
45
- for (const t of targets) {
46
- const n = String(t);
47
- if (nameSet.has(n)) {results.add(n);}
26
+ await startServer('s2', readPackageVersion(import.meta.url), {
27
+ additionalLibraries: ['react-aria'],
28
+ registerAdditionalTools: (server: McpServer) => {
29
+ server.registerTool(
30
+ 'search_s2_icons',
31
+ {
32
+ title: 'Search S2 icons',
33
+ description:
34
+ 'Searches the S2 workflow icon set by one or more terms; returns matching icon names.',
35
+ inputSchema: {terms: z.union([z.string(), z.array(z.string())])},
36
+ annotations: {readOnlyHint: true, openWorldHint: true}
37
+ },
38
+ async ({terms}) => {
39
+ const allNames = listIconNames();
40
+ const nameSet = new Set(allNames);
41
+ const aliases = await loadIconAliases();
42
+ const rawTerms = Array.isArray(terms) ? terms : [terms];
43
+ const normalized = Array.from(
44
+ new Set(
45
+ rawTerms
46
+ .map(t =>
47
+ String(t ?? '')
48
+ .trim()
49
+ .toLowerCase()
50
+ )
51
+ .filter(Boolean)
52
+ )
53
+ );
54
+ if (normalized.length === 0) {
55
+ throw new Error('Provide at least one non-empty search term.');
56
+ }
57
+ // direct name matches
58
+ const results = new Set(
59
+ allNames.filter(name => {
60
+ const nameLower = name.toLowerCase();
61
+ return normalized.some(term => nameLower.includes(term));
62
+ })
63
+ );
64
+ // alias matches
65
+ for (const [aliasKey, targets] of Object.entries(aliases)) {
66
+ if (!targets || targets.length === 0) {
67
+ continue;
68
+ }
69
+ const aliasLower = aliasKey.toLowerCase();
70
+ if (normalized.some(term => aliasLower.includes(term) || term.includes(aliasLower))) {
71
+ for (const t of targets) {
72
+ const n = String(t);
73
+ if (nameSet.has(n)) {
74
+ results.add(n);
75
+ }
76
+ }
48
77
  }
49
78
  }
79
+ return {
80
+ content: [
81
+ {
82
+ type: 'text',
83
+ text: JSON.stringify(
84
+ Array.from(results).sort((a, b) => a.localeCompare(b)),
85
+ null,
86
+ 2
87
+ )
88
+ }
89
+ ]
90
+ };
50
91
  }
51
- return {content: [{type: 'text', text: JSON.stringify(Array.from(results).sort((a, b) => a.localeCompare(b)), null, 2)}]};
52
- }
53
- );
92
+ );
54
93
 
55
- server.registerTool(
56
- 'search_s2_illustrations',
57
- {
58
- title: 'Search S2 illustrations',
59
- description: 'Searches the S2 illustrations set by one or more terms; returns matching illustration names.',
60
- inputSchema: {terms: z.union([z.string(), z.array(z.string())])}
61
- },
62
- async ({terms}) => {
63
- const allNames = listIllustrationNames();
64
- const nameSet = new Set(allNames);
65
- const aliases = await loadIllustrationAliases();
66
- const rawTerms = Array.isArray(terms) ? terms : [terms];
67
- const normalized = Array.from(new Set(rawTerms.map(t => String(t ?? '').trim().toLowerCase()).filter(Boolean)));
68
- if (normalized.length === 0) {
69
- throw new Error('Provide at least one non-empty search term.');
70
- }
71
- // direct name matches
72
- const results = new Set(allNames.filter(name => {
73
- const nameLower = name.toLowerCase();
74
- return normalized.some(term => nameLower.includes(term));
75
- }));
76
- // alias matches
77
- for (const [aliasKey, targets] of Object.entries(aliases)) {
78
- if (!targets || targets.length === 0) {continue;}
79
- const aliasLower = aliasKey.toLowerCase();
80
- if (normalized.some(term => aliasLower.includes(term) || term.includes(aliasLower))) {
81
- for (const t of targets) {
82
- const n = String(t);
83
- if (nameSet.has(n)) {results.add(n);}
94
+ server.registerTool(
95
+ 'search_s2_illustrations',
96
+ {
97
+ title: 'Search S2 illustrations',
98
+ description:
99
+ 'Searches the S2 illustrations set by one or more terms; returns matching illustration names.',
100
+ inputSchema: {terms: z.union([z.string(), z.array(z.string())])},
101
+ annotations: {readOnlyHint: true, openWorldHint: true}
102
+ },
103
+ async ({terms}) => {
104
+ const allNames = listIllustrationNames();
105
+ const nameSet = new Set(allNames);
106
+ const aliases = await loadIllustrationAliases();
107
+ const rawTerms = Array.isArray(terms) ? terms : [terms];
108
+ const normalized = Array.from(
109
+ new Set(
110
+ rawTerms
111
+ .map(t =>
112
+ String(t ?? '')
113
+ .trim()
114
+ .toLowerCase()
115
+ )
116
+ .filter(Boolean)
117
+ )
118
+ );
119
+ if (normalized.length === 0) {
120
+ throw new Error('Provide at least one non-empty search term.');
121
+ }
122
+ // direct name matches
123
+ const results = new Set(
124
+ allNames.filter(name => {
125
+ const nameLower = name.toLowerCase();
126
+ return normalized.some(term => nameLower.includes(term));
127
+ })
128
+ );
129
+ // alias matches
130
+ for (const [aliasKey, targets] of Object.entries(aliases)) {
131
+ if (!targets || targets.length === 0) {
132
+ continue;
133
+ }
134
+ const aliasLower = aliasKey.toLowerCase();
135
+ if (normalized.some(term => aliasLower.includes(term) || term.includes(aliasLower))) {
136
+ for (const t of targets) {
137
+ const n = String(t);
138
+ if (nameSet.has(n)) {
139
+ results.add(n);
140
+ }
141
+ }
84
142
  }
85
143
  }
144
+ return {
145
+ content: [
146
+ {
147
+ type: 'text',
148
+ text: JSON.stringify(
149
+ Array.from(results).sort((a, b) => a.localeCompare(b)),
150
+ null,
151
+ 2
152
+ )
153
+ }
154
+ ]
155
+ };
86
156
  }
87
- return {content: [{type: 'text', text: JSON.stringify(Array.from(results).sort((a, b) => a.localeCompare(b)), null, 2)}]};
88
- }
89
- );
157
+ );
90
158
 
91
- server.registerTool(
92
- 'get_style_macro_property_values',
93
- {
94
- title: 'Get style macro property values',
95
- description: 'Returns the allowed values for a given S2 style macro property (including expanded color/spacing value lists where applicable).',
96
- inputSchema: {propertyName: z.string()}
97
- },
98
- async ({propertyName}) => {
99
- const name = String(propertyName ?? '').trim();
100
- if (!name) {
101
- throw new Error('Provide a non-empty propertyName.');
102
- }
159
+ server.registerTool(
160
+ 'get_style_macro_property_values',
161
+ {
162
+ title: 'Get style macro property values',
163
+ description:
164
+ 'Returns the allowed values for a given S2 style macro property (including expanded color/spacing value lists where applicable).',
165
+ inputSchema: {propertyName: z.string()},
166
+ annotations: {readOnlyHint: true, openWorldHint: true}
167
+ },
168
+ async ({propertyName}) => {
169
+ const name = String(propertyName ?? '').trim();
170
+ if (!name) {
171
+ throw new Error('Provide a non-empty propertyName.');
172
+ }
103
173
 
104
- const all = loadStyleMacroPropertyValues();
105
- let def = all[name];
106
- if (!def) {
107
- // fallback to case-insensitive lookup
108
- const lower = name.toLowerCase();
109
- const matchKey = Object.keys(all).find(k => k.toLowerCase() === lower);
110
- if (matchKey) {
111
- def = all[matchKey];
174
+ const all = loadStyleMacroPropertyValues();
175
+ let def = all[name];
176
+ if (!def) {
177
+ // fallback to case-insensitive lookup
178
+ const lower = name.toLowerCase();
179
+ const matchKey = Object.keys(all).find(k => k.toLowerCase() === lower);
180
+ if (matchKey) {
181
+ def = all[matchKey];
182
+ }
112
183
  }
113
- }
114
184
 
115
- if (!def) {
116
- const available = Object.keys(all).sort((a, b) => a.localeCompare(b));
117
- throw new Error(`Unknown style macro property '${name}'. Available properties: ${available.join(', ')}`);
118
- }
185
+ if (!def) {
186
+ const available = Object.keys(all).sort((a, b) => a.localeCompare(b));
187
+ throw new Error(
188
+ `Unknown style macro property '${name}'. Available properties: ${available.join(', ')}`
189
+ );
190
+ }
119
191
 
120
- return {content: [{type: 'text', text: JSON.stringify(def, null, 2)}]};
121
- }
122
- );
192
+ return {content: [{type: 'text', text: JSON.stringify(def, null, 2)}]};
193
+ }
194
+ );
195
+ }
123
196
  });
124
197
  } catch (err) {
125
198
  console.error(errorToString(err));
package/src/s2-data.ts CHANGED
@@ -9,13 +9,18 @@ let iconIdCache: string[] | null = null;
9
9
  let illustrationIdCache: string[] | null = null;
10
10
  let iconAliasesCache: Record<string, string[]> | null = null;
11
11
  let illustrationAliasesCache: Record<string, string[]> | null = null;
12
- let styleMacroPropertyValuesCache: Record<string, {values: string[], additionalTypes?: string[]}> | null = null;
12
+ let styleMacroPropertyValuesCache: Record<
13
+ string,
14
+ {values: string[]; additionalTypes?: string[]}
15
+ > | null = null;
13
16
 
14
17
  function readBundledJson(filename: string): any | null {
15
18
  try {
16
19
  // Go up from s2/src/ to dist/, then to data/
17
20
  const p = path.resolve(__dirname, '..', '..', 'data', filename);
18
- if (!fs.existsSync(p)) {return null;}
21
+ if (!fs.existsSync(p)) {
22
+ return null;
23
+ }
19
24
  const txt = fs.readFileSync(p, 'utf8');
20
25
  return JSON.parse(txt);
21
26
  } catch {
@@ -24,31 +29,48 @@ function readBundledJson(filename: string): any | null {
24
29
  }
25
30
 
26
31
  export function listIconNames(): string[] {
27
- if (iconIdCache) {return iconIdCache;}
32
+ if (iconIdCache) {
33
+ return iconIdCache;
34
+ }
28
35
  const bundled = readBundledJson('icons.json');
29
- return (iconIdCache = Array.isArray(bundled) ? bundled.slice().sort((a, b) => a.localeCompare(b)) : []);
36
+ return (iconIdCache = Array.isArray(bundled)
37
+ ? bundled.slice().sort((a, b) => a.localeCompare(b))
38
+ : []);
30
39
  }
31
40
 
32
41
  export function listIllustrationNames(): string[] {
33
- if (illustrationIdCache) {return illustrationIdCache;}
42
+ if (illustrationIdCache) {
43
+ return illustrationIdCache;
44
+ }
34
45
  const bundled = readBundledJson('illustrations.json');
35
- return (illustrationIdCache = Array.isArray(bundled) ? bundled.slice().sort((a, b) => a.localeCompare(b)) : []);
46
+ return (illustrationIdCache = Array.isArray(bundled)
47
+ ? bundled.slice().sort((a, b) => a.localeCompare(b))
48
+ : []);
36
49
  }
37
50
 
38
51
  export async function loadIconAliases(): Promise<Record<string, string[]>> {
39
- if (iconAliasesCache) {return iconAliasesCache;}
52
+ if (iconAliasesCache) {
53
+ return iconAliasesCache;
54
+ }
40
55
  const bundled = readBundledJson('iconAliases.json');
41
- return (iconAliasesCache = (bundled && typeof bundled === 'object') ? bundled : {});
56
+ return (iconAliasesCache = bundled && typeof bundled === 'object' ? bundled : {});
42
57
  }
43
58
 
44
59
  export async function loadIllustrationAliases(): Promise<Record<string, string[]>> {
45
- if (illustrationAliasesCache) {return illustrationAliasesCache;}
60
+ if (illustrationAliasesCache) {
61
+ return illustrationAliasesCache;
62
+ }
46
63
  const bundled = readBundledJson('illustrationAliases.json');
47
- return (illustrationAliasesCache = (bundled && typeof bundled === 'object') ? bundled : {});
64
+ return (illustrationAliasesCache = bundled && typeof bundled === 'object' ? bundled : {});
48
65
  }
49
66
 
50
- export function loadStyleMacroPropertyValues(): Record<string, {values: string[], additionalTypes?: string[]}> {
51
- if (styleMacroPropertyValuesCache) {return styleMacroPropertyValuesCache;}
67
+ export function loadStyleMacroPropertyValues(): Record<
68
+ string,
69
+ {values: string[]; additionalTypes?: string[]}
70
+ > {
71
+ if (styleMacroPropertyValuesCache) {
72
+ return styleMacroPropertyValuesCache;
73
+ }
52
74
  const bundled = readBundledJson('styleMacroPropertyValues.json');
53
75
  if (!bundled || typeof bundled !== 'object' || Array.isArray(bundled)) {
54
76
  return (styleMacroPropertyValuesCache = {});