Mastra Client
JavaScript/TypeScript client library for the Mastra AI framework. This client provides a simple interface to interact with Mastra AI's APIs for agents, vectors, memory, tools, and workflows.
Installation
npm install @mastra/client-js
Quick Start
import { MastraClient } from '@mastra/client';
const client = new MastraClient({
baseUrl: 'http://localhost:4111',
});
async function main() {
const agent = client.getAgent('your-agent-id');
const response = await agent.generate({
messages: [{ role: 'user', content: "What's the weather like today?" }],
});
console.log(response);
}
Client Configuration
The client can be configured with several options:
const client = new MastraClient({
baseUrl: string;
retries?: number;
backoffMs?: number;
maxBackoffMs?: number;
headers?: Record<string, string>;
});
Available Methods
Agents
getAgents()
: Get all available agentsgetAgent(agentId)
: Get a specific agent instance
agent.details()
: Get agent detailsagent.generate(params)
: Generate a responseagent.stream(params)
: Stream a responseagent.getTool(toolId)
: Get agent tool detailsagent.evals()
: Get agent evaluationsagent.liveEvals()
: Get live evaluations
Memory
getMemoryThreads(params)
: Get memory threadscreateMemoryThread(params)
: Create a new memory threadgetMemoryThread(threadId)
: Get a memory thread instancesaveMessageToMemory(params)
: Save messages to memorygetMemoryStatus()
: Get memory system status
Tools
getTools()
: Get all available toolsgetTool(toolId)
: Get a tool instance
tool.details()
: Get tool detailstool.execute(params)
: Execute the tool
Workflows
getWorkflows()
: Get all workflowsgetWorkflow(workflowId)
: Get a workflow instance
workflow.details()
: Get workflow detailsworkflow.execute(params)
: Execute the workflowworkflow.watch(params)
: Watch the workflowworkflow.resume(params)
: Resume the workflow
Vectors
getVector(vectorName)
: Get a vector instance
vector.details(indexName)
: Get vector index detailsvector.delete(indexName)
: Delete a vector indexvector.getIndexes()
: Get all indexesvector.createIndex(params)
: Create a new indexvector.upsert(params)
: Upsert vectorsvector.query(params)
: Query vectors
Logs
getLogs(params)
: Get system logsgetLog(params)
: Get specific log entry
Telemetry
getTelemetry(params)
: Get telemetry data
Error Handling
The client includes built-in retry logic for failed requests:
- Automatically retries failed requests with exponential backoff
- Configurable retry count and backoff timing
- Throws error after max retries reached
Internal Implementation
The client uses the native fetch
API internally for making HTTP requests. All requests are automatically handled with:
- JSON serialization/deserialization
- Retry logic with exponential backoff
- Custom header management
- Error handling
License
MIT