Skip to main content
Glama

run_sql_query

Execute read-only SQL SELECT queries to retrieve data from a MySQL database. This tool enables data analysis and information extraction through direct database queries.

Instructions

Executes a read-only SQL query (SELECT statements only) against the MySQL database.

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
queryYesThe SQL SELECT query to execute.

Implementation Reference

  • Core handler for 'run_sql_query' tool: validates arguments and query type (SELECT only), executes query on MySQL connection pool, returns JSON-stringified rows or error response.
    // Handle read-only queries (SELECT)
    private async handleReadQuery(request: any, transactionId: string) {
      if (!isValidSqlQueryArgs(request.params.arguments)) {
        throw new McpError(
          ErrorCode.InvalidParams,
          'Invalid SQL query arguments.'
        );
      }
    
      const query = request.params.arguments.query;
      
      if (!isReadOnlyQuery(query)) {
        throw new McpError(
          ErrorCode.InvalidParams,
          'Only SELECT queries are allowed with run_sql_query tool.'
        );
      }
    
      console.error(`[${transactionId}] Executing SELECT query: ${query}`);
      
      try {
        const [rows] = await this.pool.query(query);
        console.error(`[${transactionId}] Query executed successfully`);
        
        return {
          content: [
            {
              type: 'text',
              text: JSON.stringify(rows, null, 2),
            },
          ],
        };
      } catch (error) {
        console.error(`[${transactionId}] Query error:`, error);
        if (error instanceof Error) {
          return {
            content: [
              {
                type: 'text',
                text: `MySQL error: ${error.message}`,
              },
            ],
            isError: true,
          };
        }
        throw error;
      }
    }
  • Input schema definition for the 'run_sql_query' tool, specifying a required 'query' string parameter.
    inputSchema: {
      type: 'object',
      properties: {
        query: {
          type: 'string',
          description: 'The SQL SELECT query to execute.',
        },
      },
      required: ['query'],
    },
  • src/index.ts:94-107 (registration)
    Tool registration in ListToolsRequestSchema response: defines name, description, and input schema for 'run_sql_query'.
    {
      name: 'run_sql_query',
      description: 'Executes a read-only SQL query (SELECT statements only) against the MySQL database.',
      inputSchema: {
        type: 'object',
        properties: {
          query: {
            type: 'string',
            description: 'The SQL SELECT query to execute.',
          },
        },
        required: ['query'],
      },
    },
  • src/index.ts:187-188 (registration)
    Dispatch logic in CallToolRequestSchema handler: routes 'run_sql_query' calls to the handleReadQuery method.
    case 'run_sql_query':
      return this.handleReadQuery(request, transactionId);
  • Helper function used by handler to validate that the query is a read-only SELECT statement.
    // Check if query is read-only (SELECT)
    const isReadOnlyQuery = (query: string): boolean => 
      query.trim().toLowerCase().startsWith('select');
Behavior4/5

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

Since no annotations are provided, the description carries the full burden of behavioral disclosure. It effectively describes key behavioral traits: it's read-only (implying no data mutation), limited to SELECT statements (constraining query types), and executes against a MySQL database (specifying the target). However, it lacks details on permissions, rate limits, error handling, or result format, which would be useful for a database 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 a single, well-structured sentence that front-loads the key information: action ('Executes'), constraint ('read-only SQL query (SELECT statements only)'), and target ('against the MySQL database'). There is no wasted verbiage, and every word contributes to clarity and utility.

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?

Given the tool's moderate complexity (executing SQL queries), lack of annotations, and no output schema, the description is reasonably complete. It covers the core purpose, usage constraints, and target database. However, it doesn't address potential behavioral aspects like result format, pagination, timeouts, or authentication needs, which could be important for an AI agent to use it effectively.

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?

The schema description coverage is 100%, with the single parameter 'query' documented as 'The SQL SELECT query to execute.' The description adds minimal value beyond this by reinforcing the SELECT-only constraint, but doesn't provide additional syntax, format, or validation details. With high schema coverage, the baseline score of 3 is appropriate.

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 ('Executes a read-only SQL query') and resource ('against the MySQL database'), with explicit limitation to 'SELECT statements only'. This distinguishes it from siblings like create_table, delete_data, insert_data, and update_data which are write operations, and from execute_sql which might allow broader SQL statements.

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

Usage Guidelines5/5

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

The description explicitly states 'read-only SQL query (SELECT statements only)', providing clear guidance on when to use this tool (for SELECT queries) versus when not to use it (for write operations like INSERT, UPDATE, DELETE, or DDL). It implicitly suggests alternatives like insert_data for INSERT queries or update_data for UPDATE queries among the sibling tools.

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

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