|
1 | 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'; |
2 | 5 | import { createMcpHandler } from 'mcp-handler'; |
3 | 6 | import type { NextRequest } from 'next/server'; |
4 | 7 | import { z } from 'zod'; |
5 | 8 |
|
6 | 9 | async function handler(request: NextRequest, { params }: { params: Promise<RouteLayoutParams> }) { |
7 | 10 | const { context } = await getStaticSiteContext(await params); |
| 11 | + const { dataFetcher, linker, site } = context; |
8 | 12 |
|
9 | 13 | const mcpHandler = createMcpHandler( |
10 | 14 | (server) => { |
11 | 15 | server.tool( |
12 | | - 'roll_dice', |
13 | | - 'Rolls an N-sided die', |
| 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.`, |
14 | 18 | { |
15 | | - sides: z.number().int().min(2), |
| 19 | + query: z.string(), |
16 | 20 | }, |
17 | | - async ({ sides }) => { |
18 | | - const value = 1 + Math.floor(Math.random() * sides); |
| 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 | + |
19 | 31 | return { |
20 | | - content: [{ type: 'text', text: `🎲 You rolled a ${value}!` }], |
| 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 | + }), |
21 | 65 | }; |
22 | 66 | } |
23 | 67 | ); |
|
0 commit comments