Skip to content

Commit f734ea1

Browse files
committed
Tools: short list with links to per-tool specs
1 parent 21a46eb commit f734ea1

1 file changed

Lines changed: 254 additions & 52 deletions

File tree

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

Lines changed: 254 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -337,33 +337,27 @@ The tool console in this sandbox stands in for Chrome DevTools MCP so you can tr
337337

338338
`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. Every tool returns a plain, serializable value described in [Types](#types), or a [`ToolError`](#types) if it fails.
341-
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*/}
340+
* [`react_get_component_tree`](#react_get_component_tree): Returns a snapshot of the component tree.
341+
* [`react_find_components`](#react_find_components): Finds components by name.
342+
* [`react_get_component_by_uid`](#react_get_component_by_uid): Returns a component's props and Hooks.
343+
* [`react_get_component_by_dom_element`](#react_get_component_by_dom_element): Returns the React host component for a DOM element.
344+
* [`react_get_parent_stack`](#react_get_parent_stack): Returns where a component is mounted in the tree.
345+
* [`react_get_owner_stack`](#react_get_owner_stack): Returns the components that created a component in their JSX.
346+
* [`react_get_owner_stack_trace`](#react_get_owner_stack_trace): Returns the owner stack of a component as a stack trace.
347+
* [`react_get_component_source`](#react_get_component_source): Returns where a component is defined.
348+
* [`react_start_profiling`](#react_start_profiling): Starts recording render timing.
349+
* [`react_stop_profiling`](#react_stop_profiling): Stops recording render timing.
350+
* [`react_get_trace_overview`](#react_get_trace_overview): Returns a summary of every commit in a recording.
351+
* [`react_get_commit_report`](#react_get_commit_report): Returns which components rendered in a commit and how long each took.
352+
353+
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.
354+
355+
Every tool returns a plain, serializable value. If a tool fails, it returns a `ToolError` instead. The return types below use these shared types:
364356

365357
```ts
366-
type ToolError = { error: string };
358+
type ToolError = {
359+
error: string;
360+
};
367361

368362
type NodeType =
369363
| 'root' | 'function' | 'class' | 'host' | 'text' | 'forwardRef' | 'memo'
@@ -376,60 +370,268 @@ type TreeNode = {
376370
type: NodeType;
377371
name: string;
378372
key: string | null;
379-
firstChild: string | null; // uid of the first child, if any
373+
firstChild: string | null; // uid of the first child, if any
380374
nextSibling: string | null; // uid of the next sibling, if any
381375
};
382376

383-
type ComponentTree = { nodes: Array<TreeNode> };
377+
type ComponentBranchEntry = {
378+
uid: string;
379+
name: string;
380+
type: NodeType;
381+
};
382+
```
383+
384+
#### `react_get_component_tree` {/*react_get_component_tree*/}
385+
386+
Returns a snapshot of the component tree of every mounted root.
387+
388+
##### Parameters {/*react_get_component_tree-parameters*/}
384389

385-
type FindComponentsResult = {
390+
* **optional** `depth`: Maximum depth to walk. Defaults to `20`.
391+
* **optional** `rootUid`: Start the snapshot from this component instead of the roots.
392+
393+
##### Returns {/*react_get_component_tree-returns*/}
394+
395+
```ts
396+
type Result = {
397+
nodes: Array<TreeNode>;
398+
} | ToolError;
399+
```
400+
401+
`nodes` is a flat list. Use `firstChild` and `nextSibling` to rebuild the tree.
402+
403+
#### `react_find_components` {/*react_find_components*/}
404+
405+
Finds components whose name contains a substring, ignoring case.
406+
407+
##### Parameters {/*react_find_components-parameters*/}
408+
409+
* `name`: The substring to search for.
410+
* **optional** `rootUid`: Only search inside this component's subtree.
411+
* **optional** `page`: The page of results to return. Defaults to `1`.
412+
* **optional** `pageSize`: Results per page. Defaults to `10`.
413+
414+
##### Returns {/*react_find_components-returns*/}
415+
416+
```ts
417+
type Result = {
386418
page: number;
387419
pageSize: number;
388420
totalCount: number;
389421
totalPages: number;
390422
results: Array<TreeNode>;
391-
};
423+
} | ToolError;
424+
```
425+
426+
#### `react_get_component_by_uid` {/*react_get_component_by_uid*/}
392427

393-
type ComponentDetails = {
428+
Returns the props and, optionally, the Hooks of one component.
429+
430+
##### Parameters {/*react_get_component_by_uid-parameters*/}
431+
432+
* `uid`: The component's uid.
433+
* **optional** `includeHooks`: Whether to inspect Hooks. Defaults to `false`.
434+
435+
##### Returns {/*react_get_component_by_uid-returns*/}
436+
437+
```ts
438+
type Result = {
394439
uid: string;
395440
type: NodeType;
396441
name: string;
397442
key?: string;
398443
props?: { [name: string]: unknown }; // without `children`
399444
hooks?: Array<HookNode>; // only when `includeHooks` is true
400-
};
445+
} | ToolError;
401446

402447
type HookNode = {
403448
id: number | null; // null for custom Hooks
404449
name: string; // for example, "State" or "Effect"
405450
value: unknown;
406451
subHooks: Array<HookNode>; // Hooks called by a custom Hook
407452
};
453+
```
408454

409-
type ComponentBranchEntry = { uid: string; name: string; type: NodeType };
455+
##### Caveats {/*react_get_component_by_uid-caveats*/}
410456

411-
type OwnerStackTrace = { stack: string };
457+
* `hooks` is only returned for function, `forwardRef`, and `memo` components.
458+
* Prop and Hook values are normalized so they can be serialized.
459+
* 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).
412460

413-
type ComponentSource = {
414-
source: { name: string; fileName: string; line: number; column: number } | null;
415-
};
461+
#### `react_get_component_by_dom_element` {/*react_get_component_by_dom_element*/}
462+
463+
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).
464+
465+
##### Parameters {/*react_get_component_by_dom_element-parameters*/}
466+
467+
* `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.
468+
469+
##### Returns {/*react_get_component_by_dom_element-returns*/}
470+
471+
```ts
472+
type Result = {
473+
uid: string;
474+
type: 'host';
475+
name: string; // the tag name, for example "button"
476+
key?: string;
477+
props?: { [name: string]: unknown };
478+
} | ToolError;
479+
```
480+
481+
#### `react_get_parent_stack` {/*react_get_parent_stack*/}
482+
483+
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.
484+
485+
##### Parameters {/*react_get_parent_stack-parameters*/}
486+
487+
* `uid`: The component's uid.
488+
489+
##### Returns {/*react_get_parent_stack-returns*/}
490+
491+
```ts
492+
type Result = Array<ComponentBranchEntry> | ToolError;
493+
```
494+
495+
The array is empty for a root.
496+
497+
#### `react_get_owner_stack` {/*react_get_owner_stack*/}
498+
499+
Returns the components that created this one through JSX, from its immediate owner up to the root owner.
500+
501+
<Note>
502+
503+
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`.
416504

417-
type ProfilingStatus =
418-
| { status: 'started'; traceName: string }
419-
| { status: 'stopped'; traceName: string; commits: number };
505+
</Note>
506+
507+
##### Parameters {/*react_get_owner_stack-parameters*/}
508+
509+
* `uid`: The component's uid.
510+
511+
##### Returns {/*react_get_owner_stack-returns*/}
512+
513+
```ts
514+
type Result = Array<ComponentBranchEntry> | ToolError;
515+
```
516+
517+
The array is empty for a root component and in production builds.
518+
519+
#### `react_get_owner_stack_trace` {/*react_get_owner_stack_trace*/}
520+
521+
Returns the raw [owner stack](/reference/react/captureOwnerStack) of a component: the JSX call sites that created it, as a stack trace.
522+
523+
##### Parameters {/*react_get_owner_stack_trace-parameters*/}
524+
525+
* `uid`: The component's uid.
526+
527+
##### Returns {/*react_get_owner_stack_trace-returns*/}
528+
529+
```ts
530+
type Result = {
531+
stack: string; // same format as Error.prototype.stack
532+
} | ToolError;
533+
```
534+
535+
`stack` is empty in production builds.
536+
537+
#### `react_get_component_source` {/*react_get_component_source*/}
538+
539+
Returns where a component is defined.
540+
541+
##### Parameters {/*react_get_component_source-parameters*/}
542+
543+
* `uid`: The component's uid.
544+
545+
##### Returns {/*react_get_component_source-returns*/}
546+
547+
```ts
548+
type Result = {
549+
source: {
550+
name: string;
551+
fileName: string;
552+
line: number;
553+
column: number;
554+
} | null;
555+
} | ToolError;
556+
```
557+
558+
`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.
559+
560+
#### `react_start_profiling` {/*react_start_profiling*/}
561+
562+
Starts recording render timing for every commit, like the Profiler tab in React DevTools. Stop recording with [`react_stop_profiling`](#react_stop_profiling).
563+
564+
##### Parameters {/*react_start_profiling-parameters*/}
565+
566+
* **optional** `traceName`: A name for the trace. One is generated if you don't pass it.
420567

421-
// Durations are in milliseconds, or null if the React build
422-
// doesn't collect profiling timing.
423-
type CommitSummary = {
424-
commit: number;
568+
##### Returns {/*react_start_profiling-returns*/}
569+
570+
```ts
571+
type Result = {
572+
status: 'started';
573+
traceName: string;
574+
} | ToolError;
575+
```
576+
577+
Returns a `ToolError` if a profiling session is already running.
578+
579+
#### `react_stop_profiling` {/*react_stop_profiling*/}
580+
581+
Stops the running profiling session.
582+
583+
##### Parameters {/*react_stop_profiling-parameters*/}
584+
585+
`react_stop_profiling` takes no parameters.
586+
587+
##### Returns {/*react_stop_profiling-returns*/}
588+
589+
```ts
590+
type Result = {
591+
status: 'stopped';
592+
traceName: string;
593+
commits: number; // number of commits recorded
594+
} | ToolError;
595+
```
596+
597+
Returns a `ToolError` if no profiling session is running.
598+
599+
#### `react_get_trace_overview` {/*react_get_trace_overview*/}
600+
601+
Returns one row per commit in a recorded trace.
602+
603+
##### Parameters {/*react_get_trace_overview-parameters*/}
604+
605+
* `traceName`: The trace to read.
606+
607+
##### Returns {/*react_get_trace_overview-returns*/}
608+
609+
```ts
610+
type Result = Array<{
611+
commit: number; // commit index, starting from 0
425612
committedAt: number; // ms since profiling started
426-
renderDuration: number | null;
427-
layoutDuration: number | null; // layout effects
428-
passiveDuration: number | null; // passive effects
613+
renderDuration: number | null; // ms spent rendering
614+
layoutDuration: number | null; // ms spent in layout effects
615+
passiveDuration: number | null; // ms spent in passive effects
429616
componentsChanged: number;
430-
};
617+
}> | ToolError;
618+
```
619+
620+
Durations are `null` when the React build doesn't collect profiling timing.
431621

432-
type CommitReport = {
622+
#### `react_get_commit_report` {/*react_get_commit_report*/}
623+
624+
Returns which components rendered in one commit of a recorded trace, and how long each took.
625+
626+
##### Parameters {/*react_get_commit_report-parameters*/}
627+
628+
* `traceName`: The trace to read.
629+
* `commitIndex`: The commit to read, starting from `0`.
630+
631+
##### Returns {/*react_get_commit_report-returns*/}
632+
633+
```ts
634+
type Result = {
433635
committedAt: number;
434636
priority: 'Sync' | 'UserBlocking' | 'Normal' | 'Idle';
435637
renderDuration: number | null;
@@ -439,10 +641,10 @@ type CommitReport = {
439641
uid: string;
440642
name: string;
441643
type: NodeType;
442-
actualDuration: number | null; // the component and its children
443-
selfDuration: number | null; // only the component itself
444-
}>; // slowest first
445-
};
644+
actualDuration: number | null; // ms rendering the component and its children
645+
selfDuration: number | null; // ms rendering only the component itself
646+
}>; // sorted by actualDuration, slowest first
647+
} | ToolError;
446648
```
447649

448650
---

0 commit comments

Comments
 (0)