Skip to main content
Glama
HenkDz

PostgreSQL MCP Server

pg_manage_functions

Manage PostgreSQL database functions by listing existing functions, creating new ones with custom parameters and return types, or removing functions when needed.

Instructions

Manage PostgreSQL functions - get, create, or drop functions with a single tool. Examples: operation="get" to list functions, operation="create" with functionName="test_func", parameters="" (empty for no params), returnType="TEXT", functionBody="SELECT 'Hello'"

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
connectionStringNoPostgreSQL connection string (optional)
operationYesOperation to perform: get (list/info), create (new function), or drop (remove function)
functionNameNoName of the function (required for create/drop, optional for get to filter)
schemaNoSchema name (defaults to public)
parametersNoFunction parameters - required for create operation, required for drop when function is overloaded. Use empty string "" for functions with no parameters
returnTypeNoReturn type of the function (required for create operation)
functionBodyNoFunction body code (required for create operation)
languageNoFunction language (defaults to plpgsql for create)
volatilityNoFunction volatility (defaults to VOLATILE for create)
securityNoFunction security context (defaults to INVOKER for create)
replaceNoWhether to replace the function if it exists (for create operation)
ifExistsNoWhether to include IF EXISTS clause (for drop operation)
cascadeNoWhether to include CASCADE clause (for drop operation)

Implementation Reference

  • Core handler logic for the 'pg_manage_functions' tool. Dispatches to internal _getFunctions, _createFunction, or _dropFunction based on the 'operation' parameter ('get', 'create', or 'drop'). Handles validation, connection resolution, and error formatting.
    execute: async (args: any, getConnectionStringVal: GetConnectionStringFn): Promise<ToolOutput> => {
      const { 
        connectionString: connStringArg,
        operation,
        functionName,
        schema,
        parameters,
        returnType,
        functionBody,
        language,
        volatility,
        security,
        replace,
        ifExists,
        cascade
      } = args as {
        connectionString?: string;
        operation: 'get' | 'create' | 'drop';
        functionName?: string;
        schema?: string;
        parameters?: string;
        returnType?: string;
        functionBody?: string;
        language?: 'sql' | 'plpgsql' | 'plpython3u';
        volatility?: 'VOLATILE' | 'STABLE' | 'IMMUTABLE';
        security?: 'INVOKER' | 'DEFINER';
        replace?: boolean;
        ifExists?: boolean;
        cascade?: boolean;
      };
    
      const resolvedConnString = getConnectionStringVal(connStringArg);
      let result: FunctionResult;
    
      try {
        switch (operation) {
          case 'get':
            result = await _getFunctions(resolvedConnString, functionName, schema);
            if (result.success) {
              return { content: [{ type: 'text', text: JSON.stringify(result.details, null, 2) || result.message }] };
            }
            break;
    
          case 'create': {
            // Debug logging to understand what's being passed
            console.error('DEBUG - Create operation parameters:', {
              functionName: functionName,
              parameters: parameters,
              returnType: returnType,
              functionBody: functionBody,
              parametersType: typeof parameters,
              parametersUndefined: parameters === undefined,
              parametersNull: parameters === null
            });
            
            // Fix validation: be more specific about which fields are missing
            const missingFields = [];
            if (!functionName) missingFields.push('functionName');
            if (!returnType) missingFields.push('returnType');
            if (!functionBody) missingFields.push('functionBody');
            
            if (missingFields.length > 0) {
              return { 
                content: [{ type: 'text', text: `Error: Missing required fields: ${missingFields.join(', ')}. Note: parameters can be empty string "" for functions with no parameters` }], 
                isError: true 
              };
            }
            
            // Normalize parameters: treat undefined, null, or whitespace-only as empty string
            const normalizedParameters: string = parameters === undefined || parameters === null ? '' : 
              (typeof parameters === 'string' && parameters.trim() === '') ? '' : String(parameters);
            result = await _createFunction(resolvedConnString, functionName as string, normalizedParameters, returnType as string, functionBody as string, {
              language,
              volatility,
              schema,
              security,
              replace
            });
            break;
          }
    
          case 'drop':
            if (!functionName) {
              return { 
                content: [{ type: 'text', text: 'Error: functionName is required for drop operation' }], 
                isError: true 
              };
            }
            result = await _dropFunction(resolvedConnString, functionName, parameters, {
              schema,
              ifExists,
              cascade
            });
            break;
    
          default:
            return { 
              content: [{ type: 'text', text: `Error: Unknown operation "${operation}". Supported operations: get, create, drop` }], 
              isError: true 
            };
        }
    
        if (result.success) {
          return { content: [{ type: 'text', text: result.message + (result.details ? ` Details: ${JSON.stringify(result.details)}` : '') }] };
        }
        return { content: [{ type: 'text', text: result.message }], isError: true };
    
      } catch (error) {
        return { 
          content: [{ type: 'text', text: `Error executing ${operation} operation: ${error instanceof Error ? error.message : String(error)}` }], 
          isError: true 
        };
      }
    }
  • Zod input schema defining parameters for pg_manage_functions tool, including operation type and specific fields for create/drop operations.
    inputSchema: z.object({
      connectionString: z.string().optional().describe('PostgreSQL connection string (optional)'),
      operation: z.enum(['get', 'create', 'drop']).describe('Operation to perform: get (list/info), create (new function), or drop (remove function)'),
      
      // Common parameters
      functionName: z.string().optional().describe('Name of the function (required for create/drop, optional for get to filter)'),
      schema: z.string().optional().describe('Schema name (defaults to public)'),
      
      // Create operation parameters
      parameters: z.string().optional().describe('Function parameters - required for create operation, required for drop when function is overloaded. Use empty string "" for functions with no parameters'),
      returnType: z.string().optional().describe('Return type of the function (required for create operation)'),
      functionBody: z.string().optional().describe('Function body code (required for create operation)'),
      language: z.enum(['sql', 'plpgsql', 'plpython3u']).optional().describe('Function language (defaults to plpgsql for create)'),
      volatility: z.enum(['VOLATILE', 'STABLE', 'IMMUTABLE']).optional().describe('Function volatility (defaults to VOLATILE for create)'),
      security: z.enum(['INVOKER', 'DEFINER']).optional().describe('Function security context (defaults to INVOKER for create)'),
      replace: z.boolean().optional().describe('Whether to replace the function if it exists (for create operation)'),
      
      // Drop operation parameters  
      ifExists: z.boolean().optional().describe('Whether to include IF EXISTS clause (for drop operation)'),
      cascade: z.boolean().optional().describe('Whether to include CASCADE clause (for drop operation)')
    }),
  • Tool registration exporting the pg_manage_functions tool object with name, description, schema, and execute handler.
    export const manageFunctionsTool: PostgresTool = {
      name: 'pg_manage_functions',
      description: 'Manage PostgreSQL functions - get, create, or drop functions with a single tool. Examples: operation="get" to list functions, operation="create" with functionName="test_func", parameters="" (empty for no params), returnType="TEXT", functionBody="SELECT \'Hello\'"',
  • Helper function to retrieve information about database functions using PostgreSQL system catalogs.
    async function _getFunctions(
      connectionString: string,
      functionName?: string,
      schema = 'public'
    ): Promise<FunctionResult> {
      const db = DatabaseConnection.getInstance();
      
      try {
        await db.connect(connectionString);
        
        let query = `
          SELECT 
            p.proname AS name,
            l.lanname AS language,
            pg_get_function_result(p.oid) AS "returnType",
            pg_get_function_arguments(p.oid) AS "arguments",
            CASE
              WHEN p.provolatile = 'i' THEN 'IMMUTABLE'
              WHEN p.provolatile = 's' THEN 'STABLE'
              WHEN p.provolatile = 'v' THEN 'VOLATILE'
            END AS volatility,
            pg_get_functiondef(p.oid) AS definition,
            a.rolname AS owner
          FROM pg_proc p
          JOIN pg_namespace n ON p.pronamespace = n.oid
          JOIN pg_language l ON p.prolang = l.oid
          JOIN pg_authid a ON p.proowner = a.oid
          WHERE n.nspname = $1
        `;
        
        const params: (string | undefined)[] = [schema];
        
        if (functionName) {
          query += ' AND p.proname = $2';
          params.push(functionName);
        }
        
        query += ' ORDER BY p.proname';
        
        const functions = await db.query<FunctionInfo>(query, params);
        
        return {
          success: true,
          message: functionName 
            ? `Function information for ${functionName}` 
            : `Found ${functions.length} functions in schema ${schema}`,
          details: functions
        };
      } catch (error) {
        return {
          success: false,
          message: `Failed to get function information: ${error instanceof Error ? error.message : String(error)}`,
          details: null
        };
      } finally {
        await db.disconnect();
      }
    }
  • Helper function to create or replace a database function with specified parameters and body.
    async function _createFunction(
      connectionString: string,
      functionName: string,
      parameters: string,
      returnType: string,
      functionBody: string,
      options: {
        language?: 'sql' | 'plpgsql' | 'plpython3u';
        volatility?: 'VOLATILE' | 'STABLE' | 'IMMUTABLE';
        schema?: string;
        security?: 'INVOKER' | 'DEFINER';
        replace?: boolean;
      } = {}
    ): Promise<FunctionResult> {
      const db = DatabaseConnection.getInstance();
      
      try {
        await db.connect(connectionString);
        
        const language = options.language || 'plpgsql';
        const volatility = options.volatility || 'VOLATILE';
        const schema = options.schema || 'public';
        const security = options.security || 'INVOKER';
        const createOrReplace = options.replace ? 'CREATE OR REPLACE' : 'CREATE';
        
        // Build function creation SQL
        const sql = `
          ${createOrReplace} FUNCTION ${schema}.${functionName}(${parameters})
          RETURNS ${returnType}
          LANGUAGE ${language}
          ${volatility}
          SECURITY ${security}
          AS $function$
          ${functionBody}
          $function$;
        `;
        
        await db.query(sql);
        
        return {
          success: true,
          message: `Function ${functionName} created successfully`,
          details: {
            name: functionName,
            schema,
            returnType,
            language,
            volatility,
            security
          }
        };
      } catch (error) {
        return {
          success: false,
          message: `Failed to create function: ${error instanceof Error ? error.message : String(error)}`,
          details: null
        };
      } finally {
        await db.disconnect();
      }
    }
  • Helper function to drop a database function, supporting overloaded functions and CASCADE.
    async function _dropFunction(
      connectionString: string,
      functionName: string,
      parameters?: string,
      options: {
        schema?: string;
        ifExists?: boolean;
        cascade?: boolean;
      } = {}
    ): Promise<FunctionResult> {
      const db = DatabaseConnection.getInstance();
      
      try {
        await db.connect(connectionString);
        
        const schema = options.schema || 'public';
        const ifExists = options.ifExists ? 'IF EXISTS' : '';
        const cascade = options.cascade ? 'CASCADE' : '';
        
        // Build function drop SQL
        let sql = `DROP FUNCTION ${ifExists} ${schema}.${functionName}`;
        
        // Add parameters if provided
        if (parameters) {
          sql += `(${parameters})`;
        }
        
        // Add cascade if specified
        if (cascade) {
          sql += ` ${cascade}`;
        }
        
        await db.query(sql);
        
        return {
          success: true,
          message: `Function ${functionName} dropped successfully`,
          details: {
            name: functionName,
            schema
          }
        };
      } catch (error) {
        return {
          success: false,
          message: `Failed to drop function: ${error instanceof Error ? error.message : String(error)}`,
          details: null
        };
      } finally {
        await db.disconnect();
      }
    }
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 it mentions the three operations (get, create, drop), it doesn't disclose critical behavioral traits: whether create/drop operations are destructive, what permissions are required, whether operations are transactional, or what happens on errors. The examples show basic usage but lack comprehensive behavioral context for a multi-operation tool.

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

Conciseness4/5

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

The description is appropriately sized with two sentences: a clear purpose statement followed by specific examples. It's front-loaded with the core functionality and uses examples efficiently to illustrate usage. Every sentence serves a purpose, though the example could be slightly more structured.

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 complex tool with 13 parameters, three distinct operations (including destructive create/drop), no annotations, and no output schema, the description is insufficient. It doesn't explain what the tool returns for different operations, error handling, transaction behavior, or the implications of create/drop operations. The examples help but don't compensate for the missing behavioral context needed for safe use.

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 13 parameters thoroughly. The description adds minimal value beyond the schema by mentioning examples like 'parameters="" (empty for no params)', but doesn't provide additional semantic context about parameter interactions or operation-specific requirements that aren't already in the schema descriptions.

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 tool's purpose: 'Manage PostgreSQL functions - get, create, or drop functions with a single tool.' It specifies the exact operations (get, create, drop) and the resource (PostgreSQL functions), distinguishing it from siblings like pg_manage_indexes or pg_manage_triggers that handle different database objects.

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

Usage Guidelines4/5

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

The description provides clear context for when to use this tool through examples: 'operation="get" to list functions, operation="create" with functionName="test_func"...' It implicitly suggests this is for PostgreSQL function management rather than other database operations, but doesn't explicitly state when not to use it or name alternatives among siblings.

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/HenkDz/postgresql-mcp-server'

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