ai-model-vault 4.2.1

Universal cross-platform XDG-compliant secure vault for AI model formats (Neural and Neurosymbolic)
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
# MCP (Model Context Protocol) & Tools Guide

## Overview

AI Model Vault provides comprehensive support for **Model Context Protocol (MCP)** and tool-based interactions, enabling AI agents and applications to execute structured operations on RAG systems, knowledge bases, and custom workflows.

## Table of Contents

- [Quick Start]#quick-start
- [MCP Tools]#mcp-tools
- [Tool Definition]#tool-definition
- [Tool Execution]#tool-execution
- [Built-in Tools]#built-in-tools
- [Custom Tools]#custom-tools
- [Tool Context]#tool-context
- [MCP Server]#mcp-server
- [Complete Examples]#complete-examples

## Quick Start

```rust
use ai_model_vault::rag::*;

// Create MCP server
let mut server = MCPServer::new();

// Register built-in RAG tools
server.register_builtin_tools()?;

// Create tool context
let ctx = ToolContext::new()
    .with_knowledge_base("my_kb".to_string());

// Execute a tool
let result = server.execute_tool(
    "search_documents",
    serde_json::json!({"query": "machine learning", "top_k": 5}),
    &ctx
)?;

println!("Results: {:?}", result.data);
```

## MCP Tools

### What is MCP?

Model Context Protocol (MCP) provides a standardized way for AI models and agents to interact with external tools and services. In AI Model Vault, MCP enables:

- **Structured Tool Invocation**: Define tools with clear input/output schemas
- **Context Management**: Pass contextual information to tools
- **Result Standardization**: Consistent result format across all tools
- **Error Handling**: Proper error reporting and handling

### Tool Architecture

```
┌─────────────┐
│  AI Agent   │
└──────┬──────┘
       │
       ▼
┌─────────────┐      ┌──────────────┐
│ MCP Server  │◄────►│  Tool Store  │
└──────┬──────┘      └──────────────┘
       │
       ▼
┌─────────────┐      ┌──────────────┐
│  Executor   │◄────►│   Context    │
└──────┬──────┘      └──────────────┘
       │
       ▼
┌─────────────┐
│   Result    │
└─────────────┘
```

## Tool Definition

### Creating a Tool

```rust
let tool = MCPTool::new(
    "search_documents".to_string(),
    "Search for documents using semantic similarity".to_string(),
)
.add_parameter("query", "string", "The search query", true)
.add_parameter("top_k", "number", "Number of results to return", false)
.add_parameter("threshold", "number", "Minimum similarity score", false)
.with_metadata("version".to_string(), "1.0".to_string())
.with_metadata("category".to_string(), "retrieval".to_string());
```

### Parameter Types

MCP tools support standard JSON Schema types:

- `string` - Text values
- `number` - Numeric values (int or float)
- `boolean` - True/false values
- `array` - Lists of values
- `object` - Nested objects/maps

### Required vs Optional Parameters

```rust
// Required parameter (third argument: true)
.add_parameter("query", "string", "Search query", true)

// Optional parameter (third argument: false)
.add_parameter("top_k", "number", "Max results", false)
```

### Tool Metadata

Add custom metadata to tools for versioning, categorization, or other purposes:

```rust
tool.with_metadata("version".to_string(), "1.0".to_string())
    .with_metadata("author".to_string(), "Team AI".to_string())
    .with_metadata("category".to_string(), "search".to_string())
```

## Tool Execution

### Tool Context

The `ToolContext` provides contextual information to tool executors:

```rust
let ctx = ToolContext::new()
    .with_document_store("store_id".to_string())
    .with_knowledge_base("kb_id".to_string())
    .with_data("user_id".to_string(), "user123".to_string())
    .with_data("session".to_string(), "abc123".to_string());
```

### Tool Results

Tools return `ToolResult` with standardized structure:

```rust
// Success
let success = ToolResult::success(serde_json::json!({
    "documents": ["doc1", "doc2"],
    "count": 2
}))
.with_metadata("execution_time".to_string(), "45ms".to_string());

// Failure
let failure = ToolResult::failure("Document not found".to_string())
    .with_metadata("error_code".to_string(), "404".to_string());
```

### Executing Tools

```rust
let result = server.execute_tool(
    "tool_name",
    serde_json::json!({"param1": "value1"}),
    &ctx
)?;

if result.success {
    println!("Success: {:?}", result.data);
} else {
    eprintln!("Error: {:?}", result.error);
}
```

## Built-in Tools

AI Model Vault includes 4 built-in RAG tools:

### 1. search_documents

Search for similar documents using vector embeddings.

**Parameters:**
- `query` (string, required): Search query text
- `top_k` (number, optional): Number of results (default: 5)
- `threshold` (number, optional): Minimum similarity threshold

**Example:**
```rust
let result = server.execute_tool(
    "search_documents",
    serde_json::json!({
        "query": "machine learning algorithms",
        "top_k": 10,
        "threshold": 0.7
    }),
    &ctx
)?;
```

### 2. add_document

Add a document to the knowledge base.

**Parameters:**
- `id` (string, required): Document ID
- `content` (string, required): Document content
- `metadata` (object, optional): Document metadata

**Example:**
```rust
let result = server.execute_tool(
    "add_document",
    serde_json::json!({
        "id": "doc123",
        "content": "Artificial intelligence is...",
        "metadata": {"category": "AI"}
    }),
    &ctx
)?;
```

### 3. chunk_text

Split text into chunks for processing.

**Parameters:**
- `text` (string, required): Text to chunk
- `chunk_size` (number, optional): Size of each chunk (default: 512)
- `overlap` (number, optional): Overlap between chunks (default: 50)

**Example:**
```rust
let result = server.execute_tool(
    "chunk_text",
    serde_json::json!({
        "text": "Long document text...",
        "chunk_size": 256,
        "overlap": 25
    }),
    &ctx
)?;
```

### 4. execute_rule

Execute a business rule with given context.

**Parameters:**
- `rule_id` (string, required): Rule identifier
- `context` (object, required): Rule execution context

**Example:**
```rust
let result = server.execute_tool(
    "execute_rule",
    serde_json::json!({
        "rule_id": "confidence_check",
        "context": {
            "confidence": 0.95,
            "model": "gpt-4"
        }
    }),
    &ctx
)?;
```

## Custom Tools

### Creating Custom Tool Executors

```rust
let mut server = MCPServer::new();

// Define the tool
let custom_tool = MCPTool::new(
    "analyze_sentiment".to_string(),
    "Analyze text sentiment".to_string(),
)
.add_parameter("text", "string", "Text to analyze", true);

// Register with executor function
server.register_tool(custom_tool, |params, ctx| {
    let text = params.get("text")
        .and_then(|v| v.as_str())
        .ok_or_else(|| VaultError::InvalidInput("Missing text".to_string()))?;

    // Custom logic here
    let sentiment = if text.contains("good") { "positive" } else { "neutral" };

    Ok(ToolResult::success(serde_json::json!({
        "sentiment": sentiment,
        "confidence": 0.85,
        "text_length": text.len()
    })))
})?;
```

### Complex Custom Tool Example

```rust
// Multi-step processing tool
let processor_tool = MCPTool::new(
    "process_document".to_string(),
    "Complete document processing pipeline".to_string(),
)
.add_parameter("doc_id", "string", "Document ID", true)
.add_parameter("steps", "array", "Processing steps", true);

server.register_tool(processor_tool, |params, ctx| {
    let doc_id = params.get("doc_id")
        .and_then(|v| v.as_str())
        .ok_or_else(|| VaultError::InvalidInput("Missing doc_id".to_string()))?;

    let steps = params.get("steps")
        .and_then(|v| v.as_array())
        .ok_or_else(|| VaultError::InvalidInput("Missing steps".to_string()))?;

    let mut results = Vec::new();
    for step in steps {
        let step_name = step.as_str().unwrap_or("unknown");
        // Process each step
        results.push(format!("Completed: {}", step_name));
    }

    Ok(ToolResult::success(serde_json::json!({
        "doc_id": doc_id,
        "steps_completed": results.len(),
        "results": results
    }))
    .with_metadata("kb".to_string(), ctx.knowledge_base.clone().unwrap_or_default()))
})?;
```

## Tool Context

### Context-Aware Tools

Tools can access context information to customize behavior:

```rust
let context_tool = MCPTool::new(
    "get_user_docs".to_string(),
    "Get documents for current user".to_string(),
);

server.register_tool(context_tool, |_params, ctx| {
    let user_id = ctx.data.get("user_id")
        .cloned()
        .unwrap_or_else(|| "anonymous".to_string());

    let kb_id = ctx.knowledge_base
        .clone()
        .unwrap_or_else(|| "default".to_string());

    Ok(ToolResult::success(serde_json::json!({
        "user_id": user_id,
        "knowledge_base": kb_id,
        "documents": [] // Would fetch from actual KB
    })))
})?;
```

### Building Rich Context

```rust
let ctx = ToolContext::new()
    // System context
    .with_document_store("production_store".to_string())
    .with_knowledge_base("main_kb".to_string())
    
    // User context
    .with_data("user_id".to_string(), "user_123".to_string())
    .with_data("role".to_string(), "admin".to_string())
    
    // Session context
    .with_data("session_id".to_string(), "sess_abc".to_string())
    .with_data("timestamp".to_string(), "2025-10-28T12:00:00Z".to_string())
    
    // Custom context
    .with_data("language".to_string(), "en".to_string())
    .with_data("region".to_string(), "us-east".to_string());
```

## MCP Server

### Server Setup

```rust
let mut server = MCPServer::new();

// Register built-in tools
server.register_builtin_tools()?;

// Register custom tools
server.register_tool(my_tool, my_executor)?;

// List all tools
let tools = server.list_tools();
for tool in tools {
    println!("Tool: {} - {}", tool.name, tool.description);
}

// Get specific tool
if let Some(tool) = server.get_tool("search_documents") {
    println!("Found: {}", tool.name);
}
```

### Tool Discovery

```rust
// List all available tools
let tools = server.list_tools();
println!("Available tools: {}", tools.len());

for tool in tools {
    println!("\n{}", tool.name);
    println!("  Description: {}", tool.description);
    
    if let Some(props) = tool.input_schema.get("properties") {
        if let Some(obj) = props.as_object() {
            println!("  Parameters:");
            for (name, schema) in obj {
                println!("    - {}: {:?}", name, schema.get("type"));
            }
        }
    }
}
```

### Error Handling

```rust
match server.execute_tool("my_tool", params, &ctx) {
    Ok(result) => {
        if result.success {
            println!("Success: {:?}", result.data);
        } else {
            eprintln!("Tool failed: {:?}", result.error);
        }
    }
    Err(e) => {
        eprintln!("Execution error: {}", e);
    }
}
```

## Complete Examples

### RAG Query Pipeline with MCP

```rust
use ai_model_vault::rag::*;

fn rag_pipeline_with_mcp(query: &str) -> Result<String, Box<dyn std::error::Error>> {
    // 1. Setup MCP server
    let mut server = MCPServer::new();
    server.register_builtin_tools()?;
    
    // 2. Create context
    let ctx = ToolContext::new()
        .with_knowledge_base("research_kb".to_string())
        .with_data("user".to_string(), "researcher_1".to_string());
    
    // 3. Chunk the query if needed
    let chunk_result = server.execute_tool(
        "chunk_text",
        serde_json::json!({
            "text": query,
            "chunk_size": 100
        }),
        &ctx
    )?;
    
    // 4. Search for relevant documents
    let search_result = server.execute_tool(
        "search_documents",
        serde_json::json!({
            "query": query,
            "top_k": 5,
            "threshold": 0.6
        }),
        &ctx
    )?;
    
    // 5. Generate response (simulated)
    Ok(format!("Generated response for: {}", query))
}
```

### Custom Agent with Tools

```rust
struct AIAgent {
    server: MCPServer,
    context: ToolContext,
}

impl AIAgent {
    fn new() -> Result<Self, Box<dyn std::error::Error>> {
        let mut server = MCPServer::new();
        server.register_builtin_tools()?;
        
        // Add agent-specific tools
        let think_tool = MCPTool::new(
            "think".to_string(),
            "Internal reasoning step".to_string(),
        )
        .add_parameter("thought", "string", "Reasoning", true);
        
        server.register_tool(think_tool, |params, _ctx| {
            let thought = params.get("thought")
                .and_then(|v| v.as_str())
                .unwrap_or("");
            
            Ok(ToolResult::success(serde_json::json!({
                "thought": thought,
                "processed": true
            })))
        })?;
        
        let context = ToolContext::new()
            .with_knowledge_base("agent_kb".to_string());
        
        Ok(Self { server, context })
    }
    
    fn execute(&self, tool: &str, params: serde_json::Value) -> Result<ToolResult, Box<dyn std::error::Error>> {
        Ok(self.server.execute_tool(tool, params, &self.context)?)
    }
    
    fn available_tools(&self) -> Vec<String> {
        self.server.list_tools()
            .iter()
            .map(|t| t.name.clone())
            .collect()
    }
}
```

## Best Practices

### 1. Tool Design

- **Single Responsibility**: Each tool should do one thing well
- **Clear Parameters**: Use descriptive parameter names and descriptions
- **Proper Validation**: Validate all input parameters
- **Error Messages**: Provide clear, actionable error messages

### 2. Context Management

- **Minimal Context**: Only include necessary context data
- **Consistent Keys**: Use standardized key names across tools
- **Immutable Context**: Treat context as read-only in executors

### 3. Error Handling

```rust
server.register_tool(tool, |params, ctx| {
    // Validate parameters
    let value = params.get("value")
        .and_then(|v| v.as_str())
        .ok_or_else(|| VaultError::InvalidInput("Missing value".to_string()))?;
    
    // Validate business logic
    if value.is_empty() {
        return Ok(ToolResult::failure("Value cannot be empty".to_string()));
    }
    
    // Execute with error handling
    match process_value(value) {
        Ok(result) => Ok(ToolResult::success(result)),
        Err(e) => Ok(ToolResult::failure(e.to_string())),
    }
})?;
```

### 4. Performance

- **Lazy Evaluation**: Only compute what's needed
- **Caching**: Cache expensive operations
- **Async Operations**: Use async for I/O-bound tools (future enhancement)
- **Resource Limits**: Set timeouts and size limits

## Integration Patterns

### With Knowledge Bases

```rust
let kb = KnowledgeBase::new("kb".to_string(), config);

// Create tool that uses KB
let kb_tool = MCPTool::new("kb_search".to_string(), "Search KB".to_string())
    .add_parameter("query", "string", "Query", true);

server.register_tool(kb_tool, move |params, _ctx| {
    let query = params.get("query").and_then(|v| v.as_str()).unwrap();
    // Use kb.retrieve() here
    Ok(ToolResult::success(serde_json::json!({"results": []})))
})?;
```

### With Rule Engines

```rust
let mut engine = RuleEngine::new();

// Tool to execute rules
let rule_tool = MCPTool::new("apply_rules".to_string(), "Apply rules".to_string())
    .add_parameter("context", "object", "Rule context", true);

server.register_tool(rule_tool, move |params, _ctx| {
    // Execute rules with engine
    Ok(ToolResult::success(serde_json::json!({"rules_applied": 0})))
})?;
```

## API Reference

### MCPTool

```rust
impl MCPTool {
    pub fn new(name: String, description: String) -> Self;
    pub fn add_parameter(self, name: &str, type: &str, desc: &str, required: bool) -> Self;
    pub fn with_metadata(self, key: String, value: String) -> Self;
}
```

### ToolContext

```rust
impl ToolContext {
    pub fn new() -> Self;
    pub fn with_document_store(self, store_id: String) -> Self;
    pub fn with_knowledge_base(self, kb_id: String) -> Self;
    pub fn with_data(self, key: String, value: String) -> Self;
}
```

### ToolResult

```rust
impl ToolResult {
    pub fn success(data: JsonValue) -> Self;
    pub fn failure(error: String) -> Self;
    pub fn with_metadata(self, key: String, value: String) -> Self;
}
```

### MCPServer

```rust
impl MCPServer {
    pub fn new() -> Self;
    pub fn register_tool<F>(&mut self, tool: MCPTool, executor: F) -> Result<()>;
    pub fn execute_tool(&self, name: &str, params: JsonValue, ctx: &ToolContext) -> Result<ToolResult>;
    pub fn list_tools(&self) -> Vec<&MCPTool>;
    pub fn get_tool(&self, name: &str) -> Option<&MCPTool>;
    pub fn register_builtin_tools(&mut self) -> Result<()>;
}
```

## See Also

- [RAG Guide]RAG.md - Complete RAG documentation
- [RAG Quick Reference]RAG_QUICKREF.md - Quick reference card
- [Examples]https://github.com/nervosys/AIModelVault/blob/master/examples/mcp_tools_demo.rs - Working examples
- [Tests]https://github.com/nervosys/AIModelVault/blob/master/tests/rag_tests.rs - Test suite