Back to Mastra

Reference: Server routes | Server

docs/src/content/en/reference/server/routes.mdx

2025-12-188.3 KB
Original Source

Server routes

Server adapters register these routes when you call server.init(). All routes are prefixed with the prefix option if configured.

Agents

MethodPathDescription
GET/api/agentsList all agents
GET/api/agents/:agentIdGet agent by ID (supports version query params)
POST/api/agents/:agentId/generateGenerate agent response
POST/api/agents/:agentId/streamStream agent response
GET/api/agents/:agentId/toolsList agent tools
POST/api/agents/:agentId/tools/:toolId/executeExecute agent tool

Get agent query parameters

GET /api/agents/:agentId accepts optional query parameters to control which stored config version is applied as overrides to code-defined agents:

ParameterTypeDefaultDescription
status'draft' | 'published''draft'Which stored version to resolve. draft returns the latest version, published returns the active published version.
versionIdstringA specific version ID to resolve. Takes precedence over status.
bash
# Get agent with latest draft overrides (default)
GET /api/agents/my-agent

# Get agent with published overrides
GET /api/agents/my-agent?status=published

# Get agent with a specific version's overrides
GET /api/agents/my-agent?versionId=abc123

Generate request body

typescript
{
  messages: CoreMessage[] | string;       // Required
  instructions?: string;                   // System instructions
  system?: string;                         // System prompt
  context?: CoreMessage[];                 // Additional context
  memory?: { key: string } | boolean;      // Memory config
  resourceId?: string;                     // Resource identifier
  threadId?: string;                       // Thread identifier
  runId?: string;                          // Run identifier
  maxSteps?: number;                       // Max tool steps
  activeTools?: string[];                  // Tools to enable
  toolChoice?: ToolChoice;                 // Tool selection mode
  requestContext?: Record<string, unknown>; // Request context
  output?: ZodSchema;                      // Structured output schema
}

Generate response

typescript
{
  text: string;
  toolCalls?: ToolCall[];
  finishReason: string;
  usage?: {
    promptTokens: number;
    completionTokens: number;
  };
}

Workflows

MethodPathDescription
GET/api/workflowsList all workflows
GET/api/workflows/:workflowIdGet workflow by ID
POST/api/workflows/:workflowId/create-runCreate a new workflow run
POST/api/workflows/:workflowId/start-asyncStart workflow and await result
POST/api/workflows/:workflowId/streamStream workflow execution
POST/api/workflows/:workflowId/resumeResume suspended workflow
POST/api/workflows/:workflowId/resume-asyncResume asynchronously
GET/api/workflows/:workflowId/runsList workflow runs
GET/api/workflows/:workflowId/runs/:runIdGet specific run

Create run request body

typescript
{
  resourceId?: string;     // Associate run with a resource (e.g., user ID)
  disableScorers?: boolean; // Disable scorers for this run
}

Start-async request body

typescript
{
  resourceId?: string;     // Associate run with a resource (e.g., user ID)
  inputData?: unknown;
  initialState?: unknown;
  requestContext?: Record<string, unknown>;
  tracingOptions?: {
    spanName?: string;
    attributes?: Record<string, unknown>;
  };
}

Stream workflow request body

typescript
{
  resourceId?: string;     // Associate run with a resource (e.g., user ID)
  inputData?: unknown;
  initialState?: unknown;
  requestContext?: Record<string, unknown>;
  closeOnSuspend?: boolean;
}

Resume request body

typescript
{
  step?: string | string[];
  resumeData?: unknown;
  requestContext?: Record<string, unknown>;
}

Tools

MethodPathDescription
GET/api/toolsList all tools
GET/api/tools/:toolIdGet tool by ID
POST/api/tools/:toolId/executeExecute tool

Execute tool request body

typescript
{
  data: unknown;  // Tool input data
  requestContext?: Record<string, unknown>;
}

Memory

MethodPathDescription
GET/api/memory/threadsList threads
GET/api/memory/threads/:threadIdGet thread
POST/api/memory/threadsCreate thread
DELETE/api/memory/threads/:threadIdDelete thread
POST/api/memory/threads/:threadId/cloneClone thread
GET/api/memory/threads/:threadId/messagesGet thread messages
POST/api/memory/threads/:threadId/messagesAdd message

Create thread request body

typescript
{
  resourceId: string;
  title?: string;
  metadata?: Record<string, unknown>;
}

Clone thread request body

typescript
{
  newThreadId?: string;           // Custom ID for cloned thread
  resourceId?: string;            // Override resource ID
  title?: string;                 // Custom title for clone
  metadata?: Record<string, unknown>;  // Additional metadata
  options?: {
    messageLimit?: number;        // Max messages to clone
    messageFilter?: {
      startDate?: Date;           // Clone messages after this date
      endDate?: Date;             // Clone messages before this date
      messageIds?: string[];      // Clone specific messages
    };
  };
}

Clone thread response

typescript
{
  thread: {
    id: string;
    resourceId: string;
    title: string;
    createdAt: Date;
    updatedAt: Date;
    metadata: {
      clone: {
        sourceThreadId: string;
        clonedAt: Date;
        lastMessageId?: string;
      };
      // ... other metadata
    };
  };
  clonedMessages: MastraDBMessage[];
}

Vectors

MethodPathDescription
POST/api/vectors/:vectorName/upsertUpsert vectors
POST/api/vectors/:vectorName/queryQuery vectors
POST/api/vectors/:vectorName/deleteDelete vectors

Upsert request body

typescript
{
  vectors: Array<{
    id: string
    values: number[]
    metadata?: Record<string, unknown>
  }>
}

Query request body

typescript
{
  vector: number[];
  topK?: number;
  filter?: Record<string, unknown>;
  includeMetadata?: boolean;
}

MCP

MethodPathDescription
GET/api/mcp/serversList MCP servers
GET/api/mcp/servers/:serverId/toolsList server tools
POST/api/mcp/:serverIdMCP HTTP transport
GET/api/mcp/:serverId/sseMCP SSE transport

Logs

MethodPathDescription
GET/api/logsList logs
GET/api/logs/:runIdGet logs by run ID

Query parameters

typescript
{
  page?: number;
  perPage?: number;
  transportId?: string;
}

Telemetry

MethodPathDescription
GET/api/telemetry/tracesList traces
GET/api/telemetry/traces/:traceIdGet trace
GET/api/telemetry/traces/:traceId/spansGet trace spans

Common query parameters

Pagination

Most list endpoints support:

typescript
{
  page?: number;   // Page number (0-indexed)
  perPage?: number; // Items per page (default: 10)
}

Filtering

Workflow runs support:

typescript
{
  fromDate?: string;  // ISO date string
  toDate?: string;    // ISO date string
  status?: string;    // Run status filter
  resourceId?: string; // Filter by resource
}

Error responses

All routes return errors in this format:

typescript
{
  error: string;      // Error message
  details?: unknown;  // Additional details
}

Common status codes:

CodeMeaning
400Bad Request - Invalid parameters
401Unauthorized - Missing/invalid auth
403Forbidden - Insufficient permissions
404Not Found - Resource doesn't exist
500Internal Server Error