textlog 0.1.1

macOS clipboard + OCR daemon exposed to Claude Code as an MCP server
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
//! `textlog__*` MCP tools, exposed to Claude Code via the rmcp stdio
//! server. Each handler is sync-storage wrapped in `spawn_blocking`
//! since `Storage::*` operate on a blocking SQLite connection.

use std::sync::Arc;

use chrono::{DateTime, Utc};
use rmcp::handler::server::tool::ToolRouter;
use rmcp::handler::server::wrapper::{Json, Parameters};
use rmcp::model::{ErrorCode, ErrorData, ServerCapabilities, ServerInfo};
use rmcp::{tool, tool_handler, tool_router, ServerHandler};

use crate::config::schema::OcrConfig;
use crate::error::Error;
use crate::ocr;
use crate::storage::{hex_lower, CaptureRow, Kind, SearchHit, Storage};

use super::schema::{
    CaptureList, CaptureSummary, ClearSinceArgs, ClearSinceResult, GetRecentArgs, KindFilter,
    ListTodayArgs, OcrImageArgs, OcrLatestResult, OcrResult, SearchArgs, SearchResult,
    SearchResults,
};

/// MCP server state — owns the Storage handle and rmcp's tool router.
#[derive(Clone)]
pub struct McpServer {
    storage: Arc<Storage>,
    ocr_cfg: Arc<OcrConfig>,
    tool_router: ToolRouter<Self>,
}

#[tool_router]
impl McpServer {
    pub fn new(storage: Arc<Storage>) -> Self {
        Self::with_ocr(storage, OcrConfig::default())
    }

    pub fn with_ocr(storage: Arc<Storage>, ocr_cfg: OcrConfig) -> Self {
        Self {
            storage,
            ocr_cfg: Arc::new(ocr_cfg),
            tool_router: Self::tool_router(),
        }
    }

    /// Most recent N captures, deduplicated by sha256.
    #[tool(
        name = "textlog__get_recent",
        description = "Fetch the N most recent clipboard captures (deduplicated by SHA-256). \
                       For images, the `text` field carries the OCR result captured at the time."
    )]
    pub async fn get_recent(
        &self,
        Parameters(args): Parameters<GetRecentArgs>,
    ) -> Result<Json<CaptureList>, ErrorData> {
        let storage = Arc::clone(&self.storage);
        let kind = filter_to_kind(args.kind);
        let n = args.n;
        let rows = blocking(move || storage.get_recent(n, kind)).await?;
        Ok(Json(CaptureList {
            captures: rows.into_iter().map(capture_summary).collect(),
        }))
    }

    /// Today's captures (UTC midnight cutoff), deduplicated by sha256.
    #[tool(
        name = "textlog__list_today",
        description = "Return every capture from today (UTC), deduplicated by SHA-256."
    )]
    pub async fn list_today(
        &self,
        Parameters(args): Parameters<ListTodayArgs>,
    ) -> Result<Json<CaptureList>, ErrorData> {
        let storage = Arc::clone(&self.storage);
        let kind = filter_to_kind(args.kind);
        let cutoff = today_midnight_utc();
        let rows = blocking(move || storage.get_recent(u32::MAX, kind)).await?;
        let captures = rows
            .into_iter()
            .filter(|r| r.ts >= cutoff)
            .map(capture_summary)
            .collect();
        Ok(Json(CaptureList { captures }))
    }

    /// FTS5 search over the SQLite index.
    #[tool(
        name = "textlog__search",
        description = "Full-text search over captured content (FTS5 syntax). \
                       Hits sharing a SHA-256 with an earlier hit in the result set \
                       are marked with `duplicate_of` so the body can be elided."
    )]
    pub async fn search(
        &self,
        Parameters(args): Parameters<SearchArgs>,
    ) -> Result<Json<SearchResults>, ErrorData> {
        let storage = Arc::clone(&self.storage);
        let SearchArgs { query, limit, since } = args;
        let since_dt = parse_since(since.as_deref())?;
        let hits = blocking(move || storage.search(&query, limit, since_dt)).await?;
        Ok(Json(SearchResults {
            hits: hits.into_iter().map(search_result).collect(),
        }))
    }

    /// OCR text from the most recent image capture (no fresh OCR call).
    #[tool(
        name = "textlog__ocr_latest",
        description = "Return the OCR text recorded for the most recent image capture, \
                       or null fields if no image has been captured yet."
    )]
    pub async fn ocr_latest(&self) -> Result<Json<OcrLatestResult>, ErrorData> {
        let storage = Arc::clone(&self.storage);
        let row = blocking(move || storage.get_latest_image()).await?;
        Ok(Json(match row {
            Some(r) => OcrLatestResult {
                text: r.content,
                confidence: r.ocr_confidence,
                captured_at: Some(r.ts.to_rfc3339()),
            },
            None => OcrLatestResult {
                text: None,
                confidence: None,
                captured_at: None,
            },
        }))
    }

    /// Privacy cut-off — drop SQLite rows at or after `ts`.
    #[tool(
        name = "textlog__clear_since",
        description = "Delete every capture row with `ts >= ts` (ISO 8601). \
                       Daily Markdown files on disk are not modified."
    )]
    pub async fn clear_since(
        &self,
        Parameters(args): Parameters<ClearSinceArgs>,
    ) -> Result<Json<ClearSinceResult>, ErrorData> {
        let storage = Arc::clone(&self.storage);
        let ts = parse_iso8601(&args.ts)?;
        let deleted = blocking(move || storage.clear_since(ts)).await?;
        Ok(Json(ClearSinceResult { deleted_count: deleted }))
    }

    /// Ad-hoc OCR of an image file outside the clipboard stream.
    #[tool(
        name = "textlog__ocr_image",
        description = "Run Apple Vision OCR on the image file at `path` (absolute path). \
                       Returns the recognized text, mean block confidence, and block count."
    )]
    pub async fn ocr_image(
        &self,
        Parameters(args): Parameters<OcrImageArgs>,
    ) -> Result<Json<OcrResult>, ErrorData> {
        let cfg = Arc::clone(&self.ocr_cfg);
        let path = args.path;
        let res = tokio::task::spawn_blocking(move || -> crate::error::Result<_> {
            let bytes = std::fs::read(&path)?;
            ocr::ocr_image(&bytes, &cfg)
        })
        .await
        .map_err(|e| {
            ErrorData::new(
                ErrorCode::INTERNAL_ERROR,
                format!("ocr task join error: {e}"),
                None,
            )
        })?
        .map_err(storage_error_to_data)?;
        Ok(Json(OcrResult {
            text: res.text,
            confidence: res.confidence,
            block_count: res.block_count,
        }))
    }
}

#[tool_handler]
impl ServerHandler for McpServer {
    fn get_info(&self) -> ServerInfo {
        // ServerInfo and Implementation are #[non_exhaustive] — mutate
        // a Default rather than construct via struct expression.
        let mut info = ServerInfo::default();
        info.capabilities = ServerCapabilities::builder().enable_tools().build();
        info.instructions = Some(
            "textlog: clipboard + OCR archive accessed via textlog__* tools. \
             Use textlog__get_recent for the latest items, textlog__search for FTS5 lookup, \
             textlog__ocr_latest for the last image's OCR text."
                .into(),
        );
        info
    }
}

// ---- helpers ---------------------------------------------------------

fn filter_to_kind(filter: Option<KindFilter>) -> Option<Kind> {
    match filter {
        None | Some(KindFilter::Any) => None,
        Some(KindFilter::Text) => Some(Kind::Text),
        Some(KindFilter::Image) => Some(Kind::Image),
    }
}

fn capture_summary(row: CaptureRow) -> CaptureSummary {
    CaptureSummary {
        id: row.id,
        ts: row.ts.to_rfc3339(),
        kind: row.kind.as_str().to_string(),
        sha256: hex_lower(&row.sha256),
        size_bytes: row.size_bytes,
        text: row.content,
        md_path: row.md_path.to_string_lossy().into_owned(),
        source_app: row.source_app,
        source_url: row.source_url,
        ocr_confidence: row.ocr_confidence,
    }
}

fn search_result(hit: SearchHit) -> SearchResult {
    SearchResult {
        duplicate_of: hit.duplicate_of,
        capture: capture_summary(hit.row),
    }
}

fn today_midnight_utc() -> DateTime<Utc> {
    let now = Utc::now();
    now.date_naive()
        .and_hms_opt(0, 0, 0)
        .expect("midnight is a valid time")
        .and_utc()
}

fn parse_since(since: Option<&str>) -> Result<Option<DateTime<Utc>>, ErrorData> {
    since.map(parse_iso8601).transpose()
}

fn parse_iso8601(s: &str) -> Result<DateTime<Utc>, ErrorData> {
    DateTime::parse_from_rfc3339(s)
        .map(|dt| dt.with_timezone(&Utc))
        .map_err(|e| {
            ErrorData::new(
                ErrorCode::INVALID_PARAMS,
                format!("invalid ISO 8601 timestamp {s:?}: {e}"),
                None,
            )
        })
}

/// Move sync storage work off the executor thread.
async fn blocking<T, F>(f: F) -> Result<T, ErrorData>
where
    T: Send + 'static,
    F: FnOnce() -> crate::error::Result<T> + Send + 'static,
{
    tokio::task::spawn_blocking(f)
        .await
        .map_err(|e| {
            ErrorData::new(
                ErrorCode::INTERNAL_ERROR,
                format!("storage task join error: {e}"),
                None,
            )
        })?
        .map_err(storage_error_to_data)
}

fn storage_error_to_data(e: Error) -> ErrorData {
    let code = match e {
        Error::Storage(_) | Error::Sqlite(_) | Error::Io(_) => ErrorCode::INTERNAL_ERROR,
        _ => ErrorCode::INTERNAL_ERROR,
    };
    ErrorData::new(code, e.to_string(), None)
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::storage::CaptureRow;
    use chrono::TimeZone;
    use std::path::Path;
    use tempfile::TempDir;

    fn server_with_storage() -> (McpServer, TempDir) {
        let tmp = TempDir::new().unwrap();
        let storage = Storage::open_in_memory(100).unwrap();
        (McpServer::new(Arc::new(storage)), tmp)
    }

    fn row(
        ts: DateTime<Utc>,
        kind: Kind,
        sha: u8,
        content: Option<&str>,
        md_dir: &Path,
    ) -> CaptureRow {
        CaptureRow {
            id: 0,
            ts,
            kind,
            sha256: [sha; 32],
            size_bytes: content.map(|c| c.len()).unwrap_or(0),
            content: content.map(String::from),
            ocr_confidence: matches!(kind, Kind::Image).then_some(0.9),
            source_app: None,
            source_url: None,
            md_path: md_dir.join("2026-04-17.md"),
        }
    }

    #[tokio::test]
    async fn get_recent_returns_empty_when_no_captures() {
        let (server, _tmp) = server_with_storage();
        let res = server
            .get_recent(Parameters(GetRecentArgs { n: 5, kind: None }))
            .await
            .unwrap();
        assert!(res.0.captures.is_empty());
    }

    #[tokio::test]
    async fn get_recent_returns_summary_with_kind_filter() {
        let (server, tmp) = server_with_storage();
        let now = Utc::now();
        server
            .storage
            .insert(&row(now, Kind::Text, 1, Some("text 1"), tmp.path()))
            .unwrap();
        server
            .storage
            .insert(&row(
                now + chrono::Duration::seconds(1),
                Kind::Image,
                2,
                Some("ocr text"),
                tmp.path(),
            ))
            .unwrap();

        let images = server
            .get_recent(Parameters(GetRecentArgs {
                n: 10,
                kind: Some(KindFilter::Image),
            }))
            .await
            .unwrap();
        let cs = &images.0.captures;
        assert_eq!(cs.len(), 1);
        assert_eq!(cs[0].kind, "image");
        assert_eq!(cs[0].text.as_deref(), Some("ocr text"));
        assert!(cs[0].sha256.starts_with("0202"));
    }

    #[tokio::test]
    async fn get_recent_kind_any_returns_all() {
        let (server, tmp) = server_with_storage();
        let now = Utc::now();
        server.storage.insert(&row(now, Kind::Text, 1, Some("a"), tmp.path())).unwrap();
        server.storage.insert(&row(now + chrono::Duration::seconds(1), Kind::Image, 2, Some("b"), tmp.path())).unwrap();

        let res = server
            .get_recent(Parameters(GetRecentArgs {
                n: 10,
                kind: Some(KindFilter::Any),
            }))
            .await
            .unwrap();
        assert_eq!(res.0.captures.len(), 2);
    }

    #[tokio::test]
    async fn search_returns_hits_with_duplicate_of() {
        let (server, tmp) = server_with_storage();
        let base = Utc.with_ymd_and_hms(2026, 4, 17, 10, 0, 0).unwrap();
        // Two rows, same sha256, both contain "needle".
        server
            .storage
            .insert(&row(base, Kind::Text, 7, Some("needle alpha"), tmp.path()))
            .unwrap();
        server
            .storage
            .insert(&row(
                base + chrono::Duration::seconds(60),
                Kind::Text,
                7,
                Some("needle alpha"),
                tmp.path(),
            ))
            .unwrap();

        let res = server
            .search(Parameters(SearchArgs {
                query: "needle".into(),
                limit: 10,
                since: None,
            }))
            .await
            .unwrap();
        let hits = &res.0.hits;
        assert_eq!(hits.len(), 2);
        assert_eq!(hits[0].duplicate_of, None, "first hit is canonical");
        assert!(hits[1].duplicate_of.is_some(), "second hit points back");
    }

    #[tokio::test]
    async fn search_rejects_invalid_since() {
        let (server, _tmp) = server_with_storage();
        let err = server
            .search(Parameters(SearchArgs {
                query: "x".into(),
                limit: 5,
                since: Some("not a date".into()),
            }))
            .await
            .err()
            .expect("expected an error");
        assert_eq!(err.code, ErrorCode::INVALID_PARAMS);
    }

    #[tokio::test]
    async fn ocr_latest_returns_null_when_no_image() {
        let (server, _tmp) = server_with_storage();
        let res = server.ocr_latest().await.unwrap();
        assert!(res.0.text.is_none());
        assert!(res.0.captured_at.is_none());
    }

    #[tokio::test]
    async fn ocr_latest_returns_text_from_latest_image() {
        let (server, tmp) = server_with_storage();
        let base = Utc.with_ymd_and_hms(2026, 4, 17, 10, 0, 0).unwrap();
        server
            .storage
            .insert(&row(base, Kind::Image, 1, Some("first ocr"), tmp.path()))
            .unwrap();
        server
            .storage
            .insert(&row(
                base + chrono::Duration::seconds(60),
                Kind::Image,
                2,
                Some("latest ocr"),
                tmp.path(),
            ))
            .unwrap();

        let res = server.ocr_latest().await.unwrap();
        assert_eq!(res.0.text.as_deref(), Some("latest ocr"));
        assert!(res.0.confidence.is_some());
        assert!(res.0.captured_at.is_some());
    }

    #[tokio::test]
    async fn clear_since_returns_count() {
        let (server, tmp) = server_with_storage();
        let base = Utc.with_ymd_and_hms(2026, 4, 17, 10, 0, 0).unwrap();
        server
            .storage
            .insert(&row(base, Kind::Text, 1, Some("before"), tmp.path()))
            .unwrap();
        server
            .storage
            .insert(&row(
                base + chrono::Duration::seconds(60),
                Kind::Text,
                2,
                Some("after"),
                tmp.path(),
            ))
            .unwrap();

        let cutoff = (base + chrono::Duration::seconds(30)).to_rfc3339();
        let res = server
            .clear_since(Parameters(ClearSinceArgs { ts: cutoff }))
            .await
            .unwrap();
        assert_eq!(res.0.deleted_count, 1, "only the row at +60s deleted");
    }

    #[tokio::test]
    async fn clear_since_rejects_invalid_ts() {
        let (server, _tmp) = server_with_storage();
        let err = server
            .clear_since(Parameters(ClearSinceArgs {
                ts: "yesterday".into(),
            }))
            .await
            .err()
            .expect("expected an error");
        assert_eq!(err.code, ErrorCode::INVALID_PARAMS);
    }

    #[tokio::test]
    async fn list_today_returns_only_todays_rows() {
        let (server, tmp) = server_with_storage();
        // Insert one row from yesterday and one from today.
        let yesterday = Utc::now() - chrono::Duration::days(1);
        let today = Utc::now();
        server
            .storage
            .insert(&row(yesterday, Kind::Text, 1, Some("old"), tmp.path()))
            .unwrap();
        server
            .storage
            .insert(&row(today, Kind::Text, 2, Some("fresh"), tmp.path()))
            .unwrap();

        let res = server
            .list_today(Parameters(ListTodayArgs { kind: None }))
            .await
            .unwrap();
        let cs = &res.0.captures;
        assert_eq!(cs.len(), 1);
        assert_eq!(cs[0].text.as_deref(), Some("fresh"));
    }

    #[tokio::test]
    async fn ocr_image_returns_io_error_for_missing_path() {
        let (server, tmp) = server_with_storage();
        let missing = tmp.path().join("does-not-exist.png");
        let err = server
            .ocr_image(Parameters(OcrImageArgs {
                path: missing.to_string_lossy().into_owned(),
            }))
            .await
            .err()
            .expect("expected an error");
        // std::fs::read on a missing file → Error::Io → INTERNAL_ERROR.
        let msg = format!("{}", err.message);
        assert!(msg.contains("I/O") || msg.contains("No such"), "got: {msg}");
    }

    #[test]
    fn server_info_advertises_tools_capability() {
        let (server, _tmp) = server_with_storage();
        let info = server.get_info();
        assert!(info.capabilities.tools.is_some());
        assert!(info.instructions.unwrap().contains("textlog"));
    }
}