-
Notifications
You must be signed in to change notification settings - Fork 48.8k
[mcp] Add MCP tool to print out the component tree of the currently open React App #33305
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
23 commits
Select commit
Hold shift + click to select a range
0e5c79c
Bruteforcing react devtools
jorge-cab 8fa3dfc
Smarter Devtools integration
jorge-cab a75932b
Port relevant logic from react devtools
jorge-cab 76dddd1
Port complete
jorge-cab 94718f1
Add component tree function to devtools and finish adding componentTr…
jorge-cab 2852c9d
Merge remote-tracking branch 'origin/main' into component-tree-tool
jorge-cab 26315d6
Cleanup React Devtools port attempt
jorge-cab d6d929e
More cleanup
jorge-cab 789e5f0
fix url redefinition
jorge-cab a85b0b0
Error handling
jorge-cab 049bfbb
Add IS_INTERNAL build time flag and gate getComponentTree()
jorge-cab c5ab27a
Add IS_INTERNAL build time flag and gate getComponentTree()
jorge-cab 1e4614b
Add IS_INTERNAL flag to eslintrc
jorge-cab ab86a5e
Address comments
jorge-cab 81c3a53
Error handling
jorge-cab 183bd4f
Fix CI
jorge-cab 9cae1ce
Remove getComponentTree() definition gating
jorge-cab 9275c83
Fix naming on react-devtools.js
jorge-cab 6c71a77
Fix tests
jorge-cab df0a663
Remove unused code
jorge-cab a586117
Gate __internal_only_getComponentTree definition
jorge-cab ecb1861
Fix lints
jorge-cab ca1d5e0
Fix flow
jorge-cab File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
38 changes: 38 additions & 0 deletions
38
compiler/packages/react-mcp-server/src/tools/componentTree.ts
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
import puppeteer from 'puppeteer'; | ||
|
||
export async function parseReactComponentTree(url: string): Promise<string> { | ||
try { | ||
const browser = await puppeteer.connect({ | ||
browserURL: 'http://127.0.0.1:9222', | ||
defaultViewport: null, | ||
}); | ||
|
||
const pages = await browser.pages(); | ||
|
||
let localhostPage = null; | ||
for (const page of pages) { | ||
const pageUrl = await page.url(); | ||
|
||
if (pageUrl.startsWith(url)) { | ||
localhostPage = page; | ||
break; | ||
} | ||
} | ||
|
||
if (localhostPage) { | ||
const componentTree = await localhostPage.evaluate(() => { | ||
return (window as any).__REACT_DEVTOOLS_GLOBAL_HOOK__.rendererInterfaces | ||
.get(1) | ||
.__internal_only_getComponentTree(); | ||
}); | ||
|
||
return componentTree; | ||
} else { | ||
throw new Error( | ||
`Could not open the page at ${url}. Is your server running?`, | ||
); | ||
} | ||
} catch (error) { | ||
throw new Error('Failed extract component tree' + error); | ||
} | ||
} |
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
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
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
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
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
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
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
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
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
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
get(1)
will usually return you the Fiber renderer, basically the client-side renderer of React.In case of RSC, there could also be another renderer. I am not sure about the order of registration, but it would probably be registered after the Fiber one.
For component tree, we probably care only about Fiber renderer, but worth keeping in mind that there could be rare cases where there are multiple renderers.