skill-cli 0.3.0

Command-line interface for the Skill runtime - install, run, and manage AI agent skills
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
//! MCP Integration Tests for Claude Bridge Context Engineering
//!
//! These tests validate the MCP (Model Context Protocol) server implementation,
//! focusing on context engineering features that help AI agents manage output
//! size and extract relevant information from skill executions.
//!
//! # Overview
//!
//! The MCP server exposes skills as tools that can be executed by AI agents like
//! Claude. These tests validate the complete JSON-RPC 2.0 protocol implementation
//! including initialization, tool discovery, tool execution, and context
//! engineering features.
//!
//! # Test Framework
//!
//! - `McpTestServer`: Manages MCP server process lifecycle
//!   - Spawns `skill serve` command as subprocess
//!   - Handles JSON-RPC communication via stdin/stdout
//!   - Automatic cleanup via `kill_on_drop`
//!   - Timeout protection for hanging operations
//! - JSON-RPC 2.0 protocol compliance validation
//! - Async test execution with tokio runtime
//!
//! # Running Tests
//!
//! Tests are marked `#[ignore]` because they require:
//! - A built `skill` binary
//! - A running Kubernetes cluster (for kubernetes skill tests)
//!
//! ```bash
//! # Build the skill binary first
//! cargo build --bin skill
//!
//! # Run all MCP tests
//! cargo test --test mcp_claude_bridge_tests -- --ignored
//!
//! # Run a specific test
//! cargo test test_mcp_tool_execution -- --ignored --nocapture
//!
//! # Compile tests without running
//! cargo test --test mcp_claude_bridge_tests --no-run
//! ```
//!
//! # MCP Protocol Reference
//!
//! - **Specification**: MCP 2024-11-05
//! - **Transport**: stdio (JSON-RPC over stdin/stdout)
//! - **Format**: JSON-RPC 2.0
//! - **Methods**:
//!   - `initialize`: Protocol handshake
//!   - `tools/list`: List available tools
//!   - `tools/call`: Execute a tool
//!
//! ## Protocol Flow
//!
//! 1. Client sends `initialize` request with protocol version
//! 2. Server responds with capabilities and server info
//! 3. Client sends `notifications/initialized` notification
//! 4. Client can now call `tools/list` and `tools/call`
//!
//! # Context Engineering Features Tested
//!
//! Context engineering helps AI agents manage token limits and extract relevant
//! information from potentially large tool outputs:
//!
//! - **`grep`**: Filter output lines by regex pattern (like grep command)
//! - **`head`**: Limit output to first N lines (like head -n N)
//! - **`tail`**: Limit output to last N lines (like tail -n N)
//! - **`jq`**: Extract data from JSON output using jq expressions
//! - **`max_output`**: Truncate output to maximum characters with smart truncation
//!
//! # Test Coverage
//!
//! ## Basic Protocol Tests
//! - `test_mcp_tool_execution`: Validates initialization and basic tool execution
//!
//! ## Context Engineering Tests
//! - `test_mcp_context_engineering_grep`: Validates grep filtering
//! - `test_mcp_context_engineering_head`: Validates head line limiting
//! - `test_mcp_context_engineering_jq`: Validates jq JSON extraction
//! - `test_mcp_context_engineering_max_output`: Validates output truncation
//!
//! ## Error Handling Tests
//! - `test_mcp_error_invalid_skill`: Non-existent skill error
//! - `test_mcp_error_invalid_tool`: Non-existent tool error
//! - `test_mcp_error_missing_params`: Missing required parameter error
//!
//! # Implementation Details
//!
//! ## McpTestServer
//!
//! The test server manages a child process running `skill serve`:
//!
//! - **Process Management**: Uses `tokio::process::Command` with piped stdio
//! - **Communication**: Async stdin writes and stdout reads via `tokio::io`
//! - **Timeout**: All operations wrapped in `tokio::time::timeout` (10s default)
//! - **Cleanup**: Process killed automatically via `kill_on_drop = true`
//!
//! ## Request/Response Pattern
//!
//! ```rust,ignore
//! // 1. Initialize server
//! let mut server = McpTestServer::new().await?;
//! server.initialize().await?;
//!
//! // 2. Execute tool with context engineering
//! let response = server.execute_tool(
//!     "kubernetes",
//!     "get",
//!     json!({"resource": "pods"}),
//!     Some(json!({"grep": "Running", "head": 10}))
//! ).await?;
//!
//! // 3. Validate response
//! assert_eq!(response["jsonrpc"], "2.0");
//! assert!(response["result"]["content"][0]["text"].as_str().unwrap().contains("Running"));
//! ```
//!
//! # Troubleshooting
//!
//! ## Tests Hanging
//! - Check if kubernetes cluster is running: `kubectl cluster-info`
//! - Verify skill binary exists: `which skill` or `cargo build --bin skill`
//! - Increase timeout if cluster is slow
//!
//! ## Parse Errors
//! - Server stderr is suppressed (`.stderr(Stdio::null())`)
//! - Run `skill serve` manually to see error messages
//! - Check JSON-RPC format is correct
//!
//! ## Process Leaks
//! - Ensure tests don't panic before cleanup
//! - Use `kill_on_drop = true` to ensure cleanup
//! - Check for zombie processes: `ps aux | grep skill`

use serde_json::{json, Value};
use std::io::Write;
use std::process::Stdio;
use std::time::Duration;
use tokio::io::{AsyncBufReadExt, AsyncWriteExt, BufReader};
use tokio::process::{Child, Command};
use tokio::time::timeout;

/// MCP Test Server - manages MCP server process and JSON-RPC communication
///
/// This struct spawns the `skill serve` command and provides methods for
/// sending JSON-RPC requests and receiving responses over stdin/stdout.
///
/// # Example
///
/// ```no_run
/// let mut server = McpTestServer::new().await.unwrap();
/// server.initialize().await.unwrap();
/// let response = server.execute_tool("kubernetes", "get", json!({"resource": "pods"})).await.unwrap();
/// ```
pub struct McpTestServer {
    process: Child,
    next_id: i64,
}

impl McpTestServer {
    /// Spawn a new MCP server process
    ///
    /// Starts `skill serve` with stdin/stdout piped for JSON-RPC communication.
    /// The server process will be automatically cleaned up when this struct is dropped.
    pub async fn new() -> anyhow::Result<Self> {
        let process = Command::new(env!("CARGO_BIN_EXE_skill"))
            .arg("serve")
            .stdin(Stdio::piped())
            .stdout(Stdio::piped())
            .stderr(Stdio::null()) // Suppress stderr noise in tests
            .kill_on_drop(true)
            .spawn()?;

        Ok(Self {
            process,
            next_id: 1,
        })
    }

    /// Send a JSON-RPC request and read the response
    ///
    /// # Arguments
    ///
    /// * `request` - JSON-RPC request object
    ///
    /// # Returns
    ///
    /// The JSON-RPC response as a `Value`
    async fn send_request(&mut self, request: Value) -> anyhow::Result<Value> {
        // Get stdin and stdout handles
        let stdin = self
            .process
            .stdin
            .as_mut()
            .ok_or_else(|| anyhow::anyhow!("Failed to get stdin"))?;
        let stdout = self
            .process
            .stdout
            .as_mut()
            .ok_or_else(|| anyhow::anyhow!("Failed to get stdout"))?;

        // Send request
        let request_str = serde_json::to_string(&request)? + "\n";
        stdin.write_all(request_str.as_bytes()).await?;
        stdin.flush().await?;

        // Read response with timeout
        let mut reader = BufReader::new(stdout);
        let mut line = String::new();
        timeout(Duration::from_secs(10), reader.read_line(&mut line)).await??;

        // Parse and return response
        let response: Value = serde_json::from_str(&line)?;
        Ok(response)
    }

    /// Send MCP initialize request
    ///
    /// Must be called before any other MCP operations.
    /// Performs the MCP handshake with protocol version 2024-11-05.
    pub async fn initialize(&mut self) -> anyhow::Result<Value> {
        let request = json!({
            "jsonrpc": "2.0",
            "method": "initialize",
            "id": self.next_id,
            "params": {
                "protocolVersion": "2024-11-05",
                "capabilities": {},
                "clientInfo": {
                    "name": "test-client",
                    "version": "1.0.0"
                }
            }
        });
        self.next_id += 1;

        let response = self.send_request(request).await?;

        // Send initialized notification
        let notification = json!({
            "jsonrpc": "2.0",
            "method": "notifications/initialized"
        });

        let notification_str = serde_json::to_string(&notification)? + "\n";
        let stdin = self
            .process
            .stdin
            .as_mut()
            .ok_or_else(|| anyhow::anyhow!("Failed to get stdin"))?;
        stdin.write_all(notification_str.as_bytes()).await?;
        stdin.flush().await?;

        // Give server time to process notification
        tokio::time::sleep(Duration::from_millis(100)).await;

        Ok(response)
    }

    /// List available MCP tools
    pub async fn list_tools(&mut self) -> anyhow::Result<Value> {
        let request = json!({
            "jsonrpc": "2.0",
            "method": "tools/list",
            "id": self.next_id,
            "params": {}
        });
        self.next_id += 1;

        self.send_request(request).await
    }

    /// Execute a skill tool via MCP
    ///
    /// # Arguments
    ///
    /// * `skill` - Skill name (e.g., "kubernetes")
    /// * `tool` - Tool name (e.g., "get")
    /// * `args` - Tool arguments as JSON object
    /// * `context_opts` - Optional context engineering options (grep, head, jq, etc.)
    pub async fn execute_tool(
        &mut self,
        skill: &str,
        tool: &str,
        args: Value,
        context_opts: Option<Value>,
    ) -> anyhow::Result<Value> {
        let mut arguments = json!({
            "skill": skill,
            "tool": tool,
            "args": args
        });

        // Merge context engineering options if provided
        if let Some(opts) = context_opts {
            if let (Some(args_obj), Some(opts_obj)) = (arguments.as_object_mut(), opts.as_object())
            {
                for (key, value) in opts_obj {
                    args_obj.insert(key.clone(), value.clone());
                }
            }
        }

        let request = json!({
            "jsonrpc": "2.0",
            "method": "tools/call",
            "id": self.next_id,
            "params": {
                "name": "execute",
                "arguments": arguments
            }
        });
        self.next_id += 1;

        self.send_request(request).await
    }
}

// ============================================================================
// Integration Tests
// ============================================================================

#[tokio::test]
#[ignore] // Requires skill binary to be built and kubernetes cluster available
async fn test_mcp_tool_execution() {
    let mut server = McpTestServer::new().await.unwrap();

    // Initialize MCP protocol
    let init_response = server.initialize().await.unwrap();
    assert_eq!(init_response["jsonrpc"], "2.0");
    assert!(init_response["result"]["protocolVersion"]
        .as_str()
        .unwrap()
        .starts_with("2024"));

    // Execute kubernetes:get tool
    let response = server
        .execute_tool(
            "kubernetes",
            "get",
            json!({"resource": "namespaces"}),
            None,
        )
        .await
        .unwrap();

    assert_eq!(response["jsonrpc"], "2.0");
    assert!(response.get("result").is_some());
    assert!(response.get("error").is_none());
}

#[tokio::test]
#[ignore] // Requires skill binary to be built and kubernetes cluster available
async fn test_mcp_context_engineering_grep() {
    let mut server = McpTestServer::new().await.unwrap();
    server.initialize().await.unwrap();

    // Execute with grep filter
    let response = server
        .execute_tool(
            "kubernetes",
            "get",
            json!({"resource": "namespaces"}),
            Some(json!({"grep": "default"})),
        )
        .await
        .unwrap();

    assert_eq!(response["jsonrpc"], "2.0");
    assert!(response.get("result").is_some());

    // Verify grep filtered the output
    let result_str = response["result"]["content"][0]["text"]
        .as_str()
        .unwrap();
    assert!(
        result_str.contains("default"),
        "Grep filter should include 'default'"
    );
}

#[tokio::test]
#[ignore] // Requires skill binary to be built and kubernetes cluster available
async fn test_mcp_context_engineering_head() {
    let mut server = McpTestServer::new().await.unwrap();
    server.initialize().await.unwrap();

    // Execute with head limit
    let response = server
        .execute_tool(
            "kubernetes",
            "get",
            json!({"resource": "namespaces"}),
            Some(json!({"head": 3})),
        )
        .await
        .unwrap();

    assert_eq!(response["jsonrpc"], "2.0");
    assert!(response.get("result").is_some());

    // Verify output is limited
    let result_str = response["result"]["content"][0]["text"]
        .as_str()
        .unwrap();
    let line_count = result_str.lines().count();
    assert!(
        line_count <= 3,
        "Head should limit output to 3 lines, got {}",
        line_count
    );
}

#[tokio::test]
#[ignore] // Requires skill binary to be built and kubernetes cluster available
async fn test_mcp_context_engineering_jq() {
    let mut server = McpTestServer::new().await.unwrap();
    server.initialize().await.unwrap();

    // Execute with JSON output and jq extraction
    let response = server
        .execute_tool(
            "kubernetes",
            "get",
            json!({"resource": "namespaces", "output": "json"}),
            Some(json!({"jq": ".items[].metadata.name"})),
        )
        .await
        .unwrap();

    assert_eq!(response["jsonrpc"], "2.0");
    assert!(response.get("result").is_some());

    // Verify jq extracted namespace names
    let result_str = response["result"]["content"][0]["text"]
        .as_str()
        .unwrap();
    assert!(
        result_str.contains("default") || result_str.contains("kube-"),
        "JQ should extract namespace names"
    );
}

#[tokio::test]
#[ignore] // Requires skill binary to be built and kubernetes cluster available
async fn test_mcp_context_engineering_max_output() {
    let mut server = McpTestServer::new().await.unwrap();
    server.initialize().await.unwrap();

    // Execute with max_output truncation
    let response = server
        .execute_tool(
            "kubernetes",
            "get",
            json!({"resource": "pods", "all-namespaces": "true"}),
            Some(json!({"max_output": 500})),
        )
        .await
        .unwrap();

    assert_eq!(response["jsonrpc"], "2.0");
    assert!(response.get("result").is_some());

    // Verify output is truncated
    let result_str = response["result"]["content"][0]["text"]
        .as_str()
        .unwrap();
    assert!(
        result_str.len() <= 600, // Some buffer for metadata
        "Max output should truncate to ~500 chars, got {}",
        result_str.len()
    );
}

#[tokio::test]
#[ignore] // Requires skill binary to be built
async fn test_mcp_error_invalid_skill() {
    let mut server = McpTestServer::new().await.unwrap();
    server.initialize().await.unwrap();

    // Execute with non-existent skill
    let response = server
        .execute_tool("nonexistent_skill_xyz", "get", json!({}), None)
        .await
        .unwrap();

    assert_eq!(response["jsonrpc"], "2.0");
    assert!(response.get("error").is_some());
    assert!(response["error"]["message"]
        .as_str()
        .unwrap()
        .contains("skill"));
}

#[tokio::test]
#[ignore] // Requires skill binary to be built and kubernetes cluster available
async fn test_mcp_error_invalid_tool() {
    let mut server = McpTestServer::new().await.unwrap();
    server.initialize().await.unwrap();

    // Execute with non-existent tool
    let response = server
        .execute_tool("kubernetes", "nonexistent_tool_xyz", json!({}), None)
        .await
        .unwrap();

    assert_eq!(response["jsonrpc"], "2.0");
    assert!(response.get("error").is_some());
}

#[tokio::test]
#[ignore] // Requires skill binary to be built and kubernetes cluster available
async fn test_mcp_error_missing_params() {
    let mut server = McpTestServer::new().await.unwrap();
    server.initialize().await.unwrap();

    // Execute kubernetes:get without required 'resource' parameter
    let response = server
        .execute_tool("kubernetes", "get", json!({}), None)
        .await
        .unwrap();

    assert_eq!(response["jsonrpc"], "2.0");
    assert!(
        response.get("error").is_some() || response["result"]["content"][0]["text"]
            .as_str()
            .unwrap()
            .contains("error"),
        "Should return error for missing required parameter"
    );
}