Skip to content

Commit 21a46eb

Browse files
committed
Reshape Tools as a grouped bullet list with shared types
1 parent 94eeac7 commit 21a46eb

1 file changed

Lines changed: 52 additions & 241 deletions

File tree

‎src/content/reference/dev-tools/react-devtools-cdt-mcp.md‎

Lines changed: 52 additions & 241 deletions
Original file line numberDiff line numberDiff line change
@@ -335,16 +335,35 @@ The tool console in this sandbox stands in for Chrome DevTools MCP so you can tr
335335

336336
### Tools {/*tools*/}
337337

338-
`react-devtools-cdt-mcp` registers a tool group called `react` with the following tools. Your agent calls them through Chrome DevTools MCP.
338+
`react-devtools-cdt-mcp` registers a tool group called `react`. Your agent calls these tools through Chrome DevTools MCP.
339339

340-
Components are identified by short, stable uids like `"r5"`. A uid stays the same across every tool and across re-renders, but not across page reloads. Get uids from [`react_get_component_tree`](#react_get_component_tree) or [`react_find_components`](#react_find_components).
340+
Components are identified by short, stable uids like `"r5"`. A uid stays the same across every tool and across re-renders, but not across page reloads. Every tool returns a plain, serializable value described in [Types](#types), or a [`ToolError`](#types) if it fails.
341341

342-
Every tool returns a plain, serializable value. If a tool fails, it returns a `ToolError` instead. The return types below use these shared types:
342+
These tools find components and read their data:
343+
344+
* <span id="react_get_component_tree" style={{scrollMarginTop: 'calc(4rem + 20px)'}} />`react_get_component_tree`: Returns a snapshot of the component tree of every mounted root as a [`ComponentTree`](#types). Takes an optional `depth` (defaults to `20`) and an optional `rootUid` to start from a specific component.
345+
* <span id="react_find_components" style={{scrollMarginTop: 'calc(4rem + 20px)'}} />`react_find_components`: Finds components whose name contains `name`, ignoring case, and returns a paginated [`FindComponentsResult`](#types). Takes an optional `rootUid` to search only a subtree, `page` (defaults to `1`), and `pageSize` (defaults to `10`).
346+
* <span id="react_get_component_by_uid" style={{scrollMarginTop: 'calc(4rem + 20px)'}} />`react_get_component_by_uid`: Returns the props of the component with the given `uid` as a [`ComponentDetails`](#types). Pass `includeHooks: true` to also inspect the Hooks of function, `forwardRef`, and `memo` components. To read Hook values, React DevTools calls the component's render function again, so keep your [components pure](/learn/keeping-components-pure).
347+
* <span id="react_get_component_by_dom_element" style={{scrollMarginTop: 'calc(4rem + 20px)'}} />`react_get_component_by_dom_element`: Returns the host component, like a `<button>`, that React rendered for a DOM `element` as a [`ComponentDetails`](#types). Chrome DevTools MCP clients pass `element` as `{uid}`, using an element uid from the page snapshot.
348+
349+
These tools explain where a component is mounted and which code created it. The **parent** of a component is where it's mounted in the tree. The **owner** is the component that created it in its JSX. For example, in `<Card><Avatar /></Card>` written inside `Profile`, `Avatar`'s parent is `Card` but its owner is `Profile`.
350+
351+
* <span id="react_get_parent_stack" style={{scrollMarginTop: 'calc(4rem + 20px)'}} />`react_get_parent_stack`: Returns the parents of the component with the given `uid`, from its immediate parent up to the root, as an array of [`ComponentBranchEntry`](#types). Parents can include host components and the root.
352+
* <span id="react_get_owner_stack" style={{scrollMarginTop: 'calc(4rem + 20px)'}} />`react_get_owner_stack`: Returns the owners of the component with the given `uid`, from its immediate owner up to the root owner, as an array of [`ComponentBranchEntry`](#types). The array is empty in production builds.
353+
* <span id="react_get_owner_stack_trace" style={{scrollMarginTop: 'calc(4rem + 20px)'}} />`react_get_owner_stack_trace`: Returns the [owner stack](/reference/react/captureOwnerStack) of the component with the given `uid` as an [`OwnerStackTrace`](#types): the JSX call sites that created it, in the same format as an `Error` stack. The stack is empty in production builds.
354+
* <span id="react_get_component_source" style={{scrollMarginTop: 'calc(4rem + 20px)'}} />`react_get_component_source`: Returns where the component with the given `uid` is defined as a [`ComponentSource`](#types). The location is `null` for host components and in production builds, and may need source maps to map back to your files.
355+
356+
These tools record render timing, like the Profiler tab in React DevTools:
357+
358+
* <span id="react_start_profiling" style={{scrollMarginTop: 'calc(4rem + 20px)'}} />`react_start_profiling`: Starts recording render timing for every commit and returns a [`ProfilingStatus`](#types). Takes an optional `traceName`. Returns a `ToolError` if a profiling session is already running.
359+
* <span id="react_stop_profiling" style={{scrollMarginTop: 'calc(4rem + 20px)'}} />`react_stop_profiling`: Stops the running profiling session and returns a [`ProfilingStatus`](#types) with the number of recorded commits. Returns a `ToolError` if no profiling session is running.
360+
* <span id="react_get_trace_overview" style={{scrollMarginTop: 'calc(4rem + 20px)'}} />`react_get_trace_overview`: Returns one [`CommitSummary`](#types) per commit in the trace named `traceName`.
361+
* <span id="react_get_commit_report" style={{scrollMarginTop: 'calc(4rem + 20px)'}} />`react_get_commit_report`: Returns the components that rendered in commit `commitIndex` of the trace named `traceName`, and how long each took, as a [`CommitReport`](#types). Commit indexes start from `0`.
362+
363+
#### Types {/*types*/}
343364

344365
```ts
345-
type ToolError = {
346-
error: string;
347-
};
366+
type ToolError = { error: string };
348367

349368
type NodeType =
350369
| 'root' | 'function' | 'class' | 'host' | 'text' | 'forwardRef' | 'memo'
@@ -357,268 +376,60 @@ type TreeNode = {
357376
type: NodeType;
358377
name: string;
359378
key: string | null;
360-
firstChild: string | null; // uid of the first child, if any
379+
firstChild: string | null; // uid of the first child, if any
361380
nextSibling: string | null; // uid of the next sibling, if any
362381
};
363382

364-
type ComponentBranchEntry = {
365-
uid: string;
366-
name: string;
367-
type: NodeType;
368-
};
369-
```
370-
371-
#### `react_get_component_tree` {/*react_get_component_tree*/}
372-
373-
Returns a snapshot of the component tree of every mounted root.
374-
375-
##### Parameters {/*react_get_component_tree-parameters*/}
376-
377-
* **optional** `depth`: Maximum depth to walk. Defaults to `20`.
378-
* **optional** `rootUid`: Start the snapshot from this component instead of the roots.
383+
type ComponentTree = { nodes: Array<TreeNode> };
379384

380-
##### Returns {/*react_get_component_tree-returns*/}
381-
382-
```ts
383-
type Result = {
384-
nodes: Array<TreeNode>;
385-
} | ToolError;
386-
```
387-
388-
`nodes` is a flat list. Use `firstChild` and `nextSibling` to rebuild the tree.
389-
390-
#### `react_find_components` {/*react_find_components*/}
391-
392-
Finds components whose name contains a substring, ignoring case.
393-
394-
##### Parameters {/*react_find_components-parameters*/}
395-
396-
* `name`: The substring to search for.
397-
* **optional** `rootUid`: Only search inside this component's subtree.
398-
* **optional** `page`: The page of results to return. Defaults to `1`.
399-
* **optional** `pageSize`: Results per page. Defaults to `10`.
400-
401-
##### Returns {/*react_find_components-returns*/}
402-
403-
```ts
404-
type Result = {
385+
type FindComponentsResult = {
405386
page: number;
406387
pageSize: number;
407388
totalCount: number;
408389
totalPages: number;
409390
results: Array<TreeNode>;
410-
} | ToolError;
411-
```
412-
413-
#### `react_get_component_by_uid` {/*react_get_component_by_uid*/}
414-
415-
Returns the props and, optionally, the Hooks of one component.
416-
417-
##### Parameters {/*react_get_component_by_uid-parameters*/}
418-
419-
* `uid`: The component's uid.
420-
* **optional** `includeHooks`: Whether to inspect Hooks. Defaults to `false`.
421-
422-
##### Returns {/*react_get_component_by_uid-returns*/}
391+
};
423392

424-
```ts
425-
type Result = {
393+
type ComponentDetails = {
426394
uid: string;
427395
type: NodeType;
428396
name: string;
429397
key?: string;
430398
props?: { [name: string]: unknown }; // without `children`
431399
hooks?: Array<HookNode>; // only when `includeHooks` is true
432-
} | ToolError;
400+
};
433401

434402
type HookNode = {
435403
id: number | null; // null for custom Hooks
436404
name: string; // for example, "State" or "Effect"
437405
value: unknown;
438406
subHooks: Array<HookNode>; // Hooks called by a custom Hook
439407
};
440-
```
441-
442-
##### Caveats {/*react_get_component_by_uid-caveats*/}
443-
444-
* `hooks` is only returned for function, `forwardRef`, and `memo` components.
445-
* Prop and Hook values are normalized so they can be serialized.
446-
* To read Hook values, React DevTools calls the component's render function again. Effects don't run, but side effects in the render body itself do. This is one more reason to keep [components pure](/learn/keeping-components-pure).
447408

448-
#### `react_get_component_by_dom_element` {/*react_get_component_by_dom_element*/}
409+
type ComponentBranchEntry = { uid: string; name: string; type: NodeType };
449410

450-
Returns the React host component for a DOM element, for example the `<button>` that React rendered. To find the component that rendered it, pass the returned `uid` to [`react_get_owner_stack`](#react_get_owner_stack).
451-
452-
##### Parameters {/*react_get_component_by_dom_element-parameters*/}
453-
454-
* `element`: A DOM element in the page. Chrome DevTools MCP clients pass `{uid}`, using an element uid from the page snapshot, and Chrome DevTools MCP resolves it to the element.
455-
456-
##### Returns {/*react_get_component_by_dom_element-returns*/}
457-
458-
```ts
459-
type Result = {
460-
uid: string;
461-
type: 'host';
462-
name: string; // the tag name, for example "button"
463-
key?: string;
464-
props?: { [name: string]: unknown };
465-
} | ToolError;
466-
```
411+
type OwnerStackTrace = { stack: string };
467412

468-
#### `react_get_parent_stack` {/*react_get_parent_stack*/}
469-
470-
Returns where a component is mounted in the rendered tree, from its immediate parent up to the root. Parents can include host components and the root itself.
471-
472-
##### Parameters {/*react_get_parent_stack-parameters*/}
473-
474-
* `uid`: The component's uid.
475-
476-
##### Returns {/*react_get_parent_stack-returns*/}
477-
478-
```ts
479-
type Result = Array<ComponentBranchEntry> | ToolError;
480-
```
481-
482-
The array is empty for a root.
483-
484-
#### `react_get_owner_stack` {/*react_get_owner_stack*/}
485-
486-
Returns the components that created this one through JSX, from its immediate owner up to the root owner.
487-
488-
<Note>
489-
490-
The **parent** of a component is where it's mounted in the tree. The **owner** is the component that created it in its JSX. For example, in `<Card><Avatar /></Card>` written inside `Profile`, `Avatar`'s parent is `Card` but its owner is `Profile`.
491-
492-
</Note>
493-
494-
##### Parameters {/*react_get_owner_stack-parameters*/}
495-
496-
* `uid`: The component's uid.
497-
498-
##### Returns {/*react_get_owner_stack-returns*/}
499-
500-
```ts
501-
type Result = Array<ComponentBranchEntry> | ToolError;
502-
```
503-
504-
The array is empty for a root component and in production builds.
505-
506-
#### `react_get_owner_stack_trace` {/*react_get_owner_stack_trace*/}
507-
508-
Returns the raw [owner stack](/reference/react/captureOwnerStack) of a component: the JSX call sites that created it, as a stack trace.
509-
510-
##### Parameters {/*react_get_owner_stack_trace-parameters*/}
511-
512-
* `uid`: The component's uid.
513-
514-
##### Returns {/*react_get_owner_stack_trace-returns*/}
515-
516-
```ts
517-
type Result = {
518-
stack: string; // same format as Error.prototype.stack
519-
} | ToolError;
520-
```
521-
522-
`stack` is empty in production builds.
523-
524-
#### `react_get_component_source` {/*react_get_component_source*/}
525-
526-
Returns where a component is defined.
527-
528-
##### Parameters {/*react_get_component_source-parameters*/}
529-
530-
* `uid`: The component's uid.
531-
532-
##### Returns {/*react_get_component_source-returns*/}
533-
534-
```ts
535-
type Result = {
536-
source: {
537-
name: string;
538-
fileName: string;
539-
line: number;
540-
column: number;
541-
} | null;
542-
} | ToolError;
543-
```
544-
545-
`source` is `null` for host components and in production builds. The location points at the code the browser loaded, so it may need source maps to map back to your files.
546-
547-
#### `react_start_profiling` {/*react_start_profiling*/}
548-
549-
Starts recording render timing for every commit, like the Profiler tab in React DevTools. Stop recording with [`react_stop_profiling`](#react_stop_profiling).
550-
551-
##### Parameters {/*react_start_profiling-parameters*/}
552-
553-
* **optional** `traceName`: A name for the trace. One is generated if you don't pass it.
554-
555-
##### Returns {/*react_start_profiling-returns*/}
556-
557-
```ts
558-
type Result = {
559-
status: 'started';
560-
traceName: string;
561-
} | ToolError;
562-
```
563-
564-
Returns a `ToolError` if a profiling session is already running.
565-
566-
#### `react_stop_profiling` {/*react_stop_profiling*/}
567-
568-
Stops the running profiling session.
569-
570-
##### Parameters {/*react_stop_profiling-parameters*/}
571-
572-
`react_stop_profiling` takes no parameters.
573-
574-
##### Returns {/*react_stop_profiling-returns*/}
575-
576-
```ts
577-
type Result = {
578-
status: 'stopped';
579-
traceName: string;
580-
commits: number; // number of commits recorded
581-
} | ToolError;
582-
```
583-
584-
Returns a `ToolError` if no profiling session is running.
585-
586-
#### `react_get_trace_overview` {/*react_get_trace_overview*/}
587-
588-
Returns one row per commit in a recorded trace.
589-
590-
##### Parameters {/*react_get_trace_overview-parameters*/}
591-
592-
* `traceName`: The trace to read.
413+
type ComponentSource = {
414+
source: { name: string; fileName: string; line: number; column: number } | null;
415+
};
593416

594-
##### Returns {/*react_get_trace_overview-returns*/}
417+
type ProfilingStatus =
418+
| { status: 'started'; traceName: string }
419+
| { status: 'stopped'; traceName: string; commits: number };
595420

596-
```ts
597-
type Result = Array<{
598-
commit: number; // commit index, starting from 0
421+
// Durations are in milliseconds, or null if the React build
422+
// doesn't collect profiling timing.
423+
type CommitSummary = {
424+
commit: number;
599425
committedAt: number; // ms since profiling started
600-
renderDuration: number | null; // ms spent rendering
601-
layoutDuration: number | null; // ms spent in layout effects
602-
passiveDuration: number | null; // ms spent in passive effects
426+
renderDuration: number | null;
427+
layoutDuration: number | null; // layout effects
428+
passiveDuration: number | null; // passive effects
603429
componentsChanged: number;
604-
}> | ToolError;
605-
```
606-
607-
Durations are `null` when the React build doesn't collect profiling timing.
608-
609-
#### `react_get_commit_report` {/*react_get_commit_report*/}
610-
611-
Returns which components rendered in one commit of a recorded trace, and how long each took.
612-
613-
##### Parameters {/*react_get_commit_report-parameters*/}
614-
615-
* `traceName`: The trace to read.
616-
* `commitIndex`: The commit to read, starting from `0`.
617-
618-
##### Returns {/*react_get_commit_report-returns*/}
430+
};
619431

620-
```ts
621-
type Result = {
432+
type CommitReport = {
622433
committedAt: number;
623434
priority: 'Sync' | 'UserBlocking' | 'Normal' | 'Idle';
624435
renderDuration: number | null;
@@ -628,10 +439,10 @@ type Result = {
628439
uid: string;
629440
name: string;
630441
type: NodeType;
631-
actualDuration: number | null; // ms rendering the component and its children
632-
selfDuration: number | null; // ms rendering only the component itself
633-
}>; // sorted by actualDuration, slowest first
634-
} | ToolError;
442+
actualDuration: number | null; // the component and its children
443+
selfDuration: number | null; // only the component itself
444+
}>; // slowest first
445+
};
635446
```
636447

637448
---

0 commit comments

Comments
 (0)