Skip to main content

Using CopilotKit

CopilotKit provides React components to quickly integrate customizable AI copilots into your application. Combined with Mastra, you can build sophisticated AI apps featuring bidirectional state synchronization and interactive UIs.

Visit the CopilotKit documentation to learn more about CopilotKit concepts, components, and advanced usage patterns.

info

For a full-stack integration approach where Mastra runs directly in your Next.js API routes, see the CopilotKit Quickstart guide.

tip

Visit Mastra's "UI Dojo" to see real-world examples of CopilotKit integrated with Mastra.

Integration Guide

Run Mastra as a standalone server and connect your Next.js frontend (with CopilotKit) to its API endpoints.

  1. Set up your directory structure. A possible directory structure could look like this:

    project-root
    ├── mastra-server
    │ ├── src
    │ │ └── mastra
    │ └── package.json
    └── my-copilot-app
    └── package.json

    Bootstrap your Mastra server:

    npx create-mastra@latest

    This command will launch an interactive wizard to help you scaffold a new Mastra project, including prompting you for a project name and setting up basic configurations. Follow the prompts to create your server project.

    Navigate to your newly created Mastra server directory:

    cd mastra-server # Replace with the actual directory name you provided

    You now have a basic Mastra server project ready. You should have the following files and folders:

    src
    └── mastra
    ├── agents
    │ └── weather-agent.ts
    ├── scorers
    │ └── weather-scorer.ts
    ├── tools
    │ └── weather-tool.ts
    ├── workflows
    │ └── weather-workflow.ts
    └── index.ts
    note

    Ensure that you have set the appropriate environment variables for your LLM provider in the .env file.

  2. Create a chat route for the CopilotKit frontend by using the registerCopilotKit() helper from @ag-ui/mastra. Add it to your Mastra project (and its peer dependencies):

    npm install --legacy-peer-deps @ag-ui/mastra @copilotkit/runtime @ag-ui/core @ag-ui/client @ag-ui/encoder @ag-ui/langgraph @ag-ui/proto

    In your src/mastra/index.ts file, register the chat route:

    src/mastra/index.ts
    import { Mastra } from '@mastra/core/mastra';
    import { registerCopilotKit } from '@ag-ui/mastra/copilotkit';
    // Rest of the imports...

    export const mastra = new Mastra({
    // Rest of the configuration...
    server: {
    cors: {
    origin: "*",
    allowMethods: ["*"],
    allowHeaders: ["*"],
    },
    apiRoutes: [
    registerCopilotKit({
    path: '/chat',
    resourceId: 'weatherAgent'
    })
    ]
    }
    });

    This will make the weatherAgent available at /chat in a CopilotKit-compatible format. You have to add the CORS configuration to allow the CopilotKit frontend to access the Mastra server. For production deployments, make sure to restrict the CORS origins to only your frontend domain.

  3. Run the Mastra server using the following command:

    npm run dev

    By default, the Mastra server will run on http://localhost:4111. Keep this server running for the next steps where we'll set up the CopilotKit frontend to connect to it.

  4. Go up one directory to your project root.

    cd ..

    Create a new Next.js project with the name my-copilot-app:

    npx create-next-app@latest my-copilot-app

    Navigate to your newly created Next.js project directory:

    cd my-copilot-app
  5. Install the CopilotKit UI packages which you'll use to display a chat interface:

    npm install @copilotkit/react-ui @copilotkit/react-core

    Open the home route of the Next.js app (usually app/page.tsx or src/app/page.tsx) and replace the existing contents with the following code to set up a basic CopilotKit chat interface:

    app/page.tsx
    import { CopilotChat } from "@copilotkit/react-ui";
    import { CopilotKit } from "@copilotkit/react-core";
    import "@copilotkit/react-ui/styles.css";

    export default function Home() {
    return (
    <CopilotKit
    runtimeUrl="http://localhost:4111/chat"
    agent="weatherAgent"
    >
    <CopilotChat
    labels={{
    title: "Weather Agent",
    initial: "Hi! 👋 Ask me about the weather, forecasts, and climate.",
    }}
    />
    </CopilotKit>
    );
    }
  6. You're ready to connect the pieces! Make sure both the Mastra server and the CopilotKit frontend are running. Start the Next.js development server:

    npm run dev

    You should now be able to chat with your agent in the browser.

Congratulations! You have successfully integrated Mastra with CopilotKit using a separate server approach. Your CopilotKit frontend now communicates with a standalone Mastra agent server.

On this page