Skip to content

Add MCP server endpoint for react.dev documentation#8348

Closed
mattcarrollcode wants to merge 2 commits intoreactjs:mainfrom
mattcarrollcode:mcp-server-endpoint
Closed

Add MCP server endpoint for react.dev documentation#8348
mattcarrollcode wants to merge 2 commits intoreactjs:mainfrom
mattcarrollcode:mcp-server-endpoint

Conversation

@mattcarrollcode
Copy link
Contributor

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/markdown header to retrieve markdown files for individual react.dev pages (#8272)

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

## 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
@github-actions
Copy link

Size changes

Details

📦 Next.js Bundle Analysis for react-dev

This analysis was generated by the Next.js Bundle Analysis action. 🤖

⚠️ Global Bundle Size Increased

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!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant