(server: McpServer, library: Library)
| 33 | } |
| 34 | |
| 35 | async function registerLibraryDocsTools(server: McpServer, library: Library) { |
| 36 | // Build page index at startup. |
| 37 | try { |
| 38 | await buildPageIndex(library); |
| 39 | } catch (e) { |
| 40 | console.warn(`Warning: failed to load ${library} docs index (${errorToString(e)}).`); |
| 41 | } |
| 42 | |
| 43 | const toolPrefix = library === 's2' ? 's2' : 'react_aria'; |
| 44 | |
| 45 | server.registerTool( |
| 46 | `list_${toolPrefix}_pages`, |
| 47 | { |
| 48 | title: |
| 49 | library === 's2' |
| 50 | ? 'List React Spectrum (@react-spectrum/s2) docs pages' |
| 51 | : 'List React Aria docs pages', |
| 52 | description: `Returns a list of available pages in the ${library} docs.`, |
| 53 | inputSchema: {includeDescription: z.boolean().optional()}, |
| 54 | annotations: {readOnlyHint: true, openWorldHint: true} |
| 55 | }, |
| 56 | async ({includeDescription}) => { |
| 57 | const pages = await buildPageIndex(library); |
| 58 | const items = pages |
| 59 | .sort((a, b) => a.key.localeCompare(b.key)) |
| 60 | .map(p => |
| 61 | includeDescription ? {name: p.name, description: p.description ?? ''} : {name: p.name} |
| 62 | ); |
| 63 | return { |
| 64 | content: [{type: 'text', text: JSON.stringify(items, null, 2)}] |
| 65 | }; |
| 66 | } |
| 67 | ); |
| 68 | |
| 69 | server.registerTool( |
| 70 | `get_${toolPrefix}_page_info`, |
| 71 | { |
| 72 | title: 'Get page info', |
| 73 | description: 'Returns page description and list of sections for a given page.', |
| 74 | inputSchema: {page_name: z.string()}, |
| 75 | annotations: {readOnlyHint: true, openWorldHint: true} |
| 76 | }, |
| 77 | async ({page_name}) => { |
| 78 | const ref = await resolvePageRef(library, page_name); |
| 79 | const info = await ensureParsedPage(ref); |
| 80 | const out = { |
| 81 | name: info.name, |
| 82 | description: info.description ?? '', |
| 83 | sections: info.sections.map(s => s.name) |
| 84 | }; |
| 85 | return {content: [{type: 'text', text: JSON.stringify(out, null, 2)}]}; |
| 86 | } |
| 87 | ); |
| 88 | |
| 89 | server.registerTool( |
| 90 | `get_${toolPrefix}_page`, |
| 91 | { |
| 92 | title: 'Get page markdown', |
no test coverage detected