MetorialDocs

Add Metorial tools to an OpenAI Agents SDK agent

Pass session.tools() from the metorial_openai_agents adapter to an Agent in Python, or point MCPServerStreamableHttp at a Metorial session URL in TypeScript.

This guide takes you from an empty folder to an OpenAI Agents SDK agent that searches the web through Metorial. In Python you pass session.tools() from the metorial_openai_agents adapter to Agent. In TypeScript you point the SDK's MCPServerStreamableHttp at a Metorial session URL.

What you'll learn

  • How to create an API key and a provider deployment from the CLI
  • How to give the agent Metorial tools in Python and TypeScript
  • How to see the tool calls the agent made

Before you start

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:

metorial-admin instance list

Create an API key

metorial-admin api-keys create \
  --type instance_access_token_secret \
  --instance-id <instance-id> \
  --name my-agent

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.

metorial-admin providers deployments create \
  --provider-id metorial-search \
  --name "Metorial Search"

Copy the deployment ID from the output. It starts with pde_.

Export the values

export METORIAL_API_KEY="metorial_sk_..."
export METORIAL_PROVIDER_DEPLOYMENT_ID="pde_..."
export OPENAI_API_KEY="sk-..."

Build the agent

Create the project

mkdir my-agent && cd my-agent
python3 -m venv .venv
source .venv/bin/activate
pip install metorial openai-agents
mkdir my-agent && cd my-agent
npm init -y
npm pkg set type=module
npm install metorial @openai/agents
npm install --save-dev tsx

Write the agent

Save this as agent.py or agent.ts. The Python SDK has an adapter for the Agents SDK. The TypeScript SDK does not, so the TypeScript version creates a session and connects the Agents SDK's own MCP client to it.

import asyncio
import os

from agents import Agent, Runner
from metorial import Metorial, metorial_openai_agents


async def main():
    metorial = Metorial(api_key=os.environ["METORIAL_API_KEY"])

    session = await metorial.connect(
        adapter=metorial_openai_agents(),
        providers=[
            {"provider_deployment_id": os.environ["METORIAL_PROVIDER_DEPLOYMENT_ID"]}
        ],
    )

    tools = session.tools()
    print("Metorial tools:", ", ".join(tool.name for tool in tools))

    agent = Agent(
        name="Research assistant",
        instructions="Use the Metorial tools to answer the question.",
        tools=tools,
    )

    result = await Runner.run(
        agent,
        "Search the web for the latest news about AI agents and summarize the top 3 stories.",
    )
    print(result.final_output)


asyncio.run(main())
import { Agent, MCPServerStreamableHttp, run } from '@openai/agents';
import Metorial from 'metorial';

let apiKey = process.env.METORIAL_API_KEY!;
let metorial = new Metorial({ apiKey });

let session = await metorial.sessions.create({
  providers: [{ providerDeploymentId: process.env.METORIAL_PROVIDER_DEPLOYMENT_ID! }]
});

let mcpServer = new MCPServerStreamableHttp({
  name: 'metorial',
  url: session.connectionUrl,
  requestInit: { headers: { Authorization: `Bearer ${apiKey}` } }
});

let agent = new Agent({
  name: 'Research assistant',
  instructions: 'Use the Metorial tools to answer the question.',
  mcpServers: [mcpServer]
});

try {
  await mcpServer.connect();

  let tools = await mcpServer.listTools();
  console.log('Metorial tools:', tools.map(tool => tool.name).join(', '));

  let result = await run(
    agent,
    'Search the web for the latest news about AI agents and summarize the top 3 stories.'
  );
  console.log(result.finalOutput);
} finally {
  await mcpServer.close();
}

Run it

python agent.py
npx tsx agent.ts

Check it worked

The first line lists the tools, then the agent prints its summary. Metorial prefixes each tool with the provider name and adds metorial_connection_status, a diagnostic tool, to every session.

Metorial tools: metorial_connection_status, metorial_search_getWebContent, metorial_search_webSearch
<a summary of three news stories>

List the tool calls Metorial recorded for the deployment:

metorial-admin sessions tool-calls list \
  --provider-deployment-id $METORIAL_PROVIDER_DEPLOYMENT_ID \
  --limit 5

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. One session can hold several.

metorial-admin providers list --search slack
metorial-admin providers deployments create --provider-id <provider-id> --name "Slack"

Apps such as Slack need a sign-in. Pass the user's provider auth config next to the deployment ID, as provider_auth_config_id in Python and providerAuthConfigId in TypeScript. Let each user connect their own account shows how to get one for each user.

Troubleshooting

Invalid API key status=401

The Python SDK raises this AuthenticationError when the key is wrong. In TypeScript the message is [METORIAL ERROR]: unauthorized - Invalid API key. Check that METORIAL_API_KEY holds the full secret and belongs to the instance where you created the deployment.

ValueError: api_key is required

METORIAL_API_KEY is empty in the shell that runs the script.

ImportError: openai-agents is required for OpenAI Agents SDK integration. Install it with: pip install openai-agents

The adapter could not import the agents package. Install openai-agents in the same virtual environment as metorial.

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 provider_auth_config_id to that entry.

Next steps