database-mcp 0.6.2

A single-binary MCP server for MySQL, MariaDB, PostgreSQL, and SQLite
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
//! CLI argument parsing and application bootstrapping.
//!
//! Contains the [`Arguments`] struct (parsed by clap), the [`Command`]
//! subcommand enum, [`LogLevel`] selector, [`From`] implementations
//! that convert parsed arguments into configuration types, and the
//! [`run`] entry point that owns the entire bootstrapping pipeline.

use clap::{Parser, Subcommand};
use database_mcp_config::{Config, DatabaseBackend, DatabaseConfig, HttpConfig};
use std::process::ExitCode;
use tracing::info;

use crate::commands::http::HttpCommand;
use crate::commands::stdio::StdioCommand;
use crate::consts::{BIN, VERSION};
use crate::error::Error;
use crate::server::create_server;

/// Log severity levels for the MCP server.
///
/// Maps directly to [`tracing::Level`] variants. Used as a
/// [`clap::ValueEnum`] for type-safe CLI argument parsing.
#[derive(Debug, Clone, Copy, PartialEq, Eq, clap::ValueEnum)]
pub enum LogLevel {
    /// Only errors.
    Error,
    /// Warnings and above.
    Warn,
    /// Informational and above (default).
    Info,
    /// Debug and above.
    Debug,
    /// All trace output.
    Trace,
}

impl std::fmt::Display for LogLevel {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            Self::Error => write!(f, "error"),
            Self::Warn => write!(f, "warn"),
            Self::Info => write!(f, "info"),
            Self::Debug => write!(f, "debug"),
            Self::Trace => write!(f, "trace"),
        }
    }
}

impl From<LogLevel> for tracing::Level {
    fn from(level: LogLevel) -> Self {
        match level {
            LogLevel::Error => Self::ERROR,
            LogLevel::Warn => Self::WARN,
            LogLevel::Info => Self::INFO,
            LogLevel::Debug => Self::DEBUG,
            LogLevel::Trace => Self::TRACE,
        }
    }
}

/// Top-level CLI arguments parsed by clap.
#[derive(Debug, Parser)]
#[command(name = "database-mcp", about = "Database MCP Server", version)]
struct Arguments {
    /// Subcommand selector.
    #[command(subcommand)]
    command: Option<Command>,

    /// Database backend
    #[arg(long = "db-backend", env = "DB_BACKEND", default_value_t = DatabaseConfig::DEFAULT_BACKEND, global = true)]
    db_backend: DatabaseBackend,

    /// Database host
    #[arg(long = "db-host", env = "DB_HOST", default_value = DatabaseConfig::DEFAULT_HOST, global = true)]
    db_host: String,

    /// Database port (default: backend-dependent)
    #[arg(long = "db-port", env = "DB_PORT", global = true)]
    db_port: Option<u16>,

    /// Database user (default: backend-dependent)
    #[arg(long = "db-user", env = "DB_USER", global = true)]
    db_user: Option<String>,

    /// Database password
    #[arg(long = "db-password", env = "DB_PASSWORD", global = true)]
    db_password: Option<String>,

    /// Database name or `SQLite` file path
    #[arg(long = "db-name", env = "DB_NAME", global = true)]
    db_name: Option<String>,

    /// Character set (MySQL/MariaDB only)
    #[arg(long = "db-charset", env = "DB_CHARSET", global = true)]
    db_charset: Option<String>,

    /// Enable SSL for database connection
    #[arg(
        long = "db-ssl",
        env = "DB_SSL",
        default_value_t = DatabaseConfig::DEFAULT_SSL,
        global = true
    )]
    db_ssl: bool,

    /// Path to CA certificate
    #[arg(long = "db-ssl-ca", env = "DB_SSL_CA", global = true)]
    db_ssl_ca: Option<String>,

    /// Path to client certificate
    #[arg(long = "db-ssl-cert", env = "DB_SSL_CERT", global = true)]
    db_ssl_cert: Option<String>,

    /// Path to a client key
    #[arg(long = "db-ssl-key", env = "DB_SSL_KEY", global = true)]
    db_ssl_key: Option<String>,

    /// Verify server certificate
    #[arg(
        long = "db-ssl-verify-cert",
        env = "DB_SSL_VERIFY_CERT",
        default_value_t = DatabaseConfig::DEFAULT_SSL_VERIFY_CERT,
        global = true
    )]
    db_ssl_verify_cert: bool,

    /// Enable read-only mode
    #[arg(
        long = "db-read-only",
        env = "DB_READ_ONLY",
        default_value_t = DatabaseConfig::DEFAULT_READ_ONLY,
        global = true
    )]
    db_read_only: bool,

    /// Maximum connection pool size
    #[arg(
        long = "db-max-pool-size",
        env = "DB_MAX_POOL_SIZE",
        default_value_t = DatabaseConfig::DEFAULT_MAX_POOL_SIZE,
        global = true,
        value_parser = clap::value_parser!(u32).range(1..)
    )]
    db_max_pool_size: u32,

    /// Connection timeout in seconds
    #[arg(
        long = "db-connection-timeout",
        env = "DB_CONNECTION_TIMEOUT",
        global = true,
        value_parser = clap::value_parser!(u64).range(1..)
    )]
    db_connection_timeout: Option<u64>,

    /// Query execution timeout in seconds (0 = disabled)
    #[arg(
        long = "db-query-timeout",
        env = "DB_QUERY_TIMEOUT",
        default_value_t = DatabaseConfig::DEFAULT_QUERY_TIMEOUT_SECS,
        global = true,
        value_parser = clap::value_parser!(u64)
    )]
    db_query_timeout: u64,

    /// Log level
    #[arg(
        long = "log-level",
        env = "LOG_LEVEL",
        default_value_t = LogLevel::Info,
        ignore_case = true,
        global = true
    )]
    log_level: LogLevel,
}

/// Top-level subcommand selector.
#[derive(Debug, Subcommand)]
enum Command {
    /// Print version information and exit.
    Version,
    /// Run in stdio mode (default).
    Stdio(StdioCommand),
    /// Run in HTTP/SSE mode.
    Http(HttpCommand),
}

impl From<&Arguments> for DatabaseConfig {
    fn from(arguments: &Arguments) -> Self {
        let backend = arguments.db_backend;
        Self {
            backend,
            host: arguments.db_host.clone(),
            port: arguments.db_port.unwrap_or_else(|| backend.default_port()),
            user: arguments
                .db_user
                .clone()
                .unwrap_or_else(|| backend.default_user().into()),
            password: arguments.db_password.clone(),
            name: arguments.db_name.clone(),
            charset: arguments.db_charset.clone(),
            ssl: arguments.db_ssl,
            ssl_ca: arguments.db_ssl_ca.clone(),
            ssl_cert: arguments.db_ssl_cert.clone(),
            ssl_key: arguments.db_ssl_key.clone(),
            ssl_verify_cert: arguments.db_ssl_verify_cert,
            read_only: arguments.db_read_only,
            max_pool_size: arguments.db_max_pool_size,
            connection_timeout: arguments.db_connection_timeout,
            query_timeout: if arguments.db_query_timeout == 0 {
                None
            } else {
                Some(arguments.db_query_timeout)
            },
        }
    }
}

impl From<&Command> for Option<HttpConfig> {
    fn from(cmd: &Command) -> Self {
        match cmd {
            Command::Http(http) => Some(HttpConfig {
                host: http.host.clone(),
                port: http.port,
                allowed_origins: http.allowed_origins.clone(),
                allowed_hosts: http.allowed_hosts.clone(),
            }),
            _ => None,
        }
    }
}

impl From<&Arguments> for Config {
    fn from(arguments: &Arguments) -> Self {
        Self {
            database: DatabaseConfig::from(arguments),
            http: arguments.command.as_ref().and_then(Into::into),
        }
    }
}

/// Parses CLI arguments, initializes the application, and runs the MCP server.
///
/// # Errors
///
/// Returns an error if:
/// - Configuration validation fails (missing/invalid values).
/// - Database connection fails (invalid URL, unreachable host, auth failure).
/// - TCP bind fails for HTTP transport (port in use, permission denied).
/// - MCP stdio transport fails to start.
#[tokio::main]
#[allow(clippy::result_large_err)]
pub async fn run() -> Result<ExitCode, Error> {
    let arguments = Arguments::parse();
    if matches!(arguments.command, Some(Command::Version)) {
        println!("{BIN} {VERSION}");
        return Ok(ExitCode::SUCCESS);
    }

    tracing_subscriber::fmt()
        .with_writer(std::io::stderr)
        .with_max_level(tracing::Level::from(arguments.log_level))
        .with_ansi(false)
        .init();

    let config = Config::from(&arguments);
    if let Err(errors) = config.validate() {
        eprintln!("Error: configuration validation failed:");
        for error in &errors {
            eprintln!("  - {error}");
        }
        return Ok(ExitCode::FAILURE);
    }

    if config.database.read_only {
        info!("Server running in READ-ONLY mode. Write operations are disabled.");
    }

    let server = create_server(&config);
    match &arguments.command {
        Some(Command::Http(cmd)) => cmd.execute(&config, server).await?,
        _ => StdioCommand.execute(server).await?,
    }

    Ok(ExitCode::SUCCESS)
}

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

    fn parse(args: &[&str]) -> Arguments {
        Arguments::try_parse_from(args).unwrap()
    }

    #[test]
    fn no_subcommand_defaults_to_stdio() {
        let args = parse(&[BIN]);
        assert!(args.command.is_none());
        assert!(Config::from(&args).http.is_none());
    }

    #[test]
    fn http_subcommand_populates_http_config() {
        let args = parse(&[BIN, "http", "--port", "8080"]);
        let config = Config::from(&args);
        assert!(config.http.is_some());
        assert_eq!(config.http.as_ref().unwrap().port, 8080);
    }

    #[test]
    fn http_subcommand_uses_default_port() {
        let args = parse(&[BIN, "http"]);
        let config = Config::from(&args);
        assert_eq!(config.http.as_ref().unwrap().port, HttpConfig::DEFAULT_PORT);
    }

    #[test]
    fn db_backend_after_http_subcommand() {
        let args = parse(&[BIN, "http", "--db-backend", "mysql"]);
        assert_eq!(args.db_backend, DatabaseBackend::Mysql);
        assert!(matches!(args.command, Some(Command::Http(_))));
    }

    #[test]
    fn db_backend_before_http_subcommand() {
        let args = parse(&[BIN, "--db-backend", "mysql", "http"]);
        assert_eq!(args.db_backend, DatabaseBackend::Mysql);
        assert!(matches!(args.command, Some(Command::Http(_))));
    }

    #[test]
    fn db_backend_with_no_subcommand() {
        let args = parse(&[BIN, "--db-backend", "postgres"]);
        assert_eq!(args.db_backend, DatabaseBackend::Postgres);
        assert!(args.command.is_none());
    }

    #[test]
    fn multiple_global_args_after_subcommand() {
        let args = parse(&[
            BIN,
            "http",
            "--db-backend",
            "mysql",
            "--db-user",
            "root",
            "--db-name",
            "mydb",
        ]);
        assert_eq!(args.db_backend, DatabaseBackend::Mysql);
        assert_eq!(args.db_user, Some("root".into()));
        assert_eq!(args.db_name, Some("mydb".into()));
    }

    #[test]
    fn db_backend_defaults_to_mysql() {
        let args = parse(&[BIN, "http"]);
        assert_eq!(args.db_backend, DatabaseBackend::Mysql);
    }

    #[test]
    fn cli_flag_overrides_default_backend() {
        let args = parse(&[BIN, "http", "--db-backend", "postgres"]);
        assert_eq!(args.db_backend, DatabaseBackend::Postgres);
    }

    #[test]
    fn db_read_only_flag() {
        let args = parse(&[BIN, "--db-read-only"]);
        assert!(args.db_read_only);
    }

    #[test]
    fn db_read_only_defaults_to_true() {
        let args = parse(&[BIN]);
        assert!(args.db_read_only);
    }

    #[test]
    fn db_max_pool_size_flag() {
        let args = parse(&[BIN, "--db-max-pool-size", "20"]);
        assert_eq!(args.db_max_pool_size, 20);
    }

    #[test]
    fn db_connection_timeout_flag() {
        let args = parse(&[BIN, "--db-connection-timeout", "5"]);
        assert_eq!(args.db_connection_timeout, Some(5));
    }

    #[test]
    fn db_connection_timeout_defaults_to_none() {
        let args = parse(&[BIN]);
        assert_eq!(args.db_connection_timeout, None);
    }

    #[test]
    fn db_connection_timeout_zero_rejected() {
        assert!(Arguments::try_parse_from([BIN, "--db-connection-timeout", "0"]).is_err());
    }

    #[test]
    fn db_query_timeout_zero_disables() {
        let args = parse(&[BIN, "--db-query-timeout", "0"]);
        let config = Config::from(&args);
        assert_eq!(config.database.query_timeout, None);
    }

    #[test]
    fn db_query_timeout_default_applied() {
        let args = parse(&[BIN]);
        let config = Config::from(&args);
        assert_eq!(
            config.database.query_timeout,
            Some(DatabaseConfig::DEFAULT_QUERY_TIMEOUT_SECS)
        );
    }

    #[test]
    fn db_connection_timeout_wired_to_config() {
        let args = parse(&[BIN, "--db-connection-timeout", "10"]);
        let config = Config::from(&args);
        assert_eq!(config.database.connection_timeout, Some(10));
    }

    #[test]
    fn valid_log_levels() {
        for level in ["error", "warn", "info", "debug", "trace"] {
            let args = parse(&[BIN, "--log-level", level]);
            assert_eq!(args.log_level.to_string(), level);
        }
    }

    #[test]
    fn invalid_log_level_rejected() {
        assert!(Arguments::try_parse_from([BIN, "--log-level", "nonsense"]).is_err());
    }

    #[test]
    fn log_level_defaults_to_info() {
        let args = parse(&[BIN]);
        assert_eq!(args.log_level, LogLevel::Info);
    }

    #[test]
    fn log_level_case_insensitive() {
        for level in ["DEBUG", "Info", "TRACE", "Warn", "ERROR"] {
            assert!(
                Arguments::try_parse_from([BIN, "--log-level", level]).is_ok(),
                "expected '{level}' to be accepted case-insensitively"
            );
        }
    }

    #[test]
    fn version_subcommand() {
        let args = parse(&[BIN, "version"]);
        assert!(matches!(args.command, Some(Command::Version)));
    }

    #[test]
    fn version_flag() {
        let err = Arguments::try_parse_from([BIN, "--version"]).expect_err("--version should cause clap to return Err");
        assert_eq!(err.kind(), clap::error::ErrorKind::DisplayVersion);
    }

    #[test]
    fn invalid_cli_args_rejected() {
        assert!(Arguments::try_parse_from([BIN, "--nonexistent-flag"]).is_err());
    }
}