How to integrate Openrouter MCP with Mastra AI

Connect Mastra AI 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 Mastra AI 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 Mastra AI 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:
  • Set up your environment so Mastra, OpenAI, and Composio work together
  • Create a Tool Router session in Composio that exposes Openrouter tools
  • Connect Mastra's MCP client to the Composio generated MCP URL
  • Fetch Openrouter tool definitions and attach them as a toolset
  • Build a Mastra agent that can reason, call tools, and return structured results
  • Run an interactive CLI where you can chat with your Openrouter agent

What is Mastra AI?

Mastra AI is a TypeScript framework for building AI agents with tool support. It provides a clean API for creating agents that can use external services through MCP.

Key features include:

  • MCP Client: Built-in support for Model Context Protocol servers
  • Toolsets: Organize tools into logical groups
  • Step Callbacks: Monitor and debug agent execution
  • OpenAI Integration: Works with OpenAI models via @ai-sdk/openai

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 step09 STEPS
1

Prerequisites

Before starting, make sure you have:
  • Node.js 18 or higher
  • A Composio account with an active API key
  • An OpenAI API key
  • Basic familiarity with TypeScript
2

Getting API Keys for OpenAI and Composio

OpenAI API Key
  • Go to the OpenAI dashboard and create an API key.
  • You need credits or a connected billing setup to use the models.
  • Store the key somewhere safe.
Composio API Key
  • Log in to the Composio dashboard.
  • Go to Settings and copy your API key.
  • This key lets your Mastra agent talk to Composio and reach Openrouter through MCP.
3

Install dependencies

bash
npm install @composio/core @mastra/core @mastra/mcp @ai-sdk/openai dotenv

Install the required packages.

What's happening:

  • @composio/core is the Composio SDK for creating MCP sessions
  • @mastra/core provides the Agent class
  • @mastra/mcp is Mastra's MCP client
  • @ai-sdk/openai is the model wrapper for OpenAI
  • dotenv loads environment variables from .env
4

Set up environment variables

bash
COMPOSIO_API_KEY=your_composio_api_key_here
COMPOSIO_USER_ID=your_user_id_here
OPENAI_API_KEY=your_openai_api_key_here

Create a .env file in your project root.

What's happening:

  • COMPOSIO_API_KEY authenticates your requests to Composio
  • COMPOSIO_USER_ID tells Composio which user this session belongs to
  • OPENAI_API_KEY lets the Mastra agent call OpenAI models
5

Import libraries and validate environment

typescript
import "dotenv/config";
import { openai } from "@ai-sdk/openai";
import { Agent } from "@mastra/core/agent";
import { MCPClient } from "@mastra/mcp";
import { Composio } from "@composio/core";
import * as readline from "readline";

import type { AiMessageType } from "@mastra/core/agent";

const openaiAPIKey = process.env.OPENAI_API_KEY;
const composioAPIKey = process.env.COMPOSIO_API_KEY;
const composioUserID = process.env.COMPOSIO_USER_ID;

if (!openaiAPIKey) throw new Error("OPENAI_API_KEY is not set");
if (!composioAPIKey) throw new Error("COMPOSIO_API_KEY is not set");
if (!composioUserID) throw new Error("COMPOSIO_USER_ID is not set");

const composio = new Composio({
  apiKey: composioAPIKey as string,
});
What's happening:
  • dotenv/config auto loads your .env so process.env.* is available
  • openai gives you a Mastra compatible model wrapper
  • Agent is the Mastra agent that will call tools and produce answers
  • MCPClient connects Mastra to your Composio MCP server
  • Composio is used to create a Tool Router session
6

Create a Tool Router session for Openrouter

typescript
async function main() {
  const session = await composio.create(
    composioUserID as string,
    {
      toolkits: ["openrouter"],
    },
  );

  const composioMCPUrl = session.mcp.url;
  console.log("Openrouter MCP URL:", composioMCPUrl);
What's happening:
  • create spins up a short-lived MCP HTTP endpoint for this user
  • The toolkits array contains "openrouter" for Openrouter access
  • session.mcp.url is the MCP URL that Mastra's MCPClient will connect to
7

Configure Mastra MCP client and fetch tools

typescript
const mcpClient = new MCPClient({
    id: composioUserID as string,
    servers: {
      nasdaq: {
        url: new URL(composioMCPUrl),
        requestInit: {
          headers: session.mcp.headers,
        },
      },
    },
    timeout: 30_000,
  });

console.log("Fetching MCP tools from Composio...");
const composioTools = await mcpClient.getTools();
console.log("Number of tools:", Object.keys(composioTools).length);
What's happening:
  • MCPClient takes an id for this client and a list of MCP servers
  • The headers property includes the x-api-key for authentication
  • getTools fetches the tool definitions exposed by the Openrouter toolkit
8

Create the Mastra agent

typescript
const agent = new Agent({
    name: "openrouter-mastra-agent",
    instructions: "You are an AI agent with Openrouter tools via Composio.",
    model: "openai/gpt-5",
  });
What's happening:
  • Agent is the core Mastra agent
  • name is just an identifier for logging and debugging
  • instructions guide the agent to use tools instead of only answering in natural language
  • model uses openai("gpt-5") to configure the underlying LLM
9

Set up interactive chat interface

typescript
let messages: AiMessageType[] = [];

console.log("Chat started! Type 'exit' or 'quit' to end.\n");

const rl = readline.createInterface({
  input: process.stdin,
  output: process.stdout,
  prompt: "> ",
});

rl.prompt();

rl.on("line", async (userInput: string) => {
  const trimmedInput = userInput.trim();

  if (["exit", "quit", "bye"].includes(trimmedInput.toLowerCase())) {
    console.log("\nGoodbye!");
    rl.close();
    process.exit(0);
  }

  if (!trimmedInput) {
    rl.prompt();
    return;
  }

  messages.push({
    id: crypto.randomUUID(),
    role: "user",
    content: trimmedInput,
  });

  console.log("\nAgent is thinking...\n");

  try {
    const response = await agent.generate(messages, {
      toolsets: {
        openrouter: composioTools,
      },
      maxSteps: 8,
    });

    const { text } = response;

    if (text && text.trim().length > 0) {
      console.log(`Agent: ${text}\n`);
        messages.push({
          id: crypto.randomUUID(),
          role: "assistant",
          content: text,
        });
      }
    } catch (error) {
      console.error("\nError:", error);
    }

    rl.prompt();
  });

  rl.on("close", async () => {
    console.log("\nSession ended.");
    await mcpClient.disconnect();
    process.exit(0);
  });
}

main().catch((err) => {
  console.error("Fatal error:", err);
  process.exit(1);
});
What's happening:
  • messages keeps the full conversation history in Mastra's expected format
  • agent.generate runs the agent with conversation history and Openrouter toolsets
  • maxSteps limits how many tool calls the agent can take in a single run
  • onStepFinish is a hook that prints intermediate steps for debugging

Complete Code

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

typescript
import "dotenv/config";
import { openai } from "@ai-sdk/openai";
import { Agent } from "@mastra/core/agent";
import { MCPClient } from "@mastra/mcp";
import { Composio } from "@composio/core";
import * as readline from "readline";

import type { AiMessageType } from "@mastra/core/agent";

const openaiAPIKey = process.env.OPENAI_API_KEY;
const composioAPIKey = process.env.COMPOSIO_API_KEY;
const composioUserID = process.env.COMPOSIO_USER_ID;

if (!openaiAPIKey) throw new Error("OPENAI_API_KEY is not set");
if (!composioAPIKey) throw new Error("COMPOSIO_API_KEY is not set");
if (!composioUserID) throw new Error("COMPOSIO_USER_ID is not set");

const composio = new Composio({ apiKey: composioAPIKey as string });

async function main() {
  const session = await composio.create(composioUserID as string, {
    toolkits: ["openrouter"],
  });

  const composioMCPUrl = session.mcp.url;

  const mcpClient = new MCPClient({
    id: composioUserID as string,
    servers: {
      openrouter: {
        url: new URL(composioMCPUrl),
        requestInit: {
          headers: session.mcp.headers,
        },
      },
    },
    timeout: 30_000,
  });

  const composioTools = await mcpClient.getTools();

  const agent = new Agent({
    name: "openrouter-mastra-agent",
    instructions: "You are an AI agent with Openrouter tools via Composio.",
    model: "openai/gpt-5",
  });

  let messages: AiMessageType[] = [];

  const rl = readline.createInterface({
    input: process.stdin,
    output: process.stdout,
    prompt: "> ",
  });

  rl.prompt();

  rl.on("line", async (input: string) => {
    const trimmed = input.trim();
    if (["exit", "quit"].includes(trimmed.toLowerCase())) {
      rl.close();
      return;
    }

    messages.push({ id: crypto.randomUUID(), role: "user", content: trimmed });

    const { text } = await agent.generate(messages, {
      toolsets: { openrouter: composioTools },
      maxSteps: 8,
    });

    if (text) {
      console.log(`Agent: ${text}\n`);
      messages.push({ id: crypto.randomUUID(), role: "assistant", content: text });
    }

    rl.prompt();
  });

  rl.on("close", async () => {
    await mcpClient.disconnect();
    process.exit(0);
  });
}

main();

Conclusion

You've built a Mastra AI agent that can interact with Openrouter through Composio's Tool Router. You can extend this further by:
  • Adding other toolkits like Gmail, Slack, or GitHub
  • Building a web-based chat interface around this agent
  • Using multiple MCP endpoints to enable cross-app workflows
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. Mastra AI 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