reasonkit-core 0.1.8

The Reasoning Engine — Auditable Reasoning for Production AI | Rust-Native | Turn Prompts into Protocols
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
# MCP (Model Context Protocol) Module

Comprehensive MCP server and client implementation for ReasonKit Core.

## Overview

The MCP module provides:

- **MCP Client**: Connect to external MCP servers and call tools
- **MCP Server**: Serve tools and resources via JSON-RPC 2.0
- **MCP Registry**: Dynamically discover and manage multiple MCP servers
- **Health Monitoring**: Automatic health checks with status tracking
- **Protocol Compliance**: Full MCP specification (2025-11-25) support
- **Rust-Only**: Zero Node.js dependencies (CONS-001 compliant)

## Architecture

```
┌─────────────────────────────────────────────────────────────┐
│ MCP Registry (Coordinator)                                  │
│   - Dynamic server discovery                                │
│   - Health monitoring (30s interval)                        │
│   - Capability aggregation                                  │
│   - Statistics and metrics                                  │
├─────────────────────────────────────────────────────────────┤
│ MCP Client (Consumer) **NEW**                               │
│   - Connect to external MCP servers                         │
│   - Tool execution via JSON-RPC 2.0                         │
│   - Resource access                                         │
│   - Automatic retry with exponential backoff                │
│   - Connection statistics tracking                          │
├─────────────────────────────────────────────────────────────┤
│ MCP Server (Provider)                                       │
│   - Serve tools and resources                               │
│   - Lifecycle management                                    │
│   - Health checks and metrics                               │
│   - Status monitoring                                       │
├─────────────────────────────────────────────────────────────┤
│ Transport Layer                                             │
│   - JSON-RPC 2.0 over stdio (implemented)                   │
│   - HTTP/SSE (placeholder)                                  │
│   - WebSocket (future)                                      │
└─────────────────────────────────────────────────────────────┘
```

## Module Structure

### Core Files

| File            | Purpose                   | Key Types                                        |
| --------------- | ------------------------- | ------------------------------------------------ |
| `mod.rs`        | Module entry point        | Re-exports all public types                      |
| `types.rs`      | MCP protocol types        | `McpRequest`, `McpResponse`, `McpError`          |
| **`client.rs`** | **NEW** MCP client        | `McpClient`, `McpClientConfig`, `McpClientTrait` |
| `server.rs`     | MCP server implementation | `McpServer`, `McpServerTrait`, `ServerStatus`    |
| `registry.rs`   | Server registry           | `McpRegistry`, `HealthCheck`, `HealthStatus`     |
| `transport.rs`  | Transport implementations | `StdioTransport`, `Transport` trait              |
| `tools.rs`      | Tool definitions          | `Tool`, `ToolResult`, `ResourceTemplate`         |
| `lifecycle.rs`  | Lifecycle management      | `InitializeParams`, `InitializeResult`           |

## Quick Start

### MCP Client Usage (NEW)

```rust
use reasonkit_core::mcp::{McpClient, McpClientConfig, McpClientTrait};
use std::collections::HashMap;

#[tokio::main]
async fn main() -> anyhow::Result<()> {
    // Configure client to connect to sequential-thinking MCP server
    let config = McpClientConfig {
        name: "sequential-thinking".to_string(),
        command: "npx".to_string(),
        args: vec![
            "-y".to_string(),
            "@modelcontextprotocol/server-sequential-thinking".to_string()
        ],
        env: HashMap::new(),
        timeout_secs: 30,
        auto_reconnect: true,
        max_retries: 3,
    };

    // Create and connect client
    let mut client = McpClient::new(config);
    client.connect().await?;

    // List available tools
    let tools = client.list_tools().await?;
    for tool in &tools {
        println!("Tool: {} - {}", tool.name,
                 tool.description.as_deref().unwrap_or(""));
    }

    // Call a tool
    let result = client.call_tool(
        "think",
        serde_json::json!({
            "query": "Explain chain-of-thought reasoning",
            "depth": 3
        })
    ).await?;

    println!("Result: {:?}", result);

    // Get client statistics
    let stats = client.stats().await;
    println!("Requests sent: {}", stats.requests_sent);
    println!("Avg response time: {:.2}ms", stats.avg_response_time_ms);

    // Check server health
    let healthy = client.ping().await?;
    println!("Server healthy: {}", healthy);

    // Disconnect
    client.disconnect().await?;

    Ok(())
}
```

### MCP Registry Usage

```rust
use reasonkit_core::mcp::{McpRegistry, McpServer, ServerInfo, ServerCapabilities};
use std::sync::Arc;

#[tokio::main]
async fn main() -> anyhow::Result<()> {
    // Create registry
    let registry = McpRegistry::new();

    // Start background health monitoring
    registry.start_health_monitoring().await;

    // Register servers (example - you'd create actual servers)
    // let server = Arc::new(create_my_server());
    // let id = registry.register_server(server, vec!["thinktool".to_string()]).await?;

    // List all available tools from all servers
    let all_tools = registry.list_all_tools().await?;
    println!("Total tools: {}", all_tools.len());

    // Find servers by tag
    let thinktool_servers = registry.find_servers_by_tag("thinktool").await;
    println!("ThinkTool servers: {}", thinktool_servers.len());

    // Get registry statistics
    let stats = registry.statistics().await;
    println!("Total servers: {}", stats.total_servers);
    println!("Healthy: {}", stats.healthy_servers);
    println!("Degraded: {}", stats.degraded_servers);

    // Stop health monitoring when done
    registry.stop_health_monitoring().await;

    Ok(())
}
```

## MCP Protocol Compliance

Based on MCP specification version: **2025-11-25**

### Implemented Features

- ✅ JSON-RPC 2.0 messaging
- ✅ Lifecycle management (`initialize`, `shutdown`)
- ✅ Tool primitives (`tools/list`, `tools/call`)
- ✅ Resource primitives (`resources/list`, `resources/read`)
- ✅ Prompt primitives (types defined)
- ✅ Notifications
- ✅ Progress tracking (types defined)
- ✅ Cancellation (types defined)
- ✅ Health checks (`ping`)

### Client Features (NEW)

| Feature               | Status | Description                         |
| --------------------- | ------ | ----------------------------------- |
| Connection Management || Connect, disconnect, reconnect      |
| Tool Discovery        || List available tools                |
| Tool Execution        || Call tools with arguments           |
| Resource Access       || List and read resources             |
| Automatic Retry       || Exponential backoff (max 3 retries) |
| Statistics            || Request/response metrics            |
| Health Checks         || Ping with 5s timeout                |
| State Management      || Connection state tracking           |

## Configuration

### Client Configuration (NEW)

```rust
pub struct McpClientConfig {
    /// Server name for identification
    pub name: String,

    /// Command to execute (e.g., "npx", "node", "python")
    pub command: String,

    /// Command arguments
    pub args: Vec<String>,

    /// Environment variables
    pub env: HashMap<String, String>,

    /// Connection timeout (default: 30s)
    pub timeout_secs: u64,

    /// Auto-reconnect on failure (default: true)
    pub auto_reconnect: bool,

    /// Maximum retry attempts (default: 3)
    pub max_retries: u32,
}
```

### Example MCP Server Commands

```bash
# Sequential Thinking Server (Node.js)
npx -y @modelcontextprotocol/server-sequential-thinking

# Filesystem Server (Python)
uvx mcp-server-filesystem /path/to/directory

# GitHub Server (Node.js)
npx -y @modelcontextprotocol/server-github

# Custom ReasonKit ThinkTool Server (Future)
rk-thinktool --module gigathink
```

## Error Handling

### Error Types

All MCP operations return `Result<T, Error>` where errors include:

- `Error::network()` - Network/transport errors
- `Error::validation()` - Schema validation errors
- `Error::NotFound` - Server/resource not found

### Retry Strategy (Client)

The client implements exponential backoff:

1. Initial request (0ms delay)
2. First retry (100ms delay)
3. Second retry (200ms delay)
4. Third retry (400ms delay)
5. Give up, return error

## Connection States

```rust
pub enum ConnectionState {
    Disconnected,  // Not connected
    Connecting,    // Connection in progress
    Connected,     // Connected and initialized
    Failed,        // Connection failed
    Reconnecting,  // Attempting to reconnect
}
```

## Statistics

### Client Statistics (NEW)

```rust
pub struct ClientStats {
    pub requests_sent: u64,
    pub responses_received: u64,
    pub errors_total: u64,
    pub avg_response_time_ms: f64,
    pub uptime_secs: u64,
    pub reconnect_attempts: u32,
    pub last_request_at: Option<DateTime<Utc>>,
}
```

### Registry Statistics

```rust
pub struct RegistryStatistics {
    pub total_servers: usize,
    pub healthy_servers: usize,
    pub degraded_servers: usize,
    pub unhealthy_servers: usize,
    pub unknown_servers: usize,
}
```

## Integration with ThinkTools

The MCP registry is designed to work seamlessly with ReasonKit's ThinkTools:

```rust
// Example: Registering ThinkTool modules as MCP servers

use reasonkit_core::mcp::{
    McpRegistry, ServerInfo, ServerCapabilities, ToolsCapability, Tool
};

#[tokio::main]
async fn main() -> anyhow::Result<()> {
    let registry = McpRegistry::new();

    // Each ThinkTool can be exposed as an MCP server:
    // - GigaThink: Expansive creative thinking
    // - LaserLogic: Precision deductive reasoning
    // - BedRock: First principles decomposition
    // - ProofGuard: Multi-source verification
    // - BrutalHonesty: Adversarial self-critique

    // Tools are automatically discovered via tools/list
    let tools = registry.list_all_tools().await?;

    for tool in tools {
        if let Some(server_name) = tool.server_name {
            println!("ThinkTool: {} from {}", tool.name, server_name);
        }
    }

    Ok(())
}
```

## Testing

```bash
# Run MCP module tests
cargo test --lib mcp

# Run with verbose output
cargo test --lib mcp -- --nocapture

# Run specific client tests
cargo test --lib mcp::client::tests
```

## Performance Considerations

### Client

- **Connection pooling**: Reuse connections when possible
- **Retry backoff**: Exponential backoff prevents server overload
- **Timeout**: 5s health check timeout prevents hanging
- **Statistics**: Minimal overhead, exponential moving average for response time

### Registry

- **Background monitoring**: Runs in separate tokio task
- **Read-write locks**: Concurrent reads, exclusive writes
- **Lazy health checks**: Only on demand or background interval

### Transport

- **Stdio buffering**: Uses `BufReader` for efficient line reading
- **Process management**: Proper cleanup on disconnect
- **JSON streaming**: Line-delimited JSON for easy parsing

## Security

Following CONS-001 (No Node.js):

- ✅ All MCP clients and servers implemented in Rust
- ✅ No Node.js dependencies
- ✅ Memory-safe communication
- ✅ Process isolation via stdio transport
- ✅ Proper error handling without exposing internals

## Future Enhancements

- [ ] HTTP/SSE transport implementation
- [ ] WebSocket transport
- [ ] Connection pooling for clients
- [ ] Load balancing across multiple servers
- [ ] Circuit breaker pattern
- [ ] Rate limiting
- [ ] Authentication/authorization
- [ ] Encryption (TLS for HTTP transport)
- [ ] Server discovery via mDNS/DNS-SD
- [ ] Dynamic capability negotiation

## References

- **MCP Specification**: <https://spec.modelcontextprotocol.io>
- **MCP GitHub**: <https://github.com/modelcontextprotocol>
- **JSON-RPC 2.0**: <https://www.jsonrpc.org/specification>
- **ReasonKit Documentation**: ../../../docs/

## License

Apache 2.0 (consistent with ReasonKit-core)

---

**Version**: 1.0.0
**Last Updated**: 2025-12-23
**Maintainer**: ReasonKit Core Team