|
| 1 | +import logging |
| 2 | +from contextlib import closing |
| 3 | +from pathlib import Path |
| 4 | +from typing import Any, List |
| 5 | + |
| 6 | +import duckdb |
| 7 | +import mcp.server.stdio |
| 8 | +import mcp.types as types |
| 9 | +from mcp.server import NotificationOptions, Server |
| 10 | +from mcp.server.models import InitializationOptions |
| 11 | +from pydantic import AnyUrl |
| 12 | + |
| 13 | +logger = logging.getLogger("mcp-server-duckdb") |
| 14 | +logger.info("Starting MCP DuckDB Server") |
| 15 | + |
| 16 | + |
| 17 | +class DuckDBDatabase: |
| 18 | + def __init__(self, db_path: str): |
| 19 | + path = Path(db_path).expanduser() |
| 20 | + dir_path = path.parent |
| 21 | + if not dir_path.exists(): |
| 22 | + logger.info(f"Creating directory: {dir_path}") |
| 23 | + dir_path.mkdir(parents=True) |
| 24 | + |
| 25 | + if not path.exists(): |
| 26 | + logger.info(f"Creating DuckDB database: {path}") |
| 27 | + duckdb.connect(str(path)).close() |
| 28 | + |
| 29 | + self.db_path = str(path) |
| 30 | + |
| 31 | + def connect(self): |
| 32 | + return duckdb.connect(self.db_path) |
| 33 | + |
| 34 | + def execute_query(self, query: object, parameters: object = None) -> List[Any]: |
| 35 | + with closing(self.connect()) as connection: |
| 36 | + return connection.execute(query, parameters).fetchall() |
| 37 | + |
| 38 | + |
| 39 | +async def main(db_path: str): |
| 40 | + logger.info(f"Starting SQLite MCP Server with DB path: {db_path}") |
| 41 | + |
| 42 | + db = DuckDBDatabase(db_path) |
| 43 | + server = Server("mcp-duckdb-server") |
| 44 | + |
| 45 | + logger.debug("Registering handlers") |
| 46 | + |
| 47 | + @server.list_resources() |
| 48 | + async def handle_list_resources() -> list[types.Resource]: |
| 49 | + """ |
| 50 | + List available duckdb resources. |
| 51 | + """ |
| 52 | + return [] |
| 53 | + |
| 54 | + @server.read_resource() |
| 55 | + async def handle_read_resource(uri: AnyUrl) -> str: |
| 56 | + """ |
| 57 | + Read a specific note's content by its URI. |
| 58 | + """ |
| 59 | + return "No data" |
| 60 | + |
| 61 | + @server.list_prompts() |
| 62 | + async def handle_list_prompts() -> list[types.Prompt]: |
| 63 | + """ |
| 64 | + List available prompts. |
| 65 | + """ |
| 66 | + return [] |
| 67 | + |
| 68 | + @server.get_prompt() |
| 69 | + async def handle_get_prompt(name: str, arguments: dict[str, str] | None) -> types.GetPromptResult: |
| 70 | + """ |
| 71 | + Generate a prompt by combining arguments with server state. |
| 72 | + """ |
| 73 | + |
| 74 | + return types.GetPromptResult( |
| 75 | + description="No", |
| 76 | + messages=[], |
| 77 | + ) |
| 78 | + |
| 79 | + @server.list_tools() |
| 80 | + async def handle_list_tools() -> list[types.Tool]: |
| 81 | + """List available tools""" |
| 82 | + return [ |
| 83 | + types.Tool( |
| 84 | + name="read-query", |
| 85 | + description="Execute a SELECT query on the DuckDB database", |
| 86 | + inputSchema={ |
| 87 | + "type": "object", |
| 88 | + "properties": { |
| 89 | + "query": { |
| 90 | + "type": "string", |
| 91 | + "description": "SELECT SQL query to execute", |
| 92 | + }, |
| 93 | + }, |
| 94 | + "required": ["query"], |
| 95 | + }, |
| 96 | + ), |
| 97 | + types.Tool( |
| 98 | + name="write-query", |
| 99 | + description="Execute an INSERT, UPDATE, or DELETE query on the DuckDB database", |
| 100 | + inputSchema={ |
| 101 | + "type": "object", |
| 102 | + "properties": { |
| 103 | + "query": { |
| 104 | + "type": "string", |
| 105 | + "description": "SQL query to execute", |
| 106 | + }, |
| 107 | + }, |
| 108 | + "required": ["query"], |
| 109 | + }, |
| 110 | + ), |
| 111 | + types.Tool( |
| 112 | + name="create-table", |
| 113 | + description="Create a new table in the DuckDB database", |
| 114 | + inputSchema={ |
| 115 | + "type": "object", |
| 116 | + "properties": { |
| 117 | + "query": { |
| 118 | + "type": "string", |
| 119 | + "description": "CREATE TABLE SQL statement", |
| 120 | + }, |
| 121 | + }, |
| 122 | + "required": ["query"], |
| 123 | + }, |
| 124 | + ), |
| 125 | + types.Tool( |
| 126 | + name="list-tables", |
| 127 | + description="List all tables in the DuckDB database", |
| 128 | + inputSchema={ |
| 129 | + "type": "object", |
| 130 | + "properties": {}, |
| 131 | + }, |
| 132 | + ), |
| 133 | + types.Tool( |
| 134 | + name="describe-table", |
| 135 | + description="Get the schema information for a specific table", |
| 136 | + inputSchema={ |
| 137 | + "type": "object", |
| 138 | + "properties": { |
| 139 | + "table_name": { |
| 140 | + "type": "string", |
| 141 | + "description": "Name of the table to describe", |
| 142 | + }, |
| 143 | + }, |
| 144 | + "required": ["table_name"], |
| 145 | + }, |
| 146 | + ), |
| 147 | + ] |
| 148 | + |
| 149 | + @server.call_tool() |
| 150 | + async def handle_call_tool( |
| 151 | + name: str, arguments: dict[str, Any] | None |
| 152 | + ) -> list[types.TextContent | types.ImageContent | types.EmbeddedResource]: |
| 153 | + """Handle tool execution requests""" |
| 154 | + try: |
| 155 | + if name == "list-tables": |
| 156 | + results = db.execute_query("SELECT * FROM information_schema.tables;") |
| 157 | + return [types.TextContent(type="text", text=str(results))] |
| 158 | + |
| 159 | + elif name == "describe-table": |
| 160 | + if not arguments or "table_name" not in arguments: |
| 161 | + raise ValueError("Missing table_name argument") |
| 162 | + results = db.execute_query("PRAGMA table_info(?)", [arguments["table_name"]]) |
| 163 | + return [types.TextContent(type="text", text=str(results))] |
| 164 | + |
| 165 | + if not arguments: |
| 166 | + raise ValueError("Missing arguments") |
| 167 | + |
| 168 | + if name == "read-query": |
| 169 | + if not arguments["query"].strip().upper().startswith("SELECT"): |
| 170 | + raise ValueError("Only SELECT queries are allowed for read-query") |
| 171 | + results = db.execute_query(arguments["query"]) |
| 172 | + return [types.TextContent(type="text", text=str(results))] |
| 173 | + |
| 174 | + elif name == "write-query": |
| 175 | + if arguments["query"].strip().upper().startswith("SELECT"): |
| 176 | + raise ValueError("SELECT queries are not allowed for write-query") |
| 177 | + results = db.execute_query(arguments["query"]) |
| 178 | + return [types.TextContent(type="text", text=str(results))] |
| 179 | + |
| 180 | + elif name == "create-table": |
| 181 | + if not arguments["query"].strip().upper().startswith("CREATE TABLE"): |
| 182 | + raise ValueError("Only CREATE TABLE statements are allowed") |
| 183 | + db.execute_query(arguments["query"]) |
| 184 | + return [types.TextContent(type="text", text="Table created successfully")] |
| 185 | + |
| 186 | + else: |
| 187 | + raise ValueError(f"Unknown tool: {name}") |
| 188 | + |
| 189 | + except duckdb.Error as e: |
| 190 | + return [types.TextContent(type="text", text=f"Database error: {str(e)}")] |
| 191 | + except Exception as e: |
| 192 | + return [types.TextContent(type="text", text=f"Error: {str(e)}")] |
| 193 | + |
| 194 | + # Run the server using stdin/stdout streams |
| 195 | + async with mcp.server.stdio.stdio_server() as (read_stream, write_stream): |
| 196 | + logger.info("DuckDB MCP Server running with stdio transport") |
| 197 | + await server.run( |
| 198 | + read_stream, |
| 199 | + write_stream, |
| 200 | + InitializationOptions( |
| 201 | + server_name="mcp-server-duckdb", |
| 202 | + server_version="0.1.0", |
| 203 | + capabilities=server.get_capabilities( |
| 204 | + notification_options=NotificationOptions(), |
| 205 | + experimental_capabilities={}, |
| 206 | + ), |
| 207 | + ), |
| 208 | + ) |
0 commit comments