Skip to content

[compiler] Add explain-compiler-diagnostic MCP tool#36879

Open
AnandSundar wants to merge 1 commit into
react:mainfrom
AnandSundar:feat/explain-compiler-diagnostic
Open

[compiler] Add explain-compiler-diagnostic MCP tool#36879
AnandSundar wants to merge 1 commit into
react:mainfrom
AnandSundar:feat/explain-compiler-diagnostic

Conversation

@AnandSundar

Copy link
Copy Markdown

Summary

When the existing compile tool returns a diagnostic, the LLM receives a cryptic formatted message with no explanation of what the rule means or how to fix it. The LLM has to guess based on the message text alone.

This PR adds a new explain-compiler-diagnostic tool to react-mcp-server that takes a diagnostic and returns a structured plain-English explanation: title, summary, category, why-it-happens, how-to-fix, an optional before/after code example, severity, and links to the relevant React docs. The tool is registered alongside the existing compile tool so a model can chain the two: compile -> on failure, explain-compiler-diagnostic.

This is the first time the MCP server ships a hand-curated knowledge base alongside its tools. The catalog is keyed by the stable ErrorCategory rule name from babel-plugin-react-compiler, so it survives compiler message-wording changes across versions. Adding a new entry is a single map-insert.

What is in the catalog (v1)

5 hand-curated entries covering the most common compiler diagnostics:

  • set-state-in-render
  • preserve-manual-memoization
  • immutability
  • purity
  • refs

All five are in the Recommended lint preset. The catalog supports incremental growth -- future entries can be added as Map inserts without changing the tool code.

How the matching works

  • Input: { message, codeContext?, rule? }.
  • If rule is supplied, the tool does an exact lookup against the catalog (fast path).
  • Otherwise it tests each entry's messagePatterns: RegExp[] against message (case-insensitive) and returns the first match.
  • On miss the tool returns a generic guidance string that points the LLM at docs, the ErrorCategory enum source, and how to file an issue.

Files changed

  • src/diagnostics/catalog.ts (new, 279 lines) -- the catalog.
  • src/tools/explainCompilerDiagnostic.ts (new, 112 lines) -- the lookup function.
  • src/index.ts (+45 lines) -- register the new tool.
  • jest.config.js (new), src/tools/__tests__/explainCompilerDiagnostic-test.ts (new, 173 lines), package.json -- bootstrap Jest for the package (the existing test script was echo "no tests").
  • README.md (+16 lines) -- document the new tool alongside the existing ones.

How I tested

  • yarn workspace react-mcp-server test -> 17/17 tests pass in 4.55s.
  • yarn workspace react-mcp-server build -> tsup produces a 1.72 MB CJS bundle.
  • Manual typecheck of the new files via tsc --strict -> clean.

I did not run the MCP inspector manually. The new tool is wired into server.tool(...) following the same pattern as the existing compile tool.

Future work (out of scope for this PR)

  • The compile tool currently only exposes {message, loc} to the LLM, no structured category field. Today the new tool works around this with regex matching + an optional direct rule-name lookup. A follow-up could expose category on the diagnostic so the matcher becomes a simple hash lookup with no regex cost.
  • Catalog growth: 5 entries is a useful start. Future entries can be added incrementally.

Adds a new MCP tool that explains React Compiler diagnostics in plain
English. Ships with 5 hand-curated entries (set-state-in-render,
preserve-manual-memoization, immutability, purity, refs) keyed by the
stable ErrorCategory rule name, plus a Jest unit test suite. The tool
falls back to a generic guidance string when no curated entry matches
the formatted message returned by the existing compile tool.
@meta-cla meta-cla Bot added the CLA Signed label Jun 25, 2026
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