Mastra Client SDK
The Mastra Client SDK provides a simple and type-safe interface to interact with Mastra REST APIs from TypeScript and JavaScript applications. It offers comprehensive support for all Mastra features including agents, vectors, memory, tools, and workflows.
Installation
npm install @mastra/client-js
Requirements
- Node.js 16.x or later
- TypeScript 4.7+ (for TypeScript users)
- Modern browser environment with Fetch API support (for browser usage)
Local Development
When developing locally, the Client SDK makes it easy to interact with your Mastra server. Simply point the client to your local server:
const client = new MastraClient({
baseUrl: "http://localhost:4111", // Default Mastra server port
});
For more information about local development, see the Local Development Guide.
Basic Configuration
The minimal configuration requires only the baseUrl
of your Mastra API endpoint:
import { MastraClient } from "@mastra/client-js";
const client = new MastraClient({
baseUrl: "http://localhost:4111",
});
Configuration Options
Here’s a complete example showing all available configuration options:
const client = new MastraClient({
// Required
baseUrl: "http://localhost:4111",
// Optional
retries: 3, // Number of retry attempts (default: 3)
backoffMs: 300, // Initial backoff time in ms (default: 300)
maxBackoffMs: 5000, // Maximum backoff time in ms (default: 5000)
headers: {
// Custom headers to include in all requests
"Custom-Header": "value",
},
});
Configuration Options
Option | Type | Default | Description |
---|---|---|---|
baseUrl | string | - | The base URL of your Mastra API endpoint (required) |
retries | number | 3 | Number of times to retry failed requests |
backoffMs | number | 300 | Initial backoff time in milliseconds |
maxBackoffMs | number | 5000 | Maximum backoff time in milliseconds |
headers | Record<string, string> | {} | Custom headers to include in all requests |
Available Resources
The client provides access to the following resources:
- Agents: Create and manage AI agents, generate responses, and handle streaming interactions
- Memory: Manage conversation threads and message history
- Tools: Access and execute tools available to agents
- Workflows: Create and manage automated workflows
- Vectors: Handle vector operations for semantic search and similarity matching
Each resource is documented in detail in its respective section.
Quick Example
Here’s a simple example of using the client with an agent:
const client = new MastraClient({
baseUrl: "http://localhost:4111",
});
// Get an agent instance
const agent = client.getAgent("your-agent-id");
// Generate a response
const response = await agent.generate({
messages: [
{
role: "user",
content: "Hello!",
},
],
});