|
| 1 | +import type { Collections } from '@nuxt/content' |
| 2 | +import { queryCollection } from '@nuxt/content/server' |
| 3 | +import { joinURL, withLeadingSlash } from 'ufo' |
| 4 | +import { z } from 'zod' |
| 5 | +import { inferSiteURL } from '../../../utils/meta' |
| 6 | +import { getAvailableLocales, getCollectionFromPath, isNavigationPath } from '../../utils/content' |
| 7 | + |
| 8 | +export default defineMcpTool({ |
| 9 | + description: `Retrieves the full content and details of a specific documentation page. |
| 10 | +
|
| 11 | +WHEN TO USE: Use this tool when you know the EXACT path to a documentation page. Common use cases: |
| 12 | +- User asks for a specific page: "Show me the getting started guide" -> /en/getting-started/installation |
| 13 | +- User asks about a known topic with a dedicated page |
| 14 | +- You found a relevant path from list-pages and want the full content |
| 15 | +- User references a specific section or guide they want to read |
| 16 | +
|
| 17 | +WHEN NOT TO USE: If you don't know the exact path and need to search/explore, use list-pages first. |
| 18 | +
|
| 19 | +WORKFLOW: This tool returns the complete page content including title, description, and full markdown. Use this when you need to provide detailed answers or code examples from specific documentation pages. |
| 20 | +`, |
| 21 | + annotations: { |
| 22 | + readOnlyHint: true, |
| 23 | + destructiveHint: false, |
| 24 | + idempotentHint: true, |
| 25 | + openWorldHint: false, |
| 26 | + }, |
| 27 | + inputSchema: { |
| 28 | + path: z.string().describe( |
| 29 | + 'The page path from list-pages or provided by the user (e.g., /en/getting-started/installation)', |
| 30 | + ), |
| 31 | + }, |
| 32 | + inputExamples: [ |
| 33 | + { path: '/en/getting-started/installation' }, |
| 34 | + { path: '/getting-started/introduction' }, |
| 35 | + ], |
| 36 | + cache: '1h', |
| 37 | + handler: async ({ path }) => { |
| 38 | + if (isNavigationPath(path)) |
| 39 | + throw createError({ statusCode: 404, message: 'Page not found' }) |
| 40 | + |
| 41 | + const event = useEvent() |
| 42 | + const config = useRuntimeConfig(event) |
| 43 | + const publicConfig = config.public as Record<string, unknown> |
| 44 | + const siteUrl = getRequestURL(event).origin || inferSiteURL() |
| 45 | + const baseURL = config.app?.baseURL || '/' |
| 46 | + |
| 47 | + const availableLocales = getAvailableLocales(publicConfig) |
| 48 | + const collectionName = availableLocales.length > 0 |
| 49 | + ? getCollectionFromPath(path, availableLocales) |
| 50 | + : 'docs' |
| 51 | + |
| 52 | + try { |
| 53 | + const page = await queryCollection(event, collectionName as keyof Collections) |
| 54 | + .path(path) |
| 55 | + .select('title', 'path', 'description') |
| 56 | + .first() as { title: string, path: string, description: string } | null |
| 57 | + |
| 58 | + if (!page) |
| 59 | + throw createError({ statusCode: 404, message: 'Page not found' }) |
| 60 | + |
| 61 | + // Use the canonical `page.path` from the database (not the raw user |
| 62 | + // input `path`) so non-canonical casing or trailing slashes don't |
| 63 | + // produce a 404 when fetching the raw markdown. Normalize so we always |
| 64 | + // produce `/raw<path>.md` with a single leading slash. |
| 65 | + const rawPath = `/raw${withLeadingSlash(page.path)}.md` |
| 66 | + const content = await event.$fetch<string>(rawPath) |
| 67 | + |
| 68 | + return { |
| 69 | + title: page.title, |
| 70 | + path: page.path, |
| 71 | + description: page.description, |
| 72 | + content, |
| 73 | + url: siteUrl ? joinURL(siteUrl, baseURL, page.path) : joinURL(baseURL, page.path), |
| 74 | + } |
| 75 | + } |
| 76 | + catch (error) { |
| 77 | + if ( |
| 78 | + error |
| 79 | + && typeof error === 'object' |
| 80 | + && 'statusCode' in error |
| 81 | + && (error as { statusCode?: number }).statusCode === 404 |
| 82 | + ) { |
| 83 | + throw error |
| 84 | + } |
| 85 | + throw createError({ statusCode: 500, message: 'Failed to get page' }) |
| 86 | + } |
| 87 | + }, |
| 88 | +}) |
0 commit comments