-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy path15_langchain_composio_notion_integration.py
More file actions
85 lines (67 loc) · 2.43 KB
/
15_langchain_composio_notion_integration.py
File metadata and controls
85 lines (67 loc) · 2.43 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# Before running, set local environment variables using the export function in the terminal and setting COMPOSIO_API_KEY, COMPOSIO_USER_ID, and ANTHROPIC_API_KEY to their respective tokens for authorization
import os
import asyncio
from dotenv import load_dotenv
from composio import Composio
from langchain_mcp_adapters.client import MultiServerMCPClient
from langchain.agents import create_agent
from langchain_anthropic import ChatAnthropic
load_dotenv()
async def main():
# --- Validate environment variables ---
if not os.getenv("COMPOSIO_API_KEY"):
raise ValueError("COMPOSIO_API_KEY is not set")
if not os.getenv("COMPOSIO_USER_ID"):
raise ValueError("COMPOSIO_USER_ID is not set")
if not os.getenv("ANTHROPIC_API_KEY"):
raise ValueError("ANTHROPIC_API_KEY is not set")
# --- 1. Initialize Composio client ---
composio = Composio(api_key=os.getenv("COMPOSIO_API_KEY"))
# --- 2. Create Tool Router session for Notion ---
session = composio.create(
user_id=os.getenv("COMPOSIO_USER_ID"),
toolkits=["notion"],
)
mcp_url = session.mcp.url
# --- 3. MCP client (Notion tools) ---
client = MultiServerMCPClient(
{
"notion": {
"transport": "streamable_http",
"url": mcp_url,
"headers": {
"x-api-key": os.getenv("COMPOSIO_API_KEY")
},
}
}
)
tools = await client.get_tools()
# --- 4. Claude LLM ---
llm = ChatAnthropic(
model="claude-sonnet-4-5-20250929",
temperature=0,
max_tokens=2048,
)
# --- 5. LangChain agent ---
agent = create_agent(
model=llm,
tools=tools,
system_prompt=(
"You are a Notion assistant. "
"Use the available tools to read Notion pages and return accurate data. "
"Do not invent content."
),
)
# --- 6. Interactive loop ---
messages = []
print("Claude-powered Notion agent ready. Type 'exit' to quit.\n")
while True:
user_input = input("You: ").strip()
if user_input.lower() in {"exit", "quit"}:
break
messages.append({"role": "user", "content": user_input})
response = await agent.ainvoke({"messages": messages})
messages = response["messages"]
print(f"\nAgent: {messages[-1].content}\n")
if __name__ == "__main__":
asyncio.run(main())