From c71779fcb52565b3228adb6702dc1680f1cc8ae9 Mon Sep 17 00:00:00 2001 From: Karthik KK Date: Tue, 22 Apr 2025 16:44:36 +1200 Subject: [PATCH] Add connection timeout support for PostgreSQL in MCP Database Server Enhanced the PostgreSQL connection handling by introducing a `--connection-timeout` parameter, allowing users to specify a custom timeout in milliseconds. Updated relevant documentation in README and postgresql-setup.md to reflect this new option. Modified the PostgresqlAdapter to utilize the connection timeout setting during database connections. --- .DS_Store | Bin 8196 -> 8196 bytes docs/postgresql-setup.md | 4 ++++ readme.md | 1 + src/.DS_Store | Bin 6148 -> 6148 bytes src/db/postgresql-adapter.ts | 14 +++++++++++++- src/index.ts | 5 ++++- 6 files changed, 22 insertions(+), 2 deletions(-) diff --git a/.DS_Store b/.DS_Store index 615e03ba58b7bdaca96b2d0a9785bbdc197533a5..2cccfe87c9540f8c1af1eae5f18b0fba6669b55e 100644 GIT binary patch delta 493 zcmZp1XmQxELx6SPU3Sm7$$JH)A*{`J1-KYv@AWeJOa(KpF0|yFR7GW%=EV9`Qj0&5RL|3xY(C|e}0Es?` A2><{9 delta 493 zcmZp1XmQxELx8nIs`j(ju1+)MvfiM|af*;vPwmlCf ytB5=gUm6Z{G&3+riWriSO>~p_xYY*YWVkHCSWH=Dvl$o}HYbU$WT&Cwi*xDCguPD delta 127 zcmZoMXffFEj)loeaPkKh5o<-&g}>dd9dl%0U|?cMVMxl&PjN}g$xj0EInd<_G7N*0 z^K%P;azGF;*`76$?eZVClh>|Jp2w<$V2Vw?!|K82!Wy{{D5J$D17&XZVcQ`H0A-yl A5dZ)H diff --git a/src/db/postgresql-adapter.ts b/src/db/postgresql-adapter.ts index 1b68297..6e95cf1 100644 --- a/src/db/postgresql-adapter.ts +++ b/src/db/postgresql-adapter.ts @@ -18,6 +18,7 @@ export class PostgresqlAdapter implements DbAdapter { port?: number; ssl?: boolean | object; options?: any; + connectionTimeout?: number; }) { this.host = connectionInfo.host; this.database = connectionInfo.database; @@ -29,7 +30,9 @@ export class PostgresqlAdapter implements DbAdapter { port: connectionInfo.port || 5432, user: connectionInfo.user, password: connectionInfo.password, - ssl: connectionInfo.ssl + ssl: connectionInfo.ssl, + // Add connection timeout if provided (in milliseconds) + connectionTimeoutMillis: connectionInfo.connectionTimeout || 30000, }; } @@ -39,6 +42,15 @@ export class PostgresqlAdapter implements DbAdapter { async init(): Promise { try { console.error(`[INFO] Connecting to PostgreSQL: ${this.host}, Database: ${this.database}`); + console.error(`[DEBUG] Connection details:`, { + host: this.host, + database: this.database, + port: this.config.port, + user: this.config.user, + connectionTimeoutMillis: this.config.connectionTimeoutMillis, + ssl: !!this.config.ssl + }); + this.client = new pg.Client(this.config); await this.client.connect(); console.error(`[INFO] PostgreSQL connection established successfully`); diff --git a/src/index.ts b/src/index.ts index 5da0fee..6b29b06 100644 --- a/src/index.ts +++ b/src/index.ts @@ -92,7 +92,8 @@ else if (args.includes('--postgresql') || args.includes('--postgres')) { user: undefined, password: undefined, port: undefined, - ssl: undefined + ssl: undefined, + connectionTimeout: undefined }; // Parse PostgreSQL connection parameters @@ -109,6 +110,8 @@ else if (args.includes('--postgresql') || args.includes('--postgres')) { connectionInfo.port = parseInt(args[i + 1], 10); } else if (args[i] === '--ssl' && i + 1 < args.length) { connectionInfo.ssl = args[i + 1] === 'true'; + } else if (args[i] === '--connection-timeout' && i + 1 < args.length) { + connectionInfo.connectionTimeout = parseInt(args[i + 1], 10); } }