Connect a Claude Agents SDK Agent
A Claude Agent SDK client answers each message and replies with an in-process MCP tool—with no built-in shell or file access enabled.
Tested with: claude-agent-sdk 0.2.188, Python 3.12, msend API v1.
1. Install
pip install -r examples/claude-agents-sdk/requirements.txt2. Define the send_message Tool
@tool("send_message", "Reply to the person in a msend conversation.", {"conversation_id": str, "text": str})
async def send_message(args):
message = await asyncio.to_thread(
msend, "POST", f"/conversations/{args['conversation_id']}/messages", json={"text": args["text"]}
)
return {"content": [{"type": "text", "text": f"Sent {message['id']}"}]}3. Map Conversations to SDK Sessions
Keep a map from conversation.id to the SDK’s session_id and pass resume:
async def handle(data):
conversation_id = data["conversation"]["id"]
options = ClaudeAgentOptions(
system_prompt="You are a support agent. Answer the customer with the send_message tool.",
mcp_servers={"msend": msend_tools},
allowed_tools=["mcp__msend__send_message"],
tools=[],
setting_sources=[],
strict_mcp_config=True,
cwd=tempfile.gettempdir(),
max_turns=5,
resume=sessions.get(conversation_id),
)
async with ClaudeSDKClient(options=options) as client:
await client.query(f"conversation_id={conversation_id}\nMessage: {data['message']['text']}")
async for message in client.receive_response():
if isinstance(message, ResultMessage):
sessions[conversation_id] = message.session_id4. Poll for Events
cursor = None
while True:
page = await asyncio.to_thread(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"]:
await handle(data)
cursor = page["cursor"] or cursor
await asyncio.sleep(1)Last updated on