Skip to main content
Glama
antonorlov

MCP PostgreSQL Server

connect_db

Establish a connection to a PostgreSQL database within the MCP PostgreSQL Server. Use this tool when explicitly requested or if other database commands encounter connection failures.

Instructions

Connect to PostgreSQL database. NOTE: Default connection exists - only use when requested or if other commands fail

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
hostYesDatabase host
portNoDatabase port (default: 5432)
userYesDatabase user
passwordYesDatabase password
databaseYesDatabase name

Implementation Reference

  • The main execution handler for the 'connect_db' tool. Validates input parameters, closes any existing database connection, updates the configuration, establishes a new connection via ensureConnection(), and returns a success response 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)}`
        );
      }
    }
  • Defines the input schema for the 'connect_db' tool, specifying properties for database connection details and 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)
    Registers the 'connect_db' tool in the ListTools handler, including its 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)
    In the CallTool request handler switch statement, dispatches 'connect_db' calls to the handleConnectDb method.
    case 'connect_db':
      return await this.handleConnectDb(request.params.arguments);

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/antonorlov/mcp-postgres-server'

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