MetorialDocs

Add Metorial tools to an AutoGen agent in Python

Connect with metorial.connect(adapter=metorial_autogen(), providers=[...]) and pass session.tools() to an AutoGen AssistantAgent. Python only.

This guide takes you from an empty folder to an AutoGen agent that searches the web through Metorial. Connect with metorial.connect(adapter=metorial_autogen(), providers=[...]) and pass session.tools() to AssistantAgent.

What you'll learn

  • How to create an API key and a provider deployment from the CLI
  • How to pass Metorial tools to an AutoGen AssistantAgent
  • 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 autogen-agentchat 'autogen-ext[openai]'

Write the agent

Save this as agent.py. The adapter ships with the metorial package and turns every tool in the session into an AutoGen FunctionTool. reflect_on_tool_use makes the agent answer in its own words after a tool call.

import asyncio
import os

from autogen_agentchat.agents import AssistantAgent
from autogen_agentchat.ui import Console
from autogen_ext.models.openai import OpenAIChatCompletionClient
from metorial import Metorial, metorial_autogen


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

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

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

    model_client = OpenAIChatCompletionClient(model="gpt-4o")
    agent = AssistantAgent(
        name="assistant",
        model_client=model_client,
        tools=tools,
        system_message="Use the Metorial tools to answer the question.",
        reflect_on_tool_use=True,
    )

    await Console(
        agent.run_stream(
            task="Search the web for the latest news about AI agents and summarize the top 3 stories."
        )
    )
    await model_client.close()


asyncio.run(main())

Run it

python agent.py

Check it worked

The first line lists the tools, then AutoGen prints the conversation. 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
---------- user ----------
Search the web for the latest news about AI agents and summarize the top 3 stories.
---------- assistant ----------
[FunctionCall(id='call_...', arguments='{"query":"..."}', name='metorial_search_webSearch')]
---------- assistant ----------
[FunctionExecutionResult(content='...', call_id='call_...', is_error=False)]
---------- assistant ----------
<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 github
metorial-admin providers deployments create --provider-id <provider-id> --name "GitHub"

Apps such as GitHub need a sign-in. Pass the user's provider auth config as provider_auth_config_id next to provider_deployment_id. Let each user connect their own account shows how to get one for each user.

Troubleshooting

ImportError: autogen-core is required for Autogen integration. Install it with: pip install autogen-agentchat autogen-ext

The adapter could not import autogen_core.tools. Install autogen-agentchat in the same virtual environment as metorial.

Invalid API key status=401

The SDK raises this AuthenticationError when the key is wrong. 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.

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