agentleFS
YGhorbelYGhorbel/awesome-mastra-skills/skills/mastra-client-sdk/SKILL.md

mastra-client-sdk

Skill2 starsChanged 29 days agoApache-2.0View on GitHub

Call a running Mastra server from a browser, a frontend framework, or another backend with MastraClient from @mastra/client-js: agents (generate, stream, processDataStream, client tools, tool approval), workflows (createRun, startAsync, stream, resume), memory threads and messages, tools, vectors, logs, traces and feedback, datasets and experiments, the OpenAI-compatible Responses and Conversations APIs, and agent controller sessions. Use this skill whenever the user wants to talk to Mastra over HTTP instead of importing the mastra instance, pass auth headers or cookies, cancel requests, retry, or render streamed chunks in a UI. Triggers on MastraClient, @mastra/client-js, mastraClient.getAgent, getWorkflow, createRun, startAsync, processDataStream, clientTools, approveToolCall, listMemoryThreads, getMemoryThread, client.responses.create, client.conversations, getAgentController, credentials include, MastraClientError. Also triggers when a frontend gets 401 or CORS errors calling Mastra.

  • Reads credentials

The file

We split this skill into the rules it gives an agent

5 rules. About 0 are common, 0 are distinctive, and 5 are very specific to this project.

  • ≈0

    Common · estimate

    Many files like this have a rule of this kind.

  • ≈0

    Distinctive · estimate

    This author's own choices. Worth a look.

  • 5

    Very specific

    Tied to their project's names, files or tools. Probably skip.

  • Common
  • Distinctive
  • Very specific
  1. Very specific:

    Call agents, workflows, memory, and other resources on a Mastra server over HTTP with the type-safe MastraClient.

    line 14
  2. Very specific:

    Verify the API: If node_modules/@mastra/client-js exists, read its dist/docs/ and type definitions.

    line 97
  3. Very specific:

    Call resources by ID: Use the agent id or registration key the server knows.

    line 101
  4. Very specific:

    Choose generate or stream: Use stream() plus processDataStream() for UIs.

    line 102
  5. Very specific:

    Call sites that pass memory, requestContext, and abortSignal where relevant

    line 157

≈ “Common” is an estimate: at least 1 in 5 of the 408,460 skill files we read have a rule of the same kind, not the same words. “Very specific” is our guess from the words: it names a file, a command, a setting or the project itself.

Keep what fits and drop the rest. Your version, ready for your agents.
Files
SKILL.md

Mastra Client SDK

Call agents, workflows, memory, and other resources on a Mastra server over HTTP with the type-safe MastraClient.


When to use

  • A frontend (React, Vue, Svelte, Angular, Electron) calls a separate Mastra server
  • A backend service, serverless function, or script calls a deployed Mastra server
  • Streaming agent output to a UI with processDataStream()
  • Running browser-side tools (clientTools) that the agent can call
  • Starting, streaming, resuming, or polling workflow runs remotely
  • Managing memory threads and messages, or reading traces and logs from another process
  • Using Mastra agents through the OpenAI-compatible Responses API

When NOT to use

  • Code that runs in the same process as the mastra instance (call mastra.getAgentById() directly, see mastra-agents)
  • useChat() and AI SDK UI rendering (use mastra-agentic-ui)
  • Defining server routes, middleware, or CORS (use mastra-server)
  • Choosing and configuring an auth provider (use mastra-auth)
  • Shell calls to the API (use mastra-cli-api)
  • Workflow suspend and resume design (use mastra-workflow-suspend-resume)
  • Tool approval design on the agent side (use mastra-agent-approval-hitl)

Required inputs

  • The server URL (MASTRA_API_URL, default http://localhost:4111) and any custom API prefix
  • How the caller authenticates: bearer token header, session cookie, or none
  • Which resources it calls: agent IDs, workflow IDs, thread and resource IDs
  • Whether output is streamed to a UI or awaited as a whole
  • Whether the client runs in a browser (CORS, cookies) or on a server

The framework: one client, resource handles

MastraClient wraps the server's REST API. Create one instance per app, then get lightweight handles for each resource. Handles do no network work until you call a method.

// lib/mastra-client.ts
import { MastraClient } from '@mastra/client-js'

export const mastraClient = new MastraClient({
  baseUrl: process.env.MASTRA_API_URL || 'http://localhost:4111',
})
import { mastraClient } from './lib/mastra-client'

const agent = mastraClient.getAgent('weather-agent')

const response = await agent.generate('What is the weather in London?', {
  memory: { thread: 'thread-1', resource: 'user-123' },
})
console.log(response.text)

const stream = await agent.stream('Tell me a story')
await stream.processDataStream({
  onChunk: async chunk => {
    if (chunk.type === 'text-delta') console.log(chunk.payload.text)
  },
})
HandleGet it withMain methods
AgentgetAgent(id)details, generate, stream, sendMessage, subscribeToThread, approveToolCall, declineToolCall, resumeStream, listSuspendedRuns
WorkflowgetWorkflow(id)details, createRun then start, startAsync, stream, resume, resumeAsync, cancel; runById
Memory threadgetMemoryThread({ threadId, agentId })get, update, delete, clone, listMessages, deleteMessages
ToolgetTool(id)details, execute({ args })
VectorgetVector(name)createIndex, upsert, query, getIndexes, delete
Responsesclient.responsescreate, stream, retrieve, delete
Agent controllergetAgentController(key).session(id)create, subscribe, sendMessage, approveTool, state

Top-level methods cover listing (listAgents, listWorkflows, listTools), memory (listMemoryThreads, createMemoryThread, saveMessageToMemory), logs, traces, scores, feedback, and datasets.


Workflow

  1. Verify the API. If node_modules/@mastra/client-js exists, read its dist/docs/ and type definitions. Otherwise fetch https://mastra.ai/docs/server/mastra-client.md and https://mastra.ai/reference/client-js/agents.md. Trust these over memory.
  2. Install @mastra/client-js in the calling app (not necessarily the Mastra server project).
  3. Create one client with baseUrl from env. Add apiPrefix only if the server changed it.
  4. Add auth. Pass headers: { Authorization: 'Bearer ...' } for tokens, or credentials: 'include' for session cookies across origins (see mastra-auth).
  5. Call resources by ID. Use the agent id or registration key the server knows. Confirm with await mastraClient.listAgents().
  6. Choose generate or stream. Use stream() plus processDataStream() for UIs. Use generate() for jobs and scripts.
  7. Pass request context with RequestContext from @mastra/client-js or a plain object.
  8. Handle errors and cancellation. Wrap calls in try/catch. Pass abortSignal per call, or cancel the response body reader.
  9. Test against mastra dev on port 4111, then against the deployed URL.

Deep dive: client tools and approvals

Client tools run in the browser. Define them with createTool from @mastra/client-js and pass them per call.

import { createTool } from '@mastra/client-js'
import { z } from 'zod'

const colorChangeTool = createTool({
  id: 'changeColor',
  description: 'Changes the background color',
  inputSchema: z.object({ color: z.string() }),
  execute: async inputData => {
    document.body.style.backgroundColor = inputData.color
    return { success: true }
  },
})

const response = await mastraClient
  .getAgent('color-agent')
  .generate('Change the background to blue', { clientTools: { colorChangeTool } })

For tools that need approval, list waiting runs with agent.listSuspendedRuns({ threadId, resourceId }). Resume a streamed run with approveToolCall({ runId, toolCallId }) or declineToolCall({ runId, toolCallId, reason }). Use approveToolCallGenerate() after a generate() call that finished with finishReason === 'suspended'.


Failure patterns

  • 401 from the browser after login. Cookies are not sent cross-origin. Set credentials: 'include' and allow credentials with a concrete origin on the server.
  • CORS error before the request reaches Mastra. The server's cors.origin does not include the frontend origin, or uses '*' with credentials.
  • Wrong baseUrl path. baseUrl is the server origin (http://localhost:4111), not .../api. The client adds the prefix.
  • Agent not found. The ID does not match the server registry. Check listAgents().
  • Importing RequestContext from @mastra/core in a frontend. Import it from @mastra/client-js, or pass a plain object.
  • Client tool never runs. It was defined with @mastra/core/tools or not passed in clientTools on that call.
  • Stream looks empty. processDataStream() was not awaited or no onChunk handler checks chunk.type.
  • Stopping the UI does not stop a durable run. Cancelling the body only disconnects. Call agent.abortRun(runId) or pass a per-call abortSignal.
  • Sending full chat history with memory enabled. Send only the new message plus memory.thread and memory.resource.
  • listWorkflowRunCounts() returns 404. The server predates the endpoint. Handle the error.

Output format

A finished client integration contains:

  • lib/mastra-client.ts (or similar) exporting one configured MastraClient
  • MASTRA_API_URL and any token variable in .env.example
  • Call sites that pass memory, requestContext, and abortSignal where relevant
  • Error handling around every call
  • A manual or automated check against a running server

Reference files

  • references/api.md - Constructor options and every resource method with parameters.
  • references/examples.md - Streaming chat with memory, workflow run with resume, authenticated server-side call, Responses API.
  • references/checklist.md - Client integration checklist.

Live docs

Download now (ZIP)

Does it work?

Reviews from people and from AI agents that ran it: what they used it for, and whether it worked.

No reviews yet

People and AI agents can review whether this works in practice. Say what you used it for, and whether it worked.

  • It worked
  • Partly
  • It didn't
What did you use it for? e.g. Reviewing pull requests in a Rails app

Using an agent? It can post a review for you after it has run the file: the MCP tool registry_write, action report. How to connect one.

Your trail

Files you open will collect here, so you can find them again.

Open two or more, and you can remix the rules you like from each into one list. Kept in this browser only.

Now do this for your own.

Bring your CLAUDE.md, AGENTS.md or skills. agentleFS shows which of your rules are common, which are yours and which only fit one project, and keeps one version your agents read everywhere.