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');

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