Skip to main content
Glama
jackyxhb

Infer MCP Server

by jackyxhb

dbQuery

Execute SQL queries on PostgreSQL databases using configured profiles to retrieve or modify data through the Infer MCP Server.

Instructions

Execute a SQL query on a PostgreSQL database using a configured profile

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
profileYesDatabase profile to use
queryYesSQL query to execute
parametersNoPositional parameters for the query
timeoutMsNoQuery timeout in milliseconds
rowLimitNoOverride maximum number of rows to return

Output Schema

TableJSON Schema
NameRequiredDescriptionDefault
rowsYes
rowCountYes
truncatedYes
durationMsYes

Implementation Reference

  • The handler function for the 'dbQuery' MCP tool. It creates a progress reporter, executes the database query via executeDatabaseQuery service, structures the output, and returns it in the expected MCP format.
      async (args, extra) => {
        const progress = createProgressReporter(extra, "dbQuery");
        const total = 1;
    
        progress?.({ progress: 0, total, message: "Dispatching database query" });
    
        const result: DatabaseQueryResult = await executeDatabaseQuery(
          args.profile,
          args.query,
          args.parameters,
          {
            timeoutMs: args.timeoutMs,
            rowLimit: args.rowLimit,
            requestId: String(extra.requestId),
            tool: "dbQuery",
            signal: extra.signal,
            onProgress: (update) => {
              progress?.({
                progress: update.progress,
                total,
                message: update.message
              });
            }
          }
        );
    
        const structuredContent: Record<string, unknown> = {
          rows: result.rows,
          rowCount: result.rowCount,
          truncated: result.truncated,
          durationMs: result.durationMs
        };
    
        return {
          content: [],
          structuredContent
        };
      }
    );
  • Zod schemas defining the input (DbQueryInputSchema) and output (DbQueryOutputShape and Schema) for the dbQuery tool.
    const DbQueryInputSchema = z.object({
      profile: z.string().describe("Database profile to use"),
      query: z.string().min(1).describe("SQL query to execute"),
      parameters: z.array(z.unknown()).optional().describe("Positional parameters for the query"),
      timeoutMs: z.number().int().positive().optional().describe("Query timeout in milliseconds"),
      rowLimit: z.number().int().positive().optional().describe("Override maximum number of rows to return")
    });
    
    const DbQueryOutputShape = {
      rows: z.array(z.record(z.string(), z.unknown())),
      rowCount: z.number().int().nonnegative(),
      truncated: z.boolean(),
      durationMs: z.number().int().nonnegative()
    };
    
    const DbQueryOutputSchema = z.object(DbQueryOutputShape);
  • The registerDbTool function that registers the 'dbQuery' tool with the MCP server, specifying name, description, schemas, and handler.
    export function registerDbTool(server: McpServer): void {
      server.registerTool(
        "dbQuery",
        {
          description: "Execute a SQL query on a PostgreSQL database using a configured profile",
          inputSchema: DbQueryInputSchema.shape,
          outputSchema: DbQueryOutputShape
        },
        async (args, extra) => {
          const progress = createProgressReporter(extra, "dbQuery");
          const total = 1;
    
          progress?.({ progress: 0, total, message: "Dispatching database query" });
    
          const result: DatabaseQueryResult = await executeDatabaseQuery(
            args.profile,
            args.query,
            args.parameters,
            {
              timeoutMs: args.timeoutMs,
              rowLimit: args.rowLimit,
              requestId: String(extra.requestId),
              tool: "dbQuery",
              signal: extra.signal,
              onProgress: (update) => {
                progress?.({
                  progress: update.progress,
                  total,
                  message: update.message
                });
              }
            }
          );
    
          const structuredContent: Record<string, unknown> = {
            rows: result.rows,
            rowCount: result.rowCount,
            truncated: result.truncated,
            durationMs: result.durationMs
          };
    
          return {
            content: [],
            structuredContent
          };
        }
      );
    }
  • Top-level registration function that calls registerDbTool to include dbQuery among all tools.
    export function registerTools(server: McpServer): void {
      registerSshTool(server);
      registerDbTool(server);
      registerTrainingTool(server);
    }
  • Core helper function that executes the SQL query on PostgreSQL with safety validations, concurrency limiting, progress reporting, timeouts, row limits, and proper error handling.
    export async function executeDatabaseQuery(
      profileName: string,
      query: string,
      values: unknown[] | undefined,
      options: DatabaseQueryOptions = {}
    ): Promise<DatabaseQueryResult> {
      const config = getConfig();
      const profile = config.databaseProfiles[profileName];
      if (!profile) {
        throw new Error(`Database profile '${profileName}' not found`);
      }
    
      validateQuerySafety(query);
      ensureQueryAllowed(query, profile.allowedStatementPatterns);
    
      const timeoutMs = Math.min(options.timeoutMs ?? profile.maxExecutionMs, profile.maxExecutionMs);
      const rowLimit = Math.min(options.rowLimit ?? profile.maxRows, profile.maxRows);
    
      options.onProgress?.({ progress: 0, message: "Waiting for database availability" });
      const release = await dbConcurrency.acquire(profileName, profile.maxConcurrent, options.signal);
    
      const client = new Client({ connectionString: profile.connectionString });
      const start = Date.now();
      const abortError = createAbortError("Database query cancelled");
      let aborted = false;
      let abortListener: (() => void) | undefined;
    
      logger.info("Executing database query", {
        profile: profileName,
        requestId: options.requestId,
        tool: options.tool
      });
    
      try {
        if (options.signal) {
          if (options.signal.aborted) {
            aborted = true;
            throw abortError;
          }
    
          abortListener = () => {
            aborted = true;
            options.onProgress?.({ progress: 1, message: "Database query cancelled" });
            if (client.connection?.stream) {
              client.connection.stream.destroy(abortError);
            }
          };
    
          options.signal.addEventListener("abort", abortListener, { once: true });
        }
    
        options.onProgress?.({ progress: 0.1, message: "Connecting to database" });
        await client.connect();
        if (aborted) {
          throw abortError;
        }
    
        options.onProgress?.({ progress: 0.3, message: "Applying session limits" });
        await applyTimeout(client, timeoutMs);
    
        options.onProgress?.({ progress: 0.6, message: "Executing query" });
        const result: QueryResult = await client.query({ text: query, values });
        if (aborted) {
          throw abortError;
        }
    
        const rows = result.rows.slice(0, rowLimit);
        const truncated = result.rows.length > rows.length;
        const rowCount = result.rowCount ?? rows.length;
        const durationMs = Date.now() - start;
    
        logger.info("Database query completed", {
          profile: profileName,
          requestId: options.requestId,
          tool: options.tool,
          rowCount,
          truncated,
          durationMs
        });
    
        options.onProgress?.({ progress: 1, message: "Database query completed" });
    
        return {
          rows,
          rowCount,
          truncated,
          durationMs
        };
      } catch (error) {
        if (aborted) {
          throw abortError;
        }
    
        logger.error("Database query failed", {
          profile: profileName,
          requestId: options.requestId,
          tool: options.tool,
          error: error instanceof Error ? error.message : String(error)
        });
        throw error;
      } finally {
        if (options.signal && abortListener) {
          options.signal.removeEventListener("abort", abortListener);
        }
        try {
          await client.end();
        } catch (endError) {
          logger.debug("Error closing database client", {
            profile: profileName,
            error: endError instanceof Error ? endError.message : String(endError)
          });
        }
        release();
      }
    }
Behavior2/5

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

No annotations are provided, so the description carries full burden. It mentions 'execute a SQL query' which implies read/write operations but doesn't disclose behavioral traits like whether it supports transactions, what happens with DDL vs DML queries, error handling, or security implications. For a database tool with no annotations, this leaves significant gaps in understanding its behavior.

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, efficient sentence that directly states the tool's purpose without any wasted words. It's appropriately sized and front-loaded with the core functionality, making it easy for an agent to quickly understand what the tool does.

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

Completeness3/5

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

Given the tool has 5 parameters, no annotations, but does have an output schema (which means return values are documented elsewhere), the description is minimally adequate. It covers the basic purpose but lacks behavioral context and usage guidance that would be helpful for a database operation tool. The existence of an output schema reduces the need to explain return values, but other gaps remain.

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 5 parameters thoroughly. The description doesn't add any parameter-specific information beyond what's in the schema (profile, query, parameters, timeoutMs, rowLimit). Baseline 3 is appropriate when the schema does the heavy lifting, though the description could have explained parameter relationships or constraints.

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 a SQL query') and target resource ('PostgreSQL database using a configured profile'), providing specific verb+resource combination. However, it doesn't differentiate from sibling tools like sshExecute or trainClassifier, which operate on different systems entirely, so it doesn't need sibling differentiation but could mention it's for database operations specifically.

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. It doesn't mention when this tool is appropriate (e.g., for database queries vs. other operations) or when not to use it (e.g., for non-SQL operations). With sibling tools like sshExecute for shell commands and trainClassifier for ML tasks, some basic differentiation would be helpful.

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/jackyxhb/InferMCPServer'

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