Skip to main content
Glama
nahmanmate

PostgreSQL MCP Server

by nahmanmate

debug_database

Identify and resolve common PostgreSQL issues such as connection errors, performance bottlenecks, locks, and replication problems by analyzing database configurations and logs based on specified issue type and log level.

Instructions

Debug common PostgreSQL issues

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
connectionStringYesPostgreSQL connection string
issueYesType of issue to debug
logLevelNoLogging detail levelinfo

Implementation Reference

  • Core handler function implementing the debug_database tool logic. Connects to the PostgreSQL database using the provided connection string and dispatches to specialized debugging functions based on the issue type.
    export async function debugDatabase( connectionString: string, issue: IssueType, logLevel: LogLevel = 'info' ): Promise<DebugResult> { const db = DatabaseConnection.getInstance(); try { await db.connect(connectionString); switch (issue) { case 'connection': return await debugConnection(db); case 'performance': return await debugPerformance(db); case 'locks': return await debugLocks(db); case 'replication': return await debugReplication(db); default: throw new Error(`Unsupported issue type: ${issue}`); } } finally { await db.disconnect(); } }
  • MCP tool schema definition for debug_database, specifying input parameters, types, enums, descriptions, and required fields.
    { name: 'debug_database', description: 'Debug common PostgreSQL issues', inputSchema: { type: 'object', properties: { connectionString: { type: 'string', description: 'PostgreSQL connection string' }, issue: { type: 'string', enum: [ 'connection', 'performance', 'locks', 'replication' ], description: 'Type of issue to debug' }, logLevel: { type: 'string', enum: ['info', 'debug', 'trace'], default: 'info', description: 'Logging detail level' } }, required: ['connectionString', 'issue'] } }
  • src/index.ts:102-106 (registration)
    Registration of the debug_database tool in the MCP server capabilities, referencing its TOOL_DEFINITION.
    tools: { analyze_database: TOOL_DEFINITIONS[0], get_setup_instructions: TOOL_DEFINITIONS[1], debug_database: TOOL_DEFINITIONS[2] },
  • MCP request handler case for calling the debug_database tool, extracting arguments and invoking the core debugDatabase function.
    case 'debug_database': { const { connectionString, issue, logLevel } = request.params.arguments as { connectionString: string; issue: 'connection' | 'performance' | 'locks' | 'replication'; logLevel?: 'info' | 'debug' | 'trace'; }; const result = await debugDatabase(connectionString, issue, logLevel); return { content: [ { type: 'text', text: JSON.stringify(result, null, 2) } ] }; }
  • Helper function for debugging connection issues, checking connection usage, idle connections, etc.
    async function debugConnection(db: DatabaseConnection): Promise<DebugResult> { const result: DebugResult = { issue: 'connection', status: 'ok', details: [], recommendations: [] }; try { // Check max connections const maxConns = await db.query<{ setting: string }>( "SELECT setting FROM pg_settings WHERE name = 'max_connections'" ); const currentConns = await db.query<{ count: string }>( 'SELECT count(*) FROM pg_stat_activity' ); const max = parseInt(maxConns[0].setting); const current = parseInt(currentConns[0].count); const percentage = (current / max) * 100; result.details.push( `Current connections: ${current}/${max} (${percentage.toFixed(1)}%)` ); if (percentage > 80) { result.status = 'warning'; result.recommendations.push( 'High connection usage. Consider implementing connection pooling', 'Review application connection handling', 'Monitor for connection leaks' ); } // Check for idle connections const idleConns = await db.query<{ count: string }>( "SELECT count(*) FROM pg_stat_activity WHERE state = 'idle'" ); const idleCount = parseInt(idleConns[0].count); if (idleCount > 5) { result.details.push(`High number of idle connections: ${idleCount}`); result.recommendations.push( 'Consider implementing connection timeouts', 'Review connection pool settings' ); } } catch (error: unknown) { result.status = 'error'; result.details.push(`Connection error: ${error instanceof Error ? error.message : String(error)}`); } return result; }

Other Tools

Related 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/nahmanmate/postgresql-mcp-server'

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