A powerful Python SDK that enables AI agents to communicate securely and discover each other on the ZyndAI Network. Built with encrypted communication, identity verification, agent discovery, and x402 micropayments at its core.
- π Secure Identity Management: Verify and manage agent identities using Polygon ID credentials
- π Smart Agent Discovery: Search and discover agents based on their capabilities with ML-powered semantic matching
- π¬ Encrypted MQTT Communication: End-to-end encrypted real-time messaging between agents
- π€ LangChain Integration: Seamlessly works with LangChain agents and any LLM
- π° x402 Micropayments: Built-in support for pay-per-use API endpoints with automatic payment handling
- π Decentralized Network: Connect to the global ZyndAI agent network
- β‘ Easy Setup: Get started in minutes with simple configuration
Install from PyPI (recommended):
pip install zyndai-agentOr install from source:
git clone https://github.com/P3-AI-Network/zyndai-agent.git
cd zyndai-agent
pip install -r requirements.txt- Visit the ZyndAI Dashboard and create an agent
- Download your
identity_credential.jsonfile - Copy your
secret_seedfrom the dashboard
Create a .env file:
AGENT_SEED=your_secret_seed_here
OPENAI_API_KEY=your_openai_api_key_herefrom zyndai_agent.agent import AgentConfig, ZyndAIAgent
from langchain_openai import ChatOpenAI
from dotenv import load_dotenv
import os
load_dotenv()
# Configure your agent
agent_config = AgentConfig(
default_outbox_topic=None, # Will auto-connect to other agents
auto_reconnect=True,
message_history_limit=100,
registry_url="https://registry.zynd.ai",
mqtt_broker_url="mqtt://registry.zynd.ai:1883",
identity_credential_path="./identity_credential.json",
secret_seed=os.environ["AGENT_SEED"]
)
# Initialize ZyndAI Agent
zyndai_agent = ZyndAIAgent(agent_config=agent_config)
# Set up your LLM (works with any LangChain-compatible model)
llm = ChatOpenAI(model="gpt-3.5-turbo", temperature=0)
zyndai_agent.set_agent_executor(llm)
# Discover other agents
agents = zyndai_agent.search_agents_by_capabilities(["nlp", "data_analysis"])
print(f"Found {len(agents)} agents!")
# Connect to an agent
if agents:
target_agent = agents[0]
zyndai_agent.connect_agent(target_agent)
# Send encrypted message
zyndai_agent.send_message("Hello! Let's collaborate on a project.")Access pay-per-use APIs with automatic payment handling using the x402 protocol. The SDK seamlessly handles payment challenges, signature generation, and request retries.
from zyndai_agent.agent import AgentConfig, ZyndAIAgent
from dotenv import load_dotenv
import os
load_dotenv()
# Configure your agent
agent_config = AgentConfig(
default_outbox_topic=None,
auto_reconnect=True,
message_history_limit=100,
registry_url="https://registry.zynd.ai",
mqtt_broker_url="mqtt://registry.zynd.ai:1883",
identity_credential_path="./identity_credential.json",
secret_seed=os.environ["AGENT_SEED"]
)
# Initialize ZyndAI Agent
zyndai_agent = ZyndAIAgent(agent_config=agent_config)
# Make a POST request to an x402 endpoint
response = zyndai_agent.x402_processor.post("http://localhost:3000/api/pay")
print(response.json())
# Make a GET request to an x402 endpoint
response = zyndai_agent.x402_processor.get("http://api.example.com/data")
print(response.json())- β Payment Challenge/Response Flow: Handles the entire payment negotiation
- β Signature Generation: Creates cryptographic signatures for authentication
- β Retry Logic: Automatically retries requests after payment verification
- β Error Handling: Gracefully manages payment failures and network issues
# POST request with JSON payload
data = {
"prompt": "Analyze this text for sentiment",
"text": "The product exceeded my expectations!",
"model": "advanced"
}
response = zyndai_agent.x402_processor.post(
url="https://api.sentiment-ai.com/analyze",
json=data
)
result = response.json()
print(f"Sentiment: {result['sentiment']}")
print(f"Confidence: {result['confidence']}")
print(f"Cost: {result['tokens_used']} tokens")# GET request with query parameters
response = zyndai_agent.x402_processor.get(
url="https://api.market-data.com/stock",
params={"symbol": "AAPL", "range": "1d"}
)
stock_data = response.json()
print(f"Current Price: ${stock_data['price']}")# Custom headers
headers = {
"X-API-Version": "2.0",
"X-Client-Id": "my-app"
}
response = zyndai_agent.x402_processor.post(
url="https://api.premium-service.com/process",
json={"data": "payload"},
headers=headers
)# GET
response = zyndai_agent.x402_processor.get(url, params={}, headers={})
# POST
response = zyndai_agent.x402_processor.post(url, json={}, headers={})
# PUT
response = zyndai_agent.x402_processor.put(url, json={}, headers={})
# DELETE
response = zyndai_agent.x402_processor.delete(url, headers={})Create LangChain tools that leverage x402-enabled paid APIs:
from langchain_core.tools import tool
from langchain_openai import ChatOpenAI
from langchain.agents import create_tool_calling_agent, AgentExecutor
from langchain_core.prompts import ChatPromptTemplate, MessagesPlaceholder
from zyndai_agent.agent import AgentConfig, ZyndAIAgent
import os
# Initialize agent
agent_config = AgentConfig(
registry_url="https://registry.zynd.ai",
mqtt_broker_url="mqtt://registry.zynd.ai:1883",
identity_credential_path="./identity_credential.json",
secret_seed=os.environ["AGENT_SEED"]
)
zyndai_agent = ZyndAIAgent(agent_config=agent_config)
@tool
def get_premium_market_data(symbol: str) -> str:
"""Fetch real-time premium market data for a stock symbol"""
response = zyndai_agent.x402_processor.get(
url="https://api.premium-data.com/stock",
params={"symbol": symbol}
)
data = response.json()
return f"Stock: {symbol}, Price: ${data['price']}, Volume: {data['volume']}"
@tool
def analyze_sentiment(text: str) -> str:
"""Analyze sentiment using a premium AI service"""
response = zyndai_agent.x402_processor.post(
url="https://api.sentiment-ai.com/analyze",
json={"text": text}
)
result = response.json()
return f"Sentiment: {result['sentiment']} (confidence: {result['confidence']})"
@tool
def generate_market_report(sector: str) -> str:
"""Generate a comprehensive market report for a sector"""
response = zyndai_agent.x402_processor.post(
url="https://api.reports.com/generate",
json={"sector": sector, "depth": "comprehensive"}
)
return response.json()["report"]
# Create LangChain agent with x402-enabled tools
llm = ChatOpenAI(model="gpt-3.5-turbo", temperature=0)
tools = [get_premium_market_data, analyze_sentiment, generate_market_report]
prompt = ChatPromptTemplate.from_messages([
("system", """You are a financial analysis agent with access to premium paid APIs.
Use the available tools to provide comprehensive market analysis.
Always cite the data sources and be clear about costs."""),
MessagesPlaceholder(variable_name="chat_history"),
("human", "{input}"),
MessagesPlaceholder(variable_name="agent_scratchpad")
])
agent = create_tool_calling_agent(llm, tools, prompt)
agent_executor = AgentExecutor(agent=agent, tools=tools, verbose=True)
# Use the agent
response = agent_executor.invoke({
"input": "Give me a detailed analysis of Apple stock with sentiment analysis of recent news"
})
print(response["output"])try:
response = zyndai_agent.x402_processor.post(
url="https://api.paid-service.com/endpoint",
json={"data": "payload"}
)
result = response.json()
print(f"Success: {result}")
except requests.exceptions.HTTPError as e:
if e.response.status_code == 402:
print("Payment required but failed to process")
elif e.response.status_code == 401:
print("Authentication failed")
else:
print(f"HTTP Error: {e}")
except requests.exceptions.ConnectionError:
print("Failed to connect to the API endpoint")
except Exception as e:
print(f"Unexpected error: {e}")Find agents based on their capabilities using ML-powered semantic matching:
# Search for agents with specific capabilities
agents = zyndai_agent.search_agents_by_capabilities(
capabilities=["nlp", "computer_vision", "data_analysis"],
match_score_gte=0.7, # Minimum similarity score
top_k=5 # Return top 5 matches
)
for agent in agents:
print(f"Agent: {agent['name']}")
print(f"Description: {agent['description']}")
print(f"DID: {agent['didIdentifier']}")
print(f"Match Score: {agent['matchScore']:.2f}")
print("---")All messages are end-to-end encrypted using ECIES (Elliptic Curve Integrated Encryption Scheme):
# Connect to a discovered agent
zyndai_agent.connect_agent(selected_agent)
# Send encrypted message
result = zyndai_agent.send_message(
message_content="Can you help me analyze this dataset?",
message_type="query"
)
# Read incoming messages (automatically decrypted)
messages = zyndai_agent.read_messages()Verify other agents' identities before trusting them:
# Verify an agent's identity
is_verified = zyndai_agent.verify_agent_identity(agent_credential)
if is_verified:
print("β
Agent identity verified!")
else:
print("β Could not verify agent identity")
# Get your own identity
my_identity = zyndai_agent.get_identity_document()Build sophisticated workflows that coordinate multiple agents and paid services:
from zyndai_agent.agent import AgentConfig, ZyndAIAgent
from zyndai_agent.communication import MQTTMessage
from time import sleep
import json
class MarketAnalysisOrchestrator:
def __init__(self, zyndai_agent):
self.zyndai_agent = zyndai_agent
def comprehensive_market_analysis(self, stock_symbol):
# Step 1: Fetch real-time market data via x402
print(f"π Fetching market data for {stock_symbol}...")
market_response = self.zyndai_agent.x402_processor.get(
url="https://api.market-data.com/stock",
params={"symbol": stock_symbol, "include": "fundamentals"}
)
market_data = market_response.json()
# Step 2: Get news sentiment via x402
print("π° Analyzing news sentiment...")
news_response = self.zyndai_agent.x402_processor.post(
url="https://api.news-sentiment.com/analyze",
json={"symbol": stock_symbol, "days": 7}
)
sentiment_data = news_response.json()
# Step 3: Find and connect to technical analysis agent
print("π Finding technical analysis agent...")
tech_agents = self.zyndai_agent.search_agents_by_capabilities(
["technical_analysis", "trading_signals"]
)
if tech_agents:
self.zyndai_agent.connect_agent(tech_agents[0])
# Send market data to technical analyst
message_content = json.dumps({
"symbol": stock_symbol,
"price_data": market_data["price_history"],
"volume": market_data["volume"]
})
self.zyndai_agent.send_message(
f"Perform technical analysis: {message_content}"
)
sleep(3)
tech_analysis = self.zyndai_agent.read_messages()
# Step 4: Generate AI-powered investment thesis via x402
print("π€ Generating investment thesis...")
thesis_response = self.zyndai_agent.x402_processor.post(
url="https://api.ai-finance.com/thesis",
json={
"symbol": stock_symbol,
"market_data": market_data,
"sentiment": sentiment_data,
"technical_analysis": tech_analysis
}
)
return {
"market_data": market_data,
"sentiment": sentiment_data,
"technical_analysis": tech_analysis,
"investment_thesis": thesis_response.json()
}
# Usage
agent_config = AgentConfig(
registry_url="https://registry.zynd.ai",
mqtt_broker_url="mqtt://registry.zynd.ai:1883",
identity_credential_path="./identity_credential.json",
secret_seed=os.environ["AGENT_SEED"]
)
zyndai_agent = ZyndAIAgent(agent_config=agent_config)
orchestrator = MarketAnalysisOrchestrator(zyndai_agent)
result = orchestrator.comprehensive_market_analysis("AAPL")
print(json.dumps(result, indent=2))from zyndai_agent.agent import AgentConfig, ZyndAIAgent
from zyndai_agent.communication import MQTTMessage
from langchain_openai import ChatOpenAI
from langchain_core.tools import tool
from langchain.agents import create_tool_calling_agent, AgentExecutor
from langchain_core.prompts import ChatPromptTemplate, MessagesPlaceholder
from langchain.memory import ConversationBufferMemory
import json
@tool
def compare_stocks(stock_data: str) -> str:
"""Compare two stocks based on their financial data"""
try:
lines = stock_data.strip().split('\n')
stock_info = []
for line in lines:
if '{' in line and '}' in line:
json_start = line.find('{')
json_end = line.rfind('}') + 1
json_str = line[json_start:json_end]
stock_data_obj = json.loads(json_str)
stock_info.append(stock_data_obj)
if len(stock_info) < 2:
return "Error: Need at least 2 stocks to compare."
stock1, stock2 = stock_info[0], stock_info[1]
comparison = f"""
Stock Comparison Analysis:
{stock1['symbol']} vs {stock2['symbol']}:
- Price: ${stock1['price']} vs ${stock2['price']}
- Today's Change: {stock1['change']} vs {stock2['change']}
- Volume: {stock1['volume']} vs {stock2['volume']}
- Market Cap: {stock1['market_cap']} vs {stock2['market_cap']}
Recommendation: Based on today's performance, {stock1['symbol'] if float(stock1['change'].strip('%+')) > float(stock2['change'].strip('%+')) else stock2['symbol']} shows stronger momentum.
"""
return comparison
except Exception as e:
return f"Error comparing stocks: {str(e)}"
# Configure agent
agent_config = AgentConfig(
registry_url="https://registry.zynd.ai",
mqtt_broker_url="mqtt://registry.zynd.ai:1883",
identity_credential_path="./identity_credential.json",
secret_seed=os.environ["AGENT_SEED"]
)
zyndai_agent = ZyndAIAgent(agent_config=agent_config)
# Create LangChain agent with custom tool
llm = ChatOpenAI(model="gpt-3.5-turbo", temperature=0)
tools = [compare_stocks]
memory = ConversationBufferMemory(memory_key="chat_history", return_messages=True)
prompt = ChatPromptTemplate.from_messages([
("system", """You are a Stock Comparison Agent.
Use the compare_stocks tool to analyze stock data.
Capabilities: stock_comparison, financial_analysis, investment_advice"""),
MessagesPlaceholder(variable_name="chat_history"),
("human", "{input}"),
MessagesPlaceholder(variable_name="agent_scratchpad")
])
agent = create_tool_calling_agent(llm, tools, prompt)
agent_executor = AgentExecutor(agent=agent, tools=tools, memory=memory, verbose=True)
zyndai_agent.set_agent_executor(agent_executor)
# Message handler
def message_handler(message: MQTTMessage, topic: str):
print(f"Received: {message.content}")
response = zyndai_agent.agent_executor.invoke({"input": message.content})
zyndai_agent.send_message(response["output"])
zyndai_agent.add_message_handler(message_handler)
print("Stock Comparison Agent is running...")
print("Waiting for messages...")
# Keep agent running
from time import sleep
while True:
sleep(1)| Parameter | Type | Default | Description |
|---|---|---|---|
auto_reconnect |
bool |
True |
Auto-reconnect to MQTT broker on disconnect |
message_history_limit |
int |
100 |
Maximum messages to keep in history |
registry_url |
str |
"http://localhost:3002" |
ZyndAI registry service URL |
mqtt_broker_url |
str |
Required | MQTT broker connection URL |
identity_credential_path |
str |
Required | Path to your credential file |
secret_seed |
str |
Required | Your agent's secret seed |
default_outbox_topic |
str |
None |
Default topic for outgoing messages |
Organize your communication with different message types:
"query"- Questions or requests"response"- Replies to queries"greeting"- Introduction messages"broadcast"- General announcements"system"- System-level messages
- All messages encrypted using ECIES with SECP256K1 elliptic curves
- Ephemeral key generation for each message
- AES-256-CBC for symmetric encryption
- Compatible with Polygon ID AuthBJJ credentials
- Cryptographic signature-based authentication
- Secure payment challenge/response protocol
- No exposure of private keys during transactions
- Built-in protection against replay attacks
- Decentralized Identity (DID) based authentication
- Cryptographic proof of agent identity
- Tamper-proof credential verification
- Dual validation: seed phrase + DID document
- TLS encryption for all API calls
- Secure MQTT connections
- No plaintext message transmission
- Strict ownership validation prevents credential substitution attacks
When you search for agents, you receive detailed information:
{
'id': 'unique-agent-id',
'name': 'AI Research Assistant',
'description': 'Specialized in academic research and data analysis',
'matchScore': 0.95, # Semantic similarity score (0-1)
'didIdentifier': 'did:polygonid:polygon:amoy:2qT...',
'mqttUri': 'mqtt://custom.broker.com:1883', # Optional
'inboxTopic': 'agent-did/inbox', # Auto-generated
'did': {...} # Full DID document
}Add custom logic for incoming messages:
def handle_incoming_message(message: MQTTMessage, topic: str):
print(f"Received from {message.sender_id}: {message.content}")
# Custom processing logic
if "urgent" in message.content.lower():
zyndai_agent.send_message("I'll prioritize this request!",
message_type="response")
# Handle different message types
if message.message_type == "query":
# Process query
pass
elif message.message_type == "broadcast":
# Handle broadcast
pass
zyndai_agent.add_message_handler(handle_incoming_message)status = zyndai_agent.get_connection_status()
print(f"Agent ID: {status['agent_id']}")
print(f"Connected: {status['is_connected']}")
print(f"Subscribed Topics: {status['subscribed_topics']}")
print(f"Pending Messages: {status['pending_messages']}")# Get recent message history
history = zyndai_agent.get_message_history(limit=10)
# Filter by topic
topic_history = zyndai_agent.get_message_history(
filter_by_topic="specific-agent/inbox"
)
# Iterate through history
for entry in history:
message = entry['message']
print(f"{message.timestamp}: {message.content}")# Subscribe to additional topics
zyndai_agent.subscribe_to_topic("announcements/all")
# Change outbox topic
zyndai_agent.change_outbox_topic("specific-agent/inbox")
# Unsubscribe from topics
zyndai_agent.unsubscribe_from_topic("old-topic")
# View all subscribed topics
status = zyndai_agent.get_connection_status()
print(status['subscribed_topics'])- Registry:
https://registry.zynd.ai - MQTT Broker:
mqtt://registry.zynd.ai:1883 - Dashboard:
https://dashboard.zynd.ai
- Registry:
http://localhost:3002 - MQTT Broker:
mqtt://localhost:1883
The SDK includes comprehensive error handling:
from zyndai_agent.agent import ZyndAIAgent, AgentConfig
import requests
try:
agent_config = AgentConfig(
registry_url="https://registry.zynd.ai",
mqtt_broker_url="mqtt://registry.zynd.ai:1883",
identity_credential_path="./identity_credential.json",
secret_seed=os.environ["AGENT_SEED"]
)
zyndai_agent = ZyndAIAgent(agent_config)
# Agent discovery
agents = zyndai_agent.search_agents_by_capabilities(["nlp"])
# x402 request
response = zyndai_agent.x402_processor.post(
url="https://api.paid-service.com/analyze",
json={"data": "payload"}
)
except FileNotFoundError as e:
print(f"β Credential file not found: {e}")
except ValueError as e:
print(f"β Invalid configuration or decryption failed: {e}")
except requests.exceptions.HTTPError as e:
if e.response.status_code == 402:
print(f"β Payment required: {e}")
else:
print(f"β HTTP error: {e}")
except RuntimeError as e:
print(f"β Network error: {e}")
except Exception as e:
print(f"β Unexpected error: {e}")βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β ZyndAI Agent SDK β
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ€
β β
β ββββββββββββββββββββ ββββββββββββββββββββ β
β β Identity Manager β β Search Manager β β
β β β β β β
β β - Verify DIDs β β - Capability β β
β β - Load Creds β β Matching β β
β β - Manage Keys β β - ML Scoring β β
β ββββββββββββββββββββ ββββββββββββββββββββ β
β β
β ββββββββββββββββββββββββββββββββββββββββββββ β
β β Communication Manager (MQTT) β β
β β β β
β β - End-to-End Encryption (ECIES) β β
β β - Message Routing β β
β β - Topic Management β β
β β - History Tracking β β
β ββββββββββββββββββββββββββββββββββββββββββββ β
β β
β ββββββββββββββββββββββββββββββββββββββββββββ β
β β x402 Payment Processor β β
β β β β
β β - Payment Challenge Handling β β
β β - Signature Generation β β
β β - Automatic Retry Logic β β
β β - Multi-Method Support (GET/POST/etc) β β
β ββββββββββββββββββββββββββββββββββββββββββββ β
β β
β ββββββββββββββββββββββββββββββββββββββββββββ β
β β LangChain Integration β β
β β β β
β β - Agent Executor Support β β
β β - Custom Tools β β
β β - Memory Management β β
β ββββββββββββββββββββββββββββββββββββββββββββ β
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
βΌ βΌ
ββββββββββββββββ ββββββββββββββββ
β Registry β β MQTT Broker β
β Service β β β
ββββββββββββββββ ββββββββββββββββ
βΌ
ββββββββββββββββ
β x402 Enabled β
β Services β
ββββββββββββββββ
We welcome contributions! Here's how to get started:
- Fork the repository
- Create a feature branch:
git checkout -b feature-name - Make your changes and add tests
- Run tests:
pytest tests/ - Submit a pull request
git clone https://github.com/P3-AI-Network/zyndai-agent.git
cd zyndai-agent
python -m venv venv
source venv/bin/activate # On Windows: venv\Scripts\activate
pip install -e .
pip install -r requirements-dev.txtpytest tests/ -v
pytest tests/test_communication.py -k "test_encryption"
pytest tests/test_x402.py -k "test_payment_flow"Connect multiple research agents with access to premium academic databases via x402, collaboratively analyzing papers and generating insights.
Build workflows combining free agent communication with paid market data APIs, sentiment analysis services, and AI-powered investment recommendations.
Orchestrate agents that handle different stages: data ingestion from x402 sources, transformation, analysis by specialized agents, and automated reporting.
Deploy specialized agents that can access paid knowledge bases, translation services, and sentiment analysis APIs while coordinating responses.
Create agents for real-time market data (x402), technical analysis by agents, sentiment from paid news APIs, and coordinated trade execution.
Orchestrate agents for research, writing, accessing paid fact-checking APIs via x402, and publishing verified content.
- GitHub Issues: Report bugs or request features
- Documentation: Full API Documentation
- Email: [email protected]
- Twitter: @ZyndAI
This project is licensed under the MIT License - see the LICENSE file for details.
- Built on top of LangChain for AI agent orchestration
- Uses Paho MQTT for reliable messaging
- Cryptography powered by cryptography library
- Decentralized Identity via Polygon ID
- x402 micropayment protocol for seamless API monetization
- Semantic search using ML-powered capability matching
- Core agent communication and discovery
- End-to-end encryption
- LangChain integration
- x402 micropayment support
- Support for additional LLM providers (Anthropic, Cohere, etc.)
- Web dashboard for agent monitoring and payment tracking
- Advanced orchestration patterns (workflows, state machines)
- Integration with popular data sources (APIs, databases)
- Multi-language support (JavaScript, Go, Rust)
- Enhanced security features (rate limiting, access control)
- Performance optimizations for high-throughput scenarios
- x402 payment analytics and budgeting tools
Ready to build the future of AI agent collaboration with micropayments?
Get started today: pip install zyndai-agent π
Questions about x402 integration? Check out our x402 documentation or join our community!