Phase 2 adds a complete REST API layer to KubeAgentic with tool execution capabilities, OpenAI-compatible endpoints, and agent management.
Files:
kubeagentic/tools/executor.py- Tool executor for REST API callskubeagentic/tools/registry.py- Tool registry for managementkubeagentic/tools/__init__.py- Tools package
Features:
- ✅ REST API tool execution with parameter substitution
- ✅ HTTP methods: GET, POST, PUT, DELETE, PATCH
- ✅ Custom headers and authentication
- ✅ Query parameters and request body support
- ✅ Error handling and result formatting
- ✅ Tool registry for discovery
Usage:
from kubeagentic.tools.executor import ToolExecutor
from kubeagentic import Agent
# Load agent with tools
agent = Agent.from_config_file("examples/vllm_advanced.yaml")
# Check available tools
print(agent.get_available_tools())
# Execute a tool
result = agent.execute_tool("get_weather", {"city": "Mumbai"})
print(result)Files:
kubeagentic/api/app.py- Main FastAPI applicationkubeagentic/api/models.py- Request/Response modelskubeagentic/api/server.py- Server scriptkubeagentic/api/__init__.py- API package
Features:
- ✅ FastAPI application with async support
- ✅ Request ID middleware
- ✅ CORS middleware
- ✅ Exception handlers
- ✅ API key authentication
- ✅ Automatic OpenAPI documentation
Endpoints Implemented:
GET /health- Health checkGET /ready- Readiness check
GET /v1/agents- List all agentsPOST /v1/agents/{agent_name}/load- Load an agent
POST /v1/chat- Simple chat endpointPOST /v1/chat/completions- OpenAI-compatible endpoint
GET /v1/agents/{agent_name}/tools- List agent toolsPOST /v1/agents/{agent_name}/tools/{tool_name}/execute- Execute a tool
Files:
kubeagentic/core/agent.py- Updated with tool support
New Agent Methods:
# Check if agent has tools
agent.has_tools # Property
# Get available tools
agent.get_available_tools() # Returns List[str]
# Execute a tool
agent.execute_tool(tool_name, parameters) # Returns DictFile: kubeagentic/cli.py
New Command:
# Start API server
kubeagentic serve --config-dir examples --port 8000
# With authentication
kubeagentic serve --config-dir examples --port 8000 --api-key YOUR_KEY
# Development mode
kubeagentic serve --config-dir examples --port 8000 --reloadIf API keys are configured, include in header:
Authorization: Bearer YOUR_API_KEY
curl http://localhost:8000/healthResponse:
{
"status": "healthy",
"version": "0.1.0",
"timestamp": "2025-10-01T22:00:00"
}curl http://localhost:8000/v1/agentsResponse:
{
"agents": [
{
"name": "vllm_assistant",
"description": "AI assistant powered by vLLM",
"status": "active",
"tools_count": 0,
"llm_provider": "vllm",
"llm_model": "mistralai/Mistral-7B-Instruct-v0.3"
}
],
"total": 1
}curl -X POST http://localhost:8000/v1/chat \
-H "Content-Type: application/json" \
-d '{
"agent_name": "vllm_assistant",
"message": "What is artificial intelligence?"
}'Response:
{
"agent_name": "vllm_assistant",
"message": "Artificial Intelligence (AI) is...",
"session_id": null,
"timestamp": "2025-10-01T22:00:00",
"metadata": {}
}curl -X POST http://localhost:8000/v1/chat/completions \
-H "Content-Type: application/json" \
-d '{
"model": "vllm_assistant",
"messages": [
{"role": "user", "content": "What is AI?"}
]
}'Response:
{
"id": "chatcmpl-abc123...",
"object": "chat.completion",
"created": 1696182000,
"model": "vllm_assistant",
"choices": [
{
"index": 0,
"message": {
"role": "assistant",
"content": "Artificial Intelligence...",
"name": null,
"tool_call_id": null
},
"finish_reason": "stop",
"logprobs": null
}
],
"usage": {
"prompt_tokens": 10,
"completion_tokens": 50,
"total_tokens": 60
}
}curl http://localhost:8000/v1/agents/vllm_advanced_assistant/toolsResponse:
{
"agent": "vllm_advanced_assistant",
"tools": [
"get_weather",
"get_available_cities",
"get_products",
"get_all_products"
],
"count": 4
}curl -X POST http://localhost:8000/v1/agents/vllm_advanced_assistant/tools/get_weather/execute \
-H "Content-Type: application/json" \
-d '{
"parameters": {
"city": "Mumbai"
}
}'Response:
{
"success": true,
"tool": "get_weather",
"result": {
"city": "Mumbai",
"temperature": 28,
"conditions": "Partly cloudy"
},
"status_code": 200
}# Activate virtual environment
source .venv/bin/activate
# Start server
python -m kubeagentic.cli serve --config-dir examples --port 8000from kubeagentic.api.server import run_server
run_server(
host="0.0.0.0",
port=8000,
agents_dir="examples",
api_keys=["your-secret-key"],
)uvicorn kubeagentic.api.app:create_app --host 0.0.0.0 --port 8000 --reloadapp = create_app(
title="KubeAgentic API",
description="REST API for KubeAgentic",
version="0.1.0",
enable_cors=True,
api_keys=["key1", "key2"], # Optional
)tools:
- name: "get_weather"
description: "Get weather for a city"
type: "rest_api"
enabled: true
parameters:
city:
type: "string"
description: "City name"
required: true
config:
url: "http://api.example.com/weather"
method: "GET"
headers:
Authorization: "Bearer TOKEN"
query_params:
city: "{city}"source .venv/bin/activate
python test_phase2.py# Test tool executor
from kubeagentic.tools.executor import ToolExecutor
executor = ToolExecutor(tools=your_tools)
result = executor.execute("tool_name", {"param": "value"})
# Test FastAPI app
from kubeagentic.api.app import create_app
app = create_app()
# Test with httpx
import httpx
client = httpx.Client()
response = client.get("http://localhost:8000/health")# Health check
curl http://localhost:8000/health
# API docs
curl http://localhost:8000/docs
# List agents
curl http://localhost:8000/v1/agents
# Chat
curl -X POST http://localhost:8000/v1/chat \
-H "Content-Type: application/json" \
-d '{"agent_name": "vllm_assistant", "message": "Hello"}'Once the server is running, visit:
- Swagger UI: http://localhost:8000/docs
- ReDoc: http://localhost:8000/redoc
- OpenAPI JSON: http://localhost:8000/openapi.json
# Set API key
python -m kubeagentic.cli serve --api-key my-secret-key
# Use in requests
curl -H "Authorization: Bearer my-secret-key" http://localhost:8000/v1/agentsCORS is enabled by default for development. For production:
app = create_app(enable_cors=False)
# Or configure specific origins
app.add_middleware(
CORSMiddleware,
allow_origins=["https://your-domain.com"],
allow_methods=["GET", "POST"],
)Every request gets a unique ID:
X-Request-ID: 550e8400-e29b-41d4-a716-446655440000
All errors return consistent format:
{
"error": "Error message",
"detail": "Detailed information",
"timestamp": "2025-10-01T22:00:00",
"request_id": "550e8400-..."
}HTTP Status Codes:
200- Success400- Bad Request401- Unauthorized404- Not Found500- Internal Server Error503- Service Unavailable
Process time is tracked in response headers:
X-Process-Time: 0.123
All endpoints support async operations:
# Async endpoint
@app.post("/v1/chat")
async def chat(request: AgentChatRequest):
response = await agent_manager.achat(...)
return response- Streaming responses (SSE)
- Session management with Redis
- Rate limiting per user
- Metrics endpoint (Prometheus)
- WebSocket support
- Tool calling in LLM responses
- Advanced authentication (JWT, OAuth2)
- Database persistence
- Caching layer
Issue: Import errors
Solution:
pip install -r requirements.txtIssue: Tool APIs not accessible
Solution:
- Check network connectivity
- Verify API endpoints are correct
- Check authentication tokens
- Review tool logs with
--log-level debug
Issue: Agent not loaded
Solution:
# Preload agents
python -m kubeagentic.cli serve --config-dir examplesIssue: Invalid API key
Solution:
- Check API key is correct
- Verify header format:
Authorization: Bearer YOUR_KEY - Restart server if keys were changed
kubeagentic/
├── api/
│ ├── __init__.py
│ ├── app.py # FastAPI application
│ ├── models.py # Request/Response models
│ └── server.py # Server runner
├── tools/
│ ├── __init__.py
│ ├── executor.py # Tool executor
│ └── registry.py # Tool registry
├── core/
│ ├── agent.py # Updated with tool support
│ └── manager.py # Agent manager
└── cli.py # Updated with serve command
See examples/ directory:
vllm_simple.yaml- Simple agentvllm_advanced.yaml- Agent with REST API tools
See test_phase2.py for comprehensive examples.
Phase 2 Status: ✅ Complete and Tested
Date: October 1, 2025
Version: 0.1.0