Skip to content

The Open Standard for Generative UI

4.3 KB
Raw Blame
---
title: "Material UI"
---

<Steps>

<Step title="Map MUI components to OpenUI" source="https://github.com/thesysdev/openui/blob/main/examples/design-systems/material-ui/src/lib/mui-genui/index.tsx">

Wrap Material UI components with defineComponent and Zod schemas, and export them through createLibrary. Charts use @mui/x-charts.

```tsx title="src/lib/mui-genui/index.tsx"
import Card from "@mui/material/Card";
import CardContent from "@mui/material/CardContent";
import Typography from "@mui/material/Typography";
import { createLibrary, defineComponent } from "@openuidev/react-lang";
import { z } from "zod";

const CardHeader = defineComponent({
  name: "CardHeader",
  props: z.object({ title: z.string(), description: z.string().optional() }),
  description: "Title/description header block for a Card.",
  component: ({ props }) => (
    <>
      <Typography variant="h3">{props.title}</Typography>
      {props.description && <Typography variant="body2">{props.description}</Typography>}
    </>
  ),
});

const ChatCard = defineComponent({
  name: "Card",
  props: z.object({ children: z.array(CardHeader.ref) }),
  description: "Vertical container for all content in a chat response.",
  component: ({ props, renderNode }) => (
    <Card>
      <CardContent>{renderNode(props.children)}</CardContent>
    </Card>
  ),
});

export const muiChatLibrary = createLibrary({
  root: "Card",
  components: [ChatCard, CardHeader], // plus every other component you wrap
});
```

</Step>

<Step title="Generate the library spec" source="https://github.com/thesysdev/openui/blob/main/examples/design-systems/material-ui/src/app/api/chat/route.ts">

Run openui generate --spec on src/library.ts. The chat route sends that spec to OpenUI Cloud and runs app-owned tools with runChatToolLoop.

```ts title="src/app/api/chat/route.ts"
import OpenAI from "openai";
// Wraps src/generated/spec.json in Cloud's prompt block (src/lib/cloud-prompt.ts).
import { cloudInstructions } from "@/lib/cloud-prompt";
// Your tools: OpenAI function declarations plus the functions that run them.
import { appToolDeclarations, appToolExecutors } from "@/lib/app-tools";
// Runs tool calls on the server until the model answers. Copy src/lib/tool-loop.ts.
import { runChatToolLoop } from "@/lib/tool-loop";

const client = new OpenAI({
  baseURL: "https://api.thesys.dev/v1/embed",
  apiKey: process.env.THESYS_API_KEY,
});
const encoder = new TextEncoder();

export async function POST(req: Request) {
  const { messages } = await req.json();
  const body = new ReadableStream({
    async start(controller) {
      await runChatToolLoop({
        client,
        model: "google/gemini-3.6-flash-free",
        messages: [{ role: "system", content: cloudInstructions() }, ...messages],
        tools: appToolDeclarations,
        executors: appToolExecutors,
        enqueue: (chunk) =>
          controller.enqueue(encoder.encode(`data: ${JSON.stringify(chunk)}\n\n`)),
      });
      controller.enqueue(encoder.encode("data: [DONE]\n\n"));
      controller.close();
    },
  });

  return new Response(body, { headers: { "Content-Type": "text/event-stream" } });
}
```

</Step>

<Step title="Render inside your MUI theme" source="https://github.com/thesysdev/openui/blob/main/examples/design-systems/material-ui/src/app/page.tsx">

Render responses with Renderer, or with AgentInterface as the example does, inside your MUI ThemeProvider. Generated components then use the app's theme and light or dark mode.

```tsx title="src/app/page.tsx"
"use client";

import CssBaseline from "@mui/material/CssBaseline";
import { ThemeProvider } from "@mui/material/styles";
import {
  AgentInterface,
  fetchLLM,
  openAIAdapter,
  openAIMessageFormat,
  useSystemThemeMode,
} from "@openuidev/react-ui";
import { useMemo } from "react";
// Your wrapped components (step 1) and your MUI createTheme() factory.
import { muiChatLibrary } from "@/lib/mui-genui";
import { createAppTheme } from "@/lib/mui-genui/theme";

const llm = fetchLLM({
  url: "/api/chat",
  streamAdapter: openAIAdapter(),
  messageFormat: openAIMessageFormat,
});

export default function Page() {
  const mode = useSystemThemeMode();
  const theme = useMemo(() => createAppTheme(mode), [mode]);

  return (
    <ThemeProvider theme={theme}>
      <CssBaseline />
      <AgentInterface llm={llm} componentLibrary={muiChatLibrary} theme={{ mode }} />
    </ThemeProvider>
  );
}
```

</Step>

</Steps>