opendbpylot 0.3.0

Natural-language-to-SQL via Retrieval-Augmented Generation, in Rust
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
//! MCP (Model Context Protocol) stdio server: `dbpylot mcp`.
//!
//! Serves the engine's capabilities as MCP tools to any agent host — OpenPylot,
//! Claude Desktop, Claude Code, or the MCP Inspector. JSON-RPC 2.0, one JSON
//! object per line, over stdin/stdout.
//!
//! Protocol invariants (do not break these):
//! - stdout carries *only* JSON-RPC responses — logs go to stderr
//!   (see `app::init_tracing_stderr`), and nothing here may `println!`.
//! - Every incoming message that carries an `id` gets exactly one response
//!   line; messages without an `id` (true notifications) get silence. Some
//!   hosts (OpenPylot) send `notifications/initialized` *with* an id and then
//!   block waiting for a reply, while spec-strict hosts send it without one —
//!   this rule keeps both working.
//! - An unconfigured engine never kills the server: `initialize` and
//!   `tools/list` always work, and `tools/call` retries the (purely local)
//!   engine construction so `dbpylot init` can be run without a restart.

use std::sync::Arc;

use anyhow::Result;
use serde_json::{json, Value};
use tokio::io::{AsyncBufReadExt, AsyncWriteExt, BufReader};
use tokio::sync::Mutex;

use crate::opendbpylot::OpenDbPylot;
use crate::sqlrunner::QueryResult;

/// Rows are capped in tool results so a big table can't blow up the host
/// agent's context window. The full count is reported in `row_count`.
const MAX_RESULT_ROWS: usize = 200;

/// MCP protocol revision we speak (also echoed back from the client if it
/// proposes one — we accept any and let the client decide compatibility).
const PROTOCOL_VERSION: &str = "2024-11-05";

struct McpState {
    /// `None` = not configured yet. Retried lazily on each `tools/call`
    /// (construction is local file I/O only — no network).
    engine: Mutex<Option<Arc<OpenDbPylot>>>,
    /// Whether `engine()` may retry `build_configured()` when the slot is
    /// empty. True in the real server; false in tests, which must not read
    /// the developer's actual `~/.opendbpylot` configuration.
    lazy_rebuild: bool,
}

impl McpState {
    fn new(engine: Option<OpenDbPylot>) -> Self {
        Self { engine: Mutex::new(engine.map(Arc::new)), lazy_rebuild: true }
    }

    /// The configured engine, building it now if setup happened after startup.
    async fn engine(&self) -> Option<Arc<OpenDbPylot>> {
        let mut slot = self.engine.lock().await;
        if slot.is_none() && self.lazy_rebuild {
            if let Ok(Some(bot)) = crate::cli::build_configured() {
                *slot = Some(Arc::new(bot));
            }
        }
        slot.clone()
    }
}

/// Entry point for `dbpylot mcp`: serve MCP over stdin/stdout until EOF.
pub async fn serve_stdio() -> Result<()> {
    // Tolerate an unconfigured or broken setup at startup; tools report it.
    let state = McpState::new(crate::cli::build_configured().unwrap_or(None));

    let mut lines = BufReader::new(tokio::io::stdin()).lines();
    let mut stdout = tokio::io::stdout();

    while let Some(line) = lines.next_line().await? {
        if line.trim().is_empty() {
            continue;
        }
        if let Some(response) = handle_line(&state, &line).await {
            stdout.write_all(response.to_string().as_bytes()).await?;
            stdout.write_all(b"\n").await?;
            stdout.flush().await?;
        }
    }
    // EOF: the host closed the pipe — clean shutdown.
    Ok(())
}

/// Handle one incoming JSON-RPC line. Returns `Some(response)` iff the message
/// carries an `id` (or is unparseable, which gets a parse error).
async fn handle_line(state: &McpState, line: &str) -> Option<Value> {
    let msg: Value = match serde_json::from_str(line) {
        Ok(v) => v,
        Err(_) => return Some(rpc_error(Value::Null, -32700, "Parse error")),
    };

    // Keep the id as a raw Value: clients may use numbers or strings.
    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);

    let Some(id) = id else {
        // A true notification: nothing may be written back.
        return None;
    };

    let result = match method {
        "initialize" => {
            let client_version = params
                .get("protocolVersion")
                .and_then(Value::as_str)
                .unwrap_or(PROTOCOL_VERSION);
            json!({
                "protocolVersion": client_version,
                "capabilities": { "tools": {} },
                "serverInfo": {
                    "name": "opendbpylot",
                    "version": env!("CARGO_PKG_VERSION"),
                },
            })
        }
        // Some hosts send the initialized notification with an id and wait for
        // a reply (see module docs); an empty result satisfies them.
        "notifications/initialized" | "initialized" | "ping" => json!({}),
        "tools/list" => json!({ "tools": tool_definitions() }),
        "tools/call" => call_tool(state, &params).await,
        _ => return Some(rpc_error(id, -32601, &format!("Method not found: {method}"))),
    };

    Some(json!({ "jsonrpc": "2.0", "id": id, "result": result }))
}

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

// ─────────────────────────────────────────────────────────────────────────────
// Tools
// ─────────────────────────────────────────────────────────────────────────────

/// The tool catalog returned by `tools/list`. These six names and their
/// argument/result shapes are a stable public contract — agent presets and
/// skills in host applications refer to them by name.
fn tool_definitions() -> Vec<Value> {
    let no_args = json!({ "type": "object", "properties": {} });
    vec![
        json!({
            "name": "ask_database",
            "description": "Ask a natural-language question about the connected database. \
                Generates SQL via retrieval-augmented generation, executes it read-only, \
                and self-repairs failed queries. Returns the SQL, the rows, and optionally \
                a short answer.",
            "inputSchema": {
                "type": "object",
                "properties": {
                    "question": { "type": "string", "description": "The question, in plain English" }
                },
                "required": ["question"]
            }
        }),
        json!({
            "name": "run_sql",
            "description": "Run a read-only SQL query (SELECT/WITH only) directly against \
                the connected database. Write statements are rejected.",
            "inputSchema": {
                "type": "object",
                "properties": {
                    "sql": { "type": "string", "description": "A single read-only SQL statement" }
                },
                "required": ["sql"]
            }
        }),
        json!({
            "name": "list_schema",
            "description": "List everything the engine knows about the database: learned DDL, \
                documentation notes, and example questions it was trained on.",
            "inputSchema": no_args,
        }),
        json!({
            "name": "refresh_schema",
            "description": "Re-introspect the live database schema and reload it into the \
                knowledge base. Use after the database structure changes.",
            "inputSchema": no_args,
        }),
        json!({
            "name": "train",
            "description": "Teach the engine: a table DDL, a documentation note, or a \
                question→SQL example pair. Improves future SQL generation.",
            "inputSchema": {
                "type": "object",
                "properties": {
                    "kind": {
                        "type": "string",
                        "enum": ["ddl", "documentation", "question_sql"],
                        "description": "What kind of training material this is"
                    },
                    "content": { "type": "string", "description": "The DDL or documentation text (for kind=ddl/documentation)" },
                    "question": { "type": "string", "description": "For kind=question_sql" },
                    "sql": { "type": "string", "description": "For kind=question_sql" }
                },
                "required": ["kind"]
            }
        }),
        json!({
            "name": "health",
            "description": "Check whether opendbpylot is configured and the database is \
                reachable. Call this first if other tools fail.",
            "inputSchema": no_args,
        }),
    ]
}

/// Wrap a JSON payload as an MCP tool result (a single text content block).
fn text_result(payload: Value, is_error: bool) -> Value {
    json!({
        "content": [{ "type": "text", "text": payload.to_string() }],
        "isError": is_error,
    })
}

fn error_result(message: impl Into<String>) -> Value {
    text_result(json!({ "error": scrub_secrets(&message.into()) }), true)
}

/// Redact credentials from any connection-URL-like substring so a database
/// driver error can never leak the password to the MCP host — which may
/// forward tool output to a cloud LLM. Turns `scheme://user:pass@host` into
/// `scheme://user:***@host`.
fn scrub_secrets(text: &str) -> String {
    use once_cell::sync::Lazy;
    use regex::Regex;
    static RE: Lazy<Regex> = Lazy::new(|| {
        Regex::new(r"([a-zA-Z][a-zA-Z0-9+.\-]*://[^:/@\s]+):[^@/\s]+@").unwrap()
    });
    RE.replace_all(text, "$1:***@").into_owned()
}

/// A query result as JSON, capped at [`MAX_RESULT_ROWS`] rows.
fn table_json(result: &QueryResult) -> Value {
    let total = result.rows.len();
    let truncated = total > MAX_RESULT_ROWS;
    let rows: &[Vec<String>] = if truncated { &result.rows[..MAX_RESULT_ROWS] } else { &result.rows };
    json!({
        "columns": result.columns,
        "rows": rows,
        "row_count": total,
        "truncated": truncated,
    })
}

/// Dispatch a `tools/call` request. Tool-level failures are MCP results with
/// `isError: true` (never JSON-RPC errors) so the host's LLM can read them.
async fn call_tool(state: &McpState, params: &Value) -> Value {
    let name = params.get("name").and_then(Value::as_str).unwrap_or("");
    let args = params.get("arguments").cloned().unwrap_or_else(|| json!({}));

    // `run_sql` validates before touching the engine so the read-only gate is
    // enforced even while unconfigured (and testable that way).
    if name == "run_sql" {
        let sql = args.get("sql").and_then(Value::as_str).unwrap_or("").trim().to_string();
        if sql.is_empty() {
            return error_result("Missing required argument: sql");
        }
        if !crate::sql::is_read_only(&sql) {
            return error_result(
                "Blocked: only read-only queries (SELECT / WITH) are allowed. \
                 This tool cannot modify or delete data.",
            );
        }
        let Some(bot) = state.engine().await else { return not_configured() };
        return match bot.run_sql(&sql).await {
            Ok(result) => text_result(table_json(&result), false),
            Err(e) => error_result(format!("SQL error: {e}")),
        };
    }

    // `health` reports rather than errors, even when unconfigured.
    if name == "health" {
        return match state.engine().await {
            None => text_result(
                json!({ "configured": false, "connected": false,
                        "error": "not configured — run `dbpylot init`" }),
                false,
            ),
            Some(bot) => match bot.test_connection().await {
                Ok(()) => text_result(json!({ "configured": true, "connected": true, "error": null }), false),
                Err(e) => text_result(
                    json!({ "configured": true, "connected": false, "error": scrub_secrets(&e.to_string()) }),
                    false,
                ),
            },
        };
    }

    let Some(bot) = state.engine().await else { return not_configured() };

    match name {
        "ask_database" => {
            let Some(question) = args.get("question").and_then(Value::as_str).filter(|q| !q.trim().is_empty())
            else {
                return error_result("Missing required argument: question");
            };
            match bot.ask(question).await {
                Ok(ask) => {
                    let mut payload = json!({
                        "sql": ask.sql,
                        "repairs_used": ask.repairs_used,
                        "answer": ask.answer,
                        "executed": ask.result.is_some(),
                    });
                    match &ask.result {
                        Some(result) => {
                            let table = table_json(result);
                            for (k, v) in table.as_object().unwrap() {
                                payload[k.as_str()] = v.clone();
                            }
                        }
                        None => {
                            payload["note"] =
                                json!("not run — the generated SQL was not a read query");
                        }
                    }
                    text_result(payload, false)
                }
                Err(e) => error_result(format!("ask failed: {e}")),
            }
        }
        "list_schema" => {
            let ddl = bot.list_ddl().await.unwrap_or_default();
            let documentation = bot.list_documentation().await.unwrap_or_default();
            let example_questions: Vec<String> = bot
                .list_question_sql()
                .await
                .unwrap_or_default()
                .into_iter()
                .map(|qs| qs.question)
                .collect();
            text_result(
                json!({
                    "ddl": ddl,
                    "documentation": documentation,
                    "example_questions": example_questions,
                }),
                false,
            )
        }
        "refresh_schema" => match bot.train_from_schema().await {
            Ok(n) => text_result(json!({ "tables_learned": n }), false),
            Err(e) => error_result(format!("schema refresh failed: {e}")),
        },
        "train" => {
            let kind = args.get("kind").and_then(Value::as_str).unwrap_or("");
            let content = args.get("content").and_then(Value::as_str).unwrap_or("");
            let outcome = match kind {
                "ddl" if !content.trim().is_empty() => bot.train_ddl(content).await,
                "documentation" if !content.trim().is_empty() => bot.train_documentation(content).await,
                "question_sql" => {
                    let question = args.get("question").and_then(Value::as_str).unwrap_or("");
                    let sql = args.get("sql").and_then(Value::as_str).unwrap_or("");
                    if question.trim().is_empty() || sql.trim().is_empty() {
                        return error_result(
                            "kind=question_sql requires both `question` and `sql` arguments",
                        );
                    }
                    bot.train_question_sql(question, sql).await
                }
                "ddl" | "documentation" => {
                    return error_result(format!("kind={kind} requires a non-empty `content` argument"))
                }
                _ => {
                    return error_result(
                        "Invalid `kind`: expected one of ddl, documentation, question_sql",
                    )
                }
            };
            match outcome {
                Ok(()) => text_result(json!({ "trained": kind }), false),
                Err(e) => error_result(format!("training failed: {e}")),
            }
        }
        "" => error_result("Missing tool name"),
        other => error_result(format!("Unknown tool: {other}")),
    }
}

fn not_configured() -> Value {
    error_result(
        "opendbpylot is not configured. Run `dbpylot init` in a terminal to choose \
         an LLM provider and connect a database, then retry.",
    )
}

// ─────────────────────────────────────────────────────────────────────────────
// Tests
// ─────────────────────────────────────────────────────────────────────────────

#[cfg(test)]
mod tests {
    use super::*;
    use crate::embedding::local::LocalEmbedding;
    use crate::llm::mock::MockLlm;
    use crate::sqlrunner::sqlite::SqliteRunner;
    use crate::vectorstore::memory::MemoryVectorStore;

    fn unconfigured() -> McpState {
        let mut state = McpState::new(None);
        state.lazy_rebuild = false; // never read the developer's real config
        state
    }

    /// An engine over an in-memory SQLite DB with a mock LLM — no network.
    fn configured() -> McpState {
        let store = Arc::new(MemoryVectorStore::new(Arc::new(LocalEmbedding::new())));
        let bot = OpenDbPylot::new(Arc::new(MockLlm::with_default_sql()), store)
            .with_runner(Arc::new(SqliteRunner::new(":memory:".to_string())));
        McpState::new(Some(bot))
    }

    async fn call(state: &McpState, line: &str) -> Option<Value> {
        handle_line(state, line).await
    }

    fn tool_call_line(id: u64, tool: &str, args: Value) -> String {
        json!({
            "jsonrpc": "2.0", "id": id, "method": "tools/call",
            "params": { "name": tool, "arguments": args }
        })
        .to_string()
    }

    /// Parse the JSON payload out of a tool result's text content block.
    fn tool_payload(resp: &Value) -> (Value, bool) {
        let result = &resp["result"];
        let text = result["content"][0]["text"].as_str().unwrap();
        let is_error = result["isError"].as_bool().unwrap();
        (serde_json::from_str(text).unwrap(), is_error)
    }

    #[tokio::test]
    async fn initialize_reports_server_info() {
        let resp = call(
            &unconfigured(),
            &json!({"jsonrpc":"2.0","id":1,"method":"initialize","params":{
                "protocolVersion":"2024-11-05","capabilities":{},
                "clientInfo":{"name":"test","version":"0"}}})
            .to_string(),
        )
        .await
        .unwrap();
        assert_eq!(resp["id"], 1);
        assert_eq!(resp["result"]["protocolVersion"], "2024-11-05");
        assert_eq!(resp["result"]["serverInfo"]["name"], "opendbpylot");
    }

    #[tokio::test]
    async fn initialized_with_id_gets_reply_without_id_gets_silence() {
        let state = unconfigured();
        // OpenPylot-style: notification WITH an id — must get a response.
        let with_id = call(
            &state,
            &json!({"jsonrpc":"2.0","id":2,"method":"notifications/initialized"}).to_string(),
        )
        .await;
        assert!(with_id.is_some());
        assert_eq!(with_id.unwrap()["id"], 2);
        // Spec-strict: no id — must stay silent.
        let without_id =
            call(&state, &json!({"jsonrpc":"2.0","method":"notifications/initialized"}).to_string())
                .await;
        assert!(without_id.is_none());
    }

    #[tokio::test]
    async fn tools_list_exposes_the_six_tools() {
        let resp = call(
            &unconfigured(),
            &json!({"jsonrpc":"2.0","id":3,"method":"tools/list"}).to_string(),
        )
        .await
        .unwrap();
        let tools = resp["result"]["tools"].as_array().unwrap();
        let names: Vec<&str> = tools.iter().map(|t| t["name"].as_str().unwrap()).collect();
        assert_eq!(
            names,
            ["ask_database", "run_sql", "list_schema", "refresh_schema", "train", "health"]
        );
        for tool in tools {
            assert!(tool["inputSchema"].is_object(), "tool {} lacks inputSchema", tool["name"]);
        }
    }

    #[test]
    fn scrub_secrets_redacts_connection_passwords() {
        // A driver error that embeds the DSN must not leak the password.
        let leaked = "failed to connect: postgres://admin:s3cret@db.host:5432/shop is unreachable";
        let scrubbed = scrub_secrets(leaked);
        assert!(!scrubbed.contains("s3cret"), "password leaked: {scrubbed}");
        assert!(scrubbed.contains("postgres://admin:***@db.host"));
        // Non-URL text is untouched.
        assert_eq!(scrub_secrets("no such table: users"), "no such table: users");
    }

    #[tokio::test]
    async fn run_sql_blocks_writes_even_when_unconfigured() {
        let resp = call(
            &unconfigured(),
            &tool_call_line(4, "run_sql", json!({"sql": "DROP TABLE users"})),
        )
        .await
        .unwrap();
        let (payload, is_error) = tool_payload(&resp);
        assert!(is_error);
        assert!(payload["error"].as_str().unwrap().contains("read-only"));
    }

    #[tokio::test]
    async fn ask_database_unconfigured_points_to_init() {
        let resp = call(
            &unconfigured(),
            &tool_call_line(5, "ask_database", json!({"question": "how many users?"})),
        )
        .await
        .unwrap();
        let (payload, is_error) = tool_payload(&resp);
        assert!(is_error);
        assert!(payload["error"].as_str().unwrap().contains("dbpylot init"));
    }

    #[tokio::test]
    async fn unknown_method_and_garbage_get_json_rpc_errors() {
        let state = unconfigured();
        let unknown =
            call(&state, &json!({"jsonrpc":"2.0","id":6,"method":"resources/list"}).to_string())
                .await
                .unwrap();
        assert_eq!(unknown["error"]["code"], -32601);

        let garbage = call(&state, "this is not json").await.unwrap();
        assert_eq!(garbage["error"]["code"], -32700);
    }

    #[tokio::test]
    async fn run_sql_returns_rows_against_a_real_engine() {
        let resp = call(
            &configured(),
            &tool_call_line(7, "run_sql", json!({"sql": "SELECT 1 AS one, 2 AS two"})),
        )
        .await
        .unwrap();
        let (payload, is_error) = tool_payload(&resp);
        assert!(!is_error);
        assert_eq!(payload["columns"], json!(["one", "two"]));
        assert_eq!(payload["rows"], json!([["1", "2"]]));
        assert_eq!(payload["row_count"], 1);
        assert_eq!(payload["truncated"], false);
    }

    #[tokio::test]
    async fn train_documentation_round_trips_through_list_schema() {
        let state = configured();
        let trained = call(
            &state,
            &tool_call_line(8, "train", json!({"kind": "documentation",
                "content": "Revenue excludes cancelled orders."})),
        )
        .await
        .unwrap();
        let (payload, is_error) = tool_payload(&trained);
        assert!(!is_error);
        assert_eq!(payload["trained"], "documentation");

        let listed = call(&state, &tool_call_line(9, "list_schema", json!({}))).await.unwrap();
        let (payload, is_error) = tool_payload(&listed);
        assert!(!is_error);
        assert_eq!(payload["documentation"], json!(["Revenue excludes cancelled orders."]));
    }

    #[tokio::test]
    async fn train_validates_arguments() {
        let state = configured();
        let bad_kind =
            call(&state, &tool_call_line(10, "train", json!({"kind": "nonsense"}))).await.unwrap();
        assert!(tool_payload(&bad_kind).1);

        let missing_sql = call(
            &state,
            &tool_call_line(11, "train", json!({"kind": "question_sql", "question": "q"})),
        )
        .await
        .unwrap();
        assert!(tool_payload(&missing_sql).1);
    }
}