reddb-io-server 1.1.2

RedDB server-side engine: storage, runtime, replication, MCP, AI, and the gRPC/HTTP/RedWire/PG-wire dispatchers. Re-exported by the umbrella `reddb` crate.
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
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
//! MCP tool definitions for RedDB.
//!
//! Each tool exposes a specific RedDB capability to AI agents with a
//! typed JSON Schema input specification.

use crate::json::{Map, Value as JsonValue};

/// Definition of an MCP tool exposed by the RedDB server.
pub struct ToolDef {
    pub name: &'static str,
    pub description: &'static str,
    pub input_schema: JsonValue,
}

/// Build a JSON Schema object from a list of field descriptors.
fn schema(properties: Vec<(&str, &str, &str)>, required: Vec<&str>) -> JsonValue {
    let mut props = Map::new();
    for (name, field_type, description) in properties {
        let mut field = Map::new();
        field.insert(
            "type".to_string(),
            JsonValue::String(field_type.to_string()),
        );
        if !description.is_empty() {
            field.insert(
                "description".to_string(),
                JsonValue::String(description.to_string()),
            );
        }
        props.insert(name.to_string(), JsonValue::Object(field));
    }

    let mut obj = Map::new();
    obj.insert("type".to_string(), JsonValue::String("object".to_string()));
    obj.insert("properties".to_string(), JsonValue::Object(props));
    obj.insert(
        "required".to_string(),
        JsonValue::Array(
            required
                .into_iter()
                .map(|s| JsonValue::String(s.to_string()))
                .collect(),
        ),
    );
    obj.insert("additionalProperties".to_string(), JsonValue::Bool(false));
    JsonValue::Object(obj)
}

/// Build a JSON Schema object that accepts items with flexible inner types.
fn schema_with_nested(properties: Vec<(&str, JsonValue)>, required: Vec<&str>) -> JsonValue {
    let mut props = Map::new();
    for (name, descriptor) in properties {
        props.insert(name.to_string(), descriptor);
    }

    let mut obj = Map::new();
    obj.insert("type".to_string(), JsonValue::String("object".to_string()));
    obj.insert("properties".to_string(), JsonValue::Object(props));
    obj.insert(
        "required".to_string(),
        JsonValue::Array(
            required
                .into_iter()
                .map(|s| JsonValue::String(s.to_string()))
                .collect(),
        ),
    );
    obj.insert("additionalProperties".to_string(), JsonValue::Bool(false));
    JsonValue::Object(obj)
}

/// Simple string field descriptor.
fn string_field(description: &str) -> JsonValue {
    let mut f = Map::new();
    f.insert("type".to_string(), JsonValue::String("string".to_string()));
    f.insert(
        "description".to_string(),
        JsonValue::String(description.to_string()),
    );
    JsonValue::Object(f)
}

/// Simple number field descriptor.
fn number_field(description: &str) -> JsonValue {
    let mut f = Map::new();
    f.insert("type".to_string(), JsonValue::String("number".to_string()));
    f.insert(
        "description".to_string(),
        JsonValue::String(description.to_string()),
    );
    JsonValue::Object(f)
}

/// Simple integer field descriptor.
fn integer_field(description: &str) -> JsonValue {
    let mut f = Map::new();
    f.insert("type".to_string(), JsonValue::String("integer".to_string()));
    f.insert(
        "description".to_string(),
        JsonValue::String(description.to_string()),
    );
    JsonValue::Object(f)
}

/// Simple boolean field descriptor.
fn boolean_field(description: &str) -> JsonValue {
    let mut f = Map::new();
    f.insert("type".to_string(), JsonValue::String("boolean".to_string()));
    f.insert(
        "description".to_string(),
        JsonValue::String(description.to_string()),
    );
    JsonValue::Object(f)
}

fn type_field(field_type: &str) -> JsonValue {
    let mut f = Map::new();
    f.insert(
        "type".to_string(),
        JsonValue::String(field_type.to_string()),
    );
    JsonValue::Object(f)
}

/// Object field descriptor (accepts arbitrary JSON object).
fn object_field(description: &str) -> JsonValue {
    let mut f = Map::new();
    f.insert("type".to_string(), JsonValue::String("object".to_string()));
    f.insert(
        "description".to_string(),
        JsonValue::String(description.to_string()),
    );
    JsonValue::Object(f)
}

/// Array-of-numbers field descriptor.
fn number_array_field(description: &str) -> JsonValue {
    let mut items = Map::new();
    items.insert("type".to_string(), JsonValue::String("number".to_string()));

    let mut f = Map::new();
    f.insert("type".to_string(), JsonValue::String("array".to_string()));
    f.insert("items".to_string(), JsonValue::Object(items));
    f.insert(
        "description".to_string(),
        JsonValue::String(description.to_string()),
    );
    JsonValue::Object(f)
}

/// Array-of-strings field descriptor.
fn string_array_field(description: &str) -> JsonValue {
    let mut items = Map::new();
    items.insert("type".to_string(), JsonValue::String("string".to_string()));

    let mut f = Map::new();
    f.insert("type".to_string(), JsonValue::String("array".to_string()));
    f.insert("items".to_string(), JsonValue::Object(items));
    f.insert(
        "description".to_string(),
        JsonValue::String(description.to_string()),
    );
    JsonValue::Object(f)
}

/// Return all tool definitions exposed by the RedDB MCP server.
pub fn all_tools() -> Vec<ToolDef> {
    vec![
        ToolDef {
            name: "reddb_query",
            description: "Execute a SQL or universal query against RedDB. Supports SELECT, INSERT, UPDATE, DELETE, and graph queries (Gremlin, Cypher, SPARQL).\n\nALWAYS pass user-provided values via the `params` array using `$1`, `$2`, ... placeholders rather than interpolating them into the SQL string. Example: `{\"sql\": \"SELECT * FROM users WHERE id = $1\", \"params\": [42]}`. Interpolating user input directly is unsafe and brittle; the parameterized form is type-checked and immune to injection.",
            input_schema: schema_with_nested(
                vec![
                    ("sql", string_field("SQL or universal query to execute. Use `$1`, `$2`, ... placeholders for any value that came from the user.")),
                    ("params", {
                        let mut items = Map::new();
                        items.insert("description".to_string(), JsonValue::String("Bind value for the matching $N placeholder. Accepts null, boolean, number, string, array, or object.".to_string()));
                        items.insert(
                            "anyOf".to_string(),
                            JsonValue::Array(vec![
                                type_field("null"),
                                type_field("boolean"),
                                type_field("number"),
                                type_field("string"),
                                type_field("array"),
                                type_field("object"),
                            ]),
                        );
                        let mut f = Map::new();
                        f.insert("type".to_string(), JsonValue::String("array".to_string()));
                        f.insert("items".to_string(), JsonValue::Object(items));
                        f.insert(
                            "description".to_string(),
                            JsonValue::String(
                                "Positional bind values for `$1`, `$2`, ... in `sql`. Index 0 binds `$1`."
                                    .to_string(),
                            ),
                        );
                        JsonValue::Object(f)
                    }),
                ],
                vec!["sql"],
            ),
        },
        ToolDef {
            name: "reddb_collections",
            description: "List all collections in the database.",
            input_schema: schema(vec![], vec![]),
        },
        ToolDef {
            name: "reddb_insert_row",
            description: "Insert a table row into a collection.",
            input_schema: schema_with_nested(
                vec![
                    ("collection", string_field("Target collection name")),
                    ("data", object_field("Object with field name/value pairs to insert")),
                    ("metadata", object_field("Optional metadata key/value pairs")),
                ],
                vec!["collection", "data"],
            ),
        },
        ToolDef {
            name: "reddb_insert_node",
            description: "Insert a graph node into a collection.",
            input_schema: schema_with_nested(
                vec![
                    ("collection", string_field("Target collection name")),
                    ("label", string_field("Node label (identifier)")),
                    ("node_type", string_field("Optional node type classification")),
                    ("properties", object_field("Optional node properties as key/value pairs")),
                    ("metadata", object_field("Optional metadata key/value pairs")),
                ],
                vec!["collection", "label"],
            ),
        },
        ToolDef {
            name: "reddb_insert_edge",
            description: "Insert a graph edge between two nodes.",
            input_schema: schema_with_nested(
                vec![
                    ("collection", string_field("Target collection name")),
                    ("label", string_field("Edge label (relationship type)")),
                    ("from", integer_field("Source node entity ID")),
                    ("to", integer_field("Target node entity ID")),
                    ("weight", number_field("Optional edge weight (default 1.0)")),
                    ("properties", object_field("Optional edge properties")),
                    ("metadata", object_field("Optional metadata key/value pairs")),
                ],
                vec!["collection", "label", "from", "to"],
            ),
        },
        ToolDef {
            name: "reddb_insert_vector",
            description: "Insert a vector embedding into a collection.",
            input_schema: schema_with_nested(
                vec![
                    ("collection", string_field("Target collection name")),
                    ("dense", number_array_field("Dense vector (array of floats)")),
                    ("content", string_field("Optional text content associated with the vector")),
                    ("metadata", object_field("Optional metadata key/value pairs")),
                ],
                vec!["collection", "dense"],
            ),
        },
        ToolDef {
            name: "reddb_insert_document",
            description: "Insert a JSON document into a collection.",
            input_schema: schema_with_nested(
                vec![
                    ("collection", string_field("Target collection name")),
                    ("body", object_field("JSON document body")),
                    ("metadata", object_field("Optional metadata key/value pairs")),
                ],
                vec!["collection", "body"],
            ),
        },
        ToolDef {
            name: "reddb_kv_get",
            description: "Get a value by key from a key-value collection.",
            input_schema: schema(
                vec![
                    ("collection", "string", "Collection name"),
                    ("key", "string", "Key to retrieve"),
                ],
                vec!["collection", "key"],
            ),
        },
        ToolDef {
            name: "reddb_kv_set",
            description: "Set a key-value pair in a collection.",
            input_schema: schema_with_nested(
                vec![
                    ("collection", string_field("Collection name")),
                    ("key", string_field("Key to set")),
                    ("value", {
                        let mut f = Map::new();
                        f.insert("description".to_string(), JsonValue::String("Value to store (string, number, boolean, or null)".to_string()));
                        JsonValue::Object(f)
                    }),
                    ("tags", string_array_field("Optional KV invalidation tags")),
                    ("metadata", object_field("Optional metadata key/value pairs")),
                ],
                vec!["collection", "key", "value"],
            ),
        },
        ToolDef {
            name: "reddb_kv_invalidate_tags",
            description: "Delete every KV entry in a collection tagged with any listed tag.",
            input_schema: schema_with_nested(
                vec![
                    ("collection", string_field("Collection name")),
                    ("tags", string_array_field("Tags to invalidate")),
                ],
                vec!["collection", "tags"],
            ),
        },
        ToolDef {
            name: "reddb_config_get",
            description: "Get a Config value without resolving SecretRef targets.",
            input_schema: schema(
                vec![
                    ("collection", "string", "Config collection name"),
                    ("key", "string", "Config key to retrieve"),
                ],
                vec!["collection", "key"],
            ),
        },
        ToolDef {
            name: "reddb_config_put",
            description: "Set a Config value. TTL and counter operations are not supported for Config.",
            input_schema: schema_with_nested(
                vec![
                    ("collection", string_field("Config collection name")),
                    ("key", string_field("Config key to set")),
                    ("value", {
                        let mut f = Map::new();
                        f.insert("description".to_string(), JsonValue::String("Value to store, or an object when paired with secret_ref".to_string()));
                        JsonValue::Object(f)
                    }),
                    ("secret_ref", object_field("Optional { collection, key } Vault SecretRef")),
                    ("tags", string_array_field("Optional Config tags")),
                ],
                vec!["collection", "key"],
            ),
        },
        ToolDef {
            name: "reddb_config_resolve",
            description: "Explicitly resolve a Config SecretRef. Requires the corresponding Vault unseal permission.",
            input_schema: schema(
                vec![
                    ("collection", "string", "Config collection name"),
                    ("key", "string", "Config key to resolve"),
                ],
                vec!["collection", "key"],
            ),
        },
        ToolDef {
            name: "reddb_vault_get",
            description: "Get Vault metadata for a secret. Does not return plaintext.",
            input_schema: schema(
                vec![
                    ("collection", "string", "Vault collection name"),
                    ("key", "string", "Vault key to retrieve metadata for"),
                ],
                vec!["collection", "key"],
            ),
        },
        ToolDef {
            name: "reddb_vault_put",
            description: "Store a sealed Vault secret. TTL and counter operations are not supported for Vault.",
            input_schema: schema_with_nested(
                vec![
                    ("collection", string_field("Vault collection name")),
                    ("key", string_field("Vault key to set")),
                    ("value", {
                        let mut f = Map::new();
                        f.insert("description".to_string(), JsonValue::String("Secret value to seal".to_string()));
                        JsonValue::Object(f)
                    }),
                    ("tags", string_array_field("Optional Vault tags")),
                ],
                vec!["collection", "key", "value"],
            ),
        },
        ToolDef {
            name: "reddb_vault_unseal",
            description: "Explicitly unseal a Vault secret and return plaintext to an authorized caller.",
            input_schema: schema(
                vec![
                    ("collection", "string", "Vault collection name"),
                    ("key", "string", "Vault key to unseal"),
                ],
                vec!["collection", "key"],
            ),
        },
        ToolDef {
            name: "reddb_delete",
            description: "Delete an entity by ID from a collection.",
            input_schema: schema(
                vec![
                    ("collection", "string", "Collection name"),
                    ("id", "integer", "Entity ID to delete"),
                ],
                vec!["collection", "id"],
            ),
        },
        ToolDef {
            name: "reddb_search_vector",
            description: "Search for similar vectors using cosine similarity.",
            input_schema: schema_with_nested(
                vec![
                    ("collection", string_field("Collection to search in")),
                    ("vector", number_array_field("Query vector (array of floats)")),
                    ("k", integer_field("Number of results to return (default 10)")),
                    ("min_score", number_field("Minimum similarity score threshold (default 0.0)")),
                ],
                vec!["collection", "vector"],
            ),
        },
        ToolDef {
            name: "reddb_search_text",
            description: "Full-text search across collections.",
            input_schema: schema_with_nested(
                vec![
                    ("query", string_field("Search query string")),
                    ("collections", {
                        let mut items = Map::new();
                        items.insert("type".to_string(), JsonValue::String("string".to_string()));
                        let mut f = Map::new();
                        f.insert("type".to_string(), JsonValue::String("array".to_string()));
                        f.insert("items".to_string(), JsonValue::Object(items));
                        f.insert("description".to_string(), JsonValue::String("Optional list of collections to search".to_string()));
                        JsonValue::Object(f)
                    }),
                    ("limit", integer_field("Maximum number of results (default 10)")),
                    ("fuzzy", boolean_field("Enable fuzzy matching (default false)")),
                ],
                vec!["query"],
            ),
        },
        ToolDef {
            name: "reddb_health",
            description: "Check database health and return runtime statistics.",
            input_schema: schema(vec![], vec![]),
        },
        ToolDef {
            name: "reddb_graph_traverse",
            description: "Traverse the graph from a source node using BFS or DFS.",
            input_schema: schema_with_nested(
                vec![
                    ("source", string_field("Source node label to start traversal from")),
                    ("direction", string_field("Traversal direction: 'outgoing', 'incoming', or 'both' (default 'outgoing')")),
                    ("max_depth", integer_field("Maximum traversal depth (default 3)")),
                    ("strategy", string_field("Traversal strategy: 'bfs' or 'dfs' (default 'bfs')")),
                ],
                vec!["source"],
            ),
        },
        ToolDef {
            name: "reddb_graph_shortest_path",
            description: "Find the shortest path between two graph nodes.",
            input_schema: schema_with_nested(
                vec![
                    ("source", string_field("Source node label")),
                    ("target", string_field("Target node label")),
                    ("direction", string_field("Edge direction: 'outgoing', 'incoming', or 'both' (default 'outgoing')")),
                    (
                        "algorithm",
                        string_field(
                            "Path algorithm: 'bfs', 'dijkstra', 'astar', or 'bellman_ford' (default 'bfs')",
                        ),
                    ),
                ],
                vec!["source", "target"],
            ),
        },
        // Auth tools
        ToolDef {
            name: "reddb_auth_bootstrap",
            description: "Bootstrap the first admin user. Only works when no users exist yet. Returns the admin user and an API key.",
            input_schema: schema(
                vec![
                    ("username", "string", "Admin username"),
                    ("password", "string", "Admin password"),
                ],
                vec!["username", "password"],
            ),
        },
        ToolDef {
            name: "reddb_auth_create_user",
            description: "Create a new database user with a role (admin, write, or read).",
            input_schema: schema(
                vec![
                    ("username", "string", "Username for the new user"),
                    ("password", "string", "Password for the new user"),
                    ("role", "string", "Role: 'admin', 'write', or 'read'"),
                ],
                vec!["username", "password", "role"],
            ),
        },
        ToolDef {
            name: "reddb_auth_login",
            description: "Login with username and password. Returns a session token.",
            input_schema: schema(
                vec![
                    ("username", "string", "Username"),
                    ("password", "string", "Password"),
                ],
                vec!["username", "password"],
            ),
        },
        ToolDef {
            name: "reddb_auth_create_api_key",
            description: "Create a persistent API key for a user.",
            input_schema: schema(
                vec![
                    ("username", "string", "Username to create the key for"),
                    ("name", "string", "Human-readable label for the key"),
                    ("role", "string", "Role for this key: 'admin', 'write', or 'read'"),
                ],
                vec!["username", "name", "role"],
            ),
        },
        ToolDef {
            name: "reddb_auth_list_users",
            description: "List all database users and their roles.",
            input_schema: schema(vec![], vec![]),
        },
        // Update / Scan tools
        ToolDef {
            name: "reddb_update",
            description: "Update entities in a collection matching a filter.",
            input_schema: schema_with_nested(
                vec![
                    ("collection", string_field("Collection name")),
                    ("set", object_field("Key-value pairs to update")),
                    (
                        "where_filter",
                        string_field(
                            "Optional SQL WHERE clause (e.g., \"age > 21\")",
                        ),
                    ),
                ],
                vec!["collection", "set"],
            ),
        },
        ToolDef {
            name: "reddb_scan",
            description: "Scan entities from a collection with pagination.",
            input_schema: schema_with_nested(
                vec![
                    ("collection", string_field("Collection to scan")),
                    ("limit", integer_field("Maximum number of results (default 10)")),
                    ("offset", integer_field("Number of records to skip (default 0)")),
                ],
                vec!["collection"],
            ),
        },
        // Graph analytics tools
        ToolDef {
            name: "reddb_graph_centrality",
            description: "Compute centrality scores for graph nodes. Algorithms: degree, closeness, betweenness, eigenvector, pagerank.",
            input_schema: schema_with_nested(
                vec![(
                    "algorithm",
                    string_field(
                        "Centrality algorithm: 'degree', 'closeness', 'betweenness', 'eigenvector', 'pagerank'",
                    ),
                )],
                vec!["algorithm"],
            ),
        },
        ToolDef {
            name: "reddb_graph_community",
            description: "Detect communities in the graph. Algorithms: label_propagation, louvain.",
            input_schema: schema_with_nested(
                vec![
                    (
                        "algorithm",
                        string_field(
                            "Community detection algorithm: 'label_propagation' or 'louvain'",
                        ),
                    ),
                    (
                        "max_iterations",
                        integer_field("Maximum iterations (default 100)"),
                    ),
                ],
                vec!["algorithm"],
            ),
        },
        ToolDef {
            name: "reddb_graph_components",
            description: "Find connected components in the graph.",
            input_schema: schema_with_nested(
                vec![(
                    "mode",
                    string_field(
                        "Component mode: 'weakly_connected' or 'strongly_connected' (default 'weakly_connected')",
                    ),
                )],
                vec![],
            ),
        },
        ToolDef {
            name: "reddb_graph_cycles",
            description: "Detect cycles in the graph.",
            input_schema: schema_with_nested(
                vec![
                    (
                        "max_length",
                        integer_field("Maximum cycle length (default 10)"),
                    ),
                    (
                        "max_cycles",
                        integer_field("Maximum number of cycles to return (default 100)"),
                    ),
                ],
                vec![],
            ),
        },
        ToolDef {
            name: "reddb_graph_clustering",
            description: "Compute clustering coefficient for the graph.",
            input_schema: schema(vec![], vec![]),
        },
        // DDL tools
        ToolDef {
            name: "reddb_create_collection",
            description: "Create a new collection (table) in the database.",
            input_schema: schema(
                vec![("name", "string", "Collection name to create")],
                vec!["name"],
            ),
        },
        ToolDef {
            name: "reddb_drop_collection",
            description: "Drop (delete) a collection from the database.",
            input_schema: schema(
                vec![("name", "string", "Collection name to drop")],
                vec!["name"],
            ),
        },
    ]
}

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

    #[test]
    fn test_all_tools_defined() {
        let tools = all_tools();
        assert!(tools.len() >= 24);
        let names: Vec<&str> = tools.iter().map(|t| t.name).collect();
        assert!(names.contains(&"reddb_query"));
        assert!(names.contains(&"reddb_collections"));
        assert!(names.contains(&"reddb_insert_row"));
        assert!(names.contains(&"reddb_insert_node"));
        assert!(names.contains(&"reddb_insert_edge"));
        assert!(names.contains(&"reddb_insert_vector"));
        assert!(names.contains(&"reddb_insert_document"));
        assert!(names.contains(&"reddb_kv_get"));
        assert!(names.contains(&"reddb_kv_set"));
        assert!(names.contains(&"reddb_config_get"));
        assert!(names.contains(&"reddb_config_put"));
        assert!(names.contains(&"reddb_config_resolve"));
        assert!(names.contains(&"reddb_vault_get"));
        assert!(names.contains(&"reddb_vault_put"));
        assert!(names.contains(&"reddb_vault_unseal"));
        assert!(names.contains(&"reddb_delete"));
        assert!(names.contains(&"reddb_search_vector"));
        assert!(names.contains(&"reddb_search_text"));
        assert!(names.contains(&"reddb_health"));
        assert!(names.contains(&"reddb_graph_traverse"));
        assert!(names.contains(&"reddb_graph_shortest_path"));
        // New tools
        assert!(names.contains(&"reddb_update"));
        assert!(names.contains(&"reddb_scan"));
        assert!(names.contains(&"reddb_graph_centrality"));
        assert!(names.contains(&"reddb_graph_community"));
        assert!(names.contains(&"reddb_graph_components"));
        assert!(names.contains(&"reddb_graph_cycles"));
        assert!(names.contains(&"reddb_graph_clustering"));
        assert!(names.contains(&"reddb_create_collection"));
        assert!(names.contains(&"reddb_drop_collection"));
    }

    #[test]
    fn test_tool_schemas_have_type() {
        for tool in all_tools() {
            assert_eq!(
                tool.input_schema.get("type").and_then(|v| v.as_str()),
                Some("object"),
                "tool '{}' schema must have type=object",
                tool.name,
            );
        }
    }

    #[test]
    fn test_query_tool_schema_exposes_optional_params_array() {
        let tools = all_tools();
        let tool = tools.iter().find(|t| t.name == "reddb_query").unwrap();
        let props = tool
            .input_schema
            .get("properties")
            .and_then(|v| v.as_object())
            .unwrap();
        let params = props.get("params").and_then(|v| v.as_object()).unwrap();
        assert_eq!(params.get("type").and_then(|v| v.as_str()), Some("array"));

        let required = tool
            .input_schema
            .get("required")
            .and_then(|v| v.as_array())
            .unwrap();
        let required_strs: Vec<&str> = required.iter().filter_map(|v| v.as_str()).collect();
        assert!(required_strs.contains(&"sql"));
        assert!(!required_strs.contains(&"params"));
    }

    #[test]
    fn test_update_tool_schema() {
        let tools = all_tools();
        let tool = tools.iter().find(|t| t.name == "reddb_update").unwrap();
        assert_eq!(tool.name, "reddb_update");
        let props = tool
            .input_schema
            .get("properties")
            .and_then(|v| v.as_object())
            .unwrap();
        assert!(props.contains_key("collection"));
        assert!(props.contains_key("set"));
        assert!(props.contains_key("where_filter"));
        let required = tool
            .input_schema
            .get("required")
            .and_then(|v| v.as_array())
            .unwrap();
        let required_strs: Vec<&str> = required.iter().filter_map(|v| v.as_str()).collect();
        assert!(required_strs.contains(&"collection"));
        assert!(required_strs.contains(&"set"));
        assert!(!required_strs.contains(&"where_filter"));
    }

    #[test]
    fn test_scan_tool_schema() {
        let tools = all_tools();
        let tool = tools.iter().find(|t| t.name == "reddb_scan").unwrap();
        let props = tool
            .input_schema
            .get("properties")
            .and_then(|v| v.as_object())
            .unwrap();
        assert!(props.contains_key("collection"));
        assert!(props.contains_key("limit"));
        assert!(props.contains_key("offset"));
        let required = tool
            .input_schema
            .get("required")
            .and_then(|v| v.as_array())
            .unwrap();
        let required_strs: Vec<&str> = required.iter().filter_map(|v| v.as_str()).collect();
        assert!(required_strs.contains(&"collection"));
        // limit and offset are optional
        assert!(!required_strs.contains(&"limit"));
        assert!(!required_strs.contains(&"offset"));
    }

    #[test]
    fn test_graph_centrality_tool_schema() {
        let tools = all_tools();
        let tool = tools
            .iter()
            .find(|t| t.name == "reddb_graph_centrality")
            .unwrap();
        let props = tool
            .input_schema
            .get("properties")
            .and_then(|v| v.as_object())
            .unwrap();
        assert!(props.contains_key("algorithm"));
        let required = tool
            .input_schema
            .get("required")
            .and_then(|v| v.as_array())
            .unwrap();
        let required_strs: Vec<&str> = required.iter().filter_map(|v| v.as_str()).collect();
        assert!(required_strs.contains(&"algorithm"));
    }

    #[test]
    fn test_graph_community_tool_schema() {
        let tools = all_tools();
        let tool = tools
            .iter()
            .find(|t| t.name == "reddb_graph_community")
            .unwrap();
        let props = tool
            .input_schema
            .get("properties")
            .and_then(|v| v.as_object())
            .unwrap();
        assert!(props.contains_key("algorithm"));
        assert!(props.contains_key("max_iterations"));
        let required = tool
            .input_schema
            .get("required")
            .and_then(|v| v.as_array())
            .unwrap();
        let required_strs: Vec<&str> = required.iter().filter_map(|v| v.as_str()).collect();
        assert!(required_strs.contains(&"algorithm"));
        assert!(!required_strs.contains(&"max_iterations"));
    }

    #[test]
    fn test_graph_components_tool_schema() {
        let tools = all_tools();
        let tool = tools
            .iter()
            .find(|t| t.name == "reddb_graph_components")
            .unwrap();
        let props = tool
            .input_schema
            .get("properties")
            .and_then(|v| v.as_object())
            .unwrap();
        assert!(props.contains_key("mode"));
        let required = tool
            .input_schema
            .get("required")
            .and_then(|v| v.as_array())
            .unwrap();
        // mode is optional (has default)
        let required_strs: Vec<&str> = required.iter().filter_map(|v| v.as_str()).collect();
        assert!(required_strs.is_empty());
    }

    #[test]
    fn test_graph_cycles_tool_schema() {
        let tools = all_tools();
        let tool = tools
            .iter()
            .find(|t| t.name == "reddb_graph_cycles")
            .unwrap();
        let props = tool
            .input_schema
            .get("properties")
            .and_then(|v| v.as_object())
            .unwrap();
        assert!(props.contains_key("max_length"));
        assert!(props.contains_key("max_cycles"));
        let required = tool
            .input_schema
            .get("required")
            .and_then(|v| v.as_array())
            .unwrap();
        // All optional
        let required_strs: Vec<&str> = required.iter().filter_map(|v| v.as_str()).collect();
        assert!(required_strs.is_empty());
    }

    #[test]
    fn test_graph_clustering_tool_schema() {
        let tools = all_tools();
        let tool = tools
            .iter()
            .find(|t| t.name == "reddb_graph_clustering")
            .unwrap();
        let props = tool
            .input_schema
            .get("properties")
            .and_then(|v| v.as_object())
            .unwrap();
        // No required properties - takes no arguments
        assert!(props.is_empty());
    }

    #[test]
    fn test_create_collection_tool_schema() {
        let tools = all_tools();
        let tool = tools
            .iter()
            .find(|t| t.name == "reddb_create_collection")
            .unwrap();
        let props = tool
            .input_schema
            .get("properties")
            .and_then(|v| v.as_object())
            .unwrap();
        assert!(props.contains_key("name"));
        let name_type = props
            .get("name")
            .and_then(|v| v.get("type"))
            .and_then(|v| v.as_str());
        assert_eq!(name_type, Some("string"));
        let required = tool
            .input_schema
            .get("required")
            .and_then(|v| v.as_array())
            .unwrap();
        let required_strs: Vec<&str> = required.iter().filter_map(|v| v.as_str()).collect();
        assert!(required_strs.contains(&"name"));
    }

    #[test]
    fn test_drop_collection_tool_schema() {
        let tools = all_tools();
        let tool = tools
            .iter()
            .find(|t| t.name == "reddb_drop_collection")
            .unwrap();
        let props = tool
            .input_schema
            .get("properties")
            .and_then(|v| v.as_object())
            .unwrap();
        assert!(props.contains_key("name"));
        let required = tool
            .input_schema
            .get("required")
            .and_then(|v| v.as_array())
            .unwrap();
        let required_strs: Vec<&str> = required.iter().filter_map(|v| v.as_str()).collect();
        assert!(required_strs.contains(&"name"));
    }
}