lanekeep-server 0.6.0

LSP and MCP servers for lanekeep, over stdio.
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
//! The Model Context Protocol surface.
//!
//! Three tools, one per thing the CLI already does: run the checks, list the configured
//! rules, explain one. An agent host launches `lanekeep server --protocol mcp` and calls
//! them; the transport is the same JSON-RPC this crate already speaks, line-delimited
//! instead of header-delimited.
//!
//! # What the tools return
//!
//! The `agent` reporter's text, unchanged. That format exists precisely for this consumer —
//! it groups by rule, states the remediation once rather than per occurrence, and shows a
//! good and bad example — and re-rendering violations into a bespoke JSON shape here would
//! be a second answer to a question §11 already answered.
//!
//! # A failing tool is not a failing call
//!
//! MCP separates the two, and the distinction is load-bearing. A rule that throws, or a
//! config that will not load, is a *result* the model should see and act on — it comes back
//! as `isError: true` with the message as content. A JSON-RPC error is for the host, not the
//! model: no such tool, malformed arguments. Reporting a rule failure as a JSON-RPC error
//! hides it from the thing best placed to fix it.

use std::io::{BufRead, Write};

use serde_json::{Value, json};

use crate::jsonrpc::{self, Framing, Incoming, Outgoing, codes};

/// The protocol revision this server implements.
///
/// A client asking for a different one is answered with this rather than refused: the
/// specification has the server state what it speaks, and a host that cannot work with it
/// will say so. Refusing outright would turn a version skew into a dead session.
const PROTOCOL_VERSION: &str = "2024-11-05";

/// What the host can ask lanekeep to do.
///
/// A trait rather than an engine, for the same reason the LSP loop takes a closure: this
/// crate stays protocol-only, and its tests need no project on disk.
pub trait Tools {
    /// Run the project's rules and describe what they found.
    ///
    /// # Errors
    ///
    /// Returns the message to show the model when the run could not happen at all.
    fn check(&mut self) -> Result<String, String>;

    /// List the rules the project has configured.
    ///
    /// # Errors
    ///
    /// As [`Tools::check`].
    fn rules(&mut self) -> Result<String, String>;

    /// Explain one rule: what it checks and what to do about it.
    ///
    /// # Errors
    ///
    /// As [`Tools::check`], including when no such rule is configured.
    fn explain(&mut self, rule: &str) -> Result<String, String>;
}

/// The tool catalogue, as `tools/list` returns it.
#[must_use]
pub fn catalogue() -> Value {
    json!({
        "tools": [
            {
                "name": "lanekeep_check",
                "description": "Check the project against its architectural rules. \
                                Returns every violation, grouped by rule, with the \
                                remediation for each and a good and bad example. Run this \
                                after editing code to find conventions the change broke.",
                "inputSchema": { "type": "object", "properties": {} },
            },
            {
                "name": "lanekeep_rules",
                "description": "List the rules this project has configured, with what each \
                                one enforces. Use it to find out which conventions apply \
                                here before writing code, rather than after.",
                "inputSchema": { "type": "object", "properties": {} },
            },
            {
                "name": "lanekeep_explain",
                "description": "Explain one rule: what it checks, why, and what to do \
                                instead, with a good and bad example. Call it with the id \
                                from a violation to find out how to fix it.",
                "inputSchema": {
                    "type": "object",
                    "properties": {
                        "rule": {
                            "type": "string",
                            "description": "Namespaced rule id, as it appears in a \
                                            violation — for example `lanekeep/no-default-export`.",
                        },
                    },
                    "required": ["rule"],
                },
            },
        ],
    })
}

/// A tool result, successful or not.
///
/// Both are a *successful* JSON-RPC reply; `isError` is what tells the model which it got.
#[must_use]
pub fn content(text: &str, failed: bool) -> Value {
    json!({
        "content": [{ "type": "text", "text": text }],
        "isError": failed,
    })
}

/// Serve MCP against `input` and `output` until the host disconnects.
///
/// # Errors
///
/// Propagates an I/O failure on the transport. A failing tool is not one — see the module
/// docs.
pub fn serve(
    input: &mut impl BufRead,
    output: &mut impl Write,
    tools: &mut impl Tools,
) -> std::io::Result<()> {
    while let Some(raw) = jsonrpc::read(input, Framing::Lines)? {
        let Ok(message) = serde_json::from_str::<Incoming>(&raw) else {
            jsonrpc::write(
                output,
                Framing::Lines,
                &Outgoing::error(None, codes::PARSE_ERROR, "not a JSON-RPC message"),
            )?;
            continue;
        };

        let outcome = match message.method.as_str() {
            "initialize" => Some(Ok(json!({
                "protocolVersion": PROTOCOL_VERSION,
                "capabilities": { "tools": {} },
                "serverInfo": {
                    "name": "lanekeep",
                    "version": env!("CARGO_PKG_VERSION"),
                },
            }))),

            // Acknowledgements and keepalives.
            "notifications/initialized" | "initialized" => None,
            "ping" => Some(Ok(json!({}))),

            "tools/list" => Some(Ok(catalogue())),

            "tools/call" => Some(call(&message.params, tools)),

            other => Some(Err((
                codes::METHOD_NOT_FOUND,
                format!("no method `{other}`"),
            ))),
        };

        let Some(outcome) = outcome else { continue };
        if !message.expects_reply() {
            continue;
        }

        let response = match outcome {
            Ok(result) => Outgoing::result(message.id.clone(), result),
            Err((code, text)) => Outgoing::error(message.id.clone(), code, text),
        };
        jsonrpc::write(output, Framing::Lines, &response)?;
    }

    Ok(())
}

/// Dispatch one `tools/call`.
fn call(params: &Value, tools: &mut impl Tools) -> Result<Value, (i32, String)> {
    let Some(name) = params["name"].as_str() else {
        return Err((codes::INVALID_PARAMS, "`name` is required".to_owned()));
    };

    let outcome = match name {
        "lanekeep_check" => tools.check(),
        "lanekeep_rules" => tools.rules(),
        "lanekeep_explain" => {
            // A missing argument is the host's mistake, not something the model should be
            // shown as a rule failure — so it is a JSON-RPC error rather than `isError`.
            let Some(rule) = params["arguments"]["rule"].as_str() else {
                return Err((
                    codes::INVALID_PARAMS,
                    "`lanekeep_explain` needs a `rule` argument".to_owned(),
                ));
            };
            tools.explain(rule)
        }
        other => {
            return Err((codes::INVALID_PARAMS, format!("no tool `{other}`")));
        }
    };

    Ok(match outcome {
        Ok(text) => content(&text, false),
        Err(text) => content(&text, true),
    })
}

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

    /// A stand-in that records what it was asked and answers as told.
    struct Fake {
        answer: Result<String, String>,
        explained: Option<String>,
        called: Vec<&'static str>,
    }

    impl Fake {
        fn ok() -> Self {
            Self {
                answer: Ok("nothing found".to_owned()),
                explained: None,
                called: Vec::new(),
            }
        }

        fn failing() -> Self {
            Self {
                answer: Err("rule threw".to_owned()),
                explained: None,
                called: Vec::new(),
            }
        }
    }

    impl Tools for Fake {
        fn check(&mut self) -> Result<String, String> {
            self.called.push("check");
            self.answer.clone()
        }

        fn rules(&mut self) -> Result<String, String> {
            self.called.push("rules");
            self.answer.clone()
        }

        fn explain(&mut self, rule: &str) -> Result<String, String> {
            self.called.push("explain");
            self.explained = Some(rule.to_owned());
            self.answer.clone()
        }
    }

    fn exchange(messages: &[Value], tools: &mut impl Tools) -> Vec<Value> {
        use std::fmt::Write as _;

        let mut wire = String::new();
        for message in messages {
            let _ = writeln!(wire, "{message}");
        }
        let mut input = std::io::BufReader::new(wire.as_bytes());
        let mut output = Vec::new();
        serve(&mut input, &mut output, tools).expect("serves");

        String::from_utf8(output)
            .expect("utf-8")
            .lines()
            .map(|line| serde_json::from_str(line).expect("parses"))
            .collect()
    }

    #[test]
    fn initialize_states_the_protocol_version_and_the_tools_capability() {
        let replies = exchange(
            &[json!({ "jsonrpc": "2.0", "id": 1, "method": "initialize", "params": {} })],
            &mut Fake::ok(),
        );
        assert_eq!(replies.len(), 1);
        assert_eq!(replies[0]["result"]["protocolVersion"], PROTOCOL_VERSION);
        assert!(replies[0]["result"]["capabilities"]["tools"].is_object());
        assert_eq!(replies[0]["result"]["serverInfo"]["name"], "lanekeep");
    }

    #[test]
    fn the_catalogue_lists_three_tools_each_with_a_schema() {
        let replies = exchange(
            &[json!({ "jsonrpc": "2.0", "id": 1, "method": "tools/list" })],
            &mut Fake::ok(),
        );
        let tools = replies[0]["result"]["tools"].as_array().expect("an array");
        assert_eq!(tools.len(), 3);

        for tool in tools {
            assert!(tool["name"].is_string(), "{tool}");
            // The description is what a model reads to decide whether to call it, so an
            // empty one makes the tool invisible in practice.
            let description = tool["description"].as_str().expect("a description");
            assert!(
                description.len() > 40,
                "too terse to choose by: {description}"
            );
            assert_eq!(tool["inputSchema"]["type"], "object", "{tool}");
        }
    }

    #[test]
    fn explain_declares_its_required_argument() {
        let replies = exchange(
            &[json!({ "jsonrpc": "2.0", "id": 1, "method": "tools/list" })],
            &mut Fake::ok(),
        );
        let explain = replies[0]["result"]["tools"]
            .as_array()
            .expect("an array")
            .iter()
            .find(|tool| tool["name"] == "lanekeep_explain")
            .expect("present");
        assert_eq!(explain["inputSchema"]["required"][0], "rule");
    }

    #[test]
    fn calling_check_returns_its_text_as_content() {
        let mut tools = Fake::ok();
        let replies = exchange(
            &[json!({
                "jsonrpc": "2.0", "id": 1, "method": "tools/call",
                "params": { "name": "lanekeep_check", "arguments": {} }
            })],
            &mut tools,
        );
        assert_eq!(tools.called, ["check"]);
        assert_eq!(replies[0]["result"]["content"][0]["type"], "text");
        assert_eq!(replies[0]["result"]["content"][0]["text"], "nothing found");
        assert_eq!(replies[0]["result"]["isError"], false);
    }

    #[test]
    fn explain_receives_the_rule_it_was_given() {
        let mut tools = Fake::ok();
        exchange(
            &[json!({
                "jsonrpc": "2.0", "id": 1, "method": "tools/call",
                "params": {
                    "name": "lanekeep_explain",
                    "arguments": { "rule": "lanekeep/no-default-export" }
                }
            })],
            &mut tools,
        );
        assert_eq!(
            tools.explained.as_deref(),
            Some("lanekeep/no-default-export")
        );
    }

    #[test]
    fn a_failing_tool_is_a_successful_call_marked_as_an_error() {
        // The distinction that matters: a rule that threw is a result the model should see
        // and act on. Reporting it as a JSON-RPC error hides it from the thing best placed
        // to fix it.
        let replies = exchange(
            &[json!({
                "jsonrpc": "2.0", "id": 1, "method": "tools/call",
                "params": { "name": "lanekeep_check", "arguments": {} }
            })],
            &mut Fake::failing(),
        );
        assert!(
            replies[0].get("error").is_none(),
            "should not be a protocol error: {}",
            replies[0]
        );
        assert_eq!(replies[0]["result"]["isError"], true);
        assert_eq!(replies[0]["result"]["content"][0]["text"], "rule threw");
    }

    #[test]
    fn a_missing_argument_is_a_protocol_error_not_a_tool_error() {
        // The host built the call wrong. That is not something the model can fix by writing
        // different code, so it goes back as a JSON-RPC error.
        let replies = exchange(
            &[json!({
                "jsonrpc": "2.0", "id": 1, "method": "tools/call",
                "params": { "name": "lanekeep_explain", "arguments": {} }
            })],
            &mut Fake::ok(),
        );
        assert_eq!(replies[0]["error"]["code"], codes::INVALID_PARAMS);
    }

    #[test]
    fn an_unknown_tool_is_refused() {
        let replies = exchange(
            &[json!({
                "jsonrpc": "2.0", "id": 1, "method": "tools/call",
                "params": { "name": "lanekeep_deploy", "arguments": {} }
            })],
            &mut Fake::ok(),
        );
        assert_eq!(replies[0]["error"]["code"], codes::INVALID_PARAMS);
    }

    #[test]
    fn an_unknown_method_is_refused() {
        let replies = exchange(
            &[json!({ "jsonrpc": "2.0", "id": 1, "method": "resources/list" })],
            &mut Fake::ok(),
        );
        assert_eq!(replies[0]["error"]["code"], codes::METHOD_NOT_FOUND);
    }

    #[test]
    fn the_initialized_notification_is_not_answered() {
        let replies = exchange(
            &[json!({ "jsonrpc": "2.0", "method": "notifications/initialized" })],
            &mut Fake::ok(),
        );
        assert!(replies.is_empty(), "{replies:?}");
    }

    #[test]
    fn ping_is_answered() {
        let replies = exchange(
            &[json!({ "jsonrpc": "2.0", "id": 1, "method": "ping" })],
            &mut Fake::ok(),
        );
        assert_eq!(replies.len(), 1);
        assert!(replies[0]["result"].is_object());
    }

    #[test]
    fn a_malformed_line_is_answered_and_the_session_continues() {
        let wire = "not json\n{\"jsonrpc\":\"2.0\",\"id\":1,\"method\":\"ping\"}\n";
        let mut input = std::io::BufReader::new(wire.as_bytes());
        let mut output = Vec::new();
        serve(&mut input, &mut output, &mut Fake::ok()).expect("serves");

        let replies: Vec<Value> = String::from_utf8(output)
            .expect("utf-8")
            .lines()
            .map(|line| serde_json::from_str(line).expect("parses"))
            .collect();
        assert_eq!(replies.len(), 2);
        assert_eq!(replies[0]["error"]["code"], codes::PARSE_ERROR);
        assert_eq!(replies[1]["id"], 1);
    }

    #[test]
    fn every_reply_is_one_line() {
        // Line-delimited framing: a reply containing a raw newline would be read as two
        // messages, and every message after it would be off by one.
        let mut tools = Fake::ok();
        tools.answer = Ok("two\nlines".to_owned());
        let wire = json!({
            "jsonrpc": "2.0", "id": 1, "method": "tools/call",
            "params": { "name": "lanekeep_check", "arguments": {} }
        })
        .to_string()
            + "\n";

        let mut input = std::io::BufReader::new(wire.as_bytes());
        let mut output = Vec::new();
        serve(&mut input, &mut output, &mut tools).expect("serves");

        let text = String::from_utf8(output).expect("utf-8");
        assert_eq!(text.trim_end().lines().count(), 1, "{text}");
        let parsed: Value = serde_json::from_str(text.trim_end()).expect("parses");
        assert_eq!(parsed["result"]["content"][0]["text"], "two\nlines");
    }
}