Vercel AI SDK Adapter
Relevant source files
The following files were used as context for generating this wiki page:
The Vercel AI SDK Adapter is a client-side SDK designed to bridge Mastra's backend agentic capabilities with frontend applications built using Vercel's AI SDK. It provides a streamlined way to create API endpoints, typically in a Next.js application, that can be consumed by frontend hooks like useChat. This allows developers to easily integrate Mastra agents into web UIs, leveraging the power of Mastra's workflows, memory, and tool usage while using familiar frontend patterns.
The adapter's primary function is to handle the communication layer, translating requests from the AI SDK format into a format that a Mastra agent can process, and then streaming the agent's response back to the client in a compatible format.
Sources: client-sdks/ai-sdk/src/index.ts, README.md:32-34
Architecture and Core Components
The adapter's core is the createChatRoute function, which acts as a factory for creating Next.js API route handlers. This function encapsulates the logic for interacting with a specified Mastra agent.
Sources: client-sdks/ai-sdk/src/chat-route.ts:14-16
Key Components
| Component | Description | File |
|---|---|---|
createChatRoute | A higher-order function that takes a Mastra instance and options, and returns a Next.js POST route handler. | client-sdks/ai-sdk/src/chat-route.ts |
MastraChatRouteOptions | A TypeScript type defining the configuration options for createChatRoute, such as the agentId. | client-sdks/ai-sdk/src/chat-route.ts:8-11 |
POST handler | The generated route handler that processes incoming chat requests, interacts with the Mastra agent, and streams responses. | client-sdks/ai-sdk/src/chat-route.ts |
Data Flow
The following diagram illustrates the request/response flow from a client using the Vercel AI SDK's useChat hook to a Mastra agent via the adapter.
This sequence shows how a user's message is sent from the frontend, processed by the createChatRoute handler which invokes the appropriate Mastra agent, and how the agent's streaming response is piped back to the client.
Sources: client-sdks/ai-sdk/src/chat-route.ts:18-30, examples/ai-sdk-useChat/app/api/chat/route.ts:10-12
Usage and Implementation
To use the adapter, you create a Next.js API route and export a POST handler generated by createChatRoute.
Sources: examples/ai-sdk-useChat/app/api/chat/route.ts
Step-by-Step Guide
- Instantiate Mastra: Define your Mastra instance with the necessary agents, tools, and configuration.
- Create the API Route: In your Next.js project, create a file like
app/api/chat/route.ts. - Generate the Handler: Call
createChatRoute, passing your Mastra instance and specifying theagentIdyou want to expose. - Export the Handler: Export the returned function as
POST.
Example Implementation
This code snippet shows a complete API route that exposes a weatherAgent to the frontend.
// File: examples/ai-sdk-useChat/app/api/chat/route.ts
import { createChatRoute } from '@mastra/ai-sdk';
import { Mastra } from '@mastra/core';
import { weatherAgent } from '@/mastra/agents/weather-agent';
// 1. Instantiate Mastra with your agent
const mastra = new Mastra({
agents: { weatherAgent },
});
// 2. Create the chat route handler, connecting it to the 'weatherAgent'
export const POST = createChatRoute(mastra, {
agentId: 'weather-agent',
});
Sources: examples/ai-sdk-useChat/app/api/chat/route.ts:3-12
Configuration Options
The createChatRoute function accepts an options object to configure its behavior.
| Option | Type | Description | Required |
|---|---|---|---|
agentId | string | The ID of the Mastra agent to use for handling chat requests. This must match an agent ID registered in the Mastra instance. | Yes |
Sources: client-sdks/ai-sdk/src/chat-route.ts:8-11
Internal Logic of createChatRoute
The handler created by createChatRoute performs several key actions to process a request.
This flowchart outlines the internal process of the generated route handler.
Sources: client-sdks/ai-sdk/src/chat-route.ts:14-31
- Request Handling: It receives a standard
Requestobject from the Next.js runtime. - Body Parsing: It parses the incoming request body to extract the
messagesarray, which is expected to be in the Vercel AI SDK format. - Agent Retrieval: It uses the
agentIdfrom the options to retrieve the corresponding agent instance from the providedMastrainstance. - Agent Invocation: It calls the
agent.stream()method, passing the messages from the request. This initiates the agent's reasoning process. - Stream Conversion: The stream returned by the Mastra agent is converted into a Vercel AI SDK-compatible
AIStream. This is a crucial step for ensuring the frontend can correctly interpret the streaming data. - Response Streaming: Finally, it returns a
StreamingTextResponse, which streams the output of theAIStreamback to the client.
Sources: client-sdks/ai-sdk/src/chat-route.ts:14-31
Conclusion
The Vercel AI SDK Adapter is a vital component in the Mastra ecosystem for building interactive, agent-powered web applications. By providing the createChatRoute utility, it abstracts away the complexity of connecting a Mastra backend to a Vercel AI SDK-powered frontend. This enables developers to focus on building powerful agent logic in Mastra and rich user experiences in their frontend framework of choice, with a simple and standardized integration point. The adapter is mentioned as a key integration for building UIs with agentic libraries.