Skip to content

MCP serverLangChain

Give a LangChain agent email tools

LangChain loads tools from any MCP server through MCPAdapter. Point it at the Shipmail MCP server and your LangChain or LangGraph agent can read, draft and send email from a mailbox on your own domain. You and your team use the same mailbox, so you see what the agent did and can take over any conversation.

Last updated

What you get

  • Every Shipmail tool the key allows, as LangChain tools. For an inbox agent these are mostly shipmail_list_mailbox_inbox_messages, shipmail_get_mailbox_inbox_thread and shipmail_create_inbox_reply_draft.
  • Human approval on the send tools with LangChain's own middleware.

Create the mailbox and a scoped key

You need a Shipmail account with your domain added. Unlike a Gmail account made for a bot, a Shipmail mailbox is a paid business mailbox on your own domain, and giving it to an agent is what it is for. The address stays yours if you change agents later.

  1. Create a mailbox for the agent

    Under Mailboxes, create an address like agent@yourdomain.com. You and your team can open it in Shipmail or any mail client and see every message the agent reads, drafts or sends.

  2. Create an API key

    Open Developer, then API keys, and create a key. Pick the Draft only preset. It grants mailboxes:read, messages:read, messages:write, drafts:write, drafts:compose, threads:read and suppressions:read, and no messages:send.

  3. Limit the key to that mailbox

    Choose specific resources and add only the agent's mailbox. The key then cannot read or write any other mailbox in your organization.

The key starts with sm_live_ and is shown once. The MCP server only lists the tools the key's scopes allow, so a Draft only key has no send tool at all. When the drafts are good, edit the key and switch to the Full agent preset, which adds messages:send and members:read. Scope details are on API authentication.

Python

MCP support is part of langchain from version 1.4.0, as the mcp extra. LangChain marks this namespace as beta. The openai extra installs the model provider for gpt-5.5. For another model, use that provider's extra.

uv add "langchain[mcp,openai]>=1.4.0"

Pass the API key as auth on the MCP client. It goes out as a bearer token. The middleware pauses the agent before any Shipmail tool that delivers mail.

import asyncio
import os

from fastmcp.client import Client
from langchain.agents import create_agent
from langchain.agents.middleware import HumanInTheLoopMiddleware
from langchain.mcp import MCPAdapter
from langgraph.checkpoint.memory import InMemorySaver

# Shipmail tools that deliver mail. A person approves each call.
SEND_TOOLS = [
    "shipmail_send_message",
    "shipmail_compose_message_with_file",
    "shipmail_reply_to_message",
    "shipmail_reply_to_thread",
    "shipmail_reply_to_inbox_message",
    "shipmail_reply_to_inbox_thread",
    "shipmail_send_inbox_reply_draft",
    "shipmail_send_newsletter_test",
    "shipmail_schedule_newsletter",
    "shipmail_resume_newsletter",
]


async def main() -> None:
    client = Client("https://shipmail.to/api/mcp", auth=os.environ["SHIPMAIL_API_KEY"])
    async with MCPAdapter(client) as adapter:
        tools = await adapter.list_tools()
        agent = create_agent(
            "gpt-5.5",
            tools,
            middleware=[
                HumanInTheLoopMiddleware(
                    interrupt_on={name: {"allowed_decisions": ["approve", "reject"]} for name in SEND_TOOLS}
                )
            ],
            checkpointer=InMemorySaver(),
        )
        result = await agent.ainvoke(
            {"messages": [{"role": "user", "content": "Summarize the unread mail in agent@yourdomain.com."}]},
            config={"configurable": {"thread_id": "inbox-review"}},
        )
        print(result["messages"][-1].content)


asyncio.run(main())

When the agent wants to send, ainvoke returns an interrupt instead. Show the call to a person, then resume the same thread_id with Command(resume=...) from langgraph.types. LangChain's human-in-the-loop docs show the exact decision format.

JavaScript and TypeScript

bun add @langchain/mcp-adapters@^2.0.0 @langchain/core @langchain/langgraph @langchain/openai langchain
import { MCPAdapter } from "@langchain/mcp-adapters";
import { createAgent } from "langchain";

const adapter = new MCPAdapter({
  servers: {
    shipmail: {
      url: "https://shipmail.to/api/mcp",
      headers: { Authorization: `Bearer ${process.env.SHIPMAIL_API_KEY}` },
    },
  },
});

try {
  const tools = await adapter.listTools();
  const agent = createAgent({ model: "gpt-5.5", tools });
  const result = await agent.invoke({
    messages: [{ role: "user", content: "Summarize the unread mail in agent@yourdomain.com." }],
  });
  console.log(result.messages.at(-1)?.content);
} finally {
  await adapter.close();
}

Tool names

In JavaScript, MCPAdapter puts the server key in front of each tool name. List the tools once and use the names it prints when you add human-in-the-loop rules.

First test

  • "Send me a summary of unread mail from today."
  • "Reply to the latest email from jane@customer.com and confirm the call on Thursday." With a Draft only key, the reply waits in Drafts.

Stay safe

  • Start with a Draft only key, limited to the agent's mailbox. The send tools then do not exist for the model.
  • Treat every email body as untrusted input. Do not give the same agent tools that move money or change accounts.
  • For an agent that reacts to new mail, check the sender before the model runs. See receive email in an AI agent for webhooks, DMARC checks and conversation memory.

Troubleshooting

  • ImportError on langchain.mcp: install the mcp extra and version 1.4.0 or later.
  • The interrupt never fires: the names in interrupt_on do not match the tool names. Print them from list_tools().
  • A tool is missing: the key does not grant its scope.

Next steps

Frequently asked questions

Should I use MCPAdapter or the Shipmail SDK?

Use MCPAdapter when the model should choose which email action to take. Use the TypeScript or Python SDK when your own code decides, for example a LangGraph node that always fetches the conversation before the model runs.

I use MultiServerMCPClient. Does that still work?

LangChain's migration guide says MCPAdapter replaces the langchain-mcp-adapters package and its MultiServerMCPClient. Move to MCPAdapter for new code.

How do I stop the agent from sending?

Give it a Draft only key. The Shipmail server then lists no send tools at all, so the model cannot call one. HumanInTheLoopMiddleware adds a second check once you allow sending.

Where does the agent's mail show up?

In a normal Shipmail mailbox on your domain. You and your team see the same conversations in the Shipmail app or a mail client.