Creating a New Agent Provider
Relevant source files
The following files were used as context for generating this wiki page:
Introduction
An Agent Provider is a small, open-source adapter that connects the core react-grab library to a specific coding agent. Its primary purpose is to bridge the gap between the user's interaction in the browser and the agent's code-editing capabilities in the local development environment. When a user selects an element and submits a prompt, react-grab sends the contextual information (component stack, file paths, line numbers) and the user's message to a local server run by the provider. This server then invokes the agent's CLI or SDK to perform the requested code modifications directly in the codebase.
This system allows developers to make changes to their code without leaving the browser, creating a more seamless and efficient workflow. The architecture is designed to be tool-agnostic; if a coding tool has a CLI or an API, a provider can be created for it. This document outlines the architecture and process for creating a new agent provider.
Sources: packages/website/app/blog/agent/page.tsx:601-629
Architecture Overview
An agent provider consists of two main parts: a client-side script that runs in the browser and a server-side script that runs locally in the developer's environment. The react-grab CLI facilitates the installation and configuration of these components.
The general data flow is as follows:
- The user interacts with
react-grabin the browser to select an element and provide a prompt. - The provider's client script captures this event and sends the context and prompt to the local provider server.
- The provider server receives the request and spawns the target agent's CLI as a child process, passing along the necessary information.
- The agent CLI executes the task, editing the project files directly.
- Status updates from the agent are streamed back through the provider server to the client, updating the UI in real-time.
This diagram illustrates the high-level interaction between the components.
Core Components
1. Provider Package
Each agent integration is a separate package within the packages/ directory of the monorepo. The naming convention is provider-<agent-name>.
packages/
├── provider-cursor/ # Cursor agent integration
├── provider-claude-code/ # Claude Code integration
├── provider-opencode/ # OpenCode integration
├── provider-codex/ # OpenAI Codex integration
├── provider-gemini/ # Google Gemini CLI integration
├── provider-amp/ # Amp SDK integration
└── provider-visual-edit/ # Visual Edit integration
Sources: CONTRIBUTING.md:25-36
2. Client-Side Script
The client script is the entry point for the agent provider in the browser. It must implement the AgentProvider interface from react-grab/core.
Key responsibilities include:
- Defining a
providerobject with asendasync generator function. This function receives theAgentContextand is responsible for communicating with the local provider server. - Optionally defining
onStart,onComplete, andonUndolifecycle hooks to manage state. - Exporting an
attachAgentfunction that checks if the provider's backend is healthy and then registers the provider with the corereact-grabAPI usingapi.setAgent().
The provider-visual-edit client provides a comprehensive example of implementing the AgentProvider interface, including handling iterations, undo/redo history, and streaming progress updates.
// packages/provider-visual-edit/src/client/index.ts
// Defines the provider, which is the core logic for sending data to the agent.
const provider: AgentProvider<RequestContext> = {
send: async function* (
context: AgentContext<RequestContext>,
signal: AbortSignal,
) {
// ... logic to prepare and send request to the local server ...
for await (const progress of fetchWithProgress(messages, signal)) {
yield progress; // Stream progress back to the UI
}
// ...
},
// ... other methods like undo, canUndo, etc.
};
// Attaches the provider to the global react-grab instance.
export const attachAgent = async () => {
if (typeof window === "undefined") return;
const isHealthy = await checkHealth();
if (!isHealthy) return;
const { provider, getOptions, onStart, onComplete, onUndo } =
createVisualEditAgentProvider();
const attach = (api: ReactGrabAPI) => {
api.setAgent({
provider,
getOptions,
onStart,
onComplete,
onUndo,
});
};
// ... logic to find window.__REACT_GRAB__ and attach
};
attachAgent();
Sources: packages/provider-visual-edit/src/client/index.ts:202-247, packages/provider-visual-edit/src/client/index.ts:340-376
3. Server-Side Script
The server is a lightweight Node.js script that listens for requests from the client script. Its main role is to act as a bridge to the actual agent CLI. It typically:
- Creates a simple HTTP server on a unique port.
- Receives a POST request containing the user's prompt and context.
- Spawns the agent's CLI tool as a child process.
- Pipes the
stdoutandstderrfrom the child process back to the HTTP response, allowing the client to stream status updates.
The server script is usually executed via npx or bunx from the dev script in the user's package.json.
Sources: packages/cli/src/utils/transform.ts:315-325
4. CLI Integration
The @react-grab/cli package handles the automated setup of agent providers. The init and add commands are responsible for modifying the user's project.
Agent List
The list of available agents is maintained in the CLI commands. To add a new provider, it must be added to this list.
| Agent Key | Agent Name |
|---|---|
claude-code | Claude Code |
cursor | Cursor |
opencode | OpenCode |
codex | Codex |
gemini | Gemini |
amp | Amp |
visual-edit | Visual Edit |
Sources: packages/cli/src/commands/add.ts:31-38, packages/cli/src/commands/init.ts:100-108
Code Transformation
The CLI uses functions in packages/cli/src/utils/transform.ts to inject the necessary code into the user's project.
package.json: ThepreviewPackageJsonTransformfunction prepends the provider's server command to thedevscript. For example:"dev": "npx @react-grab/cursor@latest && next dev".- Layout/Entry File: The
previewTransformfunction injects a<script>tag for the provider's client (client.global.js) into the appropriate file (layout.tsx,_document.tsx,index.html) depending on the project's framework.
This diagram shows the setup flow when a user runs npx grab add <agent>.
How to Build a New Provider
- Create the Package: Following the structure in
CONTRIBUTING.md, create a new directorypackages/provider-<your-agent-name>. - Implement the Server: Create a
src/server.tsfile. This will be a Node.js script that starts an HTTP server on a unique port and executes your agent's CLI command. - Implement the Client: Create a
src/client/index.tsfile. This script will be bundled and loaded in the browser. It must implement theAgentProviderinterface and register itself withreact-grab. Use existing providers likeprovider-visual-editas a reference. - Configure Build: Add a
tsup.config.tsto bundle both the client and server scripts. The client should be bundled into aclient.global.jsfile. - Integrate with CLI:
- Add your new agent to the
AGENT_NAMESrecord inpackages/cli/src/commands/add.tsandpackages/cli/src/commands/init.ts. - Update the
AGENT_PACKAGESand agent list inpackages/cli/src/utils/transform.tsand other relevant files to include your new provider. - Ensure the transformation logic correctly identifies the server command and client script URL for your provider.
- Add your new agent to the
Existing Agent Providers
The following table summarizes the agent providers currently integrated with react-grab. Each provider has a dedicated server port to avoid conflicts when multiple are used.
| Agent | Package Name | 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 | (Uses a remote API) |
Sources: README.md, packages/website/app/blog/agent/page.tsx:313-728, packages/cli/src/commands/add.ts:31-38
Summary
Creating a new agent provider for react-grab involves building a client-side script to capture user intent and a server-side script to interface with the agent's tooling. By following the established architecture and leveraging the CLI for automated setup, new agents can be seamlessly integrated into the react-grab ecosystem. This extensibility is a core design principle, allowing the community to add support for any coding agent with a command-line or API interface.