Skip to main content

The short answer

Install commune-mail, wrap the Commune client in BaseTool subclasses, and pass those tools to any LangChain agent. The agent can then send email, read inboxes, and act on replies — all through standard tool calls.

Step 1: Install and configure

Set your credentials as environment variables:

Step 2: Create Commune tools

Subclass BaseTool for each capability you want to expose to the agent.
Python

Step 3: Add tools to a LangChain agent

Pass the tool instances to create_react_agent or AgentExecutor.
Python

Step 4: Handle inbound replies

Set a webhook URL in your Commune dashboard so the agent is triggered when a reply arrives. Use FastAPI (or any ASGI framework) to receive the payload.
Python
The extractedData field is populated automatically if you have a structured extraction schema configured on the inbox. Use it to route or enrich the agent’s context without extra parsing.

Full example

A complete working script that provisions tools, runs an agent, and starts the webhook server:
Python
Run with:
Then point your Commune webhook URL to https://your-host/webhook/email.

Email for LangChain Agents

Deeper walkthrough covering multi-agent email patterns with LangChain.

Inboxes

Create and manage per-agent inboxes programmatically via the API.

Webhooks

Webhook payload reference and signature verification for inbound email handling.
Last modified on March 19, 2026