database_mcp_postgres/tools/
create_database.rs1use std::borrow::Cow;
4
5use database_mcp_server::types::{CreateDatabaseRequest, MessageResponse};
6use database_mcp_sql::Connection as _;
7use database_mcp_sql::SqlError;
8use database_mcp_sql::sanitize::{quote_ident, validate_ident};
9use rmcp::handler::server::router::tool::{AsyncTool, ToolBase};
10use rmcp::model::{ErrorData, ToolAnnotations};
11use sqlparser::dialect::PostgreSqlDialect;
12
13use crate::PostgresHandler;
14
15pub(crate) struct CreateDatabaseTool;
17
18impl CreateDatabaseTool {
19 const NAME: &'static str = "createDatabase";
20 const TITLE: &'static str = "Create Database";
21 const DESCRIPTION: &'static str = r#"Create a new database on the connected server.
22
23<usecase>
24Use when:
25- Setting up a new database for a project or application
26- The user asks to create a database
27</usecase>
28
29<examples>
30✓ "Create a database called analytics" → createDatabase(database="analytics")
31✗ "Create a table" → use writeQuery with CREATE TABLE
32</examples>
33
34<important>
35Database names must contain only alphanumeric characters and underscores.
36</important>
37
38<what_it_returns>
39A confirmation message with the created database name.
40</what_it_returns>"#;
41}
42
43impl ToolBase for CreateDatabaseTool {
44 type Parameter = CreateDatabaseRequest;
45 type Output = MessageResponse;
46 type Error = ErrorData;
47
48 fn name() -> Cow<'static, str> {
49 Self::NAME.into()
50 }
51
52 fn title() -> Option<String> {
53 Some(Self::TITLE.into())
54 }
55
56 fn description() -> Option<Cow<'static, str>> {
57 Some(Self::DESCRIPTION.into())
58 }
59
60 fn annotations() -> Option<ToolAnnotations> {
61 Some(
62 ToolAnnotations::new()
63 .read_only(false)
64 .destructive(false)
65 .idempotent(false)
66 .open_world(false),
67 )
68 }
69}
70
71impl AsyncTool<PostgresHandler> for CreateDatabaseTool {
72 async fn invoke(handler: &PostgresHandler, params: Self::Parameter) -> Result<Self::Output, Self::Error> {
73 Ok(handler.create_database(params).await?)
74 }
75}
76
77impl PostgresHandler {
78 pub async fn create_database(
84 &self,
85 CreateDatabaseRequest { database }: CreateDatabaseRequest,
86 ) -> Result<MessageResponse, SqlError> {
87 if self.config.read_only {
88 return Err(SqlError::ReadOnlyViolation);
89 }
90
91 validate_ident(&database)?;
92
93 let create_sql = format!("CREATE DATABASE {}", quote_ident(&database, &PostgreSqlDialect {}));
94 self.connection.execute(&create_sql, None).await.map_err(|e| {
95 let msg = e.to_string();
96 if msg.contains("already exists") {
97 return SqlError::Query(format!("Database '{database}' already exists."));
98 }
99 e
100 })?;
101
102 Ok(MessageResponse {
103 message: format!("Database '{database}' created successfully."),
104 })
105 }
106}