Skip to main content
Glama
martymarkenson

PostgreSQL MCP Server

Test Postgres Connection

test-postgres-connection

Check if your PostgreSQL database is accessible. Returns connection status to verify connectivity before executing queries.

Instructions

Test the Postgres connection and return connection status

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault

No arguments

Implementation Reference

  • The main handler function for the 'test-postgres-connection' tool. It attempts to connect to the database using initDb(), tests the connection via testDB(), and returns success/failure with configuration details.
    async () => {
      try {
        // Show current configuration (without password)
        const config = {
          host: process.env.DB_HOST,
          port: process.env.DB_PORT || '5432',
          database: process.env.DB_NAME,
          username: process.env.DB_USERNAME,
          password: '***hidden***'
        };
    
        // Attempt to connect and test
        const db = initDb();
        const testResult = await testDB(db);
        
        if (testResult.success) {
          return {
            content: [{
              type: "text",
              text: `✅ Database connection successful!\n\nConfiguration:\n${JSON.stringify(config, null, 2)}\n\nConnection test result: ${JSON.stringify(testResult.result, null, 2)}`
            }]
          };
        } else {
          return {
            content: [{
              type: "text",
              text: `❌ Database connection failed!\n\nConfiguration:\n${JSON.stringify(config, null, 2)}\n\nError: ${testResult.error}`
            }],
            isError: true
          };
        }
      } catch (error) {
        return {
          content: [{
            type: "text",
            text: `❌ Unexpected error during connection test: ${error instanceof Error ? error.message : String(error)}`
          }],
          isError: true
        };
      }
    }
  • Input schema/parameter definition for the tool. No input parameters are defined — the tool takes no arguments.
    {
      title: "Test Postgres Connection",
      description: "Test the Postgres connection and return connection status",
    },
  • src/server.ts:85-132 (registration)
    Registration of the tool via server.registerTool with name 'test-postgres-connection', a title, description, and the handler callback.
    server.registerTool(
      "test-postgres-connection",
      {
        title: "Test Postgres Connection",
        description: "Test the Postgres connection and return connection status",
      },
      async () => {
        try {
          // Show current configuration (without password)
          const config = {
            host: process.env.DB_HOST,
            port: process.env.DB_PORT || '5432',
            database: process.env.DB_NAME,
            username: process.env.DB_USERNAME,
            password: '***hidden***'
          };
    
          // Attempt to connect and test
          const db = initDb();
          const testResult = await testDB(db);
          
          if (testResult.success) {
            return {
              content: [{
                type: "text",
                text: `✅ Database connection successful!\n\nConfiguration:\n${JSON.stringify(config, null, 2)}\n\nConnection test result: ${JSON.stringify(testResult.result, null, 2)}`
              }]
            };
          } else {
            return {
              content: [{
                type: "text",
                text: `❌ Database connection failed!\n\nConfiguration:\n${JSON.stringify(config, null, 2)}\n\nError: ${testResult.error}`
              }],
              isError: true
            };
          }
        } catch (error) {
          return {
            content: [{
              type: "text",
              text: `❌ Unexpected error during connection test: ${error instanceof Error ? error.message : String(error)}`
            }],
            isError: true
          };
        }
      }
    );
  • Helper function 'testDB' used to execute a simple 'SELECT 1 as test' query against the database to verify connectivity.
    const testDB = async (db: postgres.Sql) => {
      try {
        // Simple query to test connection
        const result = await db`SELECT 1 as test`;
        return { success: true, message: "Connection successful", result };
      } catch (error) {
        return { 
          success: false, 
          message: "Connection failed", 
          error: error instanceof Error ? error.message : String(error)
        };
      }
    };
  • Helper function 'initDb' that initializes (or returns existing) database connection using 'getDb'.
    const initDb = () => {
      if(!dbConnection) {
        dbConnection = getDb();
      }
      return dbConnection;
    };
Behavior3/5

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

The description discloses the basic behavior: testing connection and returning status. No annotations are provided, so the description carries the full burden. It lacks details on whether the test modifies state, requires authentication, or has side effects, but for a test tool the behavior is straightforward.

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 a single sentence with no filler or redundant information. It is front-loaded and efficient.

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?

The tool is simple (no parameters, no output schema), and the description adequately communicates its purpose and result. It could elaborate on what 'connection status' entails (e.g., boolean, string), but the essential information is present.

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

Parameters4/5

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

The input schema has no parameters (0 params), so schema description coverage is 100%. The description does not need to add parameter details, and baseline for 0 params is 4.

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 action (test) and resource (Postgres connection) and indicates the output (connection status). It effectively distinguishes from siblings: execute-sql-query runs queries and get-all-tables retrieves schemas.

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

Usage Guidelines3/5

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

The description does not provide explicit guidance on when to use this tool versus alternatives. However, the purpose is self-explanatory as a diagnostic check, so usage is implied but not explicitly stated.

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/martymarkenson/Postgres-Connector-MCP'

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