Skip to main content
Glama

insert_data

Execute SQL INSERT queries to add new records into MySQL database tables, enabling data population and storage operations.

Instructions

Inserts data into a table in the MySQL database.

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
queryYesThe SQL INSERT INTO query to execute.

Implementation Reference

  • The primary handler function for the 'insert_data' tool. It validates the input arguments, checks that the SQL query starts with 'INSERT INTO', executes the query using the MySQL connection pool, logs the transaction, and returns a success response with the query result or an error message.
    private async handleInsertData(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 (!isInsertQuery(query)) {
        throw new McpError(
          ErrorCode.InvalidParams,
          'Only INSERT INTO queries are allowed with insert_data tool.'
        );
      }
    
      console.error(`[${transactionId}] Executing INSERT query: ${query}`);
      
      try {
        const [result] = await this.pool.query(query);
        console.error(`[${transactionId}] Data inserted successfully`);
        
        return {
          content: [
            {
              type: 'text',
              text: JSON.stringify({
                success: true,
                message: 'Data inserted successfully',
                result
              }, 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 'insert_data' tool, specifying an object with a required 'query' property of type string for the INSERT SQL statement.
    inputSchema: {
      type: 'object',
      properties: {
        query: {
          type: 'string',
          description: 'The SQL INSERT INTO query to execute.',
        },
      },
      required: ['query'],
    },
  • src/index.ts:122-135 (registration)
    Registration of the 'insert_data' tool in the tools list returned by ListToolsRequestSchema handler, including name, description, and input schema.
    {
      name: 'insert_data',
      description: 'Inserts data into a table in the MySQL database.',
      inputSchema: {
        type: 'object',
        properties: {
          query: {
            type: 'string',
            description: 'The SQL INSERT INTO query to execute.',
          },
        },
        required: ['query'],
      },
    },
  • src/index.ts:191-192 (registration)
    Dispatch registration in the CallToolRequestSchema switch statement that routes calls to 'insert_data' to the handleInsertData method.
    case 'insert_data':
      return this.handleInsertData(request, transactionId);
  • Helper function used by the handler to validate that the provided query is an INSERT INTO statement.
    // Check if query is for inserting data
    const isInsertQuery = (query: string): boolean => 
      query.trim().toLowerCase().startsWith('insert into');

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