from praisonaiagents.tools import list_available_tools, get_registry# Get only available toolsavailable_tools = list_available_tools()print(f"Available: {len(available_tools)} tools")# Get all tools (including unavailable)all_tools = get_registry().list_tools()print(f"Total registered: {len(all_tools)} tools")# Check specific toolweb_tool = get_registry().get("search_web")if hasattr(web_tool, 'check_availability'): is_available, reason = web_tool.check_availability() print(f"Web tool available: {is_available}") if not is_available: print(f"Reason: {reason}")
from praisonaiagents.tools import register_tooldef simple_function(text: str) -> str: return f"Processed: {text}"# Plain functions are always available (no protocol support yet)register_tool(simple_function)
import osfrom praisonaiagents.tools import tooldef check_environment(required_vars: list[str]): """Factory for environment-based availability checks.""" def check(): missing = [var for var in required_vars if not os.getenv(var)] if missing: return False, f"Missing environment variables: {', '.join(missing)}" return True, "" return check@tool(availability=check_environment(["OPENAI_API_KEY", "PINECONE_API_KEY"]))def ai_research(topic: str) -> str: """Research topics using AI and vector search.""" return f"AI research on: {topic}"@tool(availability=check_environment(["SLACK_TOKEN"]))def notify_team(message: str) -> str: """Send notifications to team Slack.""" return f"Notified team: {message}"
from praisonaiagents import Agentfrom praisonaiagents.tools import list_available_tools# Build agent with only available toolsavailable_tools = list_available_tools()print(f"Loading agent with {len(available_tools)} available tools")agent = Agent( name="AdaptiveAgent", instructions="Use whatever tools are available in the current environment.", tools=available_tools)# Agent automatically adapts to environment capabilities
Availability checks run at schema-build time and must be fast (< 100ms recommended).Good: Environment variable checks, import tests, quick pings
Bad: Full API calls, heavy file operations, long network requests
# Fast check@tool(availability=lambda: (bool(os.getenv("API_KEY")), "API_KEY missing"))# Slow check (avoid)def slow_check(): import requests requests.get("https://api.example.com/health", timeout=30) # Too slow! return True, ""
Cache Results When Possible
For checks that might be expensive, consider caching results.
def check_ml_stack(): # Check imports first (fast) try: import torch import transformers except ImportError as e: return False, f"Missing ML dependencies: {e}" # Then check GPU availability (slower) if not torch.cuda.is_available(): return False, "CUDA not available" return True, ""
Graceful Degradation
Design tools to degrade gracefully when dependencies are partially available.
@tool(availability=lambda: (True, "")) # Always availabledef search_content(query: str) -> str: """Search using best available method.""" # Try premium search first if os.getenv("PREMIUM_SEARCH_KEY"): return premium_search(query) # Fall back to basic search return basic_search(query)