Add Metorial tools to a Claude Agent SDK agent
Create a Metorial session, add its connection URL to mcpServers as an http server with your API key in the Authorization header, and allow mcp__metorial__*.
This guide takes you from an empty folder to a Claude Agent SDK agent that searches the web through Metorial. You create a Metorial session, pass its URL to query() under mcpServers as an http server with your API key in the Authorization header, and allow mcp__metorial__*.
What you'll learn
- How to create an API key and a provider deployment from the CLI
- How to connect the Agent SDK to a Metorial session in TypeScript and Python
- How to confirm the server connected and see the tool calls
Before you start
- Install the CLI and sign in
- An Anthropic API key
- Node.js 18.14 or later, or Python 3.10 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 or agent.py. The Agent SDK does not open a browser to sign in to MCP servers, so the session URL is paired with an API key in a header. Tool names take the form mcp__<server>__<tool>, which is why the server key metorial leads to mcp__metorial__*.
Run it
Check it worked
The output starts with the server status and the tool names, then the agent's summary. Metorial prefixes each tool 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 GitHub need a sign-in. Pass the user's provider auth config next to the deployment ID, as providerAuthConfigId in TypeScript and provider_auth_config_id in Python. Let each user connect their own account shows how to get one for each user.
A portal's Magic MCP URL expects a person to sign in through the browser, so it does not fit an agent that runs unattended. Use a session and an API key, as above.
Troubleshooting
The server status is needs-auth or failed
The Agent SDK got an authorization challenge or could not reach the URL. Check that headers holds Authorization: Bearer followed by a secret key (metorial_sk_), and that url is the session's connection URL, unchanged.
The server status is pending
The Agent SDK had not finished connecting when it sent the init message. Read the status later with mcpServerStatus() on the query in TypeScript, or get_mcp_status() on ClaudeSDKClient in Python.
[METORIAL ERROR]: unauthorized - Invalid API key
sessions.create rejected the key. In Python the message is Invalid API key status=401. 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
- Run an agent in CI or a background job: API keys and stored connections with no browser
- Connect Claude Code: for developers who use Claude Code interactively