|
| 1 | +/** |
| 2 | + * Copyright (c) Meta Platforms, Inc. and affiliates. |
| 3 | + * |
| 4 | + * This source code is licensed under the MIT license found in the |
| 5 | + * LICENSE file in the root directory of this source tree. |
| 6 | + */ |
| 7 | + |
| 8 | +import type {NextApiRequest, NextApiResponse} from 'next'; |
| 9 | +import {McpServer} from '@modelcontextprotocol/sdk/server/mcp.js'; |
| 10 | +import {StreamableHTTPServerTransport} from '@modelcontextprotocol/sdk/server/streamableHttp.js'; |
| 11 | +import {z} from 'zod'; |
| 12 | + |
| 13 | +import sidebarLearn from '../../sidebarLearn.json'; |
| 14 | +import sidebarReference from '../../sidebarReference.json'; |
| 15 | +import sidebarBlog from '../../sidebarBlog.json'; |
| 16 | +import sidebarCommunity from '../../sidebarCommunity.json'; |
| 17 | + |
| 18 | +import { |
| 19 | + type RouteItem, |
| 20 | + type PageEntry, |
| 21 | + type Sidebar, |
| 22 | + collectPages, |
| 23 | + readContentFile, |
| 24 | +} from '../../utils/docs'; |
| 25 | + |
| 26 | +// --- Sidebar types and page collection --- |
| 27 | + |
| 28 | +interface Section { |
| 29 | + section: string; |
| 30 | + pages: PageEntry[]; |
| 31 | +} |
| 32 | + |
| 33 | +// Build page index at module load time (static data) |
| 34 | +const PAGE_INDEX: Section[] = ( |
| 35 | + [sidebarLearn, sidebarReference, sidebarBlog, sidebarCommunity] as Sidebar[] |
| 36 | +).map((sidebar) => ({ |
| 37 | + section: sidebar.title, |
| 38 | + pages: collectPages(sidebar.routes), |
| 39 | +})); |
| 40 | + |
| 41 | +// --- MCP server (created once at module load) --- |
| 42 | + |
| 43 | +const server = new McpServer( |
| 44 | + { |
| 45 | + name: 'react-docs', |
| 46 | + version: '1.0.0', |
| 47 | + }, |
| 48 | + { |
| 49 | + capabilities: { |
| 50 | + tools: {}, |
| 51 | + }, |
| 52 | + } |
| 53 | +); |
| 54 | + |
| 55 | +server.registerTool( |
| 56 | + 'list_pages', |
| 57 | + { |
| 58 | + description: |
| 59 | + 'List all available React documentation pages, grouped by section (Learn, Reference, Blog, Community). Returns JSON with titles and paths.', |
| 60 | + }, |
| 61 | + async () => { |
| 62 | + return { |
| 63 | + content: [ |
| 64 | + { |
| 65 | + type: 'text' as const, |
| 66 | + text: JSON.stringify(PAGE_INDEX, null, 2), |
| 67 | + }, |
| 68 | + ], |
| 69 | + }; |
| 70 | + } |
| 71 | +); |
| 72 | + |
| 73 | +// eslint-disable-next-line @typescript-eslint/no-explicit-any -- MCP SDK generic types cause TS2589 |
| 74 | +(server.registerTool as any)( |
| 75 | + 'get_page', |
| 76 | + { |
| 77 | + description: |
| 78 | + 'Get the full markdown content of a React documentation page by its path. Use list_pages to discover available paths.', |
| 79 | + inputSchema: { |
| 80 | + path: z |
| 81 | + .string() |
| 82 | + .describe( |
| 83 | + 'Page path without leading slash, e.g. "reference/react/useState" or "blog/2024/12/05/react-19"' |
| 84 | + ), |
| 85 | + }, |
| 86 | + }, |
| 87 | + async ({path: pagePath}: {path: string}) => { |
| 88 | + const content = readContentFile(pagePath); |
| 89 | + if (content === null) { |
| 90 | + return { |
| 91 | + isError: true, |
| 92 | + content: [ |
| 93 | + { |
| 94 | + type: 'text' as const, |
| 95 | + text: `Page not found: ${pagePath}`, |
| 96 | + }, |
| 97 | + ], |
| 98 | + }; |
| 99 | + } |
| 100 | + return { |
| 101 | + content: [ |
| 102 | + { |
| 103 | + type: 'text' as const, |
| 104 | + text: content, |
| 105 | + }, |
| 106 | + ], |
| 107 | + }; |
| 108 | + } |
| 109 | +); |
| 110 | + |
| 111 | +// --- Next.js API config --- |
| 112 | + |
| 113 | +export const config = { |
| 114 | + api: { |
| 115 | + // The MCP SDK reads the raw body itself |
| 116 | + bodyParser: false, |
| 117 | + }, |
| 118 | +}; |
| 119 | + |
| 120 | +// --- Request handler --- |
| 121 | + |
| 122 | +export default async function handler( |
| 123 | + req: NextApiRequest, |
| 124 | + res: NextApiResponse |
| 125 | +) { |
| 126 | + if (req.method !== 'POST') { |
| 127 | + res.setHeader('Allow', 'POST'); |
| 128 | + res.status(405).json({error: 'Method not allowed. Use POST for MCP.'}); |
| 129 | + return; |
| 130 | + } |
| 131 | + |
| 132 | + const transport = new StreamableHTTPServerTransport({ |
| 133 | + sessionIdGenerator: undefined, |
| 134 | + }); |
| 135 | + |
| 136 | + await server.connect(transport); |
| 137 | + |
| 138 | + await transport.handleRequest(req, res); |
| 139 | +} |
0 commit comments