Skip to main content
Glama
TranChiHuu

MCP SQL Server

by TranChiHuu

execute_query

Execute SQL queries on PostgreSQL or MySQL databases to retrieve and manage data, supporting parameterized queries for secure database interactions.

Instructions

Execute a SQL query on the connected database. Returns query results.

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
queryYesSQL query to execute
paramsNoQuery parameters (for parameterized queries)

Implementation Reference

  • Implementation of the execute_query tool handler. Executes the provided SQL query on the currently connected PostgreSQL or MySQL database, handles parameterized queries, and returns formatted JSON results including rows, row count, and field information.
    async executeQuery(query, params) {
      if (!this.currentConfig) {
        throw new Error('Not connected to any database. Call connect_database first.');
      }
    
      if (this.currentConfig.type === 'postgresql') {
        if (!this.postgresPool) {
          throw new Error('PostgreSQL connection not initialized');
        }
    
        const result = await this.postgresPool.query(query, params);
        return {
          content: [
            {
              type: 'text',
              text: JSON.stringify(
                {
                  rows: result.rows,
                  rowCount: result.rowCount,
                  fields: result.fields.map((f) => ({
                    name: f.name,
                    dataTypeID: f.dataTypeID,
                  })),
                },
                null,
                2
              ),
            },
          ],
        };
      } else if (this.currentConfig.type === 'mysql') {
        if (!this.mysqlConnection) {
          throw new Error('MySQL connection not initialized');
        }
    
        const [rows, fields] = await this.mysqlConnection.query(query, params || []);
        return {
          content: [
            {
              type: 'text',
              text: JSON.stringify(
                {
                  rows: rows,
                  rowCount: Array.isArray(rows) ? rows.length : 0,
                  fields: fields?.map((f) => ({
                    name: f.name,
                    type: f.type,
                  })) || [],
                },
                null,
                2
              ),
            },
          ],
        };
      } else {
        throw new Error(`Unsupported database type: ${this.currentConfig.type}`);
      }
    }
  • Input schema definition for the execute_query tool, specifying the required 'query' string and optional 'params' array of basic types.
    inputSchema: {
      type: 'object',
      properties: {
        query: {
          type: 'string',
          description: 'SQL query to execute',
        },
        params: {
          type: 'array',
          description: 'Query parameters (for parameterized queries)',
          items: {
            type: ['string', 'number', 'boolean', 'null'],
          },
        },
      },
      required: ['query'],
    },
  • index.js:153-174 (registration)
    Registration of the execute_query tool in the list of available tools returned by ListToolsRequestHandler.
    {
      name: 'execute_query',
      description:
        'Execute a SQL query on the connected database. Returns query results.',
      inputSchema: {
        type: 'object',
        properties: {
          query: {
            type: 'string',
            description: 'SQL query to execute',
          },
          params: {
            type: 'array',
            description: 'Query parameters (for parameterized queries)',
            items: {
              type: ['string', 'number', 'boolean', 'null'],
            },
          },
        },
        required: ['query'],
      },
    },
Behavior2/5

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

With no annotations provided, the description carries the full burden of behavioral disclosure. While it states the action and return purpose ('Returns query results'), it lacks critical behavioral details such as whether this tool can execute both read and write queries, what permissions are required, potential rate limits, error handling, or transaction implications. For a database query tool with zero annotation coverage, this leaves significant gaps.

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 consists of two concise, front-loaded sentences that efficiently convey the core functionality and outcome. Every word earns its place with zero redundancy or unnecessary elaboration, making it easy for an agent to parse quickly.

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's complexity (executing arbitrary SQL queries) and the absence of both annotations and an output schema, the description is moderately complete. It covers the basic purpose and return intent but lacks details on result format, error conditions, security implications, and behavioral constraints that would be crucial for safe and effective 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 fully documents both parameters (query and params). The description adds no additional parameter semantics beyond what the schema provides, such as SQL dialect specifics, parameter binding syntax, or query validation rules. The baseline score of 3 reflects adequate but minimal value addition.

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 ('Execute a SQL query') and target resource ('on the connected database'), distinguishing it from sibling tools like connect_database, describe_table, list_tables, and disconnect_database. It provides a complete verb+resource+scope combination.

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 implies usage context through 'on the connected database', suggesting this tool should be used after establishing a connection (likely via connect_database). However, it doesn't explicitly state when to use this vs. alternatives like describe_table or list_tables, nor does it provide exclusions or prerequisites beyond the implied connection requirement.

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/TranChiHuu/postgres-mysql-mcp-server'

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