Add MCP server endpoint for react.dev documentation#8348
Closed
mattcarrollcode wants to merge 2 commits intoreactjs:mainfrom
Closed
Add MCP server endpoint for react.dev documentation#8348mattcarrollcode wants to merge 2 commits intoreactjs:mainfrom
mattcarrollcode wants to merge 2 commits intoreactjs:mainfrom
Conversation
## Summary - Add MCP server at `/api/mcp` with `list_pages` and `get_page` tools, allowing LLM agents to discover and read React documentation programmatically - Extract shared `RouteItem`, `PageEntry`, `collectPages`, and `readContentFile` into `src/utils/docs.ts`, replacing duplicated logic in the MCP endpoint, markdown API, and llms.txt page - Add path traversal protection in `readContentFile` (validates resolved paths stay within `src/content/`) - Add in-memory content cache for repeated file reads - Restrict markdown API endpoint to GET method - Add `@modelcontextprotocol/sdk` and `zod` dependencies ## Test plan - `yarn tsc` passes - `yarn build` succeeds - `GET /api/md/reference/react/useState` returns markdown content - `GET /llms.txt` returns sitemap - `POST /api/mcp` responds to MCP protocol requests - Path traversal attempts (e.g. `../../CLAUDE`) return null, not file contents - Non-GET requests to `/api/md/*` return 405
Size changesDetails📦 Next.js Bundle Analysis for react-devThis analysis was generated by the Next.js Bundle Analysis action. 🤖
|
| Page | Size (compressed) |
|---|---|
global |
115.14 KB (🟡 +1 B) |
Details
The global bundle is the javascript bundle that loads alongside every page. It is in its own category because its impact is much higher - an increase to its size means that every page on your website loads slower, and a decrease means every page loads faster.
Any third party scripts you have added directly to your app using the <script> tag are not accounted for in this analysis
If you want further insight into what is behind the changes, give @next/bundle-analyzer a try!
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Note: A MCP server to access the React docs is not likely to be needed for most LLMs. Most LLMs should use the preexisting /llms.txt for a overview of what is available in the React docs and the
Accept: text/markdownheader to retrieve markdown files for individual react.dev pages (#8272)Summary
/api/mcpwithlist_pagesandget_pagetools, allowing LLM agents to discover and read React documentation programmaticallyRouteItem,PageEntry,collectPages, andreadContentFileintosrc/utils/docs.ts, replacing duplicated logic in the MCP endpoint, markdown API, and llms.txt pagereadContentFile(validates resolved paths stay withinsrc/content/)@modelcontextprotocol/sdkandzoddependenciesTest plan
yarn tscpassesyarn buildsucceedsGET /api/md/reference/react/useStatereturns markdown contentGET /llms.txtreturns sitemapPOST /api/mcpresponds to MCP protocol requests../../CLAUDE) return null, not file contents/api/md/*return 405