Skip to main content

Use mails.ai with LangGraph

Load the mails.ai MCP tools into a LangGraph agent, in Python.

This guide connects LangGraph to the hosted mails.ai MCP server at https://api.mails.ai/mcp. It sends your API key in the Authorization header and gets the tools that key may call, as any other MCP client does (the tool list). With a test key (mk_test_…), the tools run in the sandbox and no email leaves mails.ai.

Before you start

The sample reads your key from MAILS_API_KEY. The framework also needs a key for its model provider, set the way its own documentation says.

export MAILS_API_KEY=mk_test_xxxxxxxx

The sample asks the agent to email reply@test.mails.ai. Nothing sent to that address leaves mails.ai, and it answers, so your agent gets a reply.received event back.

Install

pip install "langchain[mcp,anthropic]"

Write the agent

LangChain’s create_agent builds the agent on LangGraph, and LangChain’s MCP adapter loads the mails tools into it. The adapter sends the key you pass as auth as a bearer token. It needs langchain[mcp] 1.4.0 or newer, and LangChain marks it as beta, so its API may change.

Save this as agent.py:

import asyncio
import os

from fastmcp.client import Client
from langchain.agents import create_agent
from langchain.mcp import MCPAdapter


async def main():
    client = Client("https://api.mails.ai/mcp", auth=os.environ["MAILS_API_KEY"])
    async with MCPAdapter(client) as adapter:
        tools = await adapter.list_tools()
        agent = create_agent("claude-sonnet-5-5", tools)
        result = await agent.ainvoke(
            {"messages": [{"role": "user", "content": "Email reply@test.mails.ai to say the report is ready."}]}
        )
        print(result["messages"][-1].content)


asyncio.run(main())

Set ANTHROPIC_API_KEY, then run python agent.py.

Next steps

Was this page helpful?