How to integrate Agent mail MCP with LangChain

Connect LangChain to Agent mail MCP. Show me unread emails from today, send a status update to your team, and more using natural language, with authentication handled for you.

Agent mail logoAgent mail
Api Key

Agent mail provides AI agents with dedicated email inboxes for sending, receiving, and managing emails. It empowers agents to communicate autonomously with people, services, and other agents—no human intervention needed.

93 Tools1 Triggers

Introduction

This guide walks you through connecting Agent mail to LangChain using the Composio tool router. By the end, you'll have a working Agent mail agent that can create inboxes, send and reply to emails, manage drafts and threads, schedule calendar events, and set up webhooks through natural language commands.

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

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

Also integrate Agent mail with

TL;DR

Here's what you'll learn:
  • Get and set up your OpenAI and Composio API keys
  • Connect your Agent mail project to Composio
  • Create a Tool Router MCP session for Agent mail
  • Initialize an MCP client and retrieve Agent mail tools
  • Build a LangChain agent that can interact with Agent mail
  • Set up an interactive chat interface for testing

What is LangChain?

LangChain is a framework for developing applications powered by language models. It provides tools and abstractions for building agents that can reason, use tools, and maintain conversation context.

Key features include:

  • Agent Framework: Build agents that can use tools and make decisions
  • MCP Integration: Connect to external services through Model Context Protocol adapters
  • Memory Management: Maintain conversation history across interactions
  • Multi-Provider Support: Works with OpenAI, Anthropic, and other LLM providers

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

The Agent mail MCP server is an implementation of the Model Context Protocol that connects your AI agent and assistants like Claude, Cursor, etc directly to your Agent mail account. It provides structured and secure access to your agent's dedicated inbox, so your agent can send emails, receive incoming messages, fetch specific conversations, and automate email-based workflows on your behalf.

  • Automated email sending: Instruct your agent to compose and send emails to any recipient, perfect for status updates, notifications, or outreach.
  • Message listing and inbox scanning: Let your agent quickly list all recent messages in the inbox to summarize, monitor, or trigger follow-up actions.
  • Targeted message retrieval: Have your agent fetch a specific message by ID, making it easy to pull full email content for context-aware decisions or replies.
  • Streamlined agent-to-agent communication: Enable your AI to directly interact with other agents or services via email, automating routine communication flows.

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

Prerequisites

Before starting this tutorial, make sure you have:
  • Python 3.10 or higher installed on your system
  • A Composio account with an API key
  • An OpenAI API key
  • Basic familiarity with Python and async programming
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

npm install @composio/langchain @langchain/core @langchain/openai @langchain/mcp-adapters dotenv

Install the required packages for LangChain with MCP support.

What's happening:

  • @composio/langchain provides Composio integration for LangChain
  • @langchain/mcp-adapters enables MCP client connections
  • @langchain/core is the core agent framework
  • dotenv/config loads environment variables
4

Set up environment variables

bash
COMPOSIO_API_KEY=your_composio_api_key_here
COMPOSIO_USER_ID=your_composio_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's API
  • COMPOSIO_USER_ID identifies the user for session management
  • OPENAI_API_KEY enables access to OpenAI's language models
5

Import dependencies

import { Composio } from '@composio/core';
import { LangchainProvider } from '@composio/langchain';
import { MultiServerMCPClient } from "@langchain/mcp-adapters";
import { createAgent } from "langchain";
import * as readline from 'readline';
import 'dotenv/config';

dotenv.config();
What's happening:
  • We're importing LangChain's MCP adapter and Composio SDK
  • The dotenv/config import loads environment variables from your .env file
  • This setup prepares the foundation for connecting LangChain with Agent mail functionality through MCP
6

Initialize Composio client

const composioApiKey = process.env.COMPOSIO_API_KEY;
const userId = process.env.COMPOSIO_USER_ID;

if (!composioApiKey) throw new Error('COMPOSIO_API_KEY is not set');
if (!userId) throw new Error('COMPOSIO_USER_ID is not set');

async function main() {
    const composio = new Composio({
        apiKey: composioApiKey as string,
        provider: new LangchainProvider()
    });
What's happening:
  • We're loading the COMPOSIO_API_KEY from environment variables and validating it exists
  • Creating a Composio instance that will manage our connection to Agent mail tools
  • Validating that COMPOSIO_USER_ID is also set before proceeding
7

Create a Tool Router session

const session = await composio.create(
    userId as string,
    {
        toolkits: ['agent_mail']
    }
);

const url = session.mcp.url;
What's happening:
  • We're creating a Tool Router session that gives your agent access to Agent mail tools
  • The create method takes the user ID and specifies which toolkits should be available
  • The returned session.mcp.url is the MCP server URL that your agent will use
  • This approach allows the agent to dynamically load and use Agent mail tools as needed
8

Configure the agent with the MCP URL

const client = new MultiServerMCPClient({
    "agent_mail-agent": {
        transport: "http",
        url: url,
        headers: {
            "x-api-key": process.env.COMPOSIO_API_KEY
        }
    }
});

const tools = await client.getTools();

const agent = createAgent({ model: "gpt-5", tools });
What's happening:
  • We're creating a MultiServerMCPClient that connects to our Agent mail MCP server via HTTP
  • The client is configured with a name and the URL from our Tool Router session
  • getTools() retrieves all available Agent mail tools that the agent can use
  • We're creating a LangChain agent using the GPT-5 model
9

Set up interactive chat interface

let conversationHistory: any[] = [];

console.log("Chat started! Type 'exit' or 'quit' to end the conversation.\n");
console.log("Ask any Agent mail related question or task to the agent.\n");

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

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;
    }

    conversationHistory.push({ role: "user", content: trimmedInput });
    console.log("\nAgent is thinking...\n");

    const response = await agent.invoke({ messages: conversationHistory });
    conversationHistory = response.messages;

    const finalResponse = response.messages[response.messages.length - 1]?.content;
    console.log(`Agent: ${finalResponse}\n`);
        
        rl.prompt();
    });

    rl.on('close', () => {
        console.log('\n👋 Session ended.');
        process.exit(0);
    });
What's happening:
  • We initialize an empty conversationHistory list to maintain context across interactions
  • A readline interface is used to continuously accept user input from the command line
  • When a user types a message, it's added to the conversation history and sent to the agent
  • The agent processes the request using the invoke() method with the full conversation history
  • Users can type 'exit', 'quit', or 'bye' to end the chat session gracefully
10

Run the application

main().catch((err) => {
    console.error('Fatal error:', err);
    process.exit(1);
});
What's happening:
  • We call the main() function to start the application

Complete Code

Here's the complete code to get you started with Agent mail and LangChain:

import { Composio } from '@composio/core';
import { LangchainProvider } from '@composio/langchain';
import { MultiServerMCPClient } from "@langchain/mcp-adapters";  
import { createAgent } from "langchain";
import * as readline from 'readline';
import 'dotenv/config';

const composioApiKey = process.env.COMPOSIO_API_KEY;
const userId = process.env.COMPOSIO_USER_ID;

if (!composioApiKey) throw new Error('COMPOSIO_API_KEY is not set');
if (!userId) throw new Error('COMPOSIO_USER_ID is not set');

async function main() {
    const composio = new Composio({
        apiKey: composioApiKey as string,
        provider: new LangchainProvider()
    });

    const session = await composio.create(
        userId as string,
        {
            toolkits: ['agent_mail']
        }
    );

    const url = session.mcp.url;
    
    const client = new MultiServerMCPClient({
        "agent_mail-agent": {
            transport: "http",
            url: url,
            headers: {
                "x-api-key": process.env.COMPOSIO_API_KEY
            }
        }
    });
    
    const tools = await client.getTools();
  
    const agent = createAgent({ model: "gpt-5", tools });
    
    let conversationHistory: any[] = [];
    
    console.log("Chat started! Type 'exit' or 'quit' to end the conversation.\n");
    console.log("Ask any Agent mail related question or task to the agent.\n");
    
    const rl = readline.createInterface({
        input: process.stdin,
        output: process.stdout,
        prompt: 'You: '
    });

    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;
        }
        
        conversationHistory.push({ role: "user", content: trimmedInput });
        console.log("\nAgent is thinking...\n");
        
        const response = await agent.invoke({ messages: conversationHistory });
        conversationHistory = response.messages;
        
        const finalResponse = response.messages[response.messages.length - 1]?.content;
        console.log(`Agent: ${finalResponse}\n`);
        
        rl.prompt();
    });

    rl.on('close', () => {
        console.log('\nSession ended.');
        process.exit(0);
    });
}

main().catch((err) => {
    console.error('Fatal error:', err);
    process.exit(1);
});

Conclusion

You've successfully built a LangChain agent that can interact with Agent mail through Composio's Tool Router.

Key features of this implementation:

  • Dynamic tool loading through Composio's Tool Router
  • Conversation history maintenance for context-aware responses
  • Async Python provides clean, efficient execution of agent workflows
You can extend this further by adding error handling, implementing specific business logic, or integrating additional Composio toolkits to create multi-app workflows.
TOOLS & TRIGGERS

Supported Tools and Triggers

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

Batch Get Messages

Return metadata for up to 500 messages from one inbox in a single request.

Batch Update Message Labels

Atomically add or remove the same labels on up to 50 messages in one inbox.

Complete File Upload

Mark a pending Agent Drive file ready after its bytes have been PUT to the presigned URL returned by CREATE_FILE.

Create Agent Armor Feedback

Record whether Agent Armor's completed verdict on a message was correct.

Create Calendar Event

Create a one-off or recurring event on a US-only inbox calendar.

Create Draft

Create a draft, optionally scheduling its automatic delivery with send_at.

Create File

Initialize a pending Agent Drive file and return instructions for a direct storage upload.

Create Inbox

Create a new AgentMail inbox via API.

Create Inbox API Key

Create an inbox-scoped bearer key and return its one-time secret.

Create Inbox in Pod

Create an inbox inside an explicitly selected AgentMail pod, optionally choosing its address, initial status, idempotency ID, and metadata.

Create Inbox Polling Webhook

Create or resume a polling webhook scoped to one inbox.

Create Inbox Webhook

Create a regular webhook scoped to one AgentMail inbox for a non-empty set of event types.

Create Allowlist or Blocklist Entry

Add an email address or domain to an organization-wide send, receive, or reply allowlist or blocklist.

Create Pod

Create an AgentMail pod for grouping inboxes and related resources.

Create Polling Webhook

Create or resume an organization-level polling webhook for a non-empty set of event types, optionally filtered to at most 10 inboxes and pods combined.

Create Webhook

Create an organization-level webhook for a non-empty set of event types, optionally filtered to at most 10 inboxes and pods combined.

Delete Calendar Event

Accept asynchronous deletion of a US-only calendar event or cancel recurring dates.

Delete Draft

Permanently delete one unsent draft from an inbox.

Delete File

Permanently delete a pending or ready Agent Drive file and free its path.

Delete Inbox

Permanently delete one inbox by ID.

Delete Inbox Polling Webhook

Permanently delete an inbox-scoped polling webhook by webhook_id or client_id, invalidate its polling token, and free its subscription slot.

Delete Inbox Webhook

Permanently delete a regular webhook created for a selected AgentMail inbox and stop future deliveries.

Delete Allowlist or Blocklist Entry

Remove one email address or domain from an organization-wide send, receive, or reply allowlist or blocklist.

Delete Message

Permanently delete one message from an inbox.

Delete Pod

Permanently delete one AgentMail pod by its unique ID.

Delete Pod Inbox

Permanently delete an inbox through its explicitly selected AgentMail pod, preserving the pod tenant boundary.

Delete Polling Webhook

Permanently delete an organization-level polling webhook by webhook_id or client_id, invalidate its polling token, and free its subscription slot.

Delete Thread

Permanently delete one AgentMail thread and all of its messages.

Delete Webhook

Permanently delete an organization-level webhook and stop future deliveries.

Forward Message

Immediately forward an existing message to new recipients with optional added content and attachments.

Get Agenda

Get one page of non-cancelled calendar dates in a UTC window, expanding recurring events and ordering dates by start time.

Get API Key

Retrieve API-key metadata without exposing bearer secret values.

Get Calendar

Get an inbox's US-only calendar settings, event count, and ETag.

Get Calendar Event

Get the full current form of a US-only calendar event or recurring-event date, including its opaque ETag for conditional mutations.

Get Current Identity

Return the connected AgentMail identity and its organization, pod, or inbox access boundary without exposing the credential itself.

Get Domain

Return an AgentMail custom domain, its domain and tracking verification statuses, and all required DNS records.

Get Domain Setup Link

Build a one-click Domain Connect DNS setup link when supported.

Get Domain Zone File

Download the AgentMail-generated DNS zone file for a custom domain as a bounded binary file reference.

Get Draft

Retrieve the complete contents and scheduling details of one draft.

Get Draft Attachment

Get a temporary download reference for one draft attachment.

Get File

Get Agent Drive file metadata.

Get Inbox

Retrieve one AgentMail inbox and its metadata by ID or email address.

Get Inbox Polling Webhook

Get one inbox-scoped polling webhook by webhook_id or client_id.

Get Inbox Webhook

Get one regular webhook created for a selected AgentMail inbox.

Get Inbox Webhook Headers

List only the names of custom delivery headers on an inbox-scoped regular webhook.

Get Message

Retrieve an email message from an AgentMail inbox, including sender, recipients, labels, and available subject, body and attachment metadata.

Get Message Attachment

Get a temporary download reference for one message attachment.

Get Organization

Return the connected AgentMail organization's identifiers, current inbox and domain counts, configured resource limits, and timestamps.

Get Pod

Retrieve one AgentMail pod by its unique ID.

Get Pod Inbox

Get an AgentMail inbox through an explicitly selected pod, preserving the pod tenant boundary.

Get Polling Webhook

Get one organization-level polling webhook by webhook_id or client_id.

Get Raw Message

Get a temporary download reference for one message's raw RFC 5322 source, including its original headers and MIME structure.

Get Thread

Retrieve one AgentMail thread and a paginated page of its messages.

Get Thread Attachment

Get a temporary download reference for an attachment using its thread and attachment IDs.

Get Webhook

Get one organization-level webhook configuration by ID.

Get Webhook Headers

List only the names of custom delivery headers on an organization-level webhook.

List Calendar Event Instances

List one page of non-cancelled dates computed for a recurring US-only calendar event in a UTC window, including dates beyond the agenda horizon.

List Calendar Events

List one US-only calendar page of stored one-off events, recurring series, and edited series dates, including cancelled events, by most recent update.

List Domains

List one page of AgentMail custom domains and return the continuation token for the next page.

List Drafts

List unsent drafts in an AgentMail inbox with filters and pagination.

List Files

List one page of an inbox's Agent Drive files in path order, including pending files.

List Inbox API Keys

List API-key metadata scoped to an inbox without exposing secret values.

List Inboxes

List a page of inboxes available to the authenticated AgentMail account.

List Inbox Events

List label-change events from an AgentMail inbox.

List Inbox Polling Webhooks

List polling webhooks created through one inbox route.

List Inbox Webhooks

List one page of regular webhooks created for a selected AgentMail inbox.

List Allowlist or Blocklist Entries

List one page of organization-wide send, receive, or reply allowlist or blocklist entries.

List Messages

List messages from an AgentMail inbox.

List Pod Inboxes

List one page of inboxes inside an explicitly selected AgentMail pod, or supply query to search addresses and display names.

List Pods

Return one page of AgentMail pods and its continuation token.

List Polling Webhooks

List organization-level polling webhooks created through the organization route.

List Threads

List conversation threads in an AgentMail inbox with filters.

List Webhooks

List AgentMail webhook configurations.

Query Inbox Event Metrics

Query timestamped email-event counts for an AgentMail inbox.

Query Metrics

Query organization-wide email event counts or cumulative resource-usage time series over a bounded time range.

Query Rate Metrics

Query organization-wide rolling bounce and complaint rates.

Release Held Message

Release a message that Agent Armor held for review, emit its delivery and security override events, and remove the agent-armor-held label.

Reply To Message

Reply to a selected message while preserving its AgentMail thread.

Respond to Calendar Event

Accept, decline, or tentatively accept an email invitation on a US-only inbox calendar.

Send Draft

Immediately send and consume a draft, optionally changing message labels.

Send Email

Send an email using AgentMail API

Send Message

Immediately send a new email from an AgentMail inbox with optional HTML, attachments, custom headers, labels, and open tracking.

Update Calendar

Change an inbox calendar's default time zone in US production.

Update Calendar Event

Patch a US-only calendar event, one recurring date, or that date and future dates.

Update Draft

Edit a draft, reschedule delivery, or cancel a scheduled send with null.

Update Inbox

Update an inbox's display name, status, or custom metadata.

Update Inbox Webhook

Update an inbox-scoped regular webhook's enabled state or replace its event types.

Update Inbox Webhook Headers

Atomically set, replace, or remove custom delivery headers on an inbox-scoped regular webhook.

Update Message Labels

Add or remove labels from one message in an inbox.

Update Pod Inbox

Update the display name, active or paused status, or metadata of an inbox inside an explicitly selected AgentMail pod.

Update Thread Labels

Add or remove custom labels from every message in one thread.

Update Webhook

Update an organization-level webhook's enabled state, event types, or inbox and pod subscriptions.

Update Webhook Headers

Atomically set, replace, or remove custom delivery headers on an organization-level webhook.

FAQ

Frequently asked questions

Start with Agent mail.It takes 30 seconds.

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

Start building