Integrating with AI Agents
Relevant source files
The following files were used as context for generating this wiki page:React Grab's AI Agent integration extends its core functionality from simply copying context to enabling direct communication with coding agents. This feature allows developers to select a UI element, provide a prompt, and have an AI agent make changes to the codebase directly, all without leaving the browser. This eliminates the manual copy-paste cycle, creating a more seamless and efficient workflow for UI development and iteration.
The integration is designed to be tool-agnostic, supporting various existing coding agents through a provider-based architecture. It works by running a local server that acts as a bridge between the React Grab client script in the browser and the agent's command-line interface (CLI) or SDK. This allows for a "visual edit" experience where changes are initiated from the UI and executed in the local development environment.
Sources: packages/website/app/blog/agent/page.tsx:173-176, 203-210, README.md:83-86
Architecture
The agent integration architecture consists of three main components: a client-side script, a local provider server, and the external AI agent's CLI/SDK.
- Client Script: In the browser, the core
react-grabscript works alongside an agent-specific client script. This combined script captures the user's intent (element selection and prompt) and communicates with the local provider server. - Provider Server: A small, local Node.js server, specific to each agent, that runs in the background. It receives requests from the client script, formats the context and prompt, and invokes the corresponding agent's CLI or SDK.
- Agent CLI/SDK: The actual AI coding agent (e.g.,
cursor-agent, Claude Agent SDK) that receives the instructions from the provider server, performs the code analysis and modification, and writes changes directly to the local filesystem.
Sources: packages/website/app/blog/agent/pagetsx:657-679, CONTRIBUTING.md:20-29
The following diagram illustrates the data flow from the user's action in the browser to the code modification by the AI agent.
This diagram shows the flow of an edit request from the browser to the local agent, resulting in file changes and a feedback loop for status updates.
Sources: packages/website/app/blog/agent/page.tsx:298-304, 674-679
User Workflow
The interaction model is designed to be quick and intuitive, keeping the developer in the context of their application's UI.
- The user holds
⌘C(or the configured activation key) and clicks on a UI element. - A small label appears over the element, showing the component name.
- The user presses
Enterto expand a prompt input field. - The user types the desired change (e.g., "make this button blue") and presses
Enteragain. - React Grab sends the prompt and the element's context (component stack, file paths, line numbers) to the local provider server.
- The provider server invokes the selected AI agent.
- The agent works on the task, and status updates are streamed back to the label in the browser (e.g., "Working", "Completed").
- Once the agent completes the edit, the application automatically reloads to reflect the changes.
Sources: packages/website/app/blog/agent/page.tsx:288-304
The sequence diagram below details the interactions between the user, the browser, and the local services.
This diagram shows the sequence of events for a visual edit task, from user input to the final application reload.
Sources: packages/website/app/blog/agent/page.tsx:288-304, 674-679
Installation and Setup
Integration is managed primarily through the grab CLI, which automates the setup process.
Automated Setup (Recommended)
The init command detects the project's framework (Next.js, Vite, etc.) and package manager, then injects the necessary scripts and dependencies.
npx grab@latest init
During initialization, you can select which AI agent integration to add. The CLI will modify package.json to include the agent's provider server in the dev script and add the agent's client script to the main layout file.
Sources: packages/cli/src/commands/init.ts:114, 800-814, README.md:19-23
Adding/Managing Integrations
If React Grab is already installed, you can add a new agent integration using the add command.
npx grab@latest add [agent]
The add command provides options to either add a new agent alongside existing ones or replace the current one.
Sources: packages/cli/src/commands/add.ts:40-44, 114-129
Manual Setup
For manual setup, two parts are required: the server-side provider and the client-side script.
- Server Setup: Modify the
devscript in yourpackage.jsonto launch the agent provider server. - Client Setup: Add the agent's client script to your application's
<head>.
Example for Claude Code in a Next.js App Router project:
package.json:{ "scripts": { "dev": "npx @react-grab/claude-code@latest && next dev" } }app/layout.tsx:import Script from "next/script"; export default function RootLayout({ children }) { return ( <html> <head> {process.env.NODE_ENV === "development" && ( <> <Script src="//unpkg.com/react-grab/dist/index.global.js" strategy="beforeInteractive" /> <Script src="//unpkg.com/@react-grab/claude-code/dist/client.global.js" strategy="lazyOnload" /> </> )} </head> <body>{children}</body> </html> ); }
Sources: packages/website/app/blog/agent/page.tsx:392-442, packages/react-grab/README.md:92-126
Supported Agents & Providers
React Grab supports multiple coding agents through dedicated provider packages. Each provider runs a local server on a specific port.
| Agent | Provider Package | Default Port |
|---|---|---|
| Claude Code | @react-grab/claude-code | 4567 |
| Cursor | @react-grab/cursor | 5567 |
| OpenCode | @react-grab/opencode | 6567 |
| Codex | @react-grab/codex | 7567 |
| Gemini | @react-grab/gemini | 8567 |
| Amp | @react-grab/amp | 9567 |
| Factory Droid | @react-grab/droid | 10567 |
| Visual Edit | @react-grab/visual-edit | N/A |
| Ami | @react-grab/ami | N/A |
Sources: CONTRIBUTING.md:22-29, packages/website/app/blog/agent/page.tsx:383-389, 446-452, 509-515, 572-578, 635-641, 698-704, 761-767, packages/cli/src/commands/add.ts:25-33
These providers are open source and can be used as a reference for integrating custom agents.
Sources: packages/website/app/blog/agent/page.tsx:681-706
Conclusion
The AI Agent integration in React Grab transforms the developer experience for UI tasks. By creating a direct channel from the browser to local coding agents, it significantly reduces the friction and latency associated with describing visual changes in text prompts. The provider-based architecture ensures flexibility and extensibility, allowing developers to connect their preferred AI tools. This feature represents a step towards a future where UI iteration is faster, more intuitive, and deeply integrated into the development workflow.
Sources: packages/website/app/blog/agent/pagetsx:279-286, 715-734