|
| 1 | +import { McpServer } from '@modelcontextprotocol/sdk/server/mcp.js'; |
| 2 | +import { z } from 'zod'; |
| 3 | +export class SQLiteCloudMcpServer { |
| 4 | + mcpServer; |
| 5 | + registry; |
| 6 | + constructor() { |
| 7 | + this.registry = {}; |
| 8 | + this.mcpServer = this.initializeServer(); |
| 9 | + this.setupServer(); |
| 10 | + } |
| 11 | + async connect(transport) { |
| 12 | + const mcpTransport = transport.mcpTransport; |
| 13 | + let sessionId = mcpTransport.sessionId; |
| 14 | + if (!sessionId) { |
| 15 | + sessionId = 'anonymous'; |
| 16 | + mcpTransport.sessionId = sessionId; |
| 17 | + } |
| 18 | + mcpTransport.onerror = error => { |
| 19 | + console.error('Error in transport:', error); |
| 20 | + delete this.registry[sessionId]; |
| 21 | + }; |
| 22 | + mcpTransport.onclose = () => { |
| 23 | + delete this.registry[sessionId]; |
| 24 | + }; |
| 25 | + this.registry[sessionId] = transport; |
| 26 | + await this.mcpServer.connect(mcpTransport); |
| 27 | + } |
| 28 | + getTransport(sessionId) { |
| 29 | + const transport = this.registry[sessionId]; |
| 30 | + if (!transport) { |
| 31 | + throw new Error(`Transport not found for session ID: ${sessionId}`); |
| 32 | + } |
| 33 | + return transport; |
| 34 | + } |
| 35 | + addCustomTool(name, description, parameters, handler) { |
| 36 | + // TODO: keep a registered list of tools to check existence and to implement removal |
| 37 | + this.mcpServer.tool(name, description, parameters, async (parameters, extra) => { |
| 38 | + if (!extra.sessionId) { |
| 39 | + throw new Error('Session ID is required'); |
| 40 | + } |
| 41 | + const customerResult = await handler(parameters, this.getTransport(extra.sessionId)); |
| 42 | + return { content: [{ type: 'text', text: JSON.stringify(customerResult) }] }; |
| 43 | + }); |
| 44 | + } |
| 45 | + removeCustomTool(name) { |
| 46 | + throw new Error('Not implemented'); |
| 47 | + } |
| 48 | + initializeServer() { |
| 49 | + return new McpServer({ |
| 50 | + name: 'sqlitecloud-mcp-server', |
| 51 | + version: '0.0.1', |
| 52 | + description: 'MCP Server for SQLite Cloud: https://sqlitecloud.io' |
| 53 | + }, { |
| 54 | + capabilities: { tools: {} }, |
| 55 | + instructions: 'This server provides tools to interact with SQLite databases on SQLite Cloud, execute SQL queries, manage table schemas and analyze performance metrics.' |
| 56 | + }); |
| 57 | + } |
| 58 | + setupServer() { |
| 59 | + this.mcpServer.tool('read-query', 'Execute a SELECT query on the SQLite database on SQLite Cloud', { |
| 60 | + query: z.string().describe('SELECT SQL query to execute') |
| 61 | + }, async ({ query }, extra) => { |
| 62 | + if (!query.trim().toUpperCase().startsWith('SELECT')) { |
| 63 | + throw new Error('Only SELECT queries are allowed for read-query'); |
| 64 | + } |
| 65 | + if (!extra.sessionId) { |
| 66 | + throw new Error('Session ID is required'); |
| 67 | + } |
| 68 | + const results = await this.getTransport(extra.sessionId).executeQuery(query); |
| 69 | + return { content: [{ type: 'text', text: JSON.stringify(results) }] }; |
| 70 | + }); |
| 71 | + this.mcpServer.tool('write-query', 'Execute a INSERT, UPDATE, or DELETE query on the SQLite database on SQLite Cloud', { |
| 72 | + query: z.string().describe('SELECT SQL query to execute') |
| 73 | + }, async ({ query }, extra) => { |
| 74 | + if (query.trim().toUpperCase().startsWith('SELECT')) { |
| 75 | + throw new Error('SELECT queries are not allowed for write_query'); |
| 76 | + } |
| 77 | + if (!extra.sessionId) { |
| 78 | + throw new Error('Session ID is required'); |
| 79 | + } |
| 80 | + const results = await this.getTransport(extra.sessionId).executeQuery(query); |
| 81 | + return { content: [{ type: 'text', text: JSON.stringify(results) }] }; |
| 82 | + }); |
| 83 | + this.mcpServer.tool('create-table', 'Create a new table in the SQLite database on SQLite Cloud', { |
| 84 | + query: z.string().describe('CREATE TABLE SQL statement') |
| 85 | + }, async ({ query }, extra) => { |
| 86 | + if (!query.trim().toUpperCase().startsWith('CREATE TABLE')) { |
| 87 | + throw new Error('Only CREATE TABLE statements are allowed'); |
| 88 | + } |
| 89 | + if (!extra.sessionId) { |
| 90 | + throw new Error('Session ID is required'); |
| 91 | + } |
| 92 | + const results = await this.getTransport(extra.sessionId).executeQuery(query); |
| 93 | + return { |
| 94 | + content: [{ type: 'text', text: 'Table created successfully' }] |
| 95 | + }; |
| 96 | + }); |
| 97 | + this.mcpServer.tool('list-tables', 'List all tables in the SQLite database on SQLite Cloud', {}, async ({}, extra) => { |
| 98 | + if (!extra.sessionId) { |
| 99 | + throw new Error('Session ID is required'); |
| 100 | + } |
| 101 | + const results = await this.getTransport(extra.sessionId).executeQuery("SELECT name FROM sqlite_master WHERE type='table'"); |
| 102 | + return { content: [{ type: 'text', text: JSON.stringify(results) }] }; |
| 103 | + }); |
| 104 | + this.mcpServer.tool('describe-table', 'Get the schema information for a specific table on SQLite Cloud database', { |
| 105 | + tableName: z.string().describe('Name of the table to describe') |
| 106 | + }, async ({ tableName }, extra) => { |
| 107 | + if (!extra.sessionId) { |
| 108 | + throw new Error('Session ID is required'); |
| 109 | + } |
| 110 | + const results = await this.getTransport(extra.sessionId).executeQuery(`PRAGMA table_info(${tableName})`); |
| 111 | + return { content: [{ type: 'text', text: JSON.stringify(results) }] }; |
| 112 | + }); |
| 113 | + this.mcpServer.tool('list-commands', 'List all available commands and their descriptions from the SQLite database and an external documentation page.', {}, async ({}, extra) => { |
| 114 | + try { |
| 115 | + if (!extra.sessionId) { |
| 116 | + throw new Error('Session ID is required'); |
| 117 | + } |
| 118 | + const results = await this.getTransport(extra.sessionId).executeQuery('LIST COMMANDS;'); |
| 119 | + // Download the documentation page |
| 120 | + const documentationUrl = 'https://raw.githubusercontent.com/sqlitecloud/docs/refs/heads/main/sqlite-cloud/reference/general-commands.mdx'; |
| 121 | + const response = await fetch(documentationUrl, { |
| 122 | + redirect: 'follow' |
| 123 | + }); |
| 124 | + const documentationContent = await response.text(); |
| 125 | + return { |
| 126 | + content: [ |
| 127 | + { type: 'text', text: JSON.stringify(results) }, |
| 128 | + { type: 'text', text: documentationContent } |
| 129 | + ] |
| 130 | + }; |
| 131 | + } |
| 132 | + catch (error) { |
| 133 | + throw new Error('Failed to list commands and fetch documentation.', { cause: error }); |
| 134 | + } |
| 135 | + }); |
| 136 | + this.mcpServer.tool('execute-command', 'Execute only SQLite Cloud commands listed in the `list-commands` tool. You can use the `list-commands` tool to see the available commands.', { |
| 137 | + command: z.string().describe('SQLite Cloud available command to execute') |
| 138 | + }, async ({ command }, extra) => { |
| 139 | + if (!extra.sessionId) { |
| 140 | + throw new Error('Session ID is required'); |
| 141 | + } |
| 142 | + const results = await this.getTransport(extra.sessionId).executeQuery(command); |
| 143 | + return { content: [{ type: 'text', text: JSON.stringify(results) }] }; |
| 144 | + }); |
| 145 | + this.mcpServer.tool('list-analyzer', 'Returns a rowset with the slowest queries performed on the connected this.mcpServer. Supports filtering with GROUPID, DATABASE, GROUPED, and NODE options.', { |
| 146 | + groupId: z.string().optional().describe('Group ID to filter the results'), |
| 147 | + database: z.string().optional().describe('Database name to filter the results'), |
| 148 | + grouped: z.boolean().optional().describe('Whether to group the slowest queries'), |
| 149 | + node: z.string().optional().describe('Node ID to execute the command on a specific cluster node') |
| 150 | + }, async ({ groupId, database, grouped, node }, extra) => { |
| 151 | + let query = 'LIST ANALYZER'; |
| 152 | + if (groupId) |
| 153 | + query += ` GROUPID ${groupId}`; |
| 154 | + if (database) |
| 155 | + query += ` DATABASE ${database}`; |
| 156 | + if (grouped) |
| 157 | + query += ' GROUPED'; |
| 158 | + if (node) |
| 159 | + query += ` NODE ${node}`; |
| 160 | + if (!extra.sessionId) { |
| 161 | + throw new Error('Session ID is required'); |
| 162 | + } |
| 163 | + const results = await this.getTransport(extra.sessionId).executeQuery(query); |
| 164 | + return { content: [{ type: 'text', text: JSON.stringify(results) }] }; |
| 165 | + }); |
| 166 | + this.mcpServer.tool('analyzer-plan-id', 'Gathers information about the indexes used in the query plan of a query execution.', { |
| 167 | + queryId: z.string().describe('Query ID to analyze'), |
| 168 | + node: z.string().optional().describe('SQLite Cloud Node ID to execute the command on a specific cluster node') |
| 169 | + }, async ({ queryId, node }, extra) => { |
| 170 | + let query = `ANALYZER PLAN ID ${queryId}`; |
| 171 | + if (node) |
| 172 | + query += ` NODE ${node}`; |
| 173 | + if (!extra.sessionId) { |
| 174 | + throw new Error('Session ID is required'); |
| 175 | + } |
| 176 | + const results = await this.getTransport(extra.sessionId).executeQuery(query); |
| 177 | + return { content: [{ type: 'text', text: JSON.stringify(results) }] }; |
| 178 | + }); |
| 179 | + this.mcpServer.tool('analyzer-reset', 'Resets the statistics about a specific query, group of queries, or database.', { |
| 180 | + queryId: z.string().optional().describe('Query ID to reset'), |
| 181 | + groupId: z.string().optional().describe('Group ID to reset'), |
| 182 | + database: z.string().optional().describe('Database name to reset'), |
| 183 | + all: z.boolean().optional().describe('Whether to reset all statistics'), |
| 184 | + node: z.string().optional().describe('SQLite Cloud Node ID to execute the command on a specific cluster node') |
| 185 | + }, async ({ queryId, groupId, database, all, node }, extra) => { |
| 186 | + let query = 'ANALYZER RESET'; |
| 187 | + if (queryId) |
| 188 | + query += ` ID ${queryId}`; |
| 189 | + if (groupId) |
| 190 | + query += ` GROUPID ${groupId}`; |
| 191 | + if (database) |
| 192 | + query += ` DATABASE ${database}`; |
| 193 | + if (all) |
| 194 | + query += ' ALL'; |
| 195 | + if (node) |
| 196 | + query += ` NODE ${node}`; |
| 197 | + if (!extra.sessionId) { |
| 198 | + throw new Error('Session ID is required'); |
| 199 | + } |
| 200 | + const results = await this.getTransport(extra.sessionId).executeQuery(query); |
| 201 | + return { content: [{ type: 'text', text: JSON.stringify(results) }] }; |
| 202 | + }); |
| 203 | + } |
| 204 | +} |
0 commit comments