@react-spectrum/mcp 0.1.0 → 1.0.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 +58 -45
- package/dist/data/icons.json +7 -0
- package/dist/data/illustrations.json +2 -0
- package/dist/data/styleMacroPropertyValues.json +5947 -0
- package/dist/data/styleProperties.json +4063 -0
- package/dist/s2/src/index.js +117 -0
- package/dist/s2/src/s2-data.js +62 -0
- package/dist/shared/src/page-manager.js +83 -0
- package/dist/shared/src/parser.js +61 -0
- package/dist/shared/src/server.js +76 -0
- package/dist/shared/src/types.js +1 -0
- package/dist/shared/src/utils.js +30 -0
- package/package.json +10 -10
- package/src/index.ts +107 -401
- package/src/s2-data.ts +57 -0
- package/dist/index.js +0 -395
- package/dist/index.js.map +0 -1
package/src/index.ts
CHANGED
|
@@ -1,420 +1,126 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
2
|
/// <reference types="node" />
|
|
3
|
-
import {
|
|
4
|
-
import
|
|
5
|
-
import {McpServer} from '@modelcontextprotocol/sdk/server/mcp.js';
|
|
6
|
-
import
|
|
7
|
-
import {StdioServerTransport} from '@modelcontextprotocol/sdk/server/stdio.js';
|
|
3
|
+
import {errorToString} from '../../shared/src/utils.js';
|
|
4
|
+
import {listIconNames, listIllustrationNames, loadIconAliases, loadIllustrationAliases, loadStyleMacroPropertyValues} from './s2-data.js';
|
|
5
|
+
import type {McpServer} from '@modelcontextprotocol/sdk/server/mcp.js';
|
|
6
|
+
import {startServer} from '../../shared/src/server.js';
|
|
8
7
|
import {z} from 'zod';
|
|
9
8
|
|
|
10
|
-
|
|
11
|
-
|
|
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.
|
|
9
|
+
// CLI entry for S2
|
|
10
|
+
(async () => {
|
|
245
11
|
try {
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
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;
|
|
12
|
+
const arg = (process.argv[2] || '').trim();
|
|
13
|
+
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.');
|
|
15
|
+
process.exit(0);
|
|
320
16
|
}
|
|
321
|
-
);
|
|
322
17
|
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
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);}
|
|
48
|
+
}
|
|
354
49
|
}
|
|
355
50
|
}
|
|
51
|
+
return {content: [{type: 'text', text: JSON.stringify(Array.from(results).sort((a, b) => a.localeCompare(b)), null, 2)}]};
|
|
356
52
|
}
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
const n = String(t);
|
|
390
|
-
if (nameSet.has(n)) {results.add(n);}
|
|
53
|
+
);
|
|
54
|
+
|
|
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);}
|
|
84
|
+
}
|
|
391
85
|
}
|
|
392
86
|
}
|
|
87
|
+
return {content: [{type: 'text', text: JSON.stringify(Array.from(results).sort((a, b) => a.localeCompare(b)), null, 2)}]};
|
|
393
88
|
}
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
89
|
+
);
|
|
90
|
+
|
|
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
|
+
}
|
|
398
103
|
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
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];
|
|
112
|
+
}
|
|
113
|
+
}
|
|
402
114
|
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
}
|
|
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
|
+
}
|
|
407
119
|
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
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);
|
|
120
|
+
return {content: [{type: 'text', text: JSON.stringify(def, null, 2)}]};
|
|
121
|
+
}
|
|
122
|
+
);
|
|
123
|
+
});
|
|
418
124
|
} catch (err) {
|
|
419
125
|
console.error(errorToString(err));
|
|
420
126
|
process.exit(1);
|
package/src/s2-data.ts
ADDED
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
import {fileURLToPath} from 'url';
|
|
2
|
+
import fs from 'fs';
|
|
3
|
+
import path from 'path';
|
|
4
|
+
|
|
5
|
+
const __filename = fileURLToPath(import.meta.url);
|
|
6
|
+
const __dirname = path.dirname(__filename);
|
|
7
|
+
|
|
8
|
+
let iconIdCache: string[] | null = null;
|
|
9
|
+
let illustrationIdCache: string[] | null = null;
|
|
10
|
+
let iconAliasesCache: Record<string, string[]> | null = null;
|
|
11
|
+
let illustrationAliasesCache: Record<string, string[]> | null = null;
|
|
12
|
+
let styleMacroPropertyValuesCache: Record<string, {values: string[], additionalTypes?: string[]}> | null = null;
|
|
13
|
+
|
|
14
|
+
function readBundledJson(filename: string): any | null {
|
|
15
|
+
try {
|
|
16
|
+
// Go up from s2/src/ to dist/, then to data/
|
|
17
|
+
const p = path.resolve(__dirname, '..', '..', 'data', filename);
|
|
18
|
+
if (!fs.existsSync(p)) {return null;}
|
|
19
|
+
const txt = fs.readFileSync(p, 'utf8');
|
|
20
|
+
return JSON.parse(txt);
|
|
21
|
+
} catch {
|
|
22
|
+
return null;
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
export function listIconNames(): string[] {
|
|
27
|
+
if (iconIdCache) {return iconIdCache;}
|
|
28
|
+
const bundled = readBundledJson('icons.json');
|
|
29
|
+
return (iconIdCache = Array.isArray(bundled) ? bundled.slice().sort((a, b) => a.localeCompare(b)) : []);
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
export function listIllustrationNames(): string[] {
|
|
33
|
+
if (illustrationIdCache) {return illustrationIdCache;}
|
|
34
|
+
const bundled = readBundledJson('illustrations.json');
|
|
35
|
+
return (illustrationIdCache = Array.isArray(bundled) ? bundled.slice().sort((a, b) => a.localeCompare(b)) : []);
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
export async function loadIconAliases(): Promise<Record<string, string[]>> {
|
|
39
|
+
if (iconAliasesCache) {return iconAliasesCache;}
|
|
40
|
+
const bundled = readBundledJson('iconAliases.json');
|
|
41
|
+
return (iconAliasesCache = (bundled && typeof bundled === 'object') ? bundled : {});
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
export async function loadIllustrationAliases(): Promise<Record<string, string[]>> {
|
|
45
|
+
if (illustrationAliasesCache) {return illustrationAliasesCache;}
|
|
46
|
+
const bundled = readBundledJson('illustrationAliases.json');
|
|
47
|
+
return (illustrationAliasesCache = (bundled && typeof bundled === 'object') ? bundled : {});
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
export function loadStyleMacroPropertyValues(): Record<string, {values: string[], additionalTypes?: string[]}> {
|
|
51
|
+
if (styleMacroPropertyValuesCache) {return styleMacroPropertyValuesCache;}
|
|
52
|
+
const bundled = readBundledJson('styleMacroPropertyValues.json');
|
|
53
|
+
if (!bundled || typeof bundled !== 'object' || Array.isArray(bundled)) {
|
|
54
|
+
return (styleMacroPropertyValuesCache = {});
|
|
55
|
+
}
|
|
56
|
+
return (styleMacroPropertyValuesCache = bundled as any);
|
|
57
|
+
}
|