|
| 1 | +import { type RouteLayoutParams, getStaticSiteContext } from '@/app/utils'; |
| 2 | +import { throwIfDataError } from '@/lib/data'; |
| 3 | +import { joinPathWithBaseURL } from '@/lib/paths'; |
| 4 | +import { findSiteSpaceBy } from '@/lib/sites'; |
| 5 | +import { createMcpHandler } from 'mcp-handler'; |
| 6 | +import type { NextRequest } from 'next/server'; |
| 7 | +import { z } from 'zod'; |
| 8 | + |
| 9 | +async function handler(request: NextRequest, { params }: { params: Promise<RouteLayoutParams> }) { |
| 10 | + const { context } = await getStaticSiteContext(await params); |
| 11 | + const { dataFetcher, linker, site } = context; |
| 12 | + |
| 13 | + const mcpHandler = createMcpHandler( |
| 14 | + (server) => { |
| 15 | + server.tool( |
| 16 | + 'searchDocumentation', |
| 17 | + `Search across the documentation to find relevant information, code examples, API references, and guides. Use this tool when you need to answer questions about ${site.title}, find specific documentation, understand how features work, or locate implementation details. The search returns contextual content with titles and direct links to the documentation pages.`, |
| 18 | + { |
| 19 | + query: z.string(), |
| 20 | + }, |
| 21 | + async ({ query }) => { |
| 22 | + const results = await throwIfDataError( |
| 23 | + dataFetcher.searchSiteContent({ |
| 24 | + organizationId: context.organizationId, |
| 25 | + siteId: site.id, |
| 26 | + query, |
| 27 | + scope: { mode: 'all' }, |
| 28 | + }) |
| 29 | + ); |
| 30 | + |
| 31 | + return { |
| 32 | + content: results.flatMap((spaceResult) => { |
| 33 | + const found = findSiteSpaceBy( |
| 34 | + context.structure, |
| 35 | + (siteSpace) => siteSpace.space.id === spaceResult.id |
| 36 | + ); |
| 37 | + const spaceURL = found?.siteSpace.urls.published; |
| 38 | + if (!spaceURL) { |
| 39 | + return []; |
| 40 | + } |
| 41 | + |
| 42 | + return spaceResult.pages.map((pageResult) => { |
| 43 | + const pageURL = linker.toAbsoluteURL( |
| 44 | + linker.toLinkForContent( |
| 45 | + joinPathWithBaseURL(spaceURL, pageResult.path) |
| 46 | + ) |
| 47 | + ); |
| 48 | + |
| 49 | + const body = pageResult.sections |
| 50 | + ?.map((section) => section.body) |
| 51 | + .join('\n'); |
| 52 | + |
| 53 | + return { |
| 54 | + type: 'text', |
| 55 | + text: [ |
| 56 | + `Title: ${pageResult.title}`, |
| 57 | + `Link: ${pageURL}`, |
| 58 | + body ? `Content: ${body}` : '', |
| 59 | + ] |
| 60 | + .filter(Boolean) |
| 61 | + .join('\n'), |
| 62 | + }; |
| 63 | + }); |
| 64 | + }), |
| 65 | + }; |
| 66 | + } |
| 67 | + ); |
| 68 | + }, |
| 69 | + { |
| 70 | + // Optional server options |
| 71 | + }, |
| 72 | + { |
| 73 | + basePath: context.linker.toPathInSite('~gitbook/'), |
| 74 | + streamableHttpEndpoint: '/mcp', |
| 75 | + maxDuration: 60, |
| 76 | + verboseLogs: true, |
| 77 | + disableSse: true, |
| 78 | + } |
| 79 | + ); |
| 80 | + |
| 81 | + return mcpHandler(request); |
| 82 | +} |
| 83 | + |
| 84 | +export { handler as GET, handler as POST }; |
0 commit comments