pmcp-server-toolkit 0.1.0

Runtime library for config-driven MCP servers — auth, secrets, static resources/prompts, [[tools]] synthesizer, code-mode wiring
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
//! Dialect-aware placeholder translation (CONN-03).
//!
//! Translates canonical `:name` named placeholders in a SQL string into the
//! positional form each backend dialect expects, while preserving the binding
//! order so the per-backend `execute()` impl can build a positional argument
//! list from the caller's `&[(String, serde_json::Value)]`.
//!
//! The translation walks the SQL char-by-char with a small state machine
//! ([`SqlWalker`]) that tracks string literals, line/block comments, and the
//! placeholder substate so a `:name` inside `'...'`, `"..."`, `-- ...`, or
//! `/* ... */` is NEVER rewritten. Each helper stays under PMAT cog 25 via the
//! split-helper form (PATTERNS Pattern G) — no cognitive-complexity allow
//! attribute is needed anywhere in this module.
//!
//! **Placeholder-recognition rule (REVIEWS H7):** in `Normal` state a `:` only
//! begins a placeholder if the NEXT char is `[A-Za-z_]`. A `::` is a Postgres
//! cast (consumed verbatim, the following type identifier is swallowed by the
//! transitional `CastTypeName` state), a `:=` is a MySQL session-var assignment,
//! and `:1bad` is malformed — all three emit the bare `:` verbatim and stay in
//! `Normal`. This eliminates the `::text` mis-translation regression class.
//!
//! Public surface lives at `pmcp_server_toolkit::sql::translate_placeholders`
//! (D-05): a free helper, NOT a trait method — every connector calls it the
//! same way, so putting it on the trait would invite per-backend drift.

// Why: dialect display names are proper nouns that clippy::doc_markdown
// otherwise flags as needing back-ticks.
#![allow(clippy::doc_markdown)]

use super::Dialect;
use std::fmt::Write as _;
use std::iter::Peekable;
use std::str::Chars;

/// Result of translating canonical `:name` placeholders into a dialect's
/// positional form, plus the binding order needed to bind values positionally.
///
/// Per-backend `execute()` impls destructure this and iterate `ordered_params`
/// to bind driver-native positional parameters from the caller's
/// `&[(String, serde_json::Value)]` named pairs.
///
/// # Example
///
/// ```
/// use pmcp_server_toolkit::sql::{translate_placeholders, Dialect, TranslatedSql};
///
/// let translated: TranslatedSql =
///     translate_placeholders("SELECT :id FROM t", Dialect::Postgres);
/// assert_eq!(translated.sql, "SELECT $1 FROM t");
/// assert_eq!(translated.ordered_params, vec!["id".to_string()]);
/// ```
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct TranslatedSql {
    /// The SQL string with placeholders rewritten into the target dialect's
    /// positional form (`$1`/`$2` for Postgres, `?` for MySQL/Athena, `:name`
    /// kept for SQLite).
    pub sql: String,
    /// Placeholder names in positional binding order. The Nth entry names the
    /// value that the Nth positional parameter should bind.
    pub ordered_params: Vec<String>,
}

/// Translate canonical `:name` placeholders in `sql` into `dialect`'s
/// positional form, returning the rewritten SQL plus the binding order.
///
/// Placeholders inside string literals (`'...'`, `"..."`), line comments
/// (`-- ...`), and block comments (`/* ... */`, nested) are left verbatim. A
/// `::text` Postgres cast, a `:=` MySQL session-var, and a malformed `:1bad`
/// are all emitted verbatim per REVIEWS H7.
///
/// # Example
///
/// ```
/// use pmcp_server_toolkit::sql::{translate_placeholders, Dialect};
///
/// // Repeated names get a fresh positional index per appearance.
/// let t = translate_placeholders("WHERE a = :a AND b = :b AND c = :a", Dialect::Postgres);
/// assert_eq!(t.sql, "WHERE a = $1 AND b = $2 AND c = $3");
/// assert_eq!(t.ordered_params, vec!["a", "b", "a"]);
///
/// // SQLite keeps the SQL byte-identical but still records bind order.
/// let s = translate_placeholders("SELECT :id FROM t", Dialect::Sqlite);
/// assert_eq!(s.sql, "SELECT :id FROM t");
/// assert_eq!(s.ordered_params, vec!["id"]);
/// ```
#[must_use]
pub fn translate_placeholders(sql: &str, dialect: Dialect) -> TranslatedSql {
    let mut walker = SqlWalker::new(sql, dialect);
    walker.run();
    walker.into_translated()
}

/// State of the [`SqlWalker`] char-by-char scan.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum State {
    /// Default scanning state — placeholders are recognized here.
    Normal,
    /// Inside a `'...'` or `"..."` literal (the quote char is carried).
    StringLiteral(char),
    /// Inside a `-- ...` line comment (ends at `\n`).
    LineComment,
    /// Inside a `/* ... */` block comment; the `usize` tracks nesting depth.
    BlockComment(usize),
    /// Reading a placeholder identifier into `pending_name`.
    Placeholder,
    /// Transitional state after `::` — swallows the following type identifier
    /// (e.g. `text` in `::text`) so no placeholder lookup happens mid-cast.
    CastTypeName,
}

/// Char-by-char SQL scanner that rewrites `:name` placeholders into a dialect's
/// positional form while skipping literals and comments.
struct SqlWalker<'a> {
    chars: Peekable<Chars<'a>>,
    state: State,
    out: String,
    order: Vec<String>,
    pg_index: usize,
    dialect: Dialect,
    pending_name: String,
}

impl<'a> SqlWalker<'a> {
    /// Initialize the walker over `sql` for `dialect`.
    fn new(sql: &'a str, dialect: Dialect) -> Self {
        Self {
            chars: sql.chars().peekable(),
            state: State::Normal,
            out: String::with_capacity(sql.len()),
            order: Vec::new(),
            pg_index: 0,
            dialect,
            pending_name: String::new(),
        }
    }

    /// Drive the scan to completion, dispatching each char by current state.
    fn run(&mut self) {
        while let Some(c) = self.chars.next() {
            match self.state {
                State::Normal => self.handle_normal(c),
                State::StringLiteral(q) => self.handle_string(c, q),
                State::LineComment => self.handle_line_comment(c),
                State::BlockComment(depth) => self.handle_block_comment(c, depth),
                State::Placeholder => self.handle_placeholder(c),
                State::CastTypeName => self.handle_cast_type_name(c),
            }
        }
        // EOF inside a placeholder: emit whatever was accumulated.
        if self.state == State::Placeholder {
            self.emit_placeholder_from_pending();
        }
    }

    /// Handle a char in `Normal` state. Delegates the `:`-precedence logic to
    /// [`Self::dispatch_colon`] (REVIEWS H7).
    fn handle_normal(&mut self, c: char) {
        match c {
            '\'' | '"' => {
                self.out.push(c);
                self.state = State::StringLiteral(c);
            },
            '-' if self.chars.peek() == Some(&'-') => {
                self.out.push(c);
                self.out.push('-');
                self.chars.next();
                self.state = State::LineComment;
            },
            '/' if self.chars.peek() == Some(&'*') => {
                self.out.push(c);
                self.out.push('*');
                self.chars.next();
                self.state = State::BlockComment(1);
            },
            ':' => self.dispatch_colon(),
            _ => self.out.push(c),
        }
    }

    /// REVIEWS H7 colon-precedence: decide whether a `:` begins a placeholder,
    /// a `::` cast, or is a verbatim character. Assumes the `:` was just
    /// consumed by `run()` and `self.state == Normal`.
    fn dispatch_colon(&mut self) {
        match self.chars.peek().copied() {
            Some(':') => {
                // `::` cast prefix — emit both colons verbatim and swallow the
                // following type identifier so no placeholder lookup occurs.
                self.out.push(':');
                self.out.push(':');
                self.chars.next();
                self.state = State::CastTypeName;
            },
            Some(n) if is_ident_start(n) => {
                // Valid placeholder start — consume the `:` (do not emit) and
                // begin reading the identifier.
                self.pending_name.clear();
                self.state = State::Placeholder;
            },
            _ => self.out.push(':'),
        }
    }

    /// Read a placeholder identifier. On a non-identifier char, flush the
    /// placeholder and re-dispatch that char from `Normal`.
    fn handle_placeholder(&mut self, c: char) {
        if is_ident_continue(c) {
            self.pending_name.push(c);
        } else {
            self.emit_placeholder_from_pending();
            self.handle_normal(c);
        }
    }

    /// Swallow the type identifier following a `::` cast, then return to Normal.
    fn handle_cast_type_name(&mut self, c: char) {
        self.out.push(c);
        if !is_ident_continue(c) {
            self.state = State::Normal;
        }
    }

    /// Emit the dialect's positional form for `pending_name`, record the bind
    /// order, and return to `Normal`. Entering `Placeholder` already required a
    /// valid identifier-start, so `pending_name` is never empty here.
    fn emit_placeholder_from_pending(&mut self) {
        match self.dialect {
            Dialect::Postgres => {
                self.pg_index += 1;
                // Writing a formatted integer into a String never fails.
                let _ = write!(self.out, "${}", self.pg_index);
            },
            Dialect::MySql | Dialect::Athena => self.out.push('?'),
            Dialect::Sqlite => {
                let _ = write!(self.out, ":{}", self.pending_name);
            },
        }
        self.order.push(std::mem::take(&mut self.pending_name));
        self.state = State::Normal;
    }

    /// Handle a char inside a `'...'` / `"..."` literal. A doubled quote
    /// (`''` / `""`) is an escape and stays inside the literal.
    fn handle_string(&mut self, c: char, q: char) {
        self.out.push(c);
        if c == q {
            if self.chars.peek() == Some(&q) {
                self.out.push(q);
                self.chars.next();
            } else {
                self.state = State::Normal;
            }
        }
    }

    /// Handle a char inside a `-- ...` line comment; ends at newline.
    fn handle_line_comment(&mut self, c: char) {
        self.out.push(c);
        if c == '\n' {
            self.state = State::Normal;
        }
    }

    /// Handle a char inside a `/* ... */` block comment, tracking nesting.
    fn handle_block_comment(&mut self, c: char, depth: usize) {
        self.out.push(c);
        if c == '*' && self.chars.peek() == Some(&'/') {
            self.out.push('/');
            self.chars.next();
            self.state = if depth <= 1 {
                State::Normal
            } else {
                State::BlockComment(depth - 1)
            };
        } else if c == '/' && self.chars.peek() == Some(&'*') {
            self.out.push('*');
            self.chars.next();
            self.state = State::BlockComment(depth + 1);
        }
    }

    /// Consume the walker into its [`TranslatedSql`] result.
    fn into_translated(self) -> TranslatedSql {
        TranslatedSql {
            sql: self.out,
            ordered_params: self.order,
        }
    }
}

/// `true` if `c` can start a `:name` placeholder identifier (`[A-Za-z_]`).
fn is_ident_start(c: char) -> bool {
    c.is_ascii_alphabetic() || c == '_'
}

/// `true` if `c` can continue a placeholder identifier (`[A-Za-z0-9_]`).
fn is_ident_continue(c: char) -> bool {
    c.is_ascii_alphanumeric() || c == '_'
}

#[cfg(test)]
mod proptests {
    use super::*;
    use proptest::prelude::*;

    proptest! {
        /// Invariant 1: idempotence for `:name`-free SQL — `translate.sql == input`
        /// for every dialect, with no recorded binds.
        #[test]
        fn idempotence_no_placeholders(s in "[A-Za-z0-9 _\\.,;\\(\\)=]*") {
            for d in [Dialect::Postgres, Dialect::MySql, Dialect::Athena, Dialect::Sqlite] {
                let t = translate_placeholders(&s, d);
                prop_assert_eq!(&t.sql, &s);
                prop_assert!(t.ordered_params.is_empty());
            }
        }

        /// Invariant 2: bind-order preservation — `ordered_params` lists
        /// placeholder names left-to-right in their textual order.
        #[test]
        fn bind_order_preserved(names in proptest::collection::vec("[a-z]{1,5}", 1..=5)) {
            let sql = names.iter().map(|n| format!(":{n}")).collect::<Vec<_>>().join(", ");
            let t = translate_placeholders(&sql, Dialect::Postgres);
            prop_assert_eq!(t.ordered_params, names);
        }

        /// Invariant 3: Postgres positional indexing — `$1..=$n` are present and
        /// contiguous, and their count equals `ordered_params.len()`.
        #[test]
        fn postgres_positional_indexing(names in proptest::collection::vec("[a-z]{1,5}", 1..=5)) {
            let sql = names.iter().map(|n| format!(":{n}")).collect::<Vec<_>>().join(", ");
            let t = translate_placeholders(&sql, Dialect::Postgres);
            prop_assert_eq!(t.ordered_params.len(), names.len());
            for i in 1..=names.len() {
                let token = format!("${i}");
                prop_assert!(t.sql.contains(&token));
            }
            // No gap above n.
            let above = format!("${}", names.len() + 1);
            prop_assert!(!t.sql.contains(&above));
        }

        /// Invariant 4: SQLite identity — `Dialect::Sqlite` keeps SQL
        /// byte-identical; only `ordered_params` differs.
        #[test]
        fn sqlite_identity(s in any::<String>()) {
            let t = translate_placeholders(&s, Dialect::Sqlite);
            prop_assert_eq!(t.sql, s);
        }

        /// Invariant 5: no panic on arbitrary `&str` input across all dialects.
        #[test]
        fn no_panic_on_arbitrary_input(s in any::<String>()) {
            for d in [Dialect::Postgres, Dialect::MySql, Dialect::Athena, Dialect::Sqlite] {
                let _ = translate_placeholders(&s, d);
            }
        }
    }
}

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

    fn t(sql: &str, d: Dialect) -> TranslatedSql {
        translate_placeholders(sql, d)
    }

    #[test]
    fn empty_input_is_identity() {
        let r = t("", Dialect::Postgres);
        assert_eq!(r.sql, "");
        assert!(r.ordered_params.is_empty());
    }

    #[test]
    fn no_placeholder_is_identity_mysql() {
        let r = t("SELECT 1", Dialect::MySql);
        assert_eq!(r.sql, "SELECT 1");
        assert!(r.ordered_params.is_empty());
    }

    #[test]
    fn single_placeholder_postgres() {
        let r = t("SELECT :id FROM t", Dialect::Postgres);
        assert_eq!(r.sql, "SELECT $1 FROM t");
        assert_eq!(r.ordered_params, vec!["id"]);
    }

    #[test]
    fn single_placeholder_mysql() {
        let r = t("SELECT :id FROM t", Dialect::MySql);
        assert_eq!(r.sql, "SELECT ? FROM t");
        assert_eq!(r.ordered_params, vec!["id"]);
    }

    #[test]
    fn single_placeholder_athena() {
        let r = t("SELECT :id FROM t", Dialect::Athena);
        assert_eq!(r.sql, "SELECT ? FROM t");
        assert_eq!(r.ordered_params, vec!["id"]);
    }

    #[test]
    fn single_placeholder_sqlite_is_identity_with_bind_order() {
        let r = t("SELECT :id FROM t", Dialect::Sqlite);
        assert_eq!(r.sql, "SELECT :id FROM t");
        assert_eq!(r.ordered_params, vec!["id"]);
    }

    #[test]
    fn repeated_name_gets_fresh_index_postgres() {
        let r = t("WHERE a = :a AND b = :b AND c = :a", Dialect::Postgres);
        assert_eq!(r.sql, "WHERE a = $1 AND b = $2 AND c = $3");
        assert_eq!(r.ordered_params, vec!["a", "b", "a"]);
    }

    #[test]
    fn three_distinct_names_all_dialects_match_must_haves() {
        let sql = "SELECT :id FROM t WHERE x = :x AND y = :id";
        let pg = t(sql, Dialect::Postgres);
        assert_eq!(pg.sql, "SELECT $1 FROM t WHERE x = $2 AND y = $3");
        assert_eq!(pg.ordered_params, vec!["id", "x", "id"]);

        let my = t(sql, Dialect::MySql);
        assert_eq!(my.sql, "SELECT ? FROM t WHERE x = ? AND y = ?");
        assert_eq!(my.ordered_params, vec!["id", "x", "id"]);

        let at = t(sql, Dialect::Athena);
        assert_eq!(at.sql, "SELECT ? FROM t WHERE x = ? AND y = ?");
        assert_eq!(at.ordered_params, vec!["id", "x", "id"]);

        let lite = t(sql, Dialect::Sqlite);
        assert_eq!(lite.sql, sql);
        assert_eq!(lite.ordered_params, vec!["id", "x", "id"]);
    }

    #[test]
    fn placeholder_inside_string_literal_not_translated() {
        let r = t("SELECT 'WHERE name = :foo' AS x", Dialect::Postgres);
        assert_eq!(r.sql, "SELECT 'WHERE name = :foo' AS x");
        assert!(r.ordered_params.is_empty());
    }

    #[test]
    fn doubled_single_quote_escape_stays_in_literal() {
        let r = t("SELECT 'it''s :foo' AS x", Dialect::Postgres);
        assert_eq!(r.sql, "SELECT 'it''s :foo' AS x");
        assert!(r.ordered_params.is_empty());
    }

    #[test]
    fn double_quoted_identifier_skips_placeholder() {
        let r = t("SELECT \"col:name\" FROM t", Dialect::Postgres);
        assert_eq!(r.sql, "SELECT \"col:name\" FROM t");
        assert!(r.ordered_params.is_empty());
    }

    #[test]
    fn placeholder_in_line_comment_not_translated() {
        let r = t("SELECT 1 -- bind :id here", Dialect::Postgres);
        assert_eq!(r.sql, "SELECT 1 -- bind :id here");
        assert!(r.ordered_params.is_empty());
    }

    #[test]
    fn line_comment_ends_at_newline() {
        let r = t("SELECT 1 -- :a\nWHERE x = :b", Dialect::Postgres);
        assert_eq!(r.sql, "SELECT 1 -- :a\nWHERE x = $1");
        assert_eq!(r.ordered_params, vec!["b"]);
    }

    #[test]
    fn placeholder_in_block_comment_not_translated() {
        let r = t("SELECT /* :foo */ 1", Dialect::Postgres);
        assert_eq!(r.sql, "SELECT /* :foo */ 1");
        assert!(r.ordered_params.is_empty());
    }

    #[test]
    fn nested_block_comment_tracked_via_depth() {
        let r = t("SELECT /* /* :foo */ :bar */ :baz", Dialect::Postgres);
        assert_eq!(r.sql, "SELECT /* /* :foo */ :bar */ $1");
        assert_eq!(r.ordered_params, vec!["baz"]);
    }

    // ---- REVIEWS H7 mandatory named tests ----

    #[test]
    fn postgres_double_colon_cast_preserves_text_identifier() {
        let r = t("SELECT :id::text FROM t", Dialect::Postgres);
        assert_eq!(
            r,
            TranslatedSql {
                sql: "SELECT $1::text FROM t".into(),
                ordered_params: vec!["id".into()],
            }
        );
    }

    #[test]
    fn postgres_double_colon_int_cast_no_placeholder() {
        let r = t("SELECT 1::int", Dialect::Postgres);
        assert_eq!(
            r,
            TranslatedSql {
                sql: "SELECT 1::int".into(),
                ordered_params: vec![],
            }
        );
    }

    #[test]
    fn mysql_session_variable_assignment_not_a_placeholder() {
        let r = t("SET @x := 5", Dialect::MySql);
        assert_eq!(
            r,
            TranslatedSql {
                sql: "SET @x := 5".into(),
                ordered_params: vec![],
            }
        );
    }

    #[test]
    fn colon_followed_by_digit_emits_verbatim() {
        let r = t("SELECT :1bad FROM t", Dialect::Postgres);
        assert_eq!(
            r,
            TranslatedSql {
                sql: "SELECT :1bad FROM t".into(),
                ordered_params: vec![],
            }
        );
    }

    #[test]
    fn string_literal_cast_both_colons_verbatim() {
        let r = t("SELECT 'foo'::text", Dialect::Postgres);
        assert_eq!(r.sql, "SELECT 'foo'::text");
        assert!(r.ordered_params.is_empty());
    }

    #[test]
    fn placeholder_then_cast_then_placeholder() {
        let r = t("SELECT :a::text, :b FROM t", Dialect::Postgres);
        assert_eq!(r.sql, "SELECT $1::text, $2 FROM t");
        assert_eq!(r.ordered_params, vec!["a", "b"]);
    }

    #[test]
    fn lone_colon_at_eof_emits_verbatim() {
        let r = t("SELECT 1:", Dialect::Postgres);
        assert_eq!(r.sql, "SELECT 1:");
        assert!(r.ordered_params.is_empty());
    }

    #[test]
    fn underscore_leading_placeholder_name() {
        let r = t("WHERE x = :_id", Dialect::Postgres);
        assert_eq!(r.sql, "WHERE x = $1");
        assert_eq!(r.ordered_params, vec!["_id"]);
    }

    #[test]
    fn unterminated_literal_does_not_panic() {
        let r = t("SELECT 'unterminated :foo", Dialect::Postgres);
        // Remainder is treated as literal content; :foo is NOT translated.
        assert_eq!(r.sql, "SELECT 'unterminated :foo");
        assert!(r.ordered_params.is_empty());
    }

    #[test]
    fn unterminated_block_comment_does_not_panic() {
        let r = t("SELECT /* :foo", Dialect::Postgres);
        assert_eq!(r.sql, "SELECT /* :foo");
        assert!(r.ordered_params.is_empty());
    }

    #[test]
    fn placeholder_at_eof_is_emitted() {
        let r = t("WHERE id = :id", Dialect::Postgres);
        assert_eq!(r.sql, "WHERE id = $1");
        assert_eq!(r.ordered_params, vec!["id"]);
    }
}