# Phase 4: MCP Tool Integration API Documentation ## Overview Phase 4 implements a complete **Model Context Protocol (MCP)** tool registry with **18+ tools** across 4 categories (RAG, Guidance, Settings, IaC) and introduces **hybrid execution mode** for automatic tool suggestion and invocation. ## Architecture ### Three-Layer Integration ``` External Clients (HTTP/MCP) ↓ ai-service HTTP API (Port 8083) ↓ Unified Tool Registry (ToolRegistry) ↓ RAG | Guidance | Settings | IaC Tools ↓ Knowledge Base | System | Configuration ``` ## API Endpoints ### 1. Ask with RAG (Optional Tool Execution) **Endpoint**: `POST /api/v1/ai/ask` **Request**: ```json { "question": "What are deployment best practices?", "context": "Optional context for the question", "enable_tool_execution": false, "max_tool_calls": 3 } ``` **Fields**: - `question` (string, required): The question to ask - `context` (string, optional): Additional context - `enable_tool_execution` (boolean, optional, default: false): Enable hybrid mode with automatic tool execution - `max_tool_calls` (integer, optional, default: 3): Maximum tools to execute in hybrid mode **Response** (Explicit Mode - default): ```json { "answer": "Based on the knowledge base, here's what I found:\n- **Best Practice 1**: ...", "sources": ["Practice 1", "Practice 2"], "confidence": 85, "reasoning": "Retrieved 3 relevant documents", "tool_executions": null } ``` **Response** (Hybrid Mode - auto-tools enabled): ```json { "answer": "Based on the knowledge base, here's what I found:\n- **Best Practice 1**: ...\n\n---\n\n**Tool Results:**\n\n**guidance_check_system_status:**\nStatus: healthy\nProvisioning: running\n\n**guidance_find_docs:**\nStatus: success\nDocumentation search results for: deployment", "sources": ["Practice 1", "Practice 2"], "confidence": 85, "reasoning": "Retrieved 3 relevant documents", "tool_executions": [ { "tool_name": "guidance_check_system_status", "result": { "status": "healthy", "tool": "guidance_check_system_status", "system": { "provisioning": "running", "services": "operational" } }, "duration_ms": 42 } ] } ``` ### 2. Execute Tool Explicitly **Endpoint**: `POST /api/v1/ai/mcp/tool` **Request**: ```json { "tool_name": "rag_semantic_search", "args": { "query": "kubernetes deployment", "top_k": 5 } } ``` **Response**: ```json { "result": { "status": "success", "tool": "rag_semantic_search", "message": "Semantic search would be performed for: kubernetes deployment", "results": [] }, "duration_ms": 12 } ``` ## Tool Registry ### Available Tools (18+ tools) #### RAG Tools (3) - **rag_ask_question**: Ask a question using RAG with knowledge base search - Args: `{question: string, context?: string, top_k?: int}` - Returns: Answer with sources and confidence - **rag_semantic_search**: Perform semantic search on knowledge base - Args: `{query: string, category?: string, top_k?: int}` - Returns: Search results from knowledge base - **rag_get_status**: Get status of RAG knowledge base - Args: `{}` - Returns: Knowledge base statistics #### Guidance Tools (5) - **guidance_check_system_status**: Check current system status - Args: `{}` - Returns: System health and service status - **guidance_suggest_next_action**: Get action suggestions based on system state - Args: `{context?: string}` - Returns: Recommended next action - **guidance_find_docs**: Find relevant documentation - Args: `{query: string, context?: string}` - Returns: Documentation search results - **guidance_troubleshoot**: Troubleshoot an issue - Args: `{error: string, context?: string}` - Returns: Diagnosis and fixes - **guidance_validate_config**: Validate configuration - Args: `{config_path: string}` - Returns: Validation results #### Settings Tools (7) - **installer_get_settings**: Get installer settings - **installer_complete_config**: Complete partial configuration - **installer_validate_config**: Validate configuration against schema - **installer_get_defaults**: Get defaults for deployment mode - **installer_platform_recommendations**: Get platform recommendations - **installer_service_recommendations**: Get service recommendations - **installer_resource_recommendations**: Get resource recommendations #### IaC Tools (3) - **iac_detect_technologies**: Detect technologies in infrastructure - **iac_analyze_completeness**: Analyze infrastructure completeness - **iac_infer_requirements**: Infer infrastructure requirements ### List Tools **Endpoint**: `GET /api/v1/ai/tools` **Response**: ```json [ { "name": "rag_ask_question", "description": "Ask a question using RAG...", "category": "Rag", "input_schema": { "type": "object", "properties": { "question": {"type": "string"}, "context": {"type": "string"}, "top_k": {"type": "integer"} }, "required": ["question"] } } ] ``` ## Hybrid Execution Mode ### How It Works 1. **RAG Query**: User asks a question with `enable_tool_execution: true` 2. **Tool Suggestion**: RAG answer is analyzed for relevant tools using keyword matching 3. **Tool Execution**: Suggested tools are executed automatically (up to `max_tool_calls`) 4. **Answer Enrichment**: Tool results are merged into the RAG answer 5. **Response**: Both RAG answer and tool results returned together ### Tool Suggestion Algorithm Tools are suggested based on keywords in the question: ``` Question contains "status" → suggest guidance_check_system_status Question contains "config" → suggest guidance_validate_config Question contains "doc" → suggest guidance_find_docs Question contains "error" → suggest guidance_troubleshoot Question contains "next" → suggest guidance_suggest_next_action Question contains "search" → suggest rag_semantic_search ``` ### Examples #### Example 1: Explicit Mode (Default) ```bash curl -X POST http://localhost:8083/api/v1/ai/ask \ -H 'Content-Type: application/json' \ -d '{ "question": "What are deployment best practices?", "enable_tool_execution": false }' ``` Response: RAG answer only (fast, predictable) #### Example 2: Hybrid Mode with Auto-Execution ```bash curl -X POST http://localhost:8083/api/v1/ai/ask \ -H 'Content-Type: application/json' \ -d '{ "question": "Is the system healthy and what are the best practices?", "enable_tool_execution": true, "max_tool_calls": 3 }' ``` Response: RAG answer + system status from guidance_check_system_status tool #### Example 3: Explicit Tool Call ```bash curl -X POST http://localhost:8083/api/v1/ai/mcp/tool \ -H 'Content-Type: application/json' \ -d '{ "tool_name": "guidance_check_system_status", "args": {} }' ``` Response: Raw tool result with timing ## Type Definitions ### AskRequest ```rust pub struct AskRequest { pub question: String, // The question to ask pub context: Option, // Optional context pub enable_tool_execution: Option, // Enable hybrid mode (default: false) pub max_tool_calls: Option, // Max tools to execute (default: 3) } ``` ### AskResponse ```rust pub struct AskResponse { pub answer: String, // Answer from RAG or combined with tools pub sources: Vec, // Source documents pub confidence: u8, // Confidence level (0-100) pub reasoning: String, // Explanation of answer pub tool_executions: Option>, // Tools executed in hybrid mode } ``` ### McpToolRequest ```rust pub struct McpToolRequest { pub tool_name: String, // Name of tool to execute pub args: serde_json::Value, // Tool arguments } ``` ### McpToolResponse ```rust pub struct McpToolResponse { pub result: serde_json::Value, // Tool result pub duration_ms: u64, // Execution time } ``` ### ToolExecution ```rust pub struct ToolExecution { pub tool_name: String, // Which tool was executed pub result: serde_json::Value, // Tool result pub duration_ms: u64, // Execution duration } ``` ## Performance Characteristics ### Explicit Mode - **Latency**: 50-200ms (RAG search only) - **Deterministic**: Same question → same answer - **Cost**: Low (single knowledge base search) - **Use case**: Production, predictable responses ### Hybrid Mode - **Latency**: 100-500ms (RAG + 1-3 tool executions) - **Variable**: Different tools run based on question keywords - **Cost**: Higher (multiple tool executions) - **Use case**: Interactive, exploratory queries - **Timeout**: 30s per tool execution ## Error Handling ### Invalid Tool Name ```json { "error": "Unknown tool: invalid_tool_xyz" } ``` ### Missing Required Arguments ```json { "error": "Tool execution failed: query parameter required" } ``` ### Tool Execution Timeout ```json { "error": "Tool execution failed: timeout exceeded" } ``` ## Best Practices ### 1. Use Explicit Mode by Default ```json { "question": "What are deployment best practices?", "enable_tool_execution": false } ``` - Faster and more predictable - Better for production systems ### 2. Enable Hybrid Mode for Interactive Queries ```json { "question": "Is the system healthy and how do I fix it?", "enable_tool_execution": true, "max_tool_calls": 3 } ``` - Better context with tool results - Good for troubleshooting ### 3. Use Explicit Tool Calls for Specific Needs ```json { "tool_name": "guidance_check_system_status", "args": {} } ``` - When you know exactly what you need - Bypass RAG altogether - Direct tool access ### 4. Set Appropriate max_tool_calls - **1**: For simple yes/no tools - **3**: Balanced (default) - **5+**: For complex queries requiring multiple tools ## Implementation Details ### Tool Registry The `ToolRegistry` maintains: - 18+ tool definitions organized by category - JSON Schema for each tool's input validation - Async execution handlers for each tool ### Hybrid Mode Flow 1. Parse AskRequest, check `enable_tool_execution` 2. Get RAG answer from knowledge base 3. Call `analyze_for_tools()` on the question 4. Execute suggested tools (respecting `max_tool_calls`) 5. Call `enrich_answer_with_results()` to merge outputs 6. Return combined response with `tool_executions` field ### Tool Suggestion Algorithm in `tool_integration.rs`: - Keyword matching against question - Confidence scoring per suggestion - Sort by confidence descending - Take top N (limited by max_tool_calls) ## Testing Run integration tests: ```bash cargo test --package ai-service --test phase4_integration_test ``` Tests include: - Tool registry initialization (16 tools verified) - Explicit tool execution (all 4 categories) - Hybrid mode with auto-execution - max_tool_calls limit enforcement - Error handling for unknown/invalid tools - Tool definition schema validation ## Future Enhancements 1. **Custom Tool Registration**: Allow plugins to register tools 2. **Tool Chaining**: Execute tools sequentially based on results 3. **Semantic Tool Selection**: Use embeddings instead of keywords 4. **Tool Caching**: Cache results for frequently executed tools 5. **Authentication**: Per-tool access control 6. **Metrics**: Tool execution statistics and performance monitoring ## Migration from Phase 3 Phase 3 provided RAG with: - Knowledge base loading - Keyword search - Basic RAG queries Phase 4 adds: - ✅ Unified tool registry (18+ tools) - ✅ Hybrid execution mode (auto-trigger tools) - ✅ Explicit tool execution - ✅ Tool result enrichment - ✅ Category-based organization - ✅ Comprehensive testing Backward compatibility: - `enable_tool_execution: false` (default) maintains Phase 3 behavior - Existing `/api/v1/ai/ask` endpoint works unchanged - New `/api/v1/ai/mcp/tool` endpoint added for explicit calls