Add Metorial tools to a Mastra agent with MCPClient
Create a Metorial session, give its connection URL to Mastra MCPClient with an Authorization header, and pass mcp.listTools() to the Agent tools option.
This guide takes you from an empty folder to a Mastra agent that searches the web through Metorial. You create a Metorial session, add its connection URL to Mastra's MCPClient with your API key in requestInit.headers, and pass await mcp.listTools() to the agent.
What you'll learn
- How to create an API key and a provider deployment from the CLI
- How to connect Mastra's
MCPClientto a Metorial session - How to see the tool calls the agent made
Before you start
- Install the CLI and sign in
- An OpenAI API key
- Node.js 22.13 or later
Set up Metorial with the CLI
Find your instance
An instance is one environment of a project. List yours and note the ID of the one the agent should use:
Create an API key
Copy the secret from the output. It starts with metorial_sk_ and is shown only once.
Deploy a provider
Metorial Search is a built-in web search provider that needs no credentials.
Copy the deployment ID from the output. It starts with pde_.
Export the values
Build the agent
Create the project
Write the agent
Save this as agent.ts. The Metorial SDK creates the session, and Mastra's own MCP client connects to it over Streamable HTTP.
Run it
Check it worked
The first line lists the tools, then the agent prints its summary. Mastra prefixes each tool with the server key metorial. Metorial prefixes it with the provider name and adds metorial_connection_status, a diagnostic tool, to every session.
List the tool calls Metorial recorded for the deployment:
Use other apps and each user's own account
Find a provider and deploy it the same way, then add its deployment ID to providers. All of them arrive through the one metorial server.
Apps such as Slack need a sign-in. Pass the user's provider auth config as providerAuthConfigId next to the deployment ID. Let each user connect their own account shows how to get one for each user.
For per-user agents, create one session and one MCPClient per user, and pass await mcp.listToolsets() as toolsets when you call the agent. Mastra's authenticate() OAuth flow is for servers that sign a person in through the browser. A Metorial session uses the API key header instead.
Troubleshooting
The tool list is empty or listTools() fails
Call mcp.listToolsWithErrors() and read errors and errorDetails. They name each server that failed to connect, with the HTTP status when there is one. A missing or wrong Authorization header is the usual cause.
[METORIAL ERROR]: unauthorized - Invalid API key
sessions.create rejected the key. Check that METORIAL_API_KEY holds the full secret and belongs to the instance where you created the deployment.
Provider requires an auth config to be provided
A provider deployment in the session needs a sign-in and got no provider auth config. Add the user's providerAuthConfigId to that entry.
Next steps
- Let each user connect their own account: per-user OAuth for Slack, Gmail, GitHub and other apps
- Limit the tools an agent can use: keep write or delete tools out of the session
- Connect any MCP client: the session URL, headers and tokens in detail
- Use Metorial with the Vercel AI SDK: the adapter for
generateTextandstreamText