Repository navigation
Expand file tree
/
Copy pathlanggraph_mcp_example.py
More file actions
116 lines (95 loc) · 3.41 KB
/
Copy pathlanggraph_mcp_example.py
File metadata and controls
116 lines (95 loc) · 3.41 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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
"""Example: LangGraph + Mnemo via MCP tools.
LangGraph agents connect to Mnemo via langchain-mcp-adapters,
exposing all 10 memory tools as LangChain tools within a graph agent.
Requirements:
pip install langgraph langchain-mcp-adapters langchain-openai
cargo build --release -p mnemo-cli
export OPENAI_API_KEY=sk-...
"""
import asyncio
from langchain_openai import ChatOpenAI
from langchain_mcp_adapters.client import MultiServerMCPClient
from langgraph.prebuilt import create_react_agent
async def main():
model = ChatOpenAI(model="gpt-4o")
# Connect to Mnemo via MCP stdio
client = MultiServerMCPClient(
{
"mnemo": {
"command": "mnemo",
"args": ["--db-path", "langgraph_demo.db", "--agent-id", "langgraph-agent"],
"transport": "stdio",
},
}
)
# Get all Mnemo tools as LangChain tools
tools = await client.get_tools()
print(f"Available tools: {[t.name for t in tools]}")
# Create a ReAct agent with memory tools
agent = create_react_agent(model, tools)
# Session 1: Store knowledge
print("\n=== Store Knowledge ===")
result = await agent.ainvoke(
{
"messages": [
{
"role": "user",
"content": (
"Remember these facts:\n"
"1. The user Alice is a Python developer\n"
"2. She works at Acme Corp\n"
"3. The project deadline is March 15th"
),
}
]
}
)
print(f"Agent: {result['messages'][-1].content}\n")
# Session 2: Recall and reason
print("=== Recall and Reason ===")
result = await agent.ainvoke(
{
"messages": [
{"role": "user", "content": "What do you know about Alice's work?"}
]
}
)
print(f"Agent: {result['messages'][-1].content}\n")
# Session 3: Complex query
print("=== Complex Query ===")
result = await agent.ainvoke(
{
"messages": [
{
"role": "user",
"content": "When is the deadline and who is working on it?",
}
]
}
)
print(f"Agent: {result['messages'][-1].content}")
# Custom StateGraph with memory tools
async def with_state_graph():
from langgraph.graph import StateGraph, MessagesState, START
from langgraph.prebuilt import ToolNode, tools_condition
from langchain.chat_models import init_chat_model
model = init_chat_model("openai:gpt-4o")
client = MultiServerMCPClient(
{"mnemo": {"command": "mnemo", "args": ["--db-path", "lg.db"], "transport": "stdio"}}
)
tools = await client.get_tools()
def call_model(state: MessagesState):
return {"messages": model.bind_tools(tools).invoke(state["messages"])}
builder = StateGraph(MessagesState)
builder.add_node("agent", call_model)
builder.add_node("tools", ToolNode(tools))
builder.add_edge(START, "agent")
builder.add_conditional_edges("agent", tools_condition)
builder.add_edge("tools", "agent")
graph = builder.compile()
result = await graph.ainvoke(
{"messages": [{"role": "user", "content": "Remember I prefer dark mode"}]}
)
print(result["messages"][-1].content)
if __name__ == "__main__":
asyncio.run(main())