execute_custom_query
Execute custom SQL queries on PostgreSQL databases to retrieve, analyze, or modify data directly through database connections.
Instructions
Execute a custom SQL query against the database. WARNING: Use with care. Do not expose to untrusted input.
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| connectionString | Yes | PostgreSQL connection string | |
| query | Yes | SQL query to execute | |
| values | No | Optional parameter values for the query | |
| timeout | No | Optional query timeout in milliseconds |
Implementation Reference
- src/tools/custom_query.ts:16-40 (handler)Core implementation of the executeCustomQuery tool handler that connects to the database, executes the SQL query with optional parameters and timeout, and returns a structured result.export async function executeCustomQuery( connectionString: string, query: string, values: unknown[] = [], options: { timeout?: number } = {} ): Promise<CustomQueryResult> { const db = DatabaseConnection.getInstance(); try { await db.connect(connectionString); const result = await db.query(query, values, options); return { success: true, message: 'Query executed successfully', details: result }; } catch (error) { return { success: false, message: `Query execution failed: ${error instanceof Error ? error.message : String(error)}`, details: null }; } finally { await db.disconnect(); } }
- src/index.ts:28-57 (schema)Tool definition including name, description, and detailed input schema for validation.const TOOL_DEFINITIONS = [ { name: 'execute_custom_query', description: 'Execute a custom SQL query against the database. WARNING: Use with care. Do not expose to untrusted input.', inputSchema: { type: 'object', properties: { connectionString: { type: 'string', description: 'PostgreSQL connection string' }, query: { type: 'string', description: 'SQL query to execute' }, values: { type: 'array', description: 'Optional parameter values for the query', items: { type: 'string' } }, timeout: { type: 'number', description: 'Optional query timeout in milliseconds' } }, required: ['connectionString', 'query'] } } ];
- src/index.ts:68-73 (registration)Registers the tool in the MCP server capabilities.tools dictionary.capabilities: { tools: TOOL_DEFINITIONS.reduce((acc, tool) => { acc[tool.name] = tool; return acc; }, {} as Record<string, any>), },
- src/index.ts:101-117 (registration)Registers the tool handler in the CallToolRequestSchema switch statement, extracting arguments, calling the implementation, and formatting the MCP response.case 'execute_custom_query': { const { connectionString, query, values, timeout } = request.params.arguments as { connectionString: string; query: string; values?: unknown[]; timeout?: number; }; const result = await executeCustomQuery(connectionString, query, values ?? [], { timeout }); return { content: [ { type: 'text', text: JSON.stringify(result, null, 2) } ] }; }