Skip to main content

Async-native Python SDK for building AI agents, ported from Rust agentrs

Project description

agentrspy

agentrspy is the Python port of the Rust project agentrs: an async-native SDK for building AI agents with a unified provider interface, composable tools, MCP integration, pluggable memory, multi-agent orchestration, and YAML-driven runtime loading.

The goal of this port is behavioral fidelity first and Python ergonomics second: keep the same core capabilities, preserve the tested runtime semantics, and expose them through idiomatic Python APIs with type hints.

Highlights

  • OpenAI, Azure OpenAI, Anthropic, Gemini, and Ollama providers
  • Built-in tools for calculation, web fetch/search, filesystem access, shell execution, and inline Python execution
  • MCP support for local stdio servers and remote Streamable HTTP endpoints
  • In-memory, sliding-window, token-aware, vector, and optional Redis memory backends
  • Single-agent loops for ReAct, single-pass reasoning, plan-and-execute, and custom instruction modes
  • Multi-agent orchestration with sequential, parallel, supervisor, and graph routing
  • YAML loading for single agents, orchestrators, and generic runtimes
  • Examples, tests, and rewritten Python-first documentation

Installation

pip install -e .

Optional extras:

pip install -e ".[redis]"
pip install -e ".[dev]"
pip install -e ".[full]"

Quick Start

from __future__ import annotations

import asyncio

from dotenv import load_dotenv

from agentrspy.prelude import Agent, CalculatorTool, LoopStrategy, OpenAiProvider


async def main() -> None:
    load_dotenv()

    llm = OpenAiProvider.from_env().build()

    agent = (
        Agent.builder()
        .llm(llm)
        .system("You are a concise assistant.")
        .tool(CalculatorTool())
        .loop_strategy(LoopStrategy.react(max_steps=4))
        .build()
    )

    output = await agent.run("What is 7 * (8 + 1)?")
    print(output.text)


if __name__ == "__main__":
    asyncio.run(main())

Project Layout

agentrspy/
├── agentrspy/                 # import-friendly wrapper package
├── src/agentrspy/core         # shared contracts, types, errors, streaming helpers
├── src/agentrspy/llm          # provider implementations
├── src/agentrspy/tools        # registry, built-ins, decorator-based tools
├── src/agentrspy/mcp          # MCP transport, protocol, adapters
├── src/agentrspy/memory       # memory backends
├── src/agentrspy/agents       # builders and execution loops
├── src/agentrspy/multi        # orchestrators, graph routing, event bus
├── src/agentrspy/config.py    # YAML runtime loading
├── examples/                  # runnable Python examples
├── tests/                     # pytest coverage for parity-critical behavior
└── docs/                      # rewritten Python documentation

Core Semantics Preserved From Rust

  • ReAct stores the assistant message before executing tool calls
  • Tool failures are returned to the model as tool error outputs instead of raising immediately
  • Sequential multi-agent routing feeds each agent the previous agent's output.text
  • Parallel multi-agent routing merges outputs as [agent_name]\n<text> blocks separated by blank lines
  • Agent streaming intentionally replays final whitespace-split tokens after completion to match the current Rust behavior
  • YAML runtime loading accepts the same practical config shapes used by the Rust examples and integration tests

Examples

  • examples/simple_agent.py
  • examples/custom_tool.py
  • examples/streaming.py
  • examples/multi_agent.py
  • examples/mcp_integration.py
  • examples/yaml_single_agent.py
  • examples/yaml_multi_agent.py
  • examples/yaml_runtime.py

Run one with:

python examples/simple_agent.py
python examples/multi_agent.py
python examples/yaml_runtime.py

Or use the module entrypoint:

python -m agentrspy --config examples/configs/single-agent.yaml --input "What is 21 * 2?"

Environment Variables

See .env.example for a ready-to-copy template.

Common values:

  • OPENAI_API_KEY
  • OPENAI_BASE_URL
  • OPENAI_MODEL
  • AZURE_OPENAI_ENDPOINT
  • AZURE_OPENAI_KEY
  • AZURE_OPENAI_MODEL
  • ANTHROPIC_API_KEY
  • GEMINI_API_KEY
  • CONTEXT7_API_KEY

Documentation

  • docs/architecture.md
  • docs/providers.md
  • docs/tools.md
  • docs/memory.md
  • docs/multi-agent.md
  • docs/yaml.md
  • docs/migration-from-rust.md
  • docs/testing.md

Testing

python -m pytest
python -m compileall src agentrspy tests examples

Notes

  • Python integers do not overflow like Rust integer types; where Rust relied on integer bounds, Python callers should validate explicitly.
  • The Rust macro-based #[tool] workflow becomes a Python @tool(...) decorator.
  • Rust feature flags become Python extras and import-time availability rather than compile-time gating.
  • Anthropic and Gemini currently implement complete() but keep streaming intentionally unsupported, matching the Rust scope.

License

MIT.

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

agentrspy-0.1.0.tar.gz (37.0 kB view details)

Uploaded Source

Built Distribution

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

agentrspy-0.1.0-py3-none-any.whl (50.2 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: agentrspy-0.1.0.tar.gz
  • Upload date:
  • Size: 37.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.4

File hashes

Hashes for agentrspy-0.1.0.tar.gz
Algorithm Hash digest
SHA256 03997f02723c76e2819d5820a1a5711e9a1a843975f4b20eafe4833f75a86b68
MD5 3a3b39751bd99bd33dbd06fb4e4ddce3
BLAKE2b-256 a8cafb6db732a09e6cc3e5304ba31edc20b1ee87981b8ccef8d22db834e47ff1

See more details on using hashes here.

File details

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

File metadata

  • Download URL: agentrspy-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 50.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.4

File hashes

Hashes for agentrspy-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 7e7e5574b0cc80bfca9c8d5d9c0083d3c707d704e37e125df0ff072e6d10706d
MD5 2448592543c626d69fcbf8bb15417655
BLAKE2b-256 3a9a1bb988484543653b052dabae00cf6bdd400a7e745320b23c87917ee20f7a

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