@react-spectrum/mcp 0.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/src/index.ts ADDED
@@ -0,0 +1,422 @@
1
+ #!/usr/bin/env node
2
+ /// <reference types="node" />
3
+ import {fileURLToPath} from 'url';
4
+ import fs from 'fs';
5
+ import {McpServer} from '@modelcontextprotocol/sdk/server/mcp.js';
6
+ import path from 'path';
7
+ import {StdioServerTransport} from '@modelcontextprotocol/sdk/server/stdio.js';
8
+ import {z} from 'zod';
9
+
10
+ type SectionInfo = {
11
+ name: string,
12
+ startLine: number, // 0-based index where section heading starts
13
+ endLine: number // exclusive end line index for section content
14
+ };
15
+
16
+ type PageInfo = {
17
+ key: string, // e.g. "s2/Button"
18
+ title: string, // from top-level heading
19
+ description?: string, // first paragraph after title
20
+ filePath: string, // absolute path to markdown file
21
+ sections: SectionInfo[]
22
+ };
23
+
24
+ type Library = 's2' | 'react-aria';
25
+
26
+ function errorToString(err: unknown): string {
27
+ if (err && typeof err === 'object' && 'stack' in err && typeof (err as any).stack === 'string') {
28
+ return (err as any).stack as string;
29
+ }
30
+ if (err && typeof err === 'object' && 'message' in err && typeof (err as any).message === 'string') {
31
+ return (err as any).message as string;
32
+ }
33
+ try {
34
+ return JSON.stringify(err);
35
+ } catch {
36
+ return String(err);
37
+ }
38
+ }
39
+
40
+ const __filename = fileURLToPath(import.meta.url);
41
+ const __dirname = path.dirname(__filename);
42
+
43
+ // CDN base for docs. Can be overridden via env variable.
44
+ const DEFAULT_CDN_BASE = process.env.DOCS_CDN_BASE
45
+ ?? 'https://reactspectrum.blob.core.windows.net/reactspectrum/7d2883a56fb1a0554864b21324d405f758deb3ce/s2-docs';
46
+
47
+ function libBaseUrl(library: Library) {
48
+ return `${DEFAULT_CDN_BASE}/${library}`;
49
+ }
50
+
51
+ async function fetchText(url: string, timeoutMs = 15000): Promise<string> {
52
+ const ctrl = new AbortController();
53
+ const id = setTimeout(() => ctrl.abort(), timeoutMs).unref?.();
54
+ try {
55
+ const res = await fetch(url, {signal: ctrl.signal, cache: 'no-store'} as any);
56
+ if (!res.ok) {
57
+ throw new Error(`HTTP ${res.status} for ${url}`);
58
+ }
59
+ return await res.text();
60
+ } finally {
61
+ clearTimeout(id as any);
62
+ }
63
+ }
64
+
65
+ // Cache of parsed pages
66
+ const pageCache = new Map<string, PageInfo>();
67
+
68
+ let iconIdCache: string[] | null = null;
69
+ let illustrationIdCache: string[] | null = null;
70
+ let iconAliasesCache: Record<string, string[]> | null = null;
71
+ let illustrationAliasesCache: Record<string, string[]> | null = null;
72
+
73
+ function readBundledJson(filename: string): any | null {
74
+ try {
75
+ const p = path.resolve(__dirname, 'data', filename); // dist/data
76
+ if (!fs.existsSync(p)) {return null;}
77
+ const txt = fs.readFileSync(p, 'utf8');
78
+ return JSON.parse(txt);
79
+ } catch {
80
+ return null;
81
+ }
82
+ }
83
+
84
+ function listIconNames(): string[] {
85
+ if (iconIdCache) {return iconIdCache;}
86
+ const bundled = readBundledJson('icons.json');
87
+ return (iconIdCache = Array.isArray(bundled) ? bundled.slice().sort((a, b) => a.localeCompare(b)) : []);
88
+ }
89
+
90
+ function listIllustrationNames(): string[] {
91
+ if (illustrationIdCache) {return illustrationIdCache;}
92
+ const bundled = readBundledJson('illustrations.json');
93
+ return (illustrationIdCache = Array.isArray(bundled) ? bundled.slice().sort((a, b) => a.localeCompare(b)) : []);
94
+ }
95
+
96
+ async function loadIconAliases(): Promise<Record<string, string[]>> {
97
+ if (iconAliasesCache) {return iconAliasesCache;}
98
+ const bundled = readBundledJson('iconAliases.json');
99
+ return (iconAliasesCache = (bundled && typeof bundled === 'object') ? bundled : {});
100
+ }
101
+
102
+ async function loadIllustrationAliases(): Promise<Record<string, string[]>> {
103
+ if (illustrationAliasesCache) {return illustrationAliasesCache;}
104
+ const bundled = readBundledJson('illustrationAliases.json');
105
+ return (illustrationAliasesCache = (bundled && typeof bundled === 'object') ? bundled : {});
106
+ }
107
+
108
+ // Whether we've loaded the page index for a library yet.
109
+ const pageIndexLoaded = new Set<Library>();
110
+
111
+ // Build a lightweight index of pages for the given library from the CDN's llms.txt.
112
+ // Populates pageCache with stubs (title from filename; description/sections omitted).
113
+ async function buildPageIndex(library: Library): Promise<PageInfo[]> {
114
+ if (pageIndexLoaded.has(library)) {
115
+ return Array.from(pageCache.values()).filter(p => p.key.startsWith(`${library}/`));
116
+ }
117
+
118
+ const pages: PageInfo[] = [];
119
+
120
+ // Read llms.txt to enumerate available pages without downloading them all.
121
+ const llmsUrl = `${libBaseUrl(library)}/llms.txt`;
122
+ const txt = await fetchText(llmsUrl);
123
+ const re = /^\s*-\s*\[([^\]]+)\]\(([^)]+)\)\s*$/;
124
+ for (const line of txt.split(/\r?\n/)) {
125
+ const m = line.match(re);
126
+ if (!m) {continue;}
127
+ const display = (m[1] || '').trim();
128
+ const href = (m[2] || '').trim();
129
+ if (!href || !/\.md$/i.test(href)) {continue;}
130
+ const key = href.replace(/\.md$/i, '').replace(/\\/g, '/');
131
+ const title = display || path.basename(key);
132
+ const url = `${DEFAULT_CDN_BASE}/${key}.md`;
133
+ const info: PageInfo = {key, title, description: undefined, filePath: url, sections: []};
134
+ pages.push(info);
135
+ pageCache.set(info.key, info);
136
+ }
137
+
138
+ pageIndexLoaded.add(library);
139
+ return pages.sort((a, b) => a.key.localeCompare(b.key));
140
+ }
141
+
142
+ function parseSectionsFromMarkdown(lines: string[]): SectionInfo[] {
143
+ const sections: SectionInfo[] = [];
144
+ let inCode = false;
145
+ for (let idx = 0; idx < lines.length; idx++) {
146
+ const line = lines[idx];
147
+ if (/^```/.test(line.trim())) {inCode = !inCode;}
148
+ if (inCode) {continue;}
149
+ if (line.startsWith('## ')) {
150
+ const name = line.replace(/^##\s+/, '').trim();
151
+ sections.push({name, startLine: idx, endLine: lines.length});
152
+ }
153
+ }
154
+ for (let s = 0; s < sections.length - 1; s++) {
155
+ sections[s].endLine = sections[s + 1].startLine;
156
+ }
157
+ return sections;
158
+ }
159
+
160
+ function extractTitleAndDescription(lines: string[]): {title: string, description?: string} {
161
+ let title = '';
162
+ let description: string | undefined = undefined;
163
+
164
+ let i = 0;
165
+ for (; i < lines.length; i++) {
166
+ const line = lines[i];
167
+ if (line.startsWith('# ')) {
168
+ title = line.replace(/^#\s+/, '').trim();
169
+ i++;
170
+ break;
171
+ }
172
+ }
173
+
174
+ let descLines: string[] = [];
175
+ let inCode = false;
176
+ for (; i < lines.length; i++) {
177
+ const line = lines[i];
178
+ if (/^```/.test(line.trim())) {inCode = !inCode;}
179
+ if (inCode) {continue;}
180
+ if (line.trim() === '') {
181
+ if (descLines.length > 0) {break;} else {continue;}
182
+ }
183
+ if (/^#{1,6}\s/.test(line) || /^</.test(line.trim())) {continue;}
184
+ descLines.push(line);
185
+ }
186
+ if (descLines.length > 0) {
187
+ description = descLines.join('\n').trim();
188
+ }
189
+
190
+ return {title, description};
191
+ }
192
+
193
+ async function ensureParsedPage(info: PageInfo): Promise<PageInfo> {
194
+ if (info.sections && info.sections.length > 0 && info.description !== undefined) {
195
+ return info;
196
+ }
197
+
198
+ const text = await fetchText(info.filePath);
199
+ const lines = text.split(/\r?\n/);
200
+ const {title, description} = extractTitleAndDescription(lines);
201
+ const sections = parseSectionsFromMarkdown(lines);
202
+ const updated = {...info, title: title || info.title, description, sections};
203
+ pageCache.set(updated.key, updated);
204
+ return updated;
205
+ }
206
+
207
+ async function resolvePageRef(library: Library, pageName: string): Promise<PageInfo> {
208
+ // Ensure index is loaded
209
+ await buildPageIndex(library);
210
+
211
+ if (pageCache.has(pageName)) {
212
+ return pageCache.get(pageName)!;
213
+ }
214
+
215
+ if (pageName.includes('/')) {
216
+ const normalized = pageName.replace(/\\/g, '/');
217
+ const prefix = normalized.split('/', 1)[0];
218
+ if (prefix !== library) {
219
+ throw new Error(`Page '${pageName}' is not in the '${library}' library.`);
220
+ }
221
+ const maybe = pageCache.get(normalized);
222
+ if (maybe) {return maybe;}
223
+ const filePath = `${DEFAULT_CDN_BASE}/${normalized}.md`;
224
+ const stub: PageInfo = {key: normalized, title: path.basename(normalized), description: undefined, filePath, sections: []};
225
+ pageCache.set(stub.key, stub);
226
+ return stub;
227
+ }
228
+
229
+ const key = `${library}/${pageName}`;
230
+ const maybe = pageCache.get(key);
231
+ if (maybe) {return maybe;}
232
+ const filePath = `${DEFAULT_CDN_BASE}/${key}.md`;
233
+ const stub: PageInfo = {key, title: pageName, description: undefined, filePath, sections: []};
234
+ pageCache.set(stub.key, stub);
235
+ return stub;
236
+ }
237
+
238
+ async function startServer(library: Library) {
239
+ const server = new McpServer({
240
+ name: library === 's2' ? 's2-docs-server' : 'react-aria-docs-server',
241
+ version: '0.1.0'
242
+ });
243
+
244
+ // Build page index at startup.
245
+ try {
246
+ await buildPageIndex(library);
247
+ } catch (e) {
248
+ console.warn(`Warning: failed to load ${library} docs index (${errorToString(e)}).`);
249
+ }
250
+
251
+ // list_pages tool
252
+ server.registerTool(
253
+ 'list_pages',
254
+ {
255
+ title: library === 's2' ? 'List React Spectrum (@react-spectrum/s2) docs pages' : 'List React Aria docs pages',
256
+ description: `Returns a list of available pages in the ${library} docs.`,
257
+ inputSchema: {includeDescription: z.boolean().optional()}
258
+ },
259
+ async ({includeDescription}) => {
260
+ const pages = await buildPageIndex(library);
261
+ const items = pages
262
+ .sort((a, b) => a.key.localeCompare(b.key))
263
+ .map(p => includeDescription ? {key: p.key, title: p.title, description: p.description ?? ''} : {key: p.key, title: p.title});
264
+ return {
265
+ content: [{type: 'text', text: JSON.stringify(items, null, 2)}]
266
+ };
267
+ }
268
+ );
269
+
270
+ // get_page_info tool
271
+ server.registerTool(
272
+ 'get_page_info',
273
+ {
274
+ title: 'Get page info',
275
+ description: 'Returns page description and list of sections for a given page.',
276
+ inputSchema: {page_name: z.string()}
277
+ },
278
+ async ({page_name}) => {
279
+ const ref = await resolvePageRef(library, page_name);
280
+ const info = await ensureParsedPage(ref);
281
+ const out = {
282
+ key: info.key,
283
+ title: info.title,
284
+ description: info.description ?? '',
285
+ sections: info.sections.map(s => s.name)
286
+ };
287
+ return {content: [{type: 'text', text: JSON.stringify(out, null, 2)}]};
288
+ }
289
+ );
290
+
291
+ // get_page tool
292
+ server.registerTool(
293
+ 'get_page',
294
+ {
295
+ title: 'Get page markdown',
296
+ description: 'Returns the full markdown content for a page, or a specific section if provided.',
297
+ inputSchema: {page_name: z.string(), section_name: z.string().optional()}
298
+ },
299
+ async ({page_name, section_name}) => {
300
+ const ref = await resolvePageRef(library, page_name);
301
+ let text: string;
302
+ text = await fetchText(ref.filePath);
303
+
304
+ if (!section_name) {
305
+ return {content: [{type: 'text', text}]} as const;
306
+ }
307
+
308
+ const lines = text.split(/\r?\n/);
309
+ const sections = parseSectionsFromMarkdown(lines);
310
+ let section = sections.find(s => s.name === section_name);
311
+ if (!section) {
312
+ section = sections.find(s => s.name.toLowerCase() === section_name.toLowerCase());
313
+ }
314
+ if (!section) {
315
+ const available = sections.map(s => s.name).join(', ');
316
+ throw new Error(`Section '${section_name}' not found in ${ref.key}. Available: ${available}`);
317
+ }
318
+ const snippet = lines.slice(section.startLine, section.endLine).join('\n');
319
+ return {content: [{type: 'text', text: snippet}]} as const;
320
+ }
321
+ );
322
+
323
+ if (library === 's2') {
324
+ // search_icons tool
325
+ server.registerTool(
326
+ 'search_icons',
327
+ {
328
+ title: 'Search S2 icons',
329
+ description: 'Searches the S2 workflow icon set by one or more terms; returns matching icon names.',
330
+ inputSchema: {terms: z.union([z.string(), z.array(z.string())])}
331
+ },
332
+ async ({terms}) => {
333
+ const allNames = listIconNames();
334
+ const nameSet = new Set(allNames);
335
+ const aliases = await loadIconAliases();
336
+ const rawTerms = Array.isArray(terms) ? terms : [terms];
337
+ const normalized = Array.from(new Set(rawTerms.map(t => String(t ?? '').trim().toLowerCase()).filter(Boolean)));
338
+ if (normalized.length === 0) {
339
+ throw new Error('Provide at least one non-empty search term.');
340
+ }
341
+ // direct name matches
342
+ const results = new Set(allNames.filter(name => {
343
+ const nameLower = name.toLowerCase();
344
+ return normalized.some(term => nameLower.includes(term));
345
+ }));
346
+ // alias matches
347
+ for (const [aliasKey, targets] of Object.entries(aliases)) {
348
+ if (!targets || targets.length === 0) {continue;}
349
+ const aliasLower = aliasKey.toLowerCase();
350
+ if (normalized.some(term => aliasLower.includes(term) || term.includes(aliasLower))) {
351
+ for (const t of targets) {
352
+ const n = String(t);
353
+ if (nameSet.has(n)) {results.add(n);}
354
+ }
355
+ }
356
+ }
357
+ return {content: [{type: 'text', text: JSON.stringify(Array.from(results).sort((a, b) => a.localeCompare(b)), null, 2)}]};
358
+ }
359
+ );
360
+
361
+ // search_illustrations tool
362
+ server.registerTool(
363
+ 'search_illustrations',
364
+ {
365
+ title: 'Search S2 illustrations',
366
+ description: 'Searches the S2 illustrations set by one or more terms; returns matching illustration names.',
367
+ inputSchema: {terms: z.union([z.string(), z.array(z.string())])}
368
+ },
369
+ async ({terms}) => {
370
+ const allNames = listIllustrationNames();
371
+ const nameSet = new Set(allNames);
372
+ const aliases = await loadIllustrationAliases();
373
+ const rawTerms = Array.isArray(terms) ? terms : [terms];
374
+ const normalized = Array.from(new Set(rawTerms.map(t => String(t ?? '').trim().toLowerCase()).filter(Boolean)));
375
+ if (normalized.length === 0) {
376
+ throw new Error('Provide at least one non-empty search term.');
377
+ }
378
+ // direct name matches
379
+ const results = new Set(allNames.filter(name => {
380
+ const nameLower = name.toLowerCase();
381
+ return normalized.some(term => nameLower.includes(term));
382
+ }));
383
+ // alias matches
384
+ for (const [aliasKey, targets] of Object.entries(aliases)) {
385
+ if (!targets || targets.length === 0) {continue;}
386
+ const aliasLower = aliasKey.toLowerCase();
387
+ if (normalized.some(term => aliasLower.includes(term) || term.includes(aliasLower))) {
388
+ for (const t of targets) {
389
+ const n = String(t);
390
+ if (nameSet.has(n)) {results.add(n);}
391
+ }
392
+ }
393
+ }
394
+ return {content: [{type: 'text', text: JSON.stringify(Array.from(results).sort((a, b) => a.localeCompare(b)), null, 2)}]};
395
+ }
396
+ );
397
+ }
398
+
399
+ const transport = new StdioServerTransport();
400
+ await server.connect(transport);
401
+ }
402
+
403
+ function printUsage() {
404
+ const usage = 'Usage: mcp <subcommand>\n\nSubcommands:\n s2 Start MCP server for React Spectrum S2 docs\n react-aria Start MCP server for React Aria docs\n\nEnvironment:\n\nExamples:\n npx @react-spectrum/mcp s2\n npx @react-spectrum/mcp react-aria';
405
+ console.log(usage);
406
+ }
407
+
408
+ // CLI entry
409
+ (async () => {
410
+ try {
411
+ const arg = (process.argv[2] || '').trim();
412
+ if (arg === '--help' || arg === '-h' || arg === 'help') {
413
+ printUsage();
414
+ process.exit(0);
415
+ }
416
+ const library: Library = arg === 'react-aria' ? 'react-aria' : 's2';
417
+ await startServer(library);
418
+ } catch (err) {
419
+ console.error(errorToString(err));
420
+ process.exit(1);
421
+ }
422
+ })();