MastraAdapter converts MCP tools into the format expected by the Mastra framework. Each tool is returned as an entry in a Record<string, MastraTool> object, where every value carries an id, a description, a Zod inputSchema, and an execute function that calls the corresponding MCP tool.
Installation
- npm
- pnpm
- yarn
zod is a required peer dependency. The adapter uses it internally to build the inputSchema for each tool. You do not need to install the Mastra SDK itself to use the adapter — the tool shape matches the Mastra interface so Mastra can consume it at runtime.Quick start
API reference
new MastraAdapter(client, options?)
Creates an adapter instance. Zod is loaded lazily on the first call to getTools().
| Parameter | Type | Description |
|---|---|---|
client | MCPClient | MultiSessionClient | The MCP client to source tools from. |
options | MastraAdapterOptions | Optional configuration (see below). |
adapter.getTools()
Fetches the tool list from all connected MCP servers and returns a Promise<Record<string, MastraTool>>. The key for each entry is the namespaced tool name (<prefix>_<toolName>).
When the underlying client is not connected, getTools() returns an empty object {} instead of throwing.
MastraAdapter.getTools(client, options?) (static)
A convenience wrapper that creates a MastraAdapter and immediately calls getTools().
MastraAdapterOptions
| Field | Type | Default | Description |
|---|---|---|---|
prefix | string | Server ID (8 chars) | Namespace prefix prepended to every tool name. |
MastraTool shape
Each value in the returned record conforms to the following interface:
Complete agent example
Connect to MCP servers
Create a
MultiSessionClient and call connect() to establish sessions with your configured MCP servers.Fetch tools with MastraAdapter
Use the static
getTools method to retrieve all MCP tools in Mastra-compatible format.Tool naming
Tool names follow the pattern<prefix>_<toolName>. The prefix defaults to the first eight characters of the server ID with hyphens removed.
prefix to control the namespace:
Using with MultiSessionClient
When you pass aMultiSessionClient, the adapter queries all connected servers concurrently and merges the results into a single flat record. If a server fails to respond, its tools are skipped with a console error and the remaining tools are returned.
Disconnected clients
Ifclient.isConnected() returns false when getTools() is called, the adapter returns {} immediately. No error is thrown — the Mastra agent simply has no tools available for that session.