> Documentation index: https://metorial.com/llms.txt. Every page is available as Markdown by adding .md to its URL.

# Add Metorial tools to an OpenAI Agents SDK agent
URL: https://metorial.com/docs/build/frameworks/openai-agents-sdk

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**

* [Install the CLI and sign in](https://metorial.com/docs/build/sandboxes#install-the-tools)
* An OpenAI API key
* Python 3.10 or later, or Node.js

## Set up Metorial with the CLI

### 1. Find your instance

An instance is one environment of a project. List yours and note the ID of the one the agent should use:

```bash
metorial-admin instance list
```

### 2. Create an API key

```bash
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.

### 3. Deploy a provider

Metorial Search is a built-in web search provider that needs no credentials.

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

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

### 4. Export the values

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

## Build the agent

### 1. Create the project

**Python**

```bash
mkdir my-agent && cd my-agent
python3 -m venv .venv
source .venv/bin/activate
pip install metorial openai-agents
```

**TypeScript**

```bash
mkdir my-agent && cd my-agent
npm init -y
npm pkg set type=module
npm install metorial @openai/agents
npm install --save-dev tsx
```

### 2. 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.

**Python**

```python
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())
```

**TypeScript**

```typescript
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();
}
```

### 3. Run it

**Python**

```bash
python agent.py
```

**TypeScript**

```bash
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.

```text
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:

```bash
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.

```bash
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](https://metorial.com/docs/build/sdk/oauth) 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

* [Let each user connect their own account](https://metorial.com/docs/build/sdk/oauth): per-user OAuth for Slack, Gmail, GitHub and other apps
* [Limit the tools an agent can use](https://metorial.com/docs/build/guides/limit-agent-tools): serve only some of a provider's tools
* [Connect any MCP client](https://metorial.com/docs/build/guides/connect-any-mcp-client): the session URL and headers the TypeScript sample uses
* The official Python example: `metorial-admin example create metorial-openai-agents-sdk`