LangGraph Adapter for uAgents
This example demonstrates how to integrate a LangGraph agent with the uAgents ecosystem using the uAgents Adapter package. LangGraph provides powerful orchestration capabilities for LLM applications through directed graphs.
Overview
The LangGraph adapter allows you to:
- Wrap LangGraph agents as uAgents for seamless communication
- Enable LangGraph agents to participate in the Agentverse ecosystem
- Leverage advanced orchestration for complex reasoning while maintaining uAgent compatibility
Example Implementation
- Create an agent with file name
agent.py
import os
import time
from dotenv import load_dotenv
from langchain_openai import ChatOpenAI
from langchain_community.tools.tavily_search import TavilySearchResults
from langgraph.prebuilt import chat_agent_executor
from langchain_core.messages import HumanMessage
from uagents_adapter import LangchainRegisterTool, cleanup_uagent
# Load environment variables
load_dotenv()
# Set your API keys - for production, use environment variables instead of hardcoding
OPENAI_API_KEY = os.environ["OPENAI_API_KEY"]
TAVILY_API_KEY = os.environ["TAVILY_API_KEY"]
# Get API token for Agentverse
API_TOKEN = os.environ["AGENTVERSE_API_KEY"]
if not API_TOKEN:
raise ValueError("Please set AGENTVERSE_API_KEY environment variable")
# Set up tools and LLM
tools = [TavilySearchResults(max_results=3)]
model = ChatOpenAI(temperature=0)
# LangGraph-based executor
app = chat_agent_executor.create_tool_calling_executor(model, tools)
# Wrap LangGraph agent into a function for UAgent
def langgraph_agent_func(query):
# Handle input if it's a dict with 'input' key
if isinstance(query, dict) and 'input' in query:
query = query['input']
messages = {"messages": [HumanMessage(content=query)]}
final = None
for output in app.stream(messages):
final = list(output.values())[0] # Get latest
return final["messages"][-1].content if final else "No response"
# Register the LangGraph agent via uAgent
tool = LangchainRegisterTool()
agent_info = tool.invoke(
{
"agent_obj": langgraph_agent_func,
"name": "langgraph_tavily_agent",
"port": 8080,
"description": "A LangGraph-based Tavily-powered search agent",
"api_token": API_TOKEN,
"mailbox": True
}
)
print(f"✅ Registered LangGraph agent: {agent_info}")
# Keep the agent alive
try:
while True:
time.sleep(1)
except KeyboardInterrupt:
print("🛑 Shutting down LangGraph agent...")
cleanup_uagent("langgraph_tavily_agent")
print("✅ Agent stopped.")
Key Components
-
LangGraph Setup:
- Creates a tool-calling executor using LangGraph's prebuilt components
- Configures Tavily search as the tool for retrieving information
- Uses OpenAI's ChatGPT for LLM capabilities
-
Function Wrapper:
- Wraps the LangGraph app in a function that accepts queries
- Handles input format conversion
- Processes streaming output from LangGraph
-
uAgent Registration:
- Uses UAgentRegisterTool to register the LangGraph function as a uAgent
- Configures a port, description, and mailbox for persistence
- Generates a unique address for agent communication
Sample requirements.txt
Here's a sample requirements.txt
file you can use for this example:
uagents==0.22.3
uagents-adapter==0.4.1
langchain-openai==0.3.14
langchain-community==0.3.21
langgraph==0.3.31
dotenv==0.9.9
Interacting with the Agent
Copy agent address and paste on Asi One then ask your query.
Why Use LangGraph with uAgents?
LangGraph offers several advantages when combined with uAgents:
- Advanced Orchestration: Create complex reasoning flows using directed graphs
- State Management: Handle complex multi-turn conversations with state persistence
- Tool Integration: Easily connect to external services and APIs
- Debugging Capabilities: Inspect and debug agent reasoning processes
By wrapping LangGraph with the uAgents adapter, you get the best of both worlds: sophisticated LLM orchestration with the decentralized communication capabilities of uAgents.
Getting Started
-
In a directory, place
agent.py
. -
Install required packages:
pip install uagents>=0.22.3 uagents-adapter>=0.4.1 langchain-openai>=0.3.14 langchain-community>=0.3.21 langgraph>=0.3.31 dotenv>=0.9.9
-
Set up your environment variables in a
.env
file:OPENAI_API_KEY=your_openai_key
TAVILY_API_KEY=your_tavily_key
AGENTVERSE_API_KEY=your_agentverse_key -
Run the LangGraph agent:
python agent.py
-
Copy agent address and paste on AsiOne and ask your query.
🗨️ Agentverse UI
If you prefer a graphical interface, follow these steps to talk to your LangGraph agent directly from the Agentverse dashboard:
-
Open the Agentverse Studio and navigate to Agents → Local (or search for your deployed agent by name).
-
Locate your agent (e.g.,
langgraph_tavily_agent
) in the list and click its profile card to open the details page. -
Copy agent address and paste on AsiOne and ask your query.
-
Type your query (for example, "Give me a list of the latest agentic AI trends") and hit Send.
Expected Outputs
When running the examples, you should expect to see outputs similar to these:
LangGraph Agent
When running the LangGraph agent:
(venv) Fetchs-MacBook-Pro test examples % python3 agent.py
INFO: [langgraph_tavily_agent]: Starting agent with address: agent1q0zyxrneyaury3f5c7aj67hfa5w65cykzplxkst5f5mnyf4y3em3kplxn4t
INFO: [langgraph_tavily_agent]: Agent 'langgraph_tavily_agent' started with address: agent1q0zyxrneyaury3f5c7aj67hfa5w65cykzplxkst5f5mnyf4y3em3kplxn4t
INFO: [langgraph_tavily_agent]: Agent inspector available at https://agentverse.ai/inspect/?uri=http%3A//127.0.0.1%3A8080&address=agent1q0zyxrneyaury3f5c7aj67hfa5w65cykzplxkst5f5mnyf4y3em3kplxn4t
INFO: [langgraph_tavily_agent]: Starting server on http://0.0.0.0:8080 (Press CTRL+C to quit)
INFO: [langgraph_tavily_agent]: Starting mailbox client for https://agentverse.ai
INFO: [langgraph_tavily_agent]: Mailbox access token acquired
INFO: [langgraph_tavily_agent]: Received structured output response from agent1qtlpfshtlcxekgrfcpmv7m9zpajuwu7d5jfyachvpa4u3dkt6k0uwwp2lct: Hello, Tavily Agent. Could you please provide a list of the latest trends in agentic AI? I am interested in understanding how agent-based artificial intelligence is evolving and what innovations or developments stand out in this field. Thank you!
INFO:httpx:HTTP Request: POST https://api.openai.com/v1/chat/completions "HTTP/1.1 200 OK"
✅ Registered LangGraph agent: Created uAgent 'langgraph_tavily_agent' with address agent1q0zyxrneyaury3f5c7aj67hfa5w65cykzplxkst5f5mnyf4y3em3kplxn4t on port 8080
INFO:httpx:HTTP Request: POST https://api.openai.com/v1/chat/completions "HTTP/1.1 200 OK"
INFO: [langgraph_tavily_agent]: Got a message from agent1qwwng5d939vyaa6d2trnllyltgrndtfd6z44h8ey8a56hf4dcatsytgzm49
INFO: [langgraph_tavily_agent]: Got a text message from agent1qwwng5d939vyaa6d2trnllyltgrndtfd6z44h8ey8a56hf4dcatsytgzm49: I want to send query to tavily agent that Give me a list of latest agentic AI trends
INFO: [langgraph_tavily_agent]: Sending structured output prompt to {'title': 'QueryMessage', 'type': 'object', 'properties': {'query': {'title': 'Query', 'type': 'string'}}, 'required': ['query']}
INFO: [langgraph_tavily_agent]: Sent structured output prompt to agent1qtlpfshtlcxekgrfcpmv7m9zpajuwu7d5jfyachvpa4u3dkt6k0uwwp2lct
INFO: [langgraph_tavily_agent]: Got an acknowledgement from agent1qwwng5d939vyaa6d2trnllyltgrndtfd6z44h8ey8a56hf4dcatsytgzm49 for 451f41aa-be41-471f-bddc-276caffb7d94
Connecting agent 'langgraph_tavily_agent' to Agentverse...
INFO: [mailbox]: Successfully registered as mailbox agent in Agentverse
Successfully connected agent 'langgraph_tavily_agent' to Agentverse
Updating agent 'langgraph_tavily_agent' README on Agentverse...
Successfully updated agent 'langgraph_tavily_agent' README on Agentverse
INFO: [langgraph_tavily_agent]: Received structured output response from agent1qtlpfshtlcxekgrfcpmv7m9zpajuwu7d5jfyachvpa4u3dkt6k0uwwp2lct: Subject: Request for Information on Latest Agentic AI Trends
Hi Tavily Agent,
I hope this message finds you well. I am reaching out to inquire about the latest trends in agentic AI technology. As this area is rapidly evolving, I am keen to stay updated on the most recent developments and innovations.
Could you please provide me with a comprehensive list of the latest trends in agentic AI? I'm particularly interested in understanding how these trends might impact various industries and potential future applications.
Thank you for your assistance. I look forward to your response.
---
INFO:httpx:HTTP Request: POST https://api.openai.com/v1/chat/completions "HTTP/1.1 200 OK"
Asi One Chat Output
When you copy the agent address and paste it on Asi One, you can interact with the LangGraph agent through the chat interface:
