Documentation Index
Fetch the complete documentation index at: https://mintlify.com/mastra-ai/mastra/llms.txt
Use this file to discover all available pages before exploring further.
The Mastra server package provides a comprehensive set of routes for interacting with your agents, workflows, and tools via HTTP.
Available Route Groups
All routes are registered under your configured prefix (default: /api).
Agent Routes
Manage and interact with agents:
| Method | Path | Description |
|---|
GET | /agents | List all registered agents |
GET | /agents/:agentId | Get agent metadata |
POST | /agents/:agentId/generate | Generate agent response |
POST | /agents/:agentId/stream | Stream agent response |
Example: Generate Response
curl -X POST http://localhost:3000/api/agents/myAgent/generate \
-H "Content-Type: application/json" \
-d '{
"messages": [
{"role": "user", "content": "What is 2+2?"}
]
}'
Example: Stream Response
const response = await fetch('http://localhost:3000/api/agents/myAgent/stream', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({
messages: [{ role: 'user', content: 'Tell me a story' }],
}),
});
const reader = response.body.getReader();
while (true) {
const { done, value } = await reader.read();
if (done) break;
console.log(new TextDecoder().decode(value));
}
Workflow Routes
Execute and manage workflows:
| Method | Path | Description |
|---|
GET | /workflows | List all registered workflows |
GET | /workflows/:workflowId | Get workflow metadata |
POST | /workflows/:workflowId/execute | Execute a workflow |
GET | /workflows/:workflowId/runs/:runId | Get workflow run status |
Example: Execute Workflow
curl -X POST http://localhost:3000/api/workflows/myWorkflow/execute \
-H "Content-Type: application/json" \
-d '{
"triggerData": {
"userId": "123",
"action": "process"
}
}'
Discover and inspect available tools:
| Method | Path | Description |
|---|
GET | /tools | List all tools |
GET | /tools/:toolId | Get tool metadata |
Memory Routes
Interact with memory stores:
| Method | Path | Description |
|---|
GET | /memory/:threadId | Get thread messages |
POST | /memory/:threadId/save | Save messages to thread |
DELETE | /memory/:threadId | Delete thread |
Observability Routes
Access telemetry and tracing data:
| Method | Path | Description |
|---|
GET | /observability/traces | List traces |
GET | /observability/traces/:traceId | Get trace details |
POST | /observability/scores | Add score to trace |
POST | /observability/feedback | Add feedback to trace |
Log Routes
Query runtime logs:
| Method | Path | Description |
|---|
GET | /logs | Query logs with filters |
Example: Query Logs
curl "http://localhost:3000/api/logs?level=error&limit=10"
Vector Routes
Vector store operations:
| Method | Path | Description |
|---|
POST | /vectors/query | Query vector store |
POST | /vectors/upsert | Upsert vectors |
Using Handlers Directly
You can also use handlers directly without the server adapter:
import { agents } from '@mastra/server/handlers';
import { Mastra } from '@mastra/core';
import { RequestContext } from '@mastra/core/request-context';
const mastra = new Mastra({ /* config */ });
// Call handler directly
const result = await agents.generateHandler({
mastra,
requestContext: new RequestContext(),
agentId: 'myAgent',
messages: [{ role: 'user', content: 'Hello' }],
});
console.log(result);
Handler Groups
Handlers are organized into modules:
import {
agents, // Agent handlers
workflows, // Workflow handlers
tools, // Tool handlers
memory, // Memory handlers
observability,// Observability handlers
logs, // Log handlers
vector, // Vector handlers
voice, // Voice handlers
} from '@mastra/server/handlers';
Response Types
Routes return different response types:
JSON Response
Standard JSON response for most routes:
{
"id": "agent-123",
"name": "My Agent",
"model": "gpt-4"
}
Stream Response
Server-Sent Events (SSE) or chunked streaming:
// SSE format
data: {"type":"text-delta","text":"Hello"}
data: {"type":"text-delta","text":" world"}
data: [DONE]
// Or chunked format (default)
{"type":"text-delta","text":"Hello"}\x1E
{"type":"text-delta","text":" world"}\x1E
Error Response
Standardized error format:
{
"error": "Invalid request body",
"issues": [
{
"field": "messages",
"message": "Required"
}
]
}
Request Validation
All routes validate requests using Zod schemas. Invalid requests return 400 status with detailed error messages:
curl -X POST http://localhost:3000/api/agents/myAgent/generate \
-H "Content-Type: application/json" \
-d '{}'
# Response:
{
"error": "Invalid request body",
"issues": [
{
"field": "messages",
"message": "Required"
}
]
}
Next Steps
Middleware
Add authentication and custom middleware
Server Adapters
Learn about different server adapters