React SDK & UI
Relevant source files
The following files were used as context for generating this wiki page:
- client-sdks/react/package.json
- client-sdks/react/src/agent/hooks.ts
- client-sdks/react/src/index.ts
- examples/ai-elements/package.json
- examples/assistant-ui/components/MyAssistant.tsx
- examples/openapi-spec-writer/src/components/open-api-spec-generator/index.tsx
- packages/playground-ui/CLAUDE.md
- packages/playground-ui/package.json
- packages/playground-ui/src/index.ts
- packages/playground/CLAUDE.md
The Mastra project provides a comprehensive set of packages for building modern, AI-powered user interfaces with React. The ecosystem is primarily composed of the @mastra/react SDK, which provides core logic and hooks for interacting with the Mastra backend, and @mastra/playground-ui, a rich component library for building development studios and other complex UIs.
This modular architecture separates business logic and data fetching from presentation, allowing developers to build custom frontends on top of Mastra's powerful features. The @mastra/react package acts as the bridge to the backend, while @mastra/playground-ui offers pre-built, high-level components that can be composed into full-featured applications like the local Mastra Playground.
Architecture Overview
The frontend architecture is designed to be modular and composable. At the core is @mastra/client-js, which is wrapped by the @mastra/react SDK to provide React-specific hooks. The @mastra/playground-ui package consumes these hooks to build stateful, domain-specific components. Finally, example applications and the local playground itself compose these UI components into complete pages and experiences.
This diagram illustrates the dependency flow between the key frontend packages.
@mastra/react SDK
The @mastra/react package is a client-side library that provides React hooks and context providers for interacting with a Mastra backend. It simplifies state management for agents, threads, and messages.
Sources: client-sdks/react/package.json:2, client-sdks/react/src/index.ts
Key Dependencies
The SDK has peer dependencies on react and react-dom, and a core dependency on @mastra/client-js for the underlying API communication.
| Dependency | Version | Type | Description |
|---|---|---|---|
react | >=19.0.0 | peerDependency | The core React library. |
react-dom | >=19.0.0 | peerDependency | Provides DOM-specific methods for React. |
@mastra/client-js | workspace:* | dependency | The underlying JavaScript client for Mastra APIs. |
Sources: client-sdks/react/package.json:52-55, client-sdks/react/package.json:48-49
Core Hooks
The SDK provides several hooks for managing agent interactions and conversation state. These hooks are designed to be used within components that are children of a MastraProvider.
| Hook | Description |
|---|---|
useAgent | Fetches a specific agent's details by its ID. |
useAgents | Fetches a list of all available agents. |
useThread | Fetches a specific conversation thread by its ID. |
useThreads | Fetches a list of all conversation threads. |
useMessage | Fetches a specific message by its ID. |
useMessages | Fetches all messages within a given thread ID. |
useRun | Fetches a specific run by its ID. |
useRuns | Fetches all runs within a given thread ID. |
useRunStep | Fetches a specific step within a run by its ID. |
useRunSteps | Fetches all steps within a given run ID. |
Sources: client-sdks/react/src/agent/hooks.ts:18-132
Data Fetching and State Management
The hooks in @mastra/react are built on top of TanStack Query (@tanstack/react-query). This provides robust data-fetching capabilities including caching, background refetching, and state management out-of-the-box. The useMastraClient hook is used internally to get an API client instance from the MastraProvider context.
This sequence diagram shows how a component uses the useAgents hook to fetch and display a list of agents.
@mastra/playground-ui Package
@mastra/playground-ui is a package of shared UI components and business logic hooks used to build Mastra's studio environments. It is designed to be environment-agnostic, working in both local development studios (using React Router) and cloud-based SaaS applications (using Next.js).
Sources: packages/playground-ui/CLAUDE.md:7-9, packages/playground/CLAUDE.md:7-9
Design Principles and Conventions
The package follows strict coding and styling guidelines to ensure consistency and reusability.
- Styling: All styling is done with Tailwind CSS. It is forbidden to use arbitrary values; instead, design tokens from
src/ds/tokens/index.tsmust be used. - Component Naming: Components use
PascalCaseand filenames usekebab-case. - Exports: Only named exports are permitted; default exports are forbidden.
- Data Fetching: All data fetching must be done using TanStack Query via hooks. Direct
fetch()calls are forbidden. - State Management: The use of
useStateanduseEffectis minimized in favor of derived values.
Sources: packages/playground-ui/CLAUDE.md:20-111
Key Dependencies
This package has a large number of dependencies related to building a rich user interface, including:
| Dependency | Description |
|---|---|
@assistant-ui/react | A library for building AI assistant UIs. |
@tanstack/react-table | Headless UI for building powerful tables and datagrids. |
@xyflow/react | A library for building node-based editors and diagrams. |
@radix-ui/* | A collection of unstyled, accessible UI primitives. |
cmdk | A command menu component for React. |
shiki | A powerful syntax highlighter. |
zod | TypeScript-first schema declaration and validation. |
tailwindcss | A utility-first CSS framework. |
@mastra/react | The core React SDK for backend communication. |
Sources: packages/playground-ui/package.json:52-111
Example Usage
The examples/openapi-spec-writer application demonstrates how to use React components to build a UI. Although it uses its own UI components, the pattern of using state, actions, and handling loading/error states is representative of how one would use @mastra/playground-ui.
// examples/openapi-spec-writer/src/components/open-api-spec-generator/index.tsx:150-151
export const CodeEditor: React.FC<CodeEditorProps> = ({ code }) => {
const lines = code.split("\n");
// ... rendering logic
}
// examples/openapi-spec-writer/src/components/open-api-spec-generator/index.tsx:232-235
const OpenApiGenerator: React.FC = () => {
const [loading, setLoading] = useState(false);
const [error, setError] = useState("");
const [openApiSpec, setOpenApiSpec] = useState("");
// ... handler functions and rendering logic
}
This example shows a typical React component structure with state management for loading, errors, and data, which is a common pattern when using the Mastra UI/SDK packages.
Sources: examples/openapi-spec-writer/src/components/open-api-spec-generator/index.tsx:150-235
Conclusion
The Mastra frontend ecosystem, centered around @mastra/react and @mastra/playground-ui, provides a powerful and flexible foundation for building AI-driven applications. The @mastra/react SDK offers a clean, hook-based API for interacting with Mastra's backend services, abstracting away the complexities of data fetching and state synchronization. The @mastra/playground-ui package builds upon this foundation, offering a rich library of reusable components that enable the rapid development of sophisticated user interfaces like the Mastra Playground. This clear separation of concerns allows developers to either leverage the pre-built UI for speed or create entirely custom frontends using the core React SDK.