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);
Behavior3/5

Does the description disclose side effects, auth requirements, rate limits, or destructive behavior?

No annotations are provided, so the description carries the full burden of behavioral disclosure. It mentions the existence of a default connection and usage constraints, which adds useful context beyond basic functionality. However, it lacks details on error handling, connection persistence, or authentication requirements, leaving gaps in behavioral understanding for a connection tool.

Agents need to know what a tool does to the world before calling it. Descriptions should go beyond structured annotations to explain consequences.

Conciseness5/5

Is the description appropriately sized, front-loaded, and free of redundancy?

The description is extremely concise and front-loaded, with the core purpose stated first followed by critical usage notes. Every sentence earns its place by providing essential information without redundancy, making it efficient and easy to parse.

Shorter descriptions cost fewer tokens and are easier for agents to parse. Every sentence should earn its place.

Completeness4/5

Given the tool's complexity, does the description cover enough for an agent to succeed on first attempt?

Given the tool's complexity (connection establishment with multiple parameters) and lack of annotations or output schema, the description is reasonably complete. It covers purpose and usage guidelines well, but could benefit from more behavioral details (e.g., what happens on success/failure) to fully compensate for the missing structured data.

Complex tools with many parameters or behaviors need more documentation. Simple tools need less. This dimension scales expectations accordingly.

Parameters3/5

Does the description clarify parameter syntax, constraints, interactions, or defaults beyond what the schema provides?

Schema description coverage is 100%, so the schema already documents all five parameters (host, port, user, password, database) with their types and descriptions. The description does not add any additional meaning or context about the parameters beyond what the schema provides, meeting the baseline expectation when schema coverage is high.

Input schemas describe structure but not intent. Descriptions should explain non-obvious parameter relationships and valid value ranges.

Purpose5/5

Does the description clearly state what the tool does and how it differs from similar tools?

The description clearly states the specific action ('Connect to PostgreSQL database') and resource ('PostgreSQL database'), making the purpose immediately understandable. It distinguishes this tool from sibling tools like 'query' or 'execute' by focusing on establishing a database connection rather than performing operations on an already connected database.

Agents choose between tools based on descriptions. A clear purpose with a specific verb and resource helps agents select the right tool.

Usage Guidelines5/5

Does the description explain when to use this tool, when not to, or what alternatives exist?

The description provides explicit guidance on when to use this tool ('only use when requested or if other commands fail') and when not to use it ('Default connection exists'), offering clear context for its application. This helps the agent avoid unnecessary invocations when a default connection is already available.

Agents often have multiple tools that could apply. Explicit usage guidance like "use X instead of Y when Z" prevents misuse.

Install Server

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

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