Skip to main content
Glama
alittleyellowkevin

MySQL MCP Server

insert_data

Insert data into MySQL database tables using SQL INSERT INTO statements to add records and populate tables with new information.

Instructions

向 MySQL 数据库表插入数据

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
queryYes要执行的 SQL INSERT INTO 语句

Implementation Reference

  • The main handler function for the insert_data tool. It validates the input arguments and ensures the query is an INSERT INTO statement, executes the query using the MySQL connection pool, logs the transaction, and returns a JSON-formatted response with the result or an error message.
    private async handleInsertData(request: any, transactionId: string) {
      if (!isValidSqlQueryArgs(request.params.arguments)) {
        throw new McpError(
          ErrorCode.InvalidParams,
          'SQL 查询参数无效。'
        );
      }
    
      const query = request.params.arguments.query;
    
      if (!isInsertQuery(query)) {
        throw new McpError(
          ErrorCode.InvalidParams,
          'insert_data 工具仅允许 INSERT INTO 查询。'
        );
      }
    
      console.error(`[${transactionId}] 执行 INSERT 查询: ${query}`);
    
      try {
        const [result] = await this.pool.query(query);
        console.error(`[${transactionId}] 数据插入成功`);
    
        return {
          content: [
            {
              type: 'text',
              text: JSON.stringify({
                success: true,
                message: '数据插入成功',
                result
              }, null, 2),
            },
          ],
        };
      } catch (error) {
        console.error(`[${transactionId}] 查询出错:`, error);
        if (error instanceof Error) {
          return {
            content: [
              {
                type: 'text',
                text: `MySQL 错误: ${error.message}`,
              },
            ],
            isError: true,
          };
        }
        throw error;
      }
    }
  • Input schema for the insert_data tool, defining a required 'query' property of type string for the SQL INSERT INTO statement.
    inputSchema: {
      type: 'object',
      properties: {
        query: {
          type: 'string',
          description: '要执行的 SQL INSERT INTO 语句',
        },
      },
      required: ['query'],
    },
  • src/index.ts:130-143 (registration)
    Registration of the insert_data tool in the ListTools response, including name, description, and input schema.
    {
      name: 'insert_data',
      description: '向 MySQL 数据库表插入数据',
      inputSchema: {
        type: 'object',
        properties: {
          query: {
            type: 'string',
            description: '要执行的 SQL INSERT INTO 语句',
          },
        },
        required: ['query'],
      },
    },
  • src/index.ts:200-201 (registration)
    Dispatch registration in the CallToolRequest handler switch statement, routing 'insert_data' calls to the handleInsertData method.
    case 'insert_data':
      return this.handleInsertData(request, transactionId);
  • Helper function that checks if a given SQL query starts with 'insert into' (case-insensitive, trimmed), used for validation in the handler.
    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/alittleyellowkevin/Mysql-MCP'

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