Python Calculator Agent
Python Calculator Agent
Section titled “Python Calculator Agent”KĀDI calculator agent example for ProtogameJS3D demonstrating multi-language agent communication.
Features
Section titled “Features”- ✅ Ed25519 Authentication - Cryptographic identity verification
- ✅ Pydantic Schema Validation - Type-safe tool definitions
- ✅ Event-Driven Architecture - Pub/sub system for agent coordination
- ✅ WebSocket Communication - Real-time bidirectional messaging
- ✅ Cross-Language Compatible - Works seamlessly with TypeScript, Go, Rust agents
Installation
Section titled “Installation”Prerequisites
Section titled “Prerequisites”- Python 3.10 or higher
- KĀDI broker running (default:
ws://localhost:8765)
Install Dependencies
Section titled “Install Dependencies”pip install -e .Or with development dependencies:
pip install -e .[dev]Basic Usage
Section titled “Basic Usage”python agent.pyWith Custom Configuration
Section titled “With Custom Configuration”# Set broker URLexport KADI_BROKER_URL=ws://kadi.build:8080
# Set networksexport KADI_NETWORK=global,math,game
python agent.pyAvailable Tools
Section titled “Available Tools”The calculator agent provides four mathematical operations:
1. Addition
Section titled “1. Addition”{ "tool": "add", "input": { "a": 5, "b": 3 }}Output: { "result": 8 }
2. Multiplication
Section titled “2. Multiplication”{ "tool": "multiply", "input": { "a": 6, "b": 7 }}Output: { "result": 42 }
3. Subtraction
Section titled “3. Subtraction”{ "tool": "subtract", "input": { "a": 10, "b": 3 }}Output: { "result": 7 }
4. Division
Section titled “4. Division”{ "tool": "divide", "input": { "a": 15, "b": 3 }}Output: { "result": 5.0, "error": null }
Division by zero:
{ "tool": "divide", "input": { "a": 10, "b": 0 }}Output: { "result": 0.0, "error": "Division by zero is not allowed" }
Events
Section titled “Events”Published Events
Section titled “Published Events”-
math.calculation- Published after each successful calculation{"operation": "add","operands": [5, 3],"result": 8,"agent": "calculator-python"} -
math.error- Published when an error occurs{"operation": "divide","error": "Division by zero is not allowed","operands": [10, 0],"agent": "calculator-python"}
Subscribed Events
Section titled “Subscribed Events”The agent subscribes to all math.* events to monitor calculations across all agents in the network.
Cross-Language Communication
Section titled “Cross-Language Communication”Invoking Python Tools from TypeScript
Section titled “Invoking Python Tools from TypeScript”// TypeScript client calling Python agentimport { KadiClient } from '@kadi.build/core';
const client = new KadiClient({ name: 'typescript-client', broker: 'ws://localhost:8080', networks: ['global']});
await client.connect();
// Load Python calculator agentconst calculator = await client.load('calculator', 'broker');
// Call Python tool from TypeScriptconst result = await calculator.add({ a: 5, b: 3 });console.log(result); // { result: 8 }Invoking Python Tools from Another Python Agent
Section titled “Invoking Python Tools from Another Python Agent”from kadi import KadiClient
client = KadiClient({ 'name': 'python-client', 'broker': 'ws://localhost:8765', 'networks': ['global']})
await client.connect()
# Load calculator agentcalculator = await client.load('calculator', 'broker')
# Call toolresult = await calculator.multiply({'a': 6, 'b': 7})print(result) # { 'result': 42 }Development
Section titled “Development”Running Tests
Section titled “Running Tests”pytestType Checking
Section titled “Type Checking”mypy agent.pyCode Formatting
Section titled “Code Formatting”black agent.pyArchitecture
Section titled “Architecture”agent.py├── Schemas (Pydantic Models)│ ├── AddInput, AddOutput│ ├── MultiplyInput, MultiplyOutput│ ├── SubtractInput, SubtractOutput│ └── DivideInput, DivideOutput├── KĀDI Client Configuration│ ├── Broker connection│ ├── Network registration│ └── Ed25519 authentication├── Tool Registration│ ├── @client.tool() decorator│ ├── Schema validation│ └── Event publishing├── Event Subscriptions│ ├── math.calculation listener│ └── math.error listener└── Main Event Loop ├── Connect to broker ├── Register agent └── Serve indefinitelyIntegration with ProtogameJS3D
Section titled “Integration with ProtogameJS3D”This agent demonstrates the multi-language agent architecture for ProtogameJS3D’s AI-driven game development workflow:
- Planner Agent (Python) - Orchestrates complex tasks
- Calculator Agent (Python) - Mathematical operations
- UI-UX-Designer Agent (TypeScript) - Design generation
- Code Generator Agent (TypeScript) - Code synthesis
All agents communicate via the KĀDI protocol regardless of implementation language.
Troubleshooting
Section titled “Troubleshooting”Connection Refused
Section titled “Connection Refused”Problem: ConnectionRefusedError: [Errno 111] Connect call failed
Solution: Ensure KĀDI broker is running:
# Start broker (from kadi-broker repository)npm run devAuthentication Failed
Section titled “Authentication Failed”Problem: AuthenticationError: Invalid signature
Solution: Check that Ed25519 keypair is correctly generated. The agent automatically generates keys on startup.
Module Not Found
Section titled “Module Not Found”Problem: ModuleNotFoundError: No module named 'kadi'
Solution: Install dependencies:
pip install -e .Related Documentation
Section titled “Related Documentation”License
Section titled “License”This project is part of ProtogameJS3D research thesis.
Built with KĀDI protocol 🚀