connect_db
Establish a connection to a PostgreSQL database using specified credentials. Use when default connection fails or when explicitly required for database operations.
Instructions
Connect to PostgreSQL database. NOTE: Default connection exists - only use when requested or if other commands fail
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| database | Yes | Database name | |
| host | Yes | Database host | |
| password | Yes | Database password | |
| port | No | Database port (default: 5432) | |
| user | Yes | Database user |
Implementation Reference
- src/index.ts:278-316 (handler)The main handler function that executes the connect_db tool logic. It validates input parameters, closes any existing connection, sets the new database configuration, connects to the PostgreSQL database using ensureConnection, and returns a success message or throws an error.private async handleConnectDb(args: any) { if (!args.host || !args.user || !args.password || !args.database) { throw new McpError( ErrorCode.InvalidParams, 'Missing required database configuration parameters' ); } // Close existing connection if any if (this.client) { await this.client.end(); this.client = null; } this.config = { host: args.host, port: args.port || 5432, user: args.user, password: args.password, database: args.database, }; try { await this.ensureConnection(); return { content: [ { type: 'text', text: 'Successfully connected to PostgreSQL database', }, ], }; } catch (error) { throw new McpError( ErrorCode.InternalError, `Failed to connect to database: ${getErrorMessage(error)}` ); } }
- src/index.ts:142-167 (schema)Input schema for the connect_db tool, defining the expected parameters for database connection (host, port, user, password, database) with required fields.inputSchema: { type: 'object', properties: { host: { type: 'string', description: 'Database host', }, port: { type: 'number', description: 'Database port (default: 5432)', }, user: { type: 'string', description: 'Database user', }, password: { type: 'string', description: 'Database password', }, database: { type: 'string', description: 'Database name', }, }, required: ['host', 'user', 'password', 'database'], },
- src/index.ts:139-168 (registration)Registration of the connect_db tool in the ListTools response, including name, description, and input schema.{ name: 'connect_db', description: 'Connect to PostgreSQL database. NOTE: Default connection exists - only use when requested or if other commands fail', inputSchema: { type: 'object', properties: { host: { type: 'string', description: 'Database host', }, port: { type: 'number', description: 'Database port (default: 5432)', }, user: { type: 'string', description: 'Database user', }, password: { type: 'string', description: 'Database password', }, database: { type: 'string', description: 'Database name', }, }, required: ['host', 'user', 'password', 'database'], }, },
- src/index.ts:257-258 (registration)Dispatch case in the CallToolRequest handler that routes connect_db calls to the handleConnectDb method.case 'connect_db': return await this.handleConnectDb(request.params.arguments);
- src/index.ts:17-23 (schema)TypeScript interface defining the DatabaseConfig used by the connect_db handler.interface DatabaseConfig { host: string; port: number; user: string; password: string; database: string; }