Skip to main content
Glama
1Levick3

PostgreSQL MCP Server

by 1Levick3

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
NameRequiredDescriptionDefault
connectionStringYesPostgreSQL connection string
queryYesSQL query to execute
valuesNoOptional parameter values for the query
timeoutNoOptional query timeout in milliseconds

Implementation Reference

  • 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(); } }
  • 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) } ] }; }

Other Tools

Latest Blog Posts

MCP directory API

We provide all the information about MCP servers via our MCP API.

curl -X GET 'https://glama.ai/api/mcp/v1/servers/1Levick3/postgresql-mcp-server'

If you have feedback or need assistance with the MCP directory API, please join our Discord server