How to integrate Openrouter MCP with Autogen

Connect Autogen to Openrouter MCP. Generate python code from this prompt, summarize this article using claude-3, and more using natural language, with authentication handled for you.

Openrouter logoOpenrouter
Api Key

OpenRouter is a unified API platform for accessing language models from various providers. It lets you easily integrate and switch between multiple LLMs with a single API.

122 Tools

Introduction

This guide walks you through connecting Openrouter to AutoGen using the Composio tool router. By the end, you'll have a working Openrouter agent that can chat with models from many providers, create embeddings, generate speech and video, rerank content, and check your credits through natural language commands.

This guide will help you understand how to give your AutoGen agent real control over a Openrouter account through Composio's Openrouter MCP server.

Before we dive in, let's take a quick look at the key ideas and tools involved.

Also integrate Openrouter with

TL;DR

Here's what you'll learn:
  • Get and set up your OpenAI and Composio API keys
  • Install the required dependencies for Autogen and Composio
  • Initialize Composio and create a Tool Router session for Openrouter
  • Wire that MCP URL into Autogen using McpWorkbench and StreamableHttpServerParams
  • Configure an Autogen AssistantAgent that can call Openrouter tools
  • Run a live chat loop where you ask the agent to perform Openrouter operations

What is AutoGen?

Autogen is a framework for building multi-agent conversational AI systems from Microsoft. It enables you to create agents that can collaborate, use tools, and maintain complex workflows.

Key features include:

  • Multi-Agent Systems: Build collaborative agent workflows
  • MCP Workbench: Native support for Model Context Protocol tools
  • Streaming HTTP: Connect to external services through streamable HTTP
  • AssistantAgent: Pre-built agent class for tool-using assistants

What is the Openrouter MCP server, and what's possible with it?

The Openrouter MCP server is an implementation of the Model Context Protocol that connects your AI agent and assistants like Claude, Cursor, etc directly to your Openrouter account. It provides structured and secure access to a wide range of large language models, so your agent can generate completions, manage model access, check credits, and retrieve generation details seamlessly on your behalf.

  • Unified model completions: Let your agent generate chat-based or text completions using any model available through Openrouter, perfect for conversation or content creation tasks.
  • Model catalog and provider discovery: Ask your agent to list all available AI models and providers, helping you compare capabilities, endpoints, and pricing in real time.
  • Credit monitoring and usage tracking: Have your agent fetch your current API credit balance, so you always know your usage limits before starting new tasks.
  • Generation result retrieval: Direct your agent to pull detailed metadata for any previous generation, including token counts, costs, and latency for analysis or auditing.
  • Endpoint and configuration info: Empower your agent to fetch the latest model endpoints and supported parameters, making it easy to fine-tune routing and optimize performance.

What is the Composio tool router, and how does it fit here?

What is Composio SDK?

Composio's Composio SDK helps agents find the right tools for a task at runtime. You can plug in multiple toolkits (like Gmail, HubSpot, and GitHub), and the agent will identify the relevant app and action to complete multi-step workflows. This can reduce token usage and improve the reliability of tool calls. Read more here: Getting started with Composio SDK

The tool router generates a secure MCP URL that your agents can access to perform actions.

How the Composio SDK works

The Composio SDK follows a three-phase workflow:

  1. Discovery: Searches for tools matching your task and returns relevant toolkits with their details.
  2. Authentication: Checks for active connections. If missing, creates an auth config and returns a connection URL via Auth Link.
  3. Execution: Executes the action using the authenticated connection.

Step-by-step Guide

Step by step08 STEPS
1

Prerequisites

You will need:

  • A Composio API key
  • An OpenAI API key (used by Autogen's OpenAIChatCompletionClient)
  • A Openrouter account you can connect to Composio
  • Some basic familiarity with Autogen and Python async
2

Getting API Keys for OpenAI and Composio

OpenAI API Key
  • Go to the OpenAI dashboard and create an API key. You'll need credits to use the models, or you can connect to another model provider.
  • Keep the API key safe.
Composio API Key
  • Log in to the Composio dashboard.
  • Navigate to your API settings and generate a new API key.
  • Store this key securely as you'll need it for authentication.
3

Install dependencies

bash
pip install composio python-dotenv
pip install autogen-agentchat autogen-ext-openai autogen-ext-tools

Install Composio, Autogen extensions, and dotenv.

What's happening:

  • composio connects your agent to Openrouter via MCP
  • autogen-agentchat provides the AssistantAgent class
  • autogen-ext-openai provides the OpenAI model client
  • autogen-ext-tools provides MCP workbench support

4

Set up environment variables

bash
COMPOSIO_API_KEY=your-composio-api-key
OPENAI_API_KEY=your-openai-api-key
USER_ID=your-user-identifier@example.com

Create a .env file in your project folder.

What's happening:

  • COMPOSIO_API_KEY is required to talk to Composio
  • OPENAI_API_KEY is used by Autogen's OpenAI client
  • USER_ID is how Composio identifies which user's Openrouter connections to use
5

Import dependencies and create Tool Router session

python
import asyncio
import os
from dotenv import load_dotenv
from composio import Composio

from autogen_agentchat.agents import AssistantAgent
from autogen_ext.models.openai import OpenAIChatCompletionClient
from autogen_ext.tools.mcp import McpWorkbench, StreamableHttpServerParams

load_dotenv()

async def main():
    # Initialize Composio and create a Openrouter session
    composio = Composio(api_key=os.getenv("COMPOSIO_API_KEY"))
    session = composio.create(
        user_id=os.getenv("USER_ID"),
        toolkits=["openrouter"]
    )
    url = session.mcp.url
What's happening:
  • load_dotenv() reads your .env file
  • Composio(api_key=...) initializes the SDK
  • create(...) creates a Tool Router session that exposes Openrouter tools
  • session.mcp.url is the MCP endpoint that Autogen will connect to
6

Configure MCP parameters for Autogen

python
# Configure MCP server parameters for Streamable HTTP
server_params = StreamableHttpServerParams(
    url=url,
    timeout=30.0,
    sse_read_timeout=300.0,
    terminate_on_close=True,
    headers={"x-api-key": os.getenv("COMPOSIO_API_KEY")}
)

Autogen expects parameters describing how to talk to the MCP server. That is what StreamableHttpServerParams is for.

What's happening:

  • url points to the Tool Router MCP endpoint from Composio
  • timeout is the HTTP timeout for requests
  • sse_read_timeout controls how long to wait when streaming responses
  • terminate_on_close=True cleans up the MCP server process when the workbench is closed
7

Create the model client and agent

python
# Create model client
model_client = OpenAIChatCompletionClient(
    model="gpt-5",
    api_key=os.getenv("OPENAI_API_KEY")
)

# Use McpWorkbench as context manager
async with McpWorkbench(server_params) as workbench:
    # Create Openrouter assistant agent with MCP tools
    agent = AssistantAgent(
        name="openrouter_assistant",
        description="An AI assistant that helps with Openrouter operations.",
        model_client=model_client,
        workbench=workbench,
        model_client_stream=True,
        max_tool_iterations=10
    )

What's happening:

  • OpenAIChatCompletionClient wraps the OpenAI model for Autogen
  • McpWorkbench connects the agent to the MCP tools
  • AssistantAgent is configured with the Openrouter tools from the workbench
8

Run the interactive chat loop

python
print("Chat started! Type 'exit' or 'quit' to end the conversation.\n")
print("Ask any Openrouter related question or task to the agent.\n")

# Conversation loop
while True:
    user_input = input("You: ").strip()

    if user_input.lower() in ["exit", "quit", "bye"]:
        print("\nGoodbye!")
        break

    if not user_input:
        continue

    print("\nAgent is thinking...\n")

    # Run the agent with streaming
    try:
        response_text = ""
        async for message in agent.run_stream(task=user_input):
            if hasattr(message, "content") and message.content:
                response_text = message.content

        # Print the final response
        if response_text:
            print(f"Agent: {response_text}\n")
        else:
            print("Agent: I encountered an issue processing your request.\n")

    except Exception as e:
        print(f"Agent: Sorry, I encountered an error: {str(e)}\n")
What's happening:
  • The script prompts you in a loop with You:
  • Autogen passes your input to the model, which decides which Openrouter tools to call via MCP
  • agent.run_stream(...) yields streaming messages as the agent thinks and calls tools
  • Typing exit, quit, or bye ends the loop

Complete Code

Here's the complete code to get you started with Openrouter and AutoGen:

python
import asyncio
import os
from dotenv import load_dotenv
from composio import Composio

from autogen_agentchat.agents import AssistantAgent
from autogen_ext.models.openai import OpenAIChatCompletionClient
from autogen_ext.tools.mcp import McpWorkbench, StreamableHttpServerParams

load_dotenv()

async def main():
    # Initialize Composio and create a Openrouter session
    composio = Composio(api_key=os.getenv("COMPOSIO_API_KEY"))
    session = composio.create(
        user_id=os.getenv("USER_ID"),
        toolkits=["openrouter"]
    )
    url = session.mcp.url

    # Configure MCP server parameters for Streamable HTTP
    server_params = StreamableHttpServerParams(
        url=url,
        timeout=30.0,
        sse_read_timeout=300.0,
        terminate_on_close=True,
        headers={"x-api-key": os.getenv("COMPOSIO_API_KEY")}
    )

    # Create model client
    model_client = OpenAIChatCompletionClient(
        model="gpt-5",
        api_key=os.getenv("OPENAI_API_KEY")
    )

    # Use McpWorkbench as context manager
    async with McpWorkbench(server_params) as workbench:
        # Create Openrouter assistant agent with MCP tools
        agent = AssistantAgent(
            name="openrouter_assistant",
            description="An AI assistant that helps with Openrouter operations.",
            model_client=model_client,
            workbench=workbench,
            model_client_stream=True,
            max_tool_iterations=10
        )

        print("Chat started! Type 'exit' or 'quit' to end the conversation.\n")
        print("Ask any Openrouter related question or task to the agent.\n")

        # Conversation loop
        while True:
            user_input = input("You: ").strip()

            if user_input.lower() in ['exit', 'quit', 'bye']:
                print("\nGoodbye!")
                break

            if not user_input:
                continue

            print("\nAgent is thinking...\n")

            # Run the agent with streaming
            try:
                response_text = ""
                async for message in agent.run_stream(task=user_input):
                    if hasattr(message, 'content') and message.content:
                        response_text = message.content

                # Print the final response
                if response_text:
                    print(f"Agent: {response_text}\n")
                else:
                    print("Agent: I encountered an issue processing your request.\n")

            except Exception as e:
                print(f"Agent: Sorry, I encountered an error: {str(e)}\n")

if __name__ == "__main__":
    asyncio.run(main())

Conclusion

You now have an Autogen assistant wired into Openrouter through Composio's Tool Router and MCP. From here you can:
  • Add more toolkits to the toolkits list, for example notion or hubspot
  • Refine the agent description to point it at specific workflows
  • Wrap this script behind a UI, Slack bot, or internal tool
Once the pattern is clear for Openrouter, you can reuse the same structure for other MCP-enabled apps with minimal code changes.
TOOLS

Supported Tools

Every Openrouter action and event your agent gets out of the box.

Activate a validated private endpoint

Make a validated draft routable.

Bulk add members to a workspace

Add multiple organization members to a workspace.

Bulk assign keys to a guardrail

Assign multiple API keys to a specific guardrail.

Bulk assign members to a guardrail

Assign multiple organization members to a specific guardrail.

Bulk remove members from a workspace

Remove multiple members from a workspace.

Bulk unassign keys from a guardrail

Unassign multiple API keys from a specific guardrail.

Bulk unassign members from a guardrail

Unassign multiple organization members from a specific guardrail.

Create a new API key

Create a new API key for the authenticated user.

Create authorization code

Create an authorization code for the PKCE flow to generate a user-controlled API key

Create a batch

Creates a batch of requests that run asynchronously against a single endpoint (`/v1/chat/completions`, `/v1/responses`, `/v1/messages`, `/v1/embeddings`).

Create a BYOK provider credential

Create a new bring-your-own-key (BYOK) provider credential.

Create Chat Completion

Generate a chat-style completion from messages and a selected model, with function-call request options.

Create Coinbase Charge

Tool to create a Coinbase charge for crypto payment to add credits to your OpenRouter account.

Create Embeddings

Create vector embeddings for text, token, or multimodal inputs.

Create a guardrail

Create a new guardrail for the authenticated user.

Create Message (Anthropic Format)

Create a message using Anthropic Messages API format via OpenRouter, with text, image, PDF, tool, and extended-thinking request options.

Create an observability destination

Create a new observability destination.

Create a preset from a chat-completions request body

Creates a preset (or a new version of an existing one) from an inference request body.

Create a preset from a messages request body

Creates a preset (or a new version of an existing one) from an inference request body.

Create a preset from a responses request body

Creates a preset (or a new version of an existing one) from an inference request body.

Create a private endpoint

Create a private endpoint as a draft.

Create a SCIM group mapping

Create a SCIM group-to-workspace role mapping.

Start a SCIM directory sync

Start a SCIM directory sync.

Create Speech

Synthesize speech from text or multi-speaker turns.

Create Transcription

Transcribe audio into text using an OpenRouter speech-to-text model.

Create Video Generation

Submit an asynchronous video generation job to OpenRouter.

Create a workspace

Create a new workspace for the authenticated user.

Delete an API key

Delete one of the authenticated account's own API keys, revoking the selected key's access.

Delete a batch

Deletes a batch in a terminal status (`completed`, `failed`, `expired`, or `cancelled`) and its stored requests and results.

Delete a BYOK provider credential

Delete (soft-delete) a bring-your-own-key (BYOK) provider credential by its `id`.

Delete a file

Deletes a file owned by the requesting workspace.

Delete a guardrail

Delete an existing guardrail.

Delete an observability destination

Delete an existing observability destination.

Delete a private endpoint

Delete a private endpoint and stop routing to it.

Delete a SCIM group mapping

Delete a SCIM group-to-workspace mapping.

Delete a workspace

Delete an existing workspace.

Delete a workspace budget

Remove the budget for a given interval.

Disable a private endpoint

Stop routing to an active endpoint without deleting it.

Download Container File Content

Download raw file content from an OpenRouter container using its canonical container and file IDs.

Download File Content

Download raw content of a server-created file.

Enable a private endpoint

Resume routing to a disabled endpoint.

Exchange authorization code for API key

Exchange an authorization code from the PKCE flow for a user-controlled API key

Exchange a workload identity token

RFC 8693 token exchange.

Generate an image

Generates an image from a text prompt via the image generation router

Get available analytics metrics and dimensions

Returns the available metrics, dimensions, filter operators, and granularities for the analytics query endpoint.

Get a single API key

Get a single API key by hash.

Top apps by token usage

Returns the top public apps on OpenRouter ranked by token usage inside the requested date window, matching the public apps marketplace on openrouter.

Get a batch

Returns a batch with its status and request counts.

List Benchmarks

Unified benchmark endpoint that aggregates scores from multiple benchmark sources (Artificial Analysis, Design Arena, and OpenRouter's own tau-bench, GPQA, and web-search evals).

Get a BYOK provider credential

Get a single bring-your-own-key (BYOK) provider credential by its `id`.

Retrieve a container file

Returns the metadata of a single file in a container.

Get Credits

Tool to get the current API credit balance for the authenticated user.

Get Current Key

Tool to get information about the currently authenticated API key.

Get file metadata

Retrieves metadata for a single file owned by the requesting workspace.

Get Generation

Tool to retrieve a generation result by its unique ID.

Get a guardrail

Get a single guardrail by ID.

Get a model by its slug

Returns full details for a single model identified by its author and slug (e.

Get Models Count

Tool to get the total count of available models on OpenRouter.

OpenRouter access token signing keys

RFC 7517 JWK Set containing the public keys OpenRouter signs access tokens with.

Get an observability destination

Fetch a single observability destination by its UUID.

Get organization settings

Get the settings of the organization associated with the authenticated management key.

Get a preset

Retrieves a preset by its slug with its currently designated version inline.

Get a specific version of a preset

Retrieves a specific version of a preset by its slug and version number.

Get a private endpoint

Get one private endpoint with its upstream configuration and current pricing.

Daily token totals for top 50 models

Returns the top 50 public models per day by total token usage on OpenRouter, plus a single aggregated `other` row per day that sums every model outside that top 50.

Get a SCIM group mapping

Get a SCIM group-to-workspace mapping.

Get SCIM directory sync status

Get SCIM directory sync status.

Cost per session by harness and model

Returns weekly refreshed, aggregated cost-per-session cells for the published harnesses.

Task classification market share

Returns the market-share breakdown of OpenRouter traffic by task classification (e.

Get a server tool

One server tool by canonical name or any accepted alias, with the models that run it natively.

Get user activity grouped by endpoint

Returns user activity data grouped by endpoint for the last 30 (completed) UTC days.

Get Video Content

Download one output from a completed OpenRouter video generation job.

Get Video Generation

Poll a video generation job for its status, outputs, and usage.

Get a workspace

Get a single workspace by ID or slug.

Get a workspace budget

Retrieve the budget for a given interval.

List API keys

List all API keys for the authenticated user.

List Available Models

List available OpenRouter models, optionally filtered by category.

List batches

Lists batches in the workspace of the authenticating API key, newest first.

List BYOK provider credentials

List the bring-your-own-key (BYOK) provider credentials for the authenticated entity's default workspace.

List container files

Lists the files in a container, in lexicographic path order.

List Embedding Models

List available OpenRouter embedding models with properties including architecture, pricing, and capabilities.

List files

Lists files belonging to the workspace of the authenticating API key.

Get stored prompt, completion, and error content for a generation

Get stored prompt, completion, and error content for a generation

List key assignments for a guardrail

List all API key assignments for a specific guardrail.

List member assignments for a guardrail

List all organization member assignments for a specific guardrail.

List guardrails

List all guardrails for the authenticated user.

List endpoints for an image model

Returns the full per-endpoint records for an image model: each endpoint's definitive supported parameters, pricing, and passthrough allowlist.

List image generation models

Lists every image generation model with its top-level supported-parameter superset and a URL to its full per-endpoint records.

List all key assignments

List all API key guardrail assignments for the authenticated user.

List all member assignments

List all organization member guardrail assignments for the authenticated user.

OpenRouter List Model Endpoints

Tool to list endpoints for a specific model.

List observability destinations

List the observability destinations configured for the authenticated entity's default workspace.

List organization members

List all members of the organization associated with the authenticated management key.

List presets

Lists all presets for the authenticated user, ordered by most recently updated first.

List versions of a preset

Lists all versions of a preset, ordered by version number ascending (oldest first).

List private endpoints

List the organization's private endpoints, including drafts.

OpenRouter List Providers

Tool to list all AI model providers available through the OpenRouter API.

List SCIM group mappings

List SCIM group-to-workspace mappings for the organization.

List SCIM groups

List SCIM groups for the organization.

List server tools

Lists every server tool OpenRouter can run on behalf of a model: accepted `tools[].

List User Models

List models filtered by user provider preferences, privacy settings, and guardrails.

List Video Models

List video generation models and their supported capabilities.

List workspace budgets

List all budgets configured for a workspace.

List workspace members

List all members of a workspace.

List workspaces

List all workspaces for the authenticated user.

OpenRouter List ZDR Endpoints

Tool to preview the impact of Zero Data Retention (ZDR) on the available endpoints.

Promote a container file into workspace documents

Copies a file from the container's sandbox prefix into the workspace's durable document storage, so it outlives the container.

Query analytics data

Execute an analytics query with specified metrics, dimensions, filters, and time range.

Rerank Content

Rank text or multimodal documents by relevance to a query.

Submit feedback for a generation

Submit structured feedback on a generation the authenticated user made.

Submit a System One request

Sends state and typed questions to a System One model such as Jev and returns its answers.

Update an API key

Update one of the authenticated account's own API keys.

Update a BYOK provider credential

Update an existing bring-your-own-key (BYOK) provider credential by its `id`.

Update a guardrail

Update an existing guardrail, or materialize an unconfigured workspace default guardrail.

Update an observability destination

Update an existing observability destination.

Update organization settings

Update the settings of the organization associated with the authenticated management key.

Update a private endpoint

Change any of the endpoint fields you own in one call; only the fields present change, and the response is the whole endpoint.

Update a SCIM group mapping

Update a SCIM group mapping role.

Update a workspace

Update an existing workspace by ID or slug.

Upload File

Upload a file for future API calls under the authenticating key's workspace.

Create or update a workspace budget

Create or update the budget for a given interval.

Validate a draft private endpoint

Send a live test request to the draft endpoint using the given workspace's BYOK credential for its provider.

FAQ

Frequently asked questions

With a standalone Openrouter MCP server, the agents and LLMs can only access a fixed set of Openrouter tools tied to that server. However, with the Composio Tool Router, agents can dynamically load tools from Openrouter and many other apps based on the task at hand, all through a single MCP endpoint.

Yes, you can. Autogen fully supports MCP integration. You get structured tool calling, message history handling, and model orchestration while Tool Router takes care of discovering and serving the right Openrouter tools.

Yes, absolutely. You can configure which Openrouter scopes and actions are allowed when connecting your account to Composio. You can also bring your own OAuth credentials or API configuration so you keep full control over what the agent can do.

All sensitive data such as tokens, keys, and configuration is fully encrypted at rest and in transit. Composio is SOC 2 Type 2 compliant and follows strict security practices so your Openrouter data and credentials are handled as safely as possible.

Start with Openrouter.It takes 30 seconds.

Managed auth, hosted MCP servers, and every Openrouter tool your agent needs.Free to start.

Start building