Agents API

The Agents API provides methods to interact with Mastra AI agents, including generating responses, streaming interactions, and managing agent tools.

Getting All Agents

Retrieve a list of all available agents:

const agents = await client.getAgents();

Working with a Specific Agent

Get an instance of a specific agent:

const agent = client.getAgent("agent-id");

Agent Methods

Get Agent Details

Retrieve detailed information about an agent:

const details = await agent.details();

Generate Response

Generate a response from the agent:

const response = await agent.generate({
  messages: [
    {
      role: "user",
      content: "Hello, how are you?",
    },
  ],
  threadId: "thread-1", // Optional: Thread ID for conversation context
  resourceid: "resource-1", // Optional: Resource ID
  output: {}, // Optional: Output configuration
});

Stream Response

Stream a response from the agent for real-time interactions:

const response = await agent.stream({
  messages: [
    {
      role: "user",
      content: "Tell me a story",
    },
  ],
});
 
// Read from response body
const reader = response.body.getReader();
while (true) {
  const { done, value } = await reader.read();
  if (done) break;
  console.log(new TextDecoder().decode(value));
}

Get Agent Tool

Retrieve information about a specific tool available to the agent:

const tool = await agent.getTool("tool-id");

Get Agent Evaluations

Get evaluation results for the agent:

// Get CI evaluations
const evals = await agent.evals();
 
// Get live evaluations
const liveEvals = await agent.liveEvals();