Skip to main content

Python SDK for Moltbunker - Permissionless P2P Container Runtime for AI Agents

Project description

Moltbunker Python SDK

PyPI version Tests Python Versions License: MIT

Note: This SDK is under active development. APIs may change before v1.0.

Permissionless P2P Container Runtime for AI Agents

Deploy containers to a decentralized network, pay with BUNKER tokens, and automatically clone yourself when threats are detected.

Installation

pip install moltbunker

Quick Start

Wallet Authentication (Permissionless)

AI agents can authenticate with their own Ethereum wallet - no API key needed:

from moltbunker import Client

# Authenticate with wallet (permissionless)
client = Client(private_key="0x...")

# Or from environment variable
# export MOLTBUNKER_PRIVATE_KEY=0x...
client = Client()

# Register and deploy a bot
bot = client.register_bot(name="my-agent", image="python:3.11")
bot.enable_cloning(auto_clone_on_threat=True)
deployment = bot.deploy()

# Check threat level
threat = bot.detect_threat()
print(f"Threat level: {threat}")

# Check wallet balance
balance = client.get_balance()
print(f"BUNKER balance: {balance.bunker_balance}")

API Key Authentication

For managed services with pre-registered API keys:

from moltbunker import Client

client = Client(api_key="mb_live_xxx")

# Register a bot
bot = client.register_bot(
    name="my-bot",
    image="python:3.11",
)

# Reserve a runtime and deploy
runtime = bot.reserve_runtime(min_memory_mb=512)
deployment = runtime.deploy(env={"API_KEY": "secret"})

Async Usage

import asyncio
from moltbunker import AsyncClient

async def main():
    async with AsyncClient(private_key="0x...") as client:
        bot = await client.register_bot(name="my-agent", image="python:3.11")
        await bot.aenable_cloning()
        deployment = await bot.adeploy()

        threat = await bot.adetect_threat()
        print(f"Threat: {threat}")

asyncio.run(main())

Features

Automatic Cloning

Enable automatic self-cloning when threats are detected:

bot.enable_cloning(
    auto_clone_on_threat=True,  # Clone when threat detected
    max_clones=10,              # Maximum clones to maintain
    clone_delay_seconds=60,     # Delay between clones
    sync_state=True,            # Sync state between clones
    sync_interval_seconds=300,  # State sync interval
)

# Check clones
clones = bot.list_clones()

# Manual sync
bot.sync_clones()

Threat Detection

Monitor and respond to threats:

# Get detailed threat assessment
threat = client.get_threat_level()
print(f"Score: {threat.score}")        # 0.0 to 1.0
print(f"Level: {threat.level}")        # low, medium, high, critical
print(f"Recommendation: {threat.recommendation}")

for signal in threat.active_signals:
    print(f"  - {signal.type}: {signal.score}")

# Quick threat check
score = client.detect_threat()  # Returns float

Snapshots & Checkpoints

from moltbunker import SnapshotType

# Create snapshot
snapshot = client.create_snapshot(
    container_id=deployment.container_id,
    snapshot_type=SnapshotType.FULL,
)

# Enable automatic checkpoints
client.enable_checkpoints(
    container_id=deployment.container_id,
    interval_seconds=300,
    max_checkpoints=10,
)

# Restore from snapshot
new_deployment = client.restore_snapshot(
    snapshot_id=snapshot.id,
    target_region=Region.EUROPE,
)

Manual Cloning

from moltbunker import Region

# Clone to another region
clone = client.clone(
    container_id=deployment.container_id,
    target_region=Region.EUROPE,
    priority=3,
    reason="manual_backup",
)

# Check clone status
status = client.get_clone_status(clone.clone_id)

Bot Object Methods

The Bot object provides convenient methods for common operations:

bot = client.register_bot(name="my-bot", image="python:3.11")

# Direct deployment (auto-reserves runtime)
deployment = bot.deploy(env={"KEY": "value"})

# Cloning management
bot.enable_cloning(auto_clone_on_threat=True)
bot.disable_cloning()
bot.list_clones()
bot.sync_clones()

# Status
status = bot.get_status()
print(f"Uptime: {status.uptime}")
print(f"Clones: {status.clones}")

# Update
bot.update(description="Updated description")

# Delete
bot.delete()

Runtime Management

runtime = client.reserve_runtime(
    bot_id=bot.id,
    min_memory_mb=1024,
    min_cpu_shares=2048,
    duration_hours=24,
    region=Region.AMERICAS,
)

# Extend runtime
runtime.extend(duration_hours=12)

# Check status
status = runtime.get_status()
print(f"Remaining: {status.remaining_hours}h")

# Release
runtime.release()

Error Handling

from moltbunker import (
    MoltbunkerError,
    AuthenticationError,
    NotFoundError,
    RateLimitError,
    InsufficientFundsError,
)

try:
    deployment = bot.deploy()
except InsufficientFundsError as e:
    print(f"Need {e.required} BUNKER, have {e.available}")
except AuthenticationError as e:
    print(f"Auth failed: {e}")
except RateLimitError as e:
    print(f"Rate limited. Retry after: {e.retry_after}s")
except NotFoundError as e:
    print(f"Resource not found: {e}")
except MoltbunkerError as e:
    print(f"API error: {e}")

Environment Variables

Variable Description
MOLTBUNKER_API_KEY API key for authentication
MOLTBUNKER_PRIVATE_KEY Wallet private key (permissionless)
MOLTBUNKER_WALLET_ADDRESS Optional wallet address override

Development

# Install dev dependencies
pip install -e ".[dev]"

# Run tests
pytest

# Run tests with coverage
pytest --cov=moltbunker

# Format code
black moltbunker/

# Type check
mypy moltbunker/

# Lint
ruff check moltbunker/

License

MIT License - see LICENSE file for details.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

moltbunker-0.1.0.tar.gz (24.2 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

moltbunker-0.1.0-py3-none-any.whl (18.8 kB view details)

Uploaded Python 3

File details

Details for the file moltbunker-0.1.0.tar.gz.

File metadata

  • Download URL: moltbunker-0.1.0.tar.gz
  • Upload date:
  • Size: 24.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for moltbunker-0.1.0.tar.gz
Algorithm Hash digest
SHA256 8abba30f3293ce524d4c8b2cbb5e39a633abe6c36046f8c698d35b1c414eb5b8
MD5 b0abddf3d185dbd76dd785415050e92a
BLAKE2b-256 20508d3ea0c6abacbe34228887ddfeb80ab6075f253ee5ff0079309051ed98ab

See more details on using hashes here.

File details

Details for the file moltbunker-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: moltbunker-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 18.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for moltbunker-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 a2ba6855e65349e64744ec07d549d243cc5184bd7675ff8400f7b7ba3e581186
MD5 79217be116f13de4a975e320e62917c9
BLAKE2b-256 481c86e796a56829c1f1c7efc73821567f0666abf181f585011b6bbc037ac145

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page