keyspoor 0.1.3

Offline secret scanner and Rust library for Git, CI and AI agents, with MCP, JSONL and SARIF
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
//! Read-only MCP 2025-11-25 over newline-delimited stdio JSON-RPC.
//!
//! Protocol references: https://modelcontextprotocol.io/specification/2025-11-25/basic/transports
//! and https://modelcontextprotocol.io/specification/2025-11-25/server/tools.
//! No credential validation, file mutation, or logging to stdout is performed.
use std::io::{self, BufRead, Write};
use std::path::{Path, PathBuf};
use std::sync::{
    Arc, Mutex,
    atomic::{AtomicBool, Ordering},
    mpsc::{self, SyncSender},
};
use std::time::{Duration, Instant};

use anyhow::{Context, Result, ensure};
use serde::Deserialize;
use serde_json::{Value, json};

use crate::scan::{ScanControl, ScanEvent, ScanOptions, scan_buffer, scan_paths_stream};
use crate::{Engine, ScanReport};

const REQUEST_LIMIT: usize = 8 * 1024 * 1024;
const PROTOCOL_VERSION: &str = "2025-11-25";
const RESULT_LIMIT: usize = 100;
const RESULT_BYTES: usize = 512 * 1024;
type Output = Arc<Mutex<io::Stdout>>;

/// Serve until stdin closes. Paths resolve relative to the canonical server root.
/// Traversal does not follow directory symlinks; explicitly requested paths must
/// resolve inside root. This is not an OS sandbox against concurrent file changes.
pub fn serve(engine: &Engine, root: &Path, max_bytes: u64) -> Result<()> {
    let root = root.canonicalize().context("cannot resolve MCP root")?;
    ensure!(root.is_dir(), "MCP root must be a directory");
    let stdin = io::stdin();
    let output = Arc::new(Mutex::new(io::stdout()));
    let mut input = stdin.lock();
    std::thread::scope(|scope| -> Result<()> {
        let (jobs, pending) = mpsc::sync_channel::<Job>(1);
        let worker_output = Arc::clone(&output);
        let worker_root = &root;
        let worker = scope.spawn(move || -> Result<()> {
            for job in pending {
                let result = execute(engine, worker_root, max_bytes, &job, &worker_output);
                // The scan has finished. A subsequent request may now be queued.
                job.done.store(true, Ordering::Release);
                // MCP cancellation has no final response; the internal scan summary
                // remains incomplete, and the connection stays available for reuse.
                if job.cancelled_by_client.load(Ordering::Acquire) {
                    continue;
                }
                let response = match result {
                    Ok(result) => json!({"jsonrpc":"2.0", "id":job.id, "result":result}),
                    Err(_) => error(job.id, -32603, "Scan worker failed"),
                };
                send(&worker_output, &response)?;
            }
            Ok(())
        });
        let mut state = State::New;
        let mut active: Option<Active> = None;
        let reader_result = (|| -> Result<()> {
            while let Some(frame) = read_frame(&mut input)? {
                if active
                    .as_ref()
                    .is_some_and(|scan| scan.done.load(Ordering::Acquire))
                {
                    active = None;
                }
                let response = match frame {
                    Some(bytes) => match serde_json::from_slice::<Value>(&bytes) {
                        Ok(request) => dispatch(&mut state, &mut active, &jobs, request),
                        Err(_) => Some(error(Value::Null, -32700, "Parse error")),
                    },
                    None => Some(error(Value::Null, -32600, "Request exceeds 8 MiB limit")),
                };
                if let Some(response) = response {
                    send(&output, &response)?;
                }
            }
            Ok(())
        })();
        if reader_result.is_err()
            && let Some(active) = &active
        {
            active.control.cancel();
        }
        drop(jobs);
        let worker_result = worker
            .join()
            .map_err(|_| anyhow::anyhow!("MCP scan worker panicked"))?;
        reader_result?;
        worker_result
    })
}

fn send(output: &Output, message: &Value) -> Result<()> {
    let mut writer = output
        .lock()
        .map_err(|_| anyhow::anyhow!("MCP output lock poisoned"))?;
    serde_json::to_writer(&mut *writer, message)?;
    writeln!(writer)?;
    writer.flush()?;
    Ok(())
}

struct Active {
    id: Value,
    control: ScanControl,
    done: Arc<AtomicBool>,
    cancelled_by_client: Arc<AtomicBool>,
}

struct Job {
    id: Value,
    input: ToolInput,
    progress_token: Option<Value>,
    control: ScanControl,
    done: Arc<AtomicBool>,
    cancelled_by_client: Arc<AtomicBool>,
}

enum ToolInput {
    Text(TextArgs),
    Paths(PathsArgs),
}

// Read bounded chunks and drain only the oversized frame, never its successor.
// Outer None means EOF; inner None means a frame exceeded the limit.
fn read_frame(input: &mut impl BufRead) -> io::Result<Option<Option<Vec<u8>>>> {
    let mut frame = Vec::new();
    let mut oversized = false;
    let mut seen = false;
    loop {
        let chunk = input.fill_buf()?;
        if chunk.is_empty() {
            return Ok(seen.then_some((!oversized).then_some(frame)));
        }
        seen = true;
        let end = chunk.iter().position(|&byte| byte == b'\n');
        let take = end.map_or(chunk.len(), |end| end + 1);
        if !oversized {
            if frame.len().saturating_add(take) > REQUEST_LIMIT {
                oversized = true;
                frame.clear();
            } else {
                frame.extend_from_slice(&chunk[..take]);
            }
        }
        input.consume(take);
        if end.is_some() {
            return Ok(Some((!oversized).then_some(frame)));
        }
    }
}

#[derive(PartialEq)]
enum State {
    New,
    Initializing,
    Ready,
}

fn error(id: Value, code: i32, message: &str) -> Value {
    json!({"jsonrpc":"2.0", "id":id, "error":{"code":code,"message":message}})
}

fn dispatch(
    state: &mut State,
    active: &mut Option<Active>,
    jobs: &SyncSender<Job>,
    request: Value,
) -> Option<Value> {
    let Some(object) = request.as_object() else {
        return Some(error(Value::Null, -32600, "Invalid Request"));
    };
    let id = object.get("id").cloned();
    let valid_id = id
        .as_ref()
        .is_none_or(|id| id.is_string() || id.is_i64() || id.is_u64());
    let method = object.get("method").and_then(Value::as_str);
    if object.get("jsonrpc").and_then(Value::as_str) != Some("2.0") || method.is_none() || !valid_id
    {
        return Some(error(Value::Null, -32600, "Invalid Request"));
    }
    let method = method?;
    let params = object.get("params");
    // Notifications never receive a response and never execute tools.
    let Some(id) = id else {
        if method == "notifications/initialized"
            && *state == State::Initializing
            && params.is_none_or(Value::is_object)
        {
            *state = State::Ready;
        }
        if method == "notifications/cancelled"
            && let (Some(scan), Some(request_id)) =
                (active.as_ref(), params.and_then(|p| p.get("requestId")))
            && request_id == &scan.id
        {
            scan.cancelled_by_client.store(true, Ordering::Release);
            scan.control.cancel();
        }
        return None;
    };
    if params.is_some_and(|params| !params.is_object()) {
        return Some(error(id, -32602, "Invalid params"));
    }
    let result = match method {
        "ping" => json!({}),
        "initialize" => {
            if *state != State::New {
                return Some(error(id, -32600, "Already initialized"));
            }
            let valid = params.is_some_and(|params| {
                params["protocolVersion"].is_string()
                    && params["capabilities"].is_object()
                    && params["clientInfo"]["name"].is_string()
                    && params["clientInfo"]["version"].is_string()
            });
            if !valid {
                return Some(error(id, -32602, "Invalid initialize params"));
            }
            *state = State::Initializing;
            json!({
                "protocolVersion":PROTOCOL_VERSION,
                "capabilities":{"tools":{}},
                "serverInfo":{"name":"keyspoor","version":env!("CARGO_PKG_VERSION")}
            })
        }
        "tools/list" | "tools/call" if *state != State::Ready => {
            return Some(error(id, -32000, "Initialization is not complete"));
        }
        "tools/list" => {
            if params.is_some_and(|params| params.get("cursor").is_some()) {
                return Some(error(
                    id,
                    -32602,
                    "Invalid cursor; tool list is not paginated",
                ));
            }
            tools()
        }
        "tools/call" => {
            let Some(params) = params else {
                return Some(error(id, -32602, "Missing tool call params"));
            };
            let Some(name) = params["name"].as_str() else {
                return Some(error(id, -32602, "Missing tool name"));
            };
            if !matches!(name, "scan_text" | "scan_paths") {
                return Some(error(id, -32602, "Unknown tool"));
            }
            let arguments = params
                .get("arguments")
                .cloned()
                .unwrap_or_else(|| json!({}));
            if !arguments.is_object() {
                return Some(error(id, -32602, "Tool arguments must be an object"));
            }
            let input = match name {
                "scan_text" => serde_json::from_value::<TextArgs>(arguments).map(ToolInput::Text),
                _ => serde_json::from_value::<PathsArgs>(arguments).map(ToolInput::Paths),
            };
            let input = match input {
                Ok(input) => input,
                Err(_) => {
                    return Some(tool_error(
                        id,
                        name,
                        "Arguments do not match the tool input schema",
                    ));
                }
            };
            if params.get("_meta").is_some_and(|meta| !meta.is_object()) {
                return Some(error(id, -32602, "Invalid request metadata"));
            }
            let progress_token = params
                .get("_meta")
                .and_then(|meta| meta.get("progressToken"))
                .cloned();
            if progress_token
                .as_ref()
                .is_some_and(|token| !token.is_string() && !token.is_i64() && !token.is_u64())
            {
                return Some(error(id, -32602, "Invalid progress token"));
            }
            if active.is_some() {
                return Some(tool_error(
                    id,
                    name,
                    "A scan is already running; cancel it or wait for its response",
                ));
            }
            let control = ScanControl::default();
            let done = Arc::new(AtomicBool::new(false));
            let cancelled_by_client = Arc::new(AtomicBool::new(false));
            let job = Job {
                id: id.clone(),
                input,
                progress_token,
                control: control.clone(),
                done: Arc::clone(&done),
                cancelled_by_client: Arc::clone(&cancelled_by_client),
            };
            if jobs.send(job).is_err() {
                return Some(error(id, -32603, "Scan worker is unavailable"));
            }
            *active = Some(Active {
                id,
                control,
                done,
                cancelled_by_client,
            });
            return None;
        }
        _ => return Some(error(id, -32601, "Method not found")),
    };
    Some(json!({"jsonrpc":"2.0", "id":id, "result":result}))
}

fn tools() -> Value {
    let annotations = json!({"readOnlyHint":true,"destructiveHint":false,"idempotentHint":true,"openWorldHint":false});
    json!({"tools":[
        {"name":"scan_text", "description":"Scan supplied text offline. Returns a redacted report with at most 100 findings/errors and a shared 512 KiB preview budget. Check complete, finding_count, error_count and output_truncated.",
         "inputSchema":{"type":"object","properties":{"text":{"type":"string"},"path":{"type":"string","description":"Logical filename, not read from disk; defaults to stdin"}},"required":["text"],"additionalProperties":false},
         "annotations":annotations},
        {"name":"scan_paths", "description":"Read files or directories within the configured server root, respecting ignore files. Does not follow directory symlinks. Returns a bounded redacted preview with complete, total counts and output_truncated. Only one scan may run at a time; requests support cancellation and optional progressToken.",
         "inputSchema":{"type":"object","properties":{"paths":{"type":"array","items":{"type":"string"},"minItems":1}},"required":["paths"],"additionalProperties":false},
         "annotations":annotations}
    ]})
}

#[derive(Deserialize)]
#[serde(deny_unknown_fields)]
struct TextArgs {
    text: String,
    #[serde(default = "stdin_name")]
    path: String,
}

fn stdin_name() -> String {
    "stdin".into()
}

#[derive(Deserialize)]
#[serde(deny_unknown_fields)]
struct PathsArgs {
    paths: Vec<PathBuf>,
}

fn failed(tool: &str, message: &str) -> ScanReport {
    let mut report = ScanReport::default();
    report.fail(tool, message);
    report
}

// Counts describe the whole scan, while these bounded arrays are only a preview.
#[derive(Default)]
struct Preview {
    report: ScanReport,
    finding_count: u64,
    error_count: u64,
    bytes: usize,
}

impl Preview {
    fn push(&mut self, event: ScanEvent) -> Result<()> {
        match event {
            ScanEvent::Finding(finding) => {
                self.finding_count += 1;
                if self.report.findings.len() < RESULT_LIMIT {
                    let size = serde_json::to_vec(&finding)?.len();
                    if self.bytes.saturating_add(size) <= RESULT_BYTES {
                        self.bytes += size;
                        self.report.findings.push(finding);
                    }
                }
            }
            ScanEvent::Error(error) => {
                self.error_count += 1;
                self.report.complete = false;
                if self.report.errors.len() < RESULT_LIMIT {
                    let size = serde_json::to_vec(&error)?.len();
                    if self.bytes.saturating_add(size) <= RESULT_BYTES {
                        self.bytes += size;
                        self.report.errors.push(error);
                    }
                }
            }
            ScanEvent::Progress(stats) => self.report.stats = stats,
        }
        Ok(())
    }

    fn from_report(mut report: ScanReport) -> Result<Self> {
        let mut preview = Self::default();
        for finding in std::mem::take(&mut report.findings) {
            preview.push(ScanEvent::Finding(finding))?;
        }
        for error in std::mem::take(&mut report.errors) {
            preview.push(ScanEvent::Error(error))?;
        }
        preview.report.complete = report.complete;
        preview.report.stats = report.stats;
        preview.report.context = report.context;
        Ok(preview)
    }

    fn result(self) -> Result<Value> {
        let is_error = !self.report.complete || self.error_count > 0;
        let truncated = self.finding_count > self.report.findings.len() as u64
            || self.error_count > self.report.errors.len() as u64;
        let mut report = serde_json::to_value(self.report)?;
        report["finding_count"] = json!(self.finding_count);
        report["error_count"] = json!(self.error_count);
        report["output_truncated"] = json!(truncated);
        Ok(
            json!({"content":[{"type":"text","text":report.to_string()}],
            "structuredContent":report,"isError":is_error}),
        )
    }
}

fn tool_error(id: Value, tool: &str, message: &str) -> Value {
    match Preview::from_report(failed(tool, message)).and_then(Preview::result) {
        Ok(result) => json!({"jsonrpc":"2.0", "id":id, "result":result}),
        Err(_) => error(id, -32603, "Cannot serialize scan report"),
    }
}

fn progress(output: &Output, token: &Value, files: u64) -> Result<()> {
    send(
        output,
        &json!({"jsonrpc":"2.0","method":"notifications/progress",
        "params":{"progressToken":token,"progress":files,"message":"Scanning local inputs"}}),
    )
}

fn execute(
    engine: &Engine,
    root: &Path,
    max_bytes: u64,
    job: &Job,
    output: &Output,
) -> Result<Value> {
    if !job.control.is_cancelled()
        && let Some(token) = &job.progress_token
    {
        progress(output, token, 0)?;
    }
    let mut reported_files = 0;
    let mut preview = match &job.input {
        ToolInput::Text(args) => {
            let report = if job.control.is_cancelled() {
                failed("scan_text", "Scan cancelled")
            } else {
                scan_buffer(engine, &args.path, args.text.as_bytes(), max_bytes)
            };
            Preview::from_report(report)?
        }
        ToolInput::Paths(args) => {
            if args.paths.is_empty() {
                return Preview::from_report(failed(
                    "scan_paths",
                    "At least one path is required",
                ))?
                .result();
            }
            let mut paths = Vec::with_capacity(args.paths.len());
            for path in &args.paths {
                if job.control.is_cancelled() {
                    return Preview::from_report(failed("scan_paths", "Scan cancelled"))?.result();
                }
                let candidate = if path.is_absolute() {
                    path.clone()
                } else {
                    root.join(path)
                };
                let Ok(canonical) = candidate.canonicalize() else {
                    return Preview::from_report(failed(
                        "scan_paths",
                        "A requested path cannot be resolved; no paths were scanned",
                    ))?
                    .result();
                };
                if !canonical.starts_with(root) {
                    return Preview::from_report(failed(
                        "scan_paths",
                        "A requested path is outside the server root; no paths were scanned",
                    ))?
                    .result();
                }
                paths.push(canonical);
            }
            let options = ScanOptions {
                max_bytes,
                ..Default::default()
            };
            let mut preview = Preview::default();
            let mut last_progress = Instant::now();
            let summary = scan_paths_stream(engine, &paths, &options, &job.control, &mut |event| {
                if let ScanEvent::Progress(stats) = &event
                    && !job.control.is_cancelled()
                    && stats.files > reported_files
                    && last_progress.elapsed() >= Duration::from_millis(100)
                {
                    if let Some(token) = &job.progress_token {
                        progress(output, token, stats.files)?;
                    }
                    reported_files = stats.files;
                    last_progress = Instant::now();
                }
                preview.push(event)
            });
            match summary {
                Ok(summary) => {
                    preview.report.complete = summary.complete;
                    preview.report.stats = summary.stats;
                    preview.report.context = summary.context;
                    preview.finding_count = summary.finding_count;
                    preview.error_count = summary.error_count;
                }
                Err(_) => {
                    preview.push(ScanEvent::Error(crate::ScanError {
                        path: "scan_paths".into(),
                        message: "Filesystem scan failed; inputs were not fully scanned".into(),
                    }))?;
                }
            }
            preview
        }
    };
    if job.control.is_cancelled() {
        preview.report.complete = false;
        if preview.error_count == 0 {
            preview.push(ScanEvent::Error(crate::ScanError {
                path: "scan".into(),
                message: "Scan cancelled".into(),
            }))?;
        }
    }
    if !job.control.is_cancelled()
        && preview.report.stats.files > reported_files
        && let Some(token) = &job.progress_token
    {
        progress(output, token, preview.report.stats.files)?;
    }
    preview.result()
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn active_scan_accepts_ping_and_cancellation_and_rejects_busy_calls() {
        let mut state = State::Ready;
        let mut active = None;
        let (jobs, pending) = mpsc::sync_channel(1);
        let call = |id| {
            json!({"jsonrpc":"2.0","id":id,"method":"tools/call",
            "params":{"name":"scan_text","arguments":{"text":"safe"}}})
        };
        assert!(dispatch(&mut state, &mut active, &jobs, call(2)).is_none());
        // Hold the queued job instead of racing a worker against fixture I/O.
        let job = pending.recv().unwrap();
        assert!(!job.done.load(Ordering::Acquire));
        let busy = dispatch(&mut state, &mut active, &jobs, call(3)).unwrap();
        assert_eq!(busy["id"], 3);
        assert_eq!(busy["result"]["isError"], true);
        assert_eq!(busy["result"]["structuredContent"]["complete"], false);
        assert!(matches!(pending.try_recv(), Err(mpsc::TryRecvError::Empty)));
        let ping = dispatch(
            &mut state,
            &mut active,
            &jobs,
            json!({"jsonrpc":"2.0","id":4,"method":"ping"}),
        )
        .unwrap();
        assert_eq!(ping["id"], 4);
        assert_eq!(ping["result"], json!({}));
        let cancel = |id| {
            json!({"jsonrpc":"2.0","method":"notifications/cancelled",
            "params":{"requestId":id}})
        };
        assert!(dispatch(&mut state, &mut active, &jobs, cancel(999)).is_none());
        assert!(!job.control.is_cancelled());
        assert!(!job.cancelled_by_client.load(Ordering::Acquire));
        assert!(dispatch(&mut state, &mut active, &jobs, cancel(2)).is_none());
        assert!(job.control.is_cancelled());
        assert!(job.cancelled_by_client.load(Ordering::Acquire));

        let engine = Engine::new(crate::EngineConfig::default()).unwrap();
        let output = Arc::new(Mutex::new(io::stdout()));
        let result = execute(&engine, Path::new("."), 1024, &job, &output).unwrap();
        assert_eq!(result["isError"], true);
        assert_eq!(result["structuredContent"]["complete"], false);
        assert_eq!(
            result["structuredContent"]["errors"][0]["message"],
            "Scan cancelled"
        );
    }
}