Skip to main content
Glama
perrypixel

Simple PostgreSQL MCP Server

by perrypixel

execute_query

Execute SQL queries on PostgreSQL databases with configurable read-only or write permissions to interact with database data.

Instructions

Execute SQL queries on PostgreSQL database

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
queryYesSQL query to execute
readOnlyNoIf true, only allows read-only queries (SELECT, EXPLAIN, etc.)

Implementation Reference

  • The core handler function that executes the SQL query against the PostgreSQL database using DatabaseConnection, enforces read-only restrictions, measures execution time, and returns structured results.
    export async function executeQuery(
      connectionString: string,
      query: string,
      readOnly: boolean = false
    ): Promise<QueryResult> {
      const db = DatabaseConnection.getInstance();
      const startTime = Date.now();
      
      try {
        // Check if query is allowed in readOnly mode
        if (readOnly && !isReadOnlyQuery(query)) {
          throw new Error('Only read-only queries are allowed when readOnly is set to true');
        }
        
        await db.connect(connectionString);
        
        // Execute the query
        const result = await db.query(query);
        const executionTime = Date.now() - startTime;
        
        return {
          success: true,
          rowCount: result.rowCount || 0,
          rows: result.rows || [],
          command: result.command || 'UNKNOWN',
          executionTime
        };
      } catch (error) {
        const executionTime = Date.now() - startTime;
        throw new Error(`Query execution failed: ${error instanceof Error ? error.message : String(error)} (execution time: ${executionTime}ms)`);
      } finally {
        await db.disconnect();
      }
    }
  • The tool definition including name, description, and input schema for validating arguments to execute_query.
      name: 'execute_query',
      description: 'Execute SQL queries on PostgreSQL database',
      inputSchema: {
        type: 'object',
        properties: {
          query: {
            type: 'string',
            description: 'SQL query to execute'
          },
          readOnly: {
            type: 'boolean',
            description: 'If true, only allows read-only queries (SELECT, EXPLAIN, etc.)',
            default: false
          }
        },
        required: ['query']
      }
    }
  • src/index.ts:50-53 (registration)
    Registration of the execute_query tool in the MCP server capabilities.
    capabilities: {
      tools: {
        execute_query: TOOL_DEFINITIONS[0]
      },
  • src/index.ts:78-96 (registration)
    Request handler dispatch for execute_query tool, which extracts parameters, enforces server mode, calls the executeQuery handler, and formats the response.
    case 'execute_query': {
      const { query, readOnly = false } = request.params.arguments as {
        query: string;
        readOnly?: boolean;
      };
      
      // Enforce readonly mode if server is in readonly mode
      const enforceReadOnly = this.serverMode === 'readonly' || readOnly;
      
      const result = await executeQuery(this.connectionString, query, enforceReadOnly);
      return {
        content: [
          {
            type: 'text',
            text: JSON.stringify(result, null, 2)
          }
        ]
      };
    }
  • Helper function to determine if a SQL query is read-only by checking starting command and excluding certain modifiers.
    function isReadOnlyQuery(query: string): boolean {
      // Normalize query - trim whitespace and convert to uppercase for comparison
      const normalizedQuery = query.trim().toUpperCase();
      
      // Check if query starts with SELECT, EXPLAIN, SHOW, etc.
      const readOnlyCommands = [
        'SELECT', 
        'EXPLAIN', 
        'SHOW',
        'WITH', // CTE that ends with SELECT
        'ANALYZE',
        'DESCRIBE'
      ];
      
      return readOnlyCommands.some(cmd => normalizedQuery.startsWith(cmd)) &&
        !normalizedQuery.includes('INTO') && // Exclude SELECT INTO
        !normalizedQuery.includes('FOR UPDATE') && // Exclude locking queries
        !normalizedQuery.includes('FOR SHARE');
    }
Behavior2/5

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

With no annotations provided, the description carries full burden for behavioral disclosure. While 'Execute SQL queries' implies mutation capability, it doesn't address critical behavioral aspects like authentication requirements, transaction handling, error behavior, rate limits, or what happens with destructive queries. The description is too minimal for a database query 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 with a single, clear sentence that states exactly what the tool does. There's zero wasted language or unnecessary elaboration.

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

Completeness2/5

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

For a database query execution tool with no annotations and no output schema, the description is insufficient. It doesn't explain what kind of results to expect, error handling, transaction behavior, or security implications. The minimal description leaves too many critical questions unanswered.

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?

With 100% schema description coverage, the schema already documents both parameters thoroughly. The description adds no additional parameter semantics beyond what's in the schema, so it meets the baseline for high schema coverage.

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

Purpose4/5

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

The description clearly states the action ('Execute SQL queries') and target resource ('PostgreSQL database'), providing a specific verb+resource combination. However, without sibling tools to differentiate from, it cannot achieve the highest score for sibling distinction.

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

Usage Guidelines2/5

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

The description provides no guidance on when to use this tool versus alternatives, prerequisites, or specific contexts. It simply states what the tool does without any usage instructions or exclusions.

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/perrypixel/Simple-Postgres-MCP'

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