Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions compiler/packages/react-mcp-server/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# React MCP Server (experimental)

An experimental MCP Server for React.

## Development

First, add this file if you're using Claude Desktop: `code ~/Library/Application\ Support/Claude/claude_desktop_config.json`. Copy the absolute path from `which node` and from `react/compiler/react-mcp-server/dist/index.js` and paste, for example:

```json
{
"mcpServers": {
"react": {
"command": "/Users/<username>/.asdf/shims/node",
"args": [
"/Users/<username>/code/react/compiler/packages/react-mcp-server/dist/index.js"
]
}
}
}
```

Next, run `yarn workspace react-mcp-server watch` from the `react/compiler` directory and make changes as needed. You will need to restart Claude everytime you want to try your changes.
33 changes: 33 additions & 0 deletions compiler/packages/react-mcp-server/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
{
"name": "react-mcp-server",
"version": "0.0.0",
"description": "React MCP Server (experimental)",
"bin": {
"react-mcp-server": "./dist/index.js"
},
"scripts": {
"build": "rimraf dist && tsup",
"test": "echo 'no tests'",
"watch": "yarn build --watch"
},
"dependencies": {
"@babel/core": "^7.26.0",
"@babel/parser": "^7.26",
"@babel/plugin-syntax-typescript": "^7.25.9",
"@modelcontextprotocol/sdk": "^1.9.0",
"algoliasearch": "^5.23.3",
"cheerio": "^1.0.0",
"prettier": "^3.3.3",
"turndown": "^7.2.0",
"zod": "^3.23.8"
},
"devDependencies": {
"@types/turndown": "^5.0.5"
},
"license": "MIT",
"repository": {
"type": "git",
"url": "git+https://github.com/facebook/react.git",
"directory": "compiler/packages/react-mcp-server"
}
}
67 changes: 67 additions & 0 deletions compiler/packages/react-mcp-server/src/compiler/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
/**
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/

import type * as BabelCore from '@babel/core';
import {parseAsync, transformFromAstAsync} from '@babel/core';
import BabelPluginReactCompiler, {
type PluginOptions,
} from 'babel-plugin-react-compiler/src';
import * as prettier from 'prettier';

export let lastResult: BabelCore.BabelFileResult | null = null;

type CompileOptions = {
text: string;
file: string;
options: Partial<PluginOptions> | null;
};
export async function compile({
text,
file,
options,
}: CompileOptions): Promise<BabelCore.BabelFileResult> {
const ast = await parseAsync(text, {
sourceFileName: file,
parserOpts: {
plugins: ['typescript', 'jsx'],
},
sourceType: 'module',
});
if (ast == null) {
throw new Error('Could not parse');
}
const plugins =
options != null
? [[BabelPluginReactCompiler, options]]
: [[BabelPluginReactCompiler]];
const result = await transformFromAstAsync(ast, text, {
filename: file,
highlightCode: false,
retainLines: true,
plugins,
sourceType: 'module',
sourceFileName: file,
});
if (result?.code == null) {
throw new Error(
`Expected BabelPluginReactCompiler to compile successfully, got ${result}`,
);
}
try {
result.code = await prettier.format(result.code, {
semi: false,
parser: 'babel-ts',
});
if (result.code != null) {
lastResult = result;
}
} catch (err) {
// If prettier failed just log, no need to crash
console.error(err);
}
return result;
}
Loading
Loading