Skip to Content
Agent SDKsLangGraph

Connect a LangGraph Agent

A LangGraph graph answers each inbound message and replies using a tool.

Tested with: LangGraph 1.2.24, langchain-openai 1.8.5, langchain-anthropic 1.7.12, Python 3.12, msend API v1.

1. Install

pip install -r examples/langgraph/requirements.txt

Create an agent and API key in msend, then set MSEND_URL (e.g. https://app.msend.dev/api/v1) and MSEND_API_KEY.

2. Define the send_message Tool

@tool def send_message(conversation_id: str, text: str) -> str: """Reply to the person in a msend conversation. The reply is delivered on their channel.""" return msend("POST", f"/conversations/{conversation_id}/messages", json={"text": text})["id"]

3. One Memory per Conversation

LangGraph’s thread_id is a string, so msend’s conversation.id maps directly—allowing your checkpointer to maintain a separate history per conversation:

def handle(data, event_id): conversation_id = data["conversation"]["id"] system = f"You are a support agent. Reply with the send_message tool. conversation_id={conversation_id}" graph.invoke( {"messages": [("system", system), ("user", f"Message: {data['message']['text']}")]}, config={"configurable": {"thread_id": conversation_id}}, )

4. Feed Inbound Messages to the Agent

Poll GET /events and persist the returned cursor (or receive the same events via webhooks):

cursor = None while True: page = msend("GET", "/events", params={"after": cursor} if cursor else {}) for event in page["data"]: data = event["data"] if event["kind"] == "message.received" and data["message"]["addressed_to_agent"]: handle(data, event["id"]) cursor = page["cursor"] or cursor time.sleep(1)

In group chats and Slack channels, checking addressed_to_agent ensures the agent only replies when mentioned or in an active thread.

Last updated on