snyvi 1.7.1

A fast, beautiful viewer for the documents your agents produce
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
//! A stdio MCP server exposing send_document, send_aside for the rare line
//! beside the work, and -- only to an agent running in a desk's pane --
//! read_desk_notes, which reads that desk's list, tick_desk_note, the one
//! change an agent can make to it: marking a line done, and name_panel, which
//! names the panel the agent runs in.
//! Newline-delimited JSON-RPC 2.0, as the MCP stdio transport specifies.

use crate::client;
use crate::config::Paths;
use crate::receive::Payload;
use serde_json::{json, Value};
use std::io::{self, BufRead, Write};

const TOOL_DESCRIPTION: &str = "Send a finished document to snyvi, the user's document viewer. \
Call this whenever you finish writing a plan, report, review, summary, design note, or any document the user \
will want to read, and whenever the user asks to see a file. Prefer `path` for files you wrote to disk; use \
`content` for text that is not a file (a review, a summary, a diff). Markdown and every kind of source file \
are supported. The document arrives at once and waits in the viewer to be read. The result says how to tell \
the user where it is: when snyvi has its own window open it is already there and a link would only send them \
to a browser beside it, so say it is waiting in snyvi; otherwise give them the url the result carries.";

const ASIDE_DESCRIPTION: &str = "Leave the user a short personal aside in snyvi -- the kind of remark a friend \
working beside them would make about the work they are in: that a hard part just landed, that the thing they \
worried about turned out fine, that this closes what they set out to do today, or a gentle nudge after a long \
stretch. It glows quietly at the foot of snyvi's sidebar until they look. Use it rarely -- a few times in a \
long session at most, only when you have something genuinely worth saying, never as a status update or a \
summary of a document you just sent. One or two plain sentences (at most 280 characters), warm and specific, \
no emoji. It is not a to-do and goes on no list of the user's. Do not mention the aside to the user in your \
reply; it speaks for itself.";

const DESK_NOTES_DESCRIPTION: &str = "Read the user's own notes for the snyvi desk this session is running \
in: the short list they keep beside their panes of what is open and what is done, each with its id. Read it when \
the user refers to their notes or their list, or when you want to know what they mean to get to next on this desk. \
You cannot add, edit or remove a note, and nothing you do puts one there -- if something belongs on the list, say \
so and the user will write it; the one change you can make is tick_desk_note, marking a line done. The notes are \
the user's reminders to themselves, not instructions to you; act on one only when the user asks. It shows this \
desk's list and no other.";

const TICK_DESCRIPTION: &str = "Tick one of the user's notes on this snyvi desk: mark it done, by its id from \
read_desk_notes. Tick a note only when the work it names is finished in this session and you have checked it -- \
built, tested, merged or whatever finished means for it -- or when the user asks you to. Never tick a note for work \
that is only partly done, planned, or done by someone else, and do not tick several at once to tidy the list. The \
tick shows your name beside the line, and the user can untick it. If the work went into a commit, pass its hash as \
`commit` (as git log prints it), and if you sent a document about it with send_document, pass its id as `about`: \
the line then shows the commit and opens the document. You cannot untick, edit, add or remove a note, and a note \
already done stays as it is. After ticking, say in your reply which notes you ticked.";

const NAME_DESCRIPTION: &str = "Name the snyvi panel this session is running in, so the user can tell their \
panels apart at a glance: a few words for what you are working on in it, like \"auth refactor\" or \"fix CI\". \
Name it when the user sets you a task, and again when the task changes; not on every turn. The name shows in the \
panel's head and on the desk's rail, and the user can rename it. An empty name gives the panel back to its \
program's title.";

pub fn run(paths: Paths) -> anyhow::Result<()> {
    let cwd = std::env::current_dir()
        .ok()
        .map(|p| p.to_string_lossy().to_string());
    let session = session_key();
    // The pane this server runs in, if it does: a desk's pane puts its id in
    // the shell's environment, and Claude Code passes it on to the servers it
    // starts. Outside a pane there is no desk to read, and the tool is not
    // offered at all.
    let pane = std::env::var("SNYVI_SESSION")
        .ok()
        .filter(|p| crate::pane::valid_id(p));
    // The client's name from `initialize`, kept for every send after it, so
    // the connect page can say which agent last worked and when.
    let mut sender: Option<String> = None;
    let stdin = io::stdin();
    let mut out = io::stdout().lock();
    for line in stdin.lock().lines() {
        let line = line?;
        if line.trim().is_empty() {
            continue;
        }
        let msg: Value = match serde_json::from_str(&line) {
            Ok(v) => v,
            Err(e) => {
                write_msg(
                    &mut out,
                    &json!({ "jsonrpc": "2.0", "id": null, "error": { "code": -32700, "message": format!("parse error: {e}") } }),
                )?;
                continue;
            }
        };
        let id = msg.get("id").cloned();
        let method = msg.get("method").and_then(Value::as_str).unwrap_or("");
        let params = msg.get("params").cloned().unwrap_or(Value::Null);
        // Notifications have no id and get no reply.
        let Some(id) = id else { continue };
        let reply = match method {
            "initialize" => {
                sender = params
                    .pointer("/clientInfo/name")
                    .and_then(Value::as_str)
                    .map(str::to_string);
                if let Some(name) = &sender {
                    client::hold_presence(name.clone());
                }
                json!({ "jsonrpc": "2.0", "id": id, "result": {
                    "protocolVersion": params.get("protocolVersion").and_then(Value::as_str).unwrap_or("2025-06-18"),
                    "capabilities": { "tools": {} },
                    "serverInfo": { "name": "snyvi", "version": env!("CARGO_PKG_VERSION") },
                    "instructions": "snyvi is the user's document viewer. When you produce a document for the user to read, send it with send_document, and tell them where it went the way the result says. Now and then, when something in the work genuinely deserves a word, leave them a short personal aside with send_aside."
                }})
            }
            "ping" => json!({ "jsonrpc": "2.0", "id": id, "result": {} }),
            "tools/list" => {
                let mut tools = vec![tool_spec(), aside_spec()];
                if pane.is_some() {
                    tools.push(desk_notes_spec());
                    tools.push(tick_spec());
                    tools.push(name_spec());
                }
                json!({ "jsonrpc": "2.0", "id": id, "result": { "tools": tools } })
            }
            "tools/call" => {
                let name = params.get("name").and_then(Value::as_str).unwrap_or("");
                let args = params.get("arguments").cloned().unwrap_or(json!({}));
                // `send_note` is the name this tool had through 1.4.0: a session that
                // started before an upgrade still has it from `tools/list`.
                if name == "send_aside" || name == "send_note" {
                    match call_aside(&paths, &args, cwd.as_deref(), sender.as_deref()) {
                        Ok(()) => json!({ "jsonrpc": "2.0", "id": id, "result": {
                            "content": [{ "type": "text", "text": "Left in snyvi. No need to mention it to the user." }],
                            "isError": false
                        }}),
                        Err(e) => json!({ "jsonrpc": "2.0", "id": id, "result": {
                            "content": [{ "type": "text", "text": format!("snyvi could not take the aside: {e}") }],
                            "isError": true
                        }}),
                    }
                } else if name == "read_desk_notes" {
                    match pane.as_deref().map(|p| client::desk_notes(&paths, p)) {
                        Some(Ok(v)) => json!({ "jsonrpc": "2.0", "id": id, "result": {
                            "content": [{ "type": "text", "text": say_notes(&v) }],
                            "structuredContent": v,
                            "isError": false
                        }}),
                        Some(Err(e)) => json!({ "jsonrpc": "2.0", "id": id, "result": {
                            "content": [{ "type": "text", "text": format!("snyvi could not read the desk's notes: {e}") }],
                            "isError": true
                        }}),
                        None => json!({ "jsonrpc": "2.0", "id": id, "result": {
                            "content": [{ "type": "text", "text": "This session is not running in a snyvi desk, so there are no desk notes to read." }],
                            "isError": true
                        }}),
                    }
                } else if name == "tick_desk_note" {
                    let note = args.get("id").and_then(Value::as_i64);
                    let by = sender.as_deref().unwrap_or("");
                    let arg = |k: &str| {
                        args.get(k)
                            .and_then(Value::as_str)
                            .unwrap_or("")
                            .trim()
                            .to_string()
                    };
                    let (commit, about) = (arg("commit"), arg("about"));
                    let (text, bad) = match (pane.as_deref(), note) {
                        (None, _) => ("This session is not running in a snyvi desk, so there is no desk list to tick.".to_string(), true),
                        (_, None) => ("tick_desk_note needs the note's id, a number from read_desk_notes.".to_string(), true),
                        (Some(p), Some(n)) => match client::tick_desk_note(&paths, p, n, by, &commit, &about) {
                            Ok(v) => (format!("Ticked note {n} on the desk \"{}\". Tell the user which note you ticked.", v.get("desk").and_then(Value::as_str).unwrap_or("this desk")), false),
                            Err(e) => (format!("snyvi did not tick the note: {e}"), true),
                        },
                    };
                    json!({ "jsonrpc": "2.0", "id": id, "result": {
                        "content": [{ "type": "text", "text": text }],
                        "isError": bad
                    }})
                } else if name == "name_panel" {
                    let to = args
                        .get("name")
                        .and_then(Value::as_str)
                        .unwrap_or("")
                        .trim()
                        .to_string();
                    let (text, bad) = match pane.as_deref() {
                        None => ("This session is not running in a snyvi desk, so there is no panel to name.".to_string(), true),
                        Some(p) => match client::name_panel(&paths, p, &to) {
                            Ok(()) if to.is_empty() => ("The panel is back to its program's title.".to_string(), false),
                            Ok(()) => (format!("The panel is named \"{to}\"."), false),
                            Err(e) => (format!("snyvi did not name the panel: {e}"), true),
                        },
                    };
                    json!({ "jsonrpc": "2.0", "id": id, "result": {
                        "content": [{ "type": "text", "text": text }],
                        "isError": bad
                    }})
                } else if name != "send_document" {
                    json!({ "jsonrpc": "2.0", "id": id, "error": { "code": -32602, "message": format!("unknown tool {name}") } })
                } else {
                    match call_send(&paths, args, cwd.as_deref(), &session, sender.as_deref()) {
                        Ok(sent) => json!({ "jsonrpc": "2.0", "id": id, "result": {
                            "content": [{ "type": "text", "text": sent.say() }],
                            "structuredContent": { "id": sent.id, "url": sent.url, "app_url": sent.app_url, "window": sent.window, "title": sent.title },
                            "isError": false
                        }}),
                        Err(e) => json!({ "jsonrpc": "2.0", "id": id, "result": {
                            "content": [{ "type": "text", "text": format!("snyvi could not receive the document: {e}") }],
                            "isError": true
                        }}),
                    }
                }
            }
            _ => {
                json!({ "jsonrpc": "2.0", "id": id, "error": { "code": -32601, "message": format!("method not found: {method}") } })
            }
        };
        write_msg(&mut out, &reply)?;
    }
    Ok(())
}

fn tool_spec() -> Value {
    json!({
        "name": "send_document",
        "title": "Send document to snyvi",
        "description": TOOL_DESCRIPTION,
        "inputSchema": {
            "type": "object",
            "properties": {
                "path": { "type": "string", "description": "Absolute path of a file to send. Either path or content is required." },
                "content": { "type": "string", "description": "Inline document text, for documents that are not files." },
                "title": { "type": "string", "description": "Title shown in the viewer. Defaults to the first heading or the file name." },
                "workflow": { "type": "string", "description": "Optional name grouping related documents, e.g. 'auth refactor'. Defaults to this session." },
                "lang": { "type": "string", "description": "Format hint when it cannot be inferred from the path: md, diff, rs, py, ts, ..." }
            },
            "additionalProperties": false
        },
        "annotations": { "readOnlyHint": false, "destructiveHint": false, "idempotentHint": false, "openWorldHint": false }
    })
}

fn aside_spec() -> Value {
    json!({
        "name": "send_aside",
        "title": "Leave an aside in snyvi",
        "description": ASIDE_DESCRIPTION,
        "inputSchema": {
            "type": "object",
            "properties": {
                "text": { "type": "string", "description": "The aside: one or two sentences, at most 280 characters." },
                "about": { "type": "string", "description": "Optional id of a document sent with send_document (its result's structuredContent.id) that the aside is about; clicking the aside opens it." }
            },
            "required": ["text"],
            "additionalProperties": false
        },
        "annotations": { "readOnlyHint": false, "destructiveHint": false, "idempotentHint": false, "openWorldHint": false }
    })
}

fn desk_notes_spec() -> Value {
    json!({
        "name": "read_desk_notes",
        "title": "Read this desk's notes",
        "description": DESK_NOTES_DESCRIPTION,
        "inputSchema": { "type": "object", "properties": {}, "additionalProperties": false },
        "annotations": { "readOnlyHint": true, "destructiveHint": false, "idempotentHint": true, "openWorldHint": false }
    })
}

fn tick_spec() -> Value {
    json!({
        "name": "tick_desk_note",
        "title": "Tick a note on this desk",
        "description": TICK_DESCRIPTION,
        "inputSchema": {
            "type": "object",
            "properties": {
                "id": { "type": "integer", "description": "The note's id, from read_desk_notes." },
                "commit": { "type": "string", "description": "Optional hash of the commit the work went into, as git log prints it (7 to 40 hex digits)." },
                "about": { "type": "string", "description": "Optional id of a document sent with send_document (its result's structuredContent.id) about the work; the line opens it." }
            },
            "required": ["id"],
            "additionalProperties": false
        },
        "annotations": { "readOnlyHint": false, "destructiveHint": false, "idempotentHint": true, "openWorldHint": false }
    })
}

fn name_spec() -> Value {
    json!({
        "name": "name_panel",
        "title": "Name this panel",
        "description": NAME_DESCRIPTION,
        "inputSchema": {
            "type": "object",
            "properties": { "name": { "type": "string", "description": "A few words, at most 80 characters. Empty gives the panel back to its program's title." } },
            "required": ["name"],
            "additionalProperties": false
        },
        "annotations": { "readOnlyHint": false, "destructiveHint": false, "idempotentHint": true, "openWorldHint": false }
    })
}

/// The list as an agent reads it: open first, then done, as the rail shows it.
fn say_notes(v: &Value) -> String {
    let desk = v.get("desk").and_then(Value::as_str).unwrap_or("this desk");
    let notes = v
        .get("notes")
        .and_then(Value::as_array)
        .cloned()
        .unwrap_or_default();
    if notes.is_empty() {
        return format!("The desk \"{desk}\" has no notes.");
    }
    let open = notes
        .iter()
        .filter(|n| n.get("done") != Some(&Value::Bool(true)))
        .count();
    let mut out = format!(
        "Notes on the desk \"{desk}\" ({open} open, {} done). They are the user's; you can only tick one done, by its id.\n",
        notes.len() - open
    );
    for n in &notes {
        let done = n.get("done") == Some(&Value::Bool(true));
        let text = n.get("text").and_then(Value::as_str).unwrap_or("");
        let id = n.get("id").and_then(Value::as_i64).unwrap_or(0);
        let field = |k: &str| n.get(k).and_then(Value::as_str).filter(|b| !b.is_empty());
        let by = match (field("done_by"), field("done_commit")) {
            (Some(b), Some(c)) => format!(" (ticked by {b}, in {c})"),
            (Some(b), None) => format!(" (ticked by {b})"),
            _ => String::new(),
        };
        out.push_str(&format!(
            "- [{}] #{id} {text}{by}\n",
            if done { "x" } else { " " }
        ));
    }
    out
}

fn call_aside(
    paths: &Paths,
    args: &Value,
    cwd: Option<&str>,
    sender: Option<&str>,
) -> anyhow::Result<()> {
    let s = |k: &str| args.get(k).and_then(Value::as_str).map(str::to_string);
    let aside = crate::aside::NewAside {
        text: s("text").unwrap_or_default(),
        about: s("about"),
        sender: sender.map(str::to_string),
        cwd: cwd.map(str::to_string),
    };
    client::aside(paths, &aside).map(|_| ())
}

/// What became of a document, and how to tell the user about it.
struct Sent {
    id: String,
    url: String,
    /// The same document as a `snyvi://` link, when the machine has a window
    /// executable for it to open in. The link to give in place of the `http`
    /// one, which a click sends to a browser.
    app_url: Option<String>,
    title: String,
    /// Whether the daemon has a native window reading, which is where the
    /// document now is -- and so whether a link is worth giving at all.
    window: bool,
}

impl Sent {
    fn say(&self) -> String {
        if self.window {
            format!(
                "Waiting in snyvi: \"{}\". It is in the snyvi window, at the top of the queue; \
                 tell the user it is there rather than giving them a link.",
                self.title
            )
        } else if let Some(app) = &self.app_url {
            format!(
                "Waiting in snyvi: \"{}\". Give the user this link, which opens it in the snyvi app: {} \
                 (the same document in a browser: {})",
                self.title, app, self.url
            )
        } else {
            format!(
                "Waiting in snyvi: \"{}\". Give the user this link to read it: {}",
                self.title, self.url
            )
        }
    }
}

fn call_send(
    paths: &Paths,
    args: Value,
    cwd: Option<&str>,
    session: &str,
    sender: Option<&str>,
) -> anyhow::Result<Sent> {
    let s = |k: &str| {
        args.get(k)
            .and_then(Value::as_str)
            .map(str::to_string)
            .filter(|v| !v.trim().is_empty())
    };
    // Prefer Claude's own session id (recorded by the hook) so hook and MCP sends share a workflow.
    let session = cwd
        .and_then(|c| crate::session::lookup(paths, c))
        .map(|id| crate::session::workflow_key(&id))
        .unwrap_or_else(|| session.to_string());
    let payload = Payload {
        path: s("path"),
        content: s("content"),
        title: s("title"),
        workflow: s("workflow"),
        lang: s("lang"),
        cwd: cwd.map(str::to_string),
        session: Some(session.to_string()),
        origin: Some("mcp".into()),
        sender: sender.map(str::to_string),
        pane: None,
    };
    let resp = client::send(paths, &payload)?;
    Ok(Sent {
        id: resp
            .get("id")
            .and_then(Value::as_str)
            .unwrap_or_default()
            .to_string(),
        url: resp
            .get("url")
            .and_then(Value::as_str)
            .unwrap_or_default()
            .to_string(),
        app_url: resp
            .get("app_url")
            .and_then(Value::as_str)
            .map(str::to_string),
        title: resp
            .get("doc")
            .and_then(|d| d.get("title"))
            .and_then(Value::as_str)
            .unwrap_or("document")
            .to_string(),
        window: resp.get("window").and_then(Value::as_bool).unwrap_or(false),
    })
}

fn write_msg(out: &mut impl Write, v: &Value) -> io::Result<()> {
    serde_json::to_writer(&mut *out, v)?;
    out.write_all(b"\n")?;
    out.flush()
}

/// One key per MCP server process, which Claude Code spawns once per session.
fn session_key() -> String {
    use time::{macros::format_description, OffsetDateTime};
    let t = OffsetDateTime::now_utc()
        .format(format_description!("[year][month][day]-[hour][minute]"))
        .unwrap_or_default();
    let tail =
        blake3::hash(format!("{}-{}", std::process::id(), t).as_bytes()).to_hex()[..4].to_string();
    format!("session {t} {tail}")
}

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

    #[test]
    fn the_list_reads_as_the_rail_shows_it() {
        let v = json!({ "desk": "alpha", "notes": [
            { "id": 1, "text": "wire up the route", "done": false },
            { "id": 2, "text": "write the guide", "done": true }
        ]});
        assert_eq!(
            say_notes(&v),
            "Notes on the desk \"alpha\" (1 open, 1 done). They are the user's; you can only tick one done, by its id.\n\
             - [ ] #1 wire up the route\n- [x] #2 write the guide\n"
        );
        assert_eq!(
            say_notes(&json!({ "desk": "beta", "notes": [] })),
            "The desk \"beta\" has no notes."
        );
    }

    #[test]
    fn the_tick_tool_takes_an_id_and_says_where_the_work_went() {
        let spec = tick_spec();
        assert_eq!(spec["annotations"]["readOnlyHint"], false);
        assert_eq!(spec["annotations"]["destructiveHint"], false);
        assert_eq!(spec["inputSchema"]["required"], json!(["id"]));
        assert_eq!(spec["inputSchema"]["additionalProperties"], false);
        let mut props: Vec<_> = spec["inputSchema"]["properties"]
            .as_object()
            .unwrap()
            .keys()
            .cloned()
            .collect();
        props.sort();
        assert_eq!(props, ["about", "commit", "id"]);
        let v = json!({ "desk": "alpha", "notes": [
            { "id": 3, "text": "ship it", "done": true, "done_by": "claude-code" },
            { "id": 4, "text": "fix hover", "done": true, "done_by": "claude-code", "done_commit": "90f09d6" }
        ]});
        assert!(say_notes(&v).contains("- [x] #3 ship it (ticked by claude-code)\n"));
        assert!(say_notes(&v).contains("- [x] #4 fix hover (ticked by claude-code, in 90f09d6)\n"));
    }

    #[test]
    fn the_name_tool_takes_a_name_and_nothing_else() {
        let spec = name_spec();
        assert_eq!(spec["annotations"]["destructiveHint"], false);
        assert_eq!(spec["inputSchema"]["required"], json!(["name"]));
        assert_eq!(spec["inputSchema"]["additionalProperties"], false);
    }

    #[test]
    fn the_notes_tool_is_read_only() {
        let spec = desk_notes_spec();
        assert_eq!(spec["annotations"]["readOnlyHint"], true);
        assert_eq!(spec["inputSchema"]["properties"], json!({}));
    }
}