A Rust implementation of a simple agent providing an AI assistant with dynamic Python tool integration capabilities.
- Dynamic Python Tool Execution: Create and execute Python tools on-the-fly using
uvx - Automatic Dependency Management: Auto-detects imports and isolates dependencies via
uvx --with - Tool Management: Register, update, and call custom Python functions dynamically
- Rich Terminal Output: Markdown rendering with syntax-highlighted code blocks
- Environment Configuration: Support for
.envfiles and automatic API key detection - Multi-Provider LLM Integration: Supports OpenRouter, OpenAI, Anthropic, Gemini AI Studio, and custom OpenAI-compatible endpoints
-
Dynamic Python Tool Execution:
- Creates Python tools dynamically at runtime
- Stores tool definitions in a registry
- Executes Python code via
uvxwith automatic dependency isolation - Auto-detects import statements for dependency management
- Supports full Python ecosystem without manual package installation
-
LLM Integration: Supports real API calls to:
- OpenRouter: Use model names with
openrouter/prefix (e.g.,openrouter/anthropic/claude-3.5-sonnet) - OpenAI: Use standard model names (e.g.,
gpt-4o,gpt-4-turbo,o1-preview) - Anthropic: Use model names with
claude-prefix (e.g.,claude-3-5-sonnet-20241022) - Gemini AI Studio: Use model names with
gemini-prefix (e.g.,gemini-2.0-flash,gemini-1.5-pro) - Custom Endpoints: Support for local LLMs and OpenAI-compatible APIs via
BASE_CUSTOM_URL - Automatically detects provider based on model name prefix or custom configuration
- OpenRouter: Use model names with
-
Tool Lifecycle:
- Create: Define Python functions with parameters
- Register: Store in Rust-managed registry
- Execute: Run via Python interpreter with JSON parameter passing
- Update: Overwrite existing tools seamlessly
-
Hybrid Architecture:
- Rust runtime for performance and safety
- Python execution for flexibility and ecosystem access
- Best of both worlds!
-
Install Rust: Ensure you have Rust installed (https://rustup.rs/)
-
Clone and Build:
git clone <repository> cd k-agent-lite cargo build
-
Environment Configuration:
cp .env.example .env # Edit .env with your API keys and model selectionExample
.envconfiguration:# For OpenRouter (supports multiple providers) LITELLM_MODEL=openrouter/anthropic/claude-3.5-sonnet OPENROUTER_API_KEY=your_openrouter_key_here # Or for OpenAI directly LITELLM_MODEL=gpt-4o OPENAI_API_KEY=your_openai_key_here # Or for Anthropic directly LITELLM_MODEL=claude-3-5-sonnet-20241022 ANTHROPIC_API_KEY=your_anthropic_key_here # Or for Gemini AI Studio LITELLM_MODEL=gemini-2.0-flash GEMINI_API_KEY=your_gemini_key_here # Or for custom OpenAI-compatible endpoint BASE_CUSTOM_URL=http://localhost:8000/v1/chat/completions BASE_CUSTOM_MODEL=llama-3.1-8b CUSTOM_API_KEY=your_custom_key_here # Optional
cargo runThen enter your task when prompted.
cargo run -- "your task here"cargo run -- -v "your task here"
# or
cargo run -- --verboseVerbose mode displays detailed information about tool calls, API requests, and execution steps.
src/
├── main.rs # Main application code
Cargo.toml # Dependencies and project metadata
Cargo.lock # Dependency lock file
.env.example # Environment variables template
README.md # This file
README_RUST.md # Additional documentation
tokio: Async runtime for asynchronous operationsserde/serde_json: JSON serialization/deserializationreqwest: HTTP client for LLM API callscolored: Terminal color outputanyhow: Error handling and contextdotenv: Environment variable loading from.envfilesclap: Command line argument parsinguuid: UUID generation for unique identifiersureq: Synchronous HTTP clientlazy_static: Global static variablestermimad: Markdown rendering in terminalsyntect: Syntax highlighting for code blocks
The implementation automatically detects the LLM provider based on the model name:
-
OpenRouter: Prefix with
openrouter/(e.g.,openrouter/anthropic/claude-3.5-sonnet)- Requires
OPENROUTER_API_KEYenvironment variable - Supports models from Anthropic, OpenAI, Google, Meta, and more
- API endpoint:
https://openrouter.ai/api/v1/chat/completions
- Requires
-
OpenAI: Use standard model names (e.g.,
gpt-4o,gpt-4-turbo,o1-preview)- Requires
OPENAI_API_KEYenvironment variable - API endpoint:
https://api.openai.com/v1/chat/completions
- Requires
-
Anthropic: Prefix with
claude-(e.g.,claude-3-5-sonnet-20241022)- Requires
ANTHROPIC_API_KEYenvironment variable - API endpoint:
https://api.anthropic.com/v1/messages
- Requires
-
Gemini AI Studio: Prefix with
gemini-(e.g.,gemini-2.0-flash,gemini-1.5-pro)- Requires
GEMINI_API_KEYenvironment variable (get from https://aistudio.google.com/apikey) - API endpoint:
https://generativelanguage.googleapis.com/v1beta/openai/chat/completions - Uses OpenAI-compatible API format
- Requires
-
Custom OpenAI-compatible Endpoints: For local LLMs, vLLM, Ollama, etc.
- Set
BASE_CUSTOM_URLto your endpoint URL - Set
BASE_CUSTOM_MODELto your model name - Optionally set
CUSTOM_API_KEY(defaults toOPENAI_API_KEYif not provided) - Example: Running a local vLLM server or Ollama with OpenAI compatibility
- Set
To add support for additional providers, modify the call_llm function in src/main.rs to add a new condition in the provider detection logic.
To add new built-in tools:
- Add the tool to the
get_available_tools()function - Add a new match arm in the
call_tool()function - Implement the tool's functionality
For more advanced dynamic tool execution, consider:
- Using a scripting engine like
rhaiorrune - Implementing a plugin system with dynamic library loading
- WebAssembly integration for sandboxed code execution
- Requires Python 3 and
uvxinstalled on the system - Tool execution has overhead from process spawning
- Limited Composio integration compared to Python version
- No built-in web search capabilities (can be added as custom Python tools)
- Maximum 50 iterations per task to prevent infinite loops
K-Agent Lite is under active development. Below are the planned features and enhancements:
Status: 🚧 In Planning
Enhance the current dynamic tool system with:
- Tool Versioning: Track tool versions and allow rollback to previous implementations
- Tool Composition: Combine multiple tools into complex workflows
- Tool Dependencies: Define dependencies between tools with automatic resolution
- Tool Validation: Runtime validation of tool inputs/outputs with JSON schema
- Tool Caching: Cache tool results for identical inputs to improve performance
- Tool Metrics: Track execution time, success rate, and resource usage per tool
- Async Tool Execution: Execute independent tools in parallel for better performance
Implementation Approach:
// Example: Enhanced tool registry with versioning
struct ToolRegistry {
tools: HashMap<String, Vec<ToolVersion>>,
metrics: HashMap<String, ToolMetrics>,
cache: LRUCache<ToolCacheKey, ToolResult>,
}Status: In Planning
Integrate Model Context Protocol for standardized context sharing and tool interoperability:
- MCP Server Support: Connect to MCP servers to access external tools and resources
- MCP Client Implementation: Expose K-Agent tools as MCP-compatible endpoints
- Resource Management: Access files, APIs, and databases through MCP resource providers
- Prompt Templates: Share and reuse prompt templates across MCP-compatible systems
- Sampling Support: Leverage MCP's sampling capabilities for LLM interactions
- Transport Layers: Support stdio, HTTP/SSE, and WebSocket transports
Key Benefits:
- Standardized tool discovery and invocation
- Seamless integration with other MCP-compatible agents
- Access to growing ecosystem of MCP servers (filesystem, database, API integrations)
- Improved context management and resource sharing
Example Use Cases:
# Connect to MCP servers for enhanced capabilities
export MCP_SERVERS="filesystem,database,github"
# Access GitHub through MCP
cargo run -- "analyze recent commits in my repository"
# Query databases through MCP
cargo run -- "get sales data from the analytics database"Status: 🚧 In Planning
Implement a reusable skills system for common agent capabilities:
-
Skill Modules: Pre-built, tested modules for common tasks
- Web Skills: Web scraping, API interaction, form filling
- Data Skills: CSV/JSON/XML parsing, data transformation, analysis
- File Skills: File operations, archive handling, text processing
- Research Skills: Web search, content summarization, fact extraction
- Code Skills: Code generation, testing, refactoring, documentation
-
Skill Discovery: Automatic detection and registration of available skills
-
Skill Dependencies: Manage Python package dependencies per skill
-
Skill Testing: Built-in test suites for skill validation
-
Skill Marketplace: Share and discover community-contributed skills
Skill Structure:
# skills/web_scraper/skill.toml
[skill]
name = "web_scraper"
version = "1.0.0"
description = "Extract data from web pages"
author = "K-Agent Community"
[dependencies]
python = ["beautifulsoup4", "requests", "lxml"]
[tools]
scrape_page = { file = "scraper.py", function = "scrape_page" }
extract_links = { file = "scraper.py", function = "extract_links" }Skill Categories:
- Communication Skills: Email, Slack, Discord integrations
- Development Skills: Git operations, CI/CD, deployment
- Productivity Skills: Calendar, task management, note-taking
- Analytics Skills: Data visualization, statistical analysis
- Security Skills: Secret scanning, vulnerability detection
Short Term (Next Release):
- ✅ UVX installation check and guidance
- 🚧 Configuration file support (YAML/TOML)
- 🚧 Structured logging with multiple levels
- 🚧 Better error handling with automatic retry logic
- 🚧 Token usage tracking and cost estimation
Medium Term:
- Plugin system for extensible tools
- WebAssembly sandbox for secure code execution
- Streaming responses for real-time output
- Memory persistence across sessions
- Context management and summarization
Long Term:
- Multi-agent coordination and collaboration
- Self-improvement through reinforcement learning
- Visual workflow editor for tool composition
- Browser-based UI for agent interaction
- Cloud deployment support (Docker, Kubernetes)
We welcome contributions! If you're interested in helping implement any of these features:
- Check the Issues page for related discussions
- Join the conversation about feature design
- Submit PRs with incremental improvements
- Share your use cases and requirements
- Plugin system for extensible tools
- WebAssembly sandbox for dynamic code execution
- Better error handling and recovery with retries
- Configuration file support (YAML/TOML)
- Structured logging system
- Composio integration for external tool access
- Streaming responses for real-time output
- Token usage tracking and cost estimation
This is a Rust implementation inspired by Pippin-Lite, a minimalistic template for dynamic self-building AI agents. Pippin-Lite is a lighter-weight cousin of the Pippin framework, designed as an educational starting point for exploring autonomous agents.
This project is licensed under the MIT License. See the LICENSE file for details.