Skip to main content
Glama

execute_sql

Execute SQL statements to modify MySQL database structure and data, including ALTER TABLE, DROP, INSERT, UPDATE, and DELETE operations.

Instructions

Executes any non-SELECT SQL statement (e.g., ALTER TABLE, DROP, etc.)

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
queryYesThe SQL statement to execute.

Implementation Reference

  • The primary handler function for the 'execute_sql' tool. It validates the input arguments, ensures the query is not a SELECT statement, executes the SQL query using the MySQL connection pool, logs the activity, and returns a structured response with the result or an error message.
    private async handleExecuteSql(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,
          'SELECT queries are not allowed with execute_sql tool.'
        );
      }
    
      console.error(`[${transactionId}] Executing general SQL: ${query}`);
    
      try {
        const [result] = await this.pool.query(query);
        console.error(`[${transactionId}] SQL executed successfully`);
    
        return {
          content: [
            {
              type: 'text',
              text: JSON.stringify({
                success: true,
                message: 'SQL executed successfully',
                result
              }, null, 2),
            },
          ],
        };
      } catch (error) {
        console.error(`[${transactionId}] SQL error:`, error);
        if (error instanceof Error) {
          return {
            content: [
              {
                type: 'text',
                text: `MySQL error: ${error.message}`,
              },
            ],
            isError: true,
          };
        }
        throw error;
      }
    }
  • src/index.ts:164-177 (registration)
    Registers the 'execute_sql' tool with the MCP server via setTools, defining its name, description, and input schema.
    {
      name: 'execute_sql',
      description: 'Executes any non-SELECT SQL statement (e.g., ALTER TABLE, DROP, etc.)',
      inputSchema: {
        type: 'object',
        properties: {
          query: {
            type: 'string',
            description: 'The SQL statement to execute.',
          },
        },
        required: ['query'],
      },
    },
  • Defines the input schema for the 'execute_sql' tool, specifying an object with a required 'query' string property.
    inputSchema: {
      type: 'object',
      properties: {
        query: {
          type: 'string',
          description: 'The SQL statement to execute.',
        },
      },
      required: ['query'],
    },
  • src/index.ts:197-198 (registration)
    In the request handler switch statement, routes calls to the 'execute_sql' tool to its handler method.
    case 'execute_sql':
      return this.handleExecuteSql(request, transactionId);

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