Skip to content

Python Calculator Agent

KĀDI calculator agent example for ProtogameJS3D demonstrating multi-language agent communication.

  • 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
  • Python 3.10 or higher
  • KĀDI broker running (default: ws://localhost:8765)
Terminal window
pip install -e .

Or with development dependencies:

Terminal window
pip install -e .[dev]
Terminal window
python agent.py
Terminal window
# Set broker URL
export KADI_BROKER_URL=ws://kadi.build:8080
# Set networks
export KADI_NETWORK=global,math,game
python agent.py

The calculator agent provides four mathematical operations:

{
"tool": "add",
"input": {
"a": 5,
"b": 3
}
}

Output: { "result": 8 }

{
"tool": "multiply",
"input": {
"a": 6,
"b": 7
}
}

Output: { "result": 42 }

{
"tool": "subtract",
"input": {
"a": 10,
"b": 3
}
}

Output: { "result": 7 }

{
"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" }

  • 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"
    }

The agent subscribes to all math.* events to monitor calculations across all agents in the network.

// TypeScript client calling Python agent
import { KadiClient } from '@kadi.build/core';
const client = new KadiClient({
name: 'typescript-client',
broker: 'ws://localhost:8080',
networks: ['global']
});
await client.connect();
// Load Python calculator agent
const calculator = await client.load('calculator', 'broker');
// Call Python tool from TypeScript
const 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 agent
calculator = await client.load('calculator', 'broker')
# Call tool
result = await calculator.multiply({'a': 6, 'b': 7})
print(result) # { 'result': 42 }
Terminal window
pytest
Terminal window
mypy agent.py
Terminal window
black agent.py
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 indefinitely

This agent demonstrates the multi-language agent architecture for ProtogameJS3D’s AI-driven game development workflow:

  1. Planner Agent (Python) - Orchestrates complex tasks
  2. Calculator Agent (Python) - Mathematical operations
  3. UI-UX-Designer Agent (TypeScript) - Design generation
  4. Code Generator Agent (TypeScript) - Code synthesis

All agents communicate via the KĀDI protocol regardless of implementation language.

Problem: ConnectionRefusedError: [Errno 111] Connect call failed

Solution: Ensure KĀDI broker is running:

Terminal window
# Start broker (from kadi-broker repository)
npm run dev

Problem: AuthenticationError: Invalid signature

Solution: Check that Ed25519 keypair is correctly generated. The agent automatically generates keys on startup.

Problem: ModuleNotFoundError: No module named 'kadi'

Solution: Install dependencies:

Terminal window
pip install -e .

This project is part of ProtogameJS3D research thesis.


Built with KĀDI protocol 🚀