LangChain
Operations
Wrap myagentmail endpoints as LangChain tools (TS or Python).
TypeScript (LangChain.js)
import { DynamicStructuredTool } from "@langchain/core/tools";
import { z } from "zod";
const KEY = process.env.MYAGENTMAIL_KEY!;
const API = "https://myagentmail.com/v1";
async function mam(method: string, path: string, body?: unknown) {
const r = await fetch(`${API}${path}`, {
method,
headers: { "X-API-Key": KEY, "Content-Type": "application/json" },
body: body ? JSON.stringify(body) : undefined,
});
if (!r.ok) throw new Error(`${method} ${path} → ${r.status}`);
return r.json();
}
export const sendEmailTool = new DynamicStructuredTool({
name: "send_email",
description: "Send an email from a specific myagentmail inbox.",
schema: z.object({
inboxId: z.string(),
to: z.string().email(),
subject: z.string(),
plainBody: z.string(),
}),
func: async ({ inboxId, ...body }) =>
JSON.stringify(await mam("POST", `/inboxes/${inboxId}/send`, { ...body, verified: true })),
});
export const listInboxMessagesTool = new DynamicStructuredTool({
name: "list_inbox_messages",
description: "List inbound messages in a myagentmail inbox.",
schema: z.object({
inboxId: z.string(),
limit: z.number().int().default(10),
}),
func: async ({ inboxId, limit }) =>
JSON.stringify(await mam("GET", `/inboxes/${inboxId}/messages?direction=inbound&limit=${limit}`)),
});
Python (LangChain)
import os, requests
from langchain_core.tools import tool
KEY = os.environ["MYAGENTMAIL_KEY"]
API = "https://myagentmail.com/v1"
H = {"X-API-Key": KEY, "Content-Type": "application/json"}
def mam(method, path, body=None):
r = requests.request(method, f"{API}{path}", headers=H, json=body)
r.raise_for_status()
return r.json()
@tool
def send_email(inbox_id: str, to: str, subject: str, plain_body: str) -> dict:
"""Send an email from a myagentmail inbox."""
return mam("POST", f"/inboxes/{inbox_id}/send",
{"to": to, "subject": subject, "plainBody": plain_body, "verified": True})
@tool
def list_inbox_messages(inbox_id: str, limit: int = 10) -> dict:
"""List recent inbound messages in a myagentmail inbox."""
return mam("GET", f"/inboxes/{inbox_id}/messages?direction=inbound&limit={limit}")
Pass these tools into your agent constructor (create_react_agent, AgentExecutor, LangGraph node, etc.) and the LLM will call them when relevant.