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.
This commit is contained in:
Karthik KK
2025-04-22 16:44:36 +12:00
parent bd819d923f
commit c71779fcb5
6 changed files with 22 additions and 2 deletions

BIN
.DS_Store vendored

Binary file not shown.

View File

@@ -25,6 +25,9 @@ node dist/src/index.js --postgresql --host localhost --database yourdb --user po
# With SSL enabled # With SSL enabled
node dist/src/index.js --postgresql --host localhost --database yourdb --user postgres --password yourpassword --ssl true node dist/src/index.js --postgresql --host localhost --database yourdb --user postgres --password yourpassword --ssl true
# With custom connection timeout (in milliseconds)
node dist/src/index.js --postgresql --host localhost --database yourdb --user postgres --password yourpassword --connection-timeout 60000
``` ```
## Command Line Arguments ## Command Line Arguments
@@ -36,6 +39,7 @@ node dist/src/index.js --postgresql --host localhost --database yourdb --user po
- `--password`: The password for the PostgreSQL user. - `--password`: The password for the PostgreSQL user.
- `--port`: The port the PostgreSQL server is listening on (default: 5432). - `--port`: The port the PostgreSQL server is listening on (default: 5432).
- `--ssl`: Whether to use SSL for the connection (true/false). - `--ssl`: Whether to use SSL for the connection (true/false).
- `--connection-timeout`: The connection timeout in milliseconds (default: 30000).
## Usage from MCP Client ## Usage from MCP Client

View File

@@ -88,6 +88,7 @@ Optional parameters:
- `--password`: Password for PostgreSQL authentication - `--password`: Password for PostgreSQL authentication
- `--port`: Port number (default: 5432) - `--port`: Port number (default: 5432)
- `--ssl`: Enable SSL connection (true/false) - `--ssl`: Enable SSL connection (true/false)
- `--connection-timeout`: Connection timeout in milliseconds (default: 30000)
## Configuring Claude Desktop ## Configuring Claude Desktop

BIN
src/.DS_Store vendored

Binary file not shown.

View File

@@ -18,6 +18,7 @@ export class PostgresqlAdapter implements DbAdapter {
port?: number; port?: number;
ssl?: boolean | object; ssl?: boolean | object;
options?: any; options?: any;
connectionTimeout?: number;
}) { }) {
this.host = connectionInfo.host; this.host = connectionInfo.host;
this.database = connectionInfo.database; this.database = connectionInfo.database;
@@ -29,7 +30,9 @@ export class PostgresqlAdapter implements DbAdapter {
port: connectionInfo.port || 5432, port: connectionInfo.port || 5432,
user: connectionInfo.user, user: connectionInfo.user,
password: connectionInfo.password, 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<void> { async init(): Promise<void> {
try { try {
console.error(`[INFO] Connecting to PostgreSQL: ${this.host}, Database: ${this.database}`); 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); this.client = new pg.Client(this.config);
await this.client.connect(); await this.client.connect();
console.error(`[INFO] PostgreSQL connection established successfully`); console.error(`[INFO] PostgreSQL connection established successfully`);

View File

@@ -92,7 +92,8 @@ else if (args.includes('--postgresql') || args.includes('--postgres')) {
user: undefined, user: undefined,
password: undefined, password: undefined,
port: undefined, port: undefined,
ssl: undefined ssl: undefined,
connectionTimeout: undefined
}; };
// Parse PostgreSQL connection parameters // Parse PostgreSQL connection parameters
@@ -109,6 +110,8 @@ else if (args.includes('--postgresql') || args.includes('--postgres')) {
connectionInfo.port = parseInt(args[i + 1], 10); connectionInfo.port = parseInt(args[i + 1], 10);
} else if (args[i] === '--ssl' && i + 1 < args.length) { } else if (args[i] === '--ssl' && i + 1 < args.length) {
connectionInfo.ssl = args[i + 1] === 'true'; connectionInfo.ssl = args[i + 1] === 'true';
} else if (args[i] === '--connection-timeout' && i + 1 < args.length) {
connectionInfo.connectionTimeout = parseInt(args[i + 1], 10);
} }
} }