examples/canvas/mastra/README.md
This is a starter template for building AI-powered canvas applications using Mastra and CopilotKit. It provides a modern Next.js application with an integrated Mastra agent that manages a visual canvas of interactive cards with real-time AI synchronization.
<div align="center">Watch the walkthrough video, click the image ⬆️
</div>Note: This repository ignores lock files (package-lock.json, yarn.lock, pnpm-lock.yaml, bun.lock) to avoid conflicts between different package managers. Each developer should generate their own lock file using their preferred package manager. After that, make sure to delete it from the .gitignore.
# You can use whatever model Mastra supports
echo "OPENAI_API_KEY=your-key-here" >> .env
# Using pnpm (recommended)
pnpm install
# Using npm
npm install
# Using yarn
yarn install
# Using bun
bun install
# Using pnpm
pnpm dev
# Using npm
npm run dev
# Using yarn
yarn dev
# Using bun
bun run dev
This will start both the UI and agent servers concurrently.
Once the application is running, you can:
Create Cards: Use the "New Item" button or ask the AI to create cards
Edit Cards: Click on any field to edit directly, or ask the AI
Execute Plans: Give the AI multi-step instructions
View JSON: Toggle between visual canvas and JSON view using the button at the bottom
The following scripts can also be run using your preferred package manager:
dev - Starts both UI and Mastra agent in development modedev:agent - Starts only the Mastra agent development serverdev:debug - Starts both with debug logging enabled (LOG_LEVEL=debug)build - Builds the application for productionstart - Starts the production serverlint - Runs ESLint for code lintinggraph TB
subgraph "Frontend (Next.js)"
UI[Canvas UI
page.tsx]
Actions[Frontend Actions
useCopilotAction]
State[State Management
useCoAgent]
Chat[CopilotChat]
end
subgraph "Integrated Backend"
Runtime[CopilotKit Runtime
src/app/api/copilotkit/route.ts]
Agent[Mastra Agent
src/mastra/agents/index.ts]
Tools[TypeScript Tools
- setPlan
- updatePlanProgress
- completePlan]
Schema[Zod Schema
AgentState]
Model[LLM
GPT-4o-mini]
end
UI <--> State
State <--> Runtime
Chat <--> Runtime
Actions --> Runtime
Runtime <--> Agent
Agent --> Tools
Agent --> Schema
Agent --> Model
style UI fill:#e1f5fe
style Agent fill:#fff3e0
style Runtime fill:#f3e5f5
click UI "https://github.com/CopilotKit/CopilotKit/blob/main/examples/canvas/mastra/src/app/page.tsx"
click Runtime "https://github.com/CopilotKit/CopilotKit/blob/main/examples/canvas/mastra/src/app/api/copilotkit/route.ts"
click Agent "https://github.com/CopilotKit/CopilotKit/blob/main/examples/canvas/mastra/src/mastra/agents/index.ts"
click Tools "https://github.com/CopilotKit/CopilotKit/blob/main/examples/canvas/mastra/src/mastra/tools/index.ts"
The main UI component is in src/app/page.tsx. It includes:
useCoAgent hook for real-time state sync with the agentuseCopilotActionThe agent logic is in src/mastra/agents/index.ts. It features:
AgentStatedev:agentEach card type has specific fields defined consistently across frontend and agent:
sequenceDiagram
participant User
participant UI as Canvas UI
participant CK as CopilotKit
participant MA as Mastra Agent
participant Tools
User->>UI: Interact with canvas
UI->>CK: Update state via useCoAgent
CK->>MA: Process in same runtime
MA->>MA: Process with GPT-4o-mini
MA->>Tools: Execute TypeScript tools
Tools-->>MA: Return results
MA->>CK: Return updated state
CK->>UI: Sync state changes
UI->>User: Display updates
Note over MA: Integrated in Next.js
Note over UI,MA: Single process architecture
src/lib/canvas/types.tsCardType unionsrc/components/canvas/CardRenderer.tsxsrc/mastra/agents/index.tssrc/app/page.tsxCardRenderer.tsxset[Type]Field[Number]src/mastra/agents/index.tssrc/mastra/tools/index.tsmodel propertysrc/app/globals.cssIf you see "I'm having trouble connecting to my tools", make sure:
.envsrc/mastra/index.tsIf the canvas and AI seem out of sync:
To enable detailed logging:
npm run dev:debug
This sets LOG_LEVEL=debug for more verbose output from Mastra.
src/mastra/index.tssrc/mastra/tools/index.ts match frontend expectationsnpm run build to check for type issuesFeel free to submit issues and enhancement requests! This starter is designed to be easily extensible.
This project is licensed under the MIT License - see the LICENSE file for details.
[!IMPORTANT] Some features are still under active development and may not yet work as expected. If you encounter a problem using this template, please report an issue to this repository.