Skip to main content

database_mcp_postgres/
handler.rs

1//! MCP handler for the `PostgreSQL` backend.
2//!
3//! Implements [`ServerHandler`] directly on [`PostgresAdapter`] using
4//! rmcp macros for tool dispatch.
5
6use database_mcp_server::server_info;
7use rmcp::model::ServerInfo;
8
9use super::PostgresAdapter;
10
11/// Backend-specific description for `PostgreSQL`.
12const DESCRIPTION: &str = "Database MCP Server for PostgreSQL";
13
14/// Backend-specific instructions for `PostgreSQL`.
15const INSTRUCTIONS: &str = r"## Workflow
16
171. Call `list_databases` to discover available databases.
182. Call `list_tables` with a `database_name` to see its tables.
193. Call `get_table_schema` with `database_name` and `table_name` to inspect columns, types, and foreign keys before writing queries.
204. Use `read_query` for read-only SQL (SELECT, SHOW, EXPLAIN).
215. Use `write_query` for data changes (INSERT, UPDATE, DELETE, CREATE, ALTER, DROP).
226. Use `create_database` to create a new database.
237. Use `drop_database` to drop an existing database.
24
25Tools accept an optional `database_name` parameter to query across databases without reconnecting.
26
27## Constraints
28
29- The `write_query`, `create_database`, and `drop_database` tools are hidden when read-only mode is active.
30- Multi-statement queries are not supported. Send one statement per request.";
31
32#[rmcp::tool_handler(router = self.build_tool_router())]
33impl rmcp::ServerHandler for PostgresAdapter {
34    fn get_info(&self) -> ServerInfo {
35        let mut info = server_info();
36        info.server_info.description = Some(DESCRIPTION.into());
37        info.instructions = Some(INSTRUCTIONS.into());
38        info
39    }
40}