insert_data
Execute SQL INSERT INTO queries to add data into a MySQL database table, simplifying data entry and management within the MySQL MCP Server.
Instructions
Inserts data into a table in the MySQL database.
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| query | Yes | The SQL INSERT INTO query to execute. |
Input Schema (JSON Schema)
{
"properties": {
"query": {
"description": "The SQL INSERT INTO query to execute.",
"type": "string"
}
},
"required": [
"query"
],
"type": "object"
}
Implementation Reference
- src/index.ts:122-135 (registration)Registration of the 'insert_data' tool in the ListTools response, 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:311-361 (handler)The handler function for 'insert_data' tool: validates input, checks for INSERT query, executes on MySQL pool, returns result or error.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; } }
- src/index.ts:125-134 (schema)Input schema for the 'insert_data' tool, defining the expected 'query' parameter.inputSchema: { type: 'object', properties: { query: { type: 'string', description: 'The SQL INSERT INTO query to execute.', }, }, required: ['query'], },
- src/index.ts:38-39 (helper)Helper function to validate if the query is an INSERT INTO statement.const isInsertQuery = (query: string): boolean => query.trim().toLowerCase().startsWith('insert into');
- src/index.ts:24-27 (helper)Helper function to validate SQL query arguments.const isValidSqlQueryArgs = (args: any): args is SqlQueryArgs => typeof args === 'object' && args !== null && typeof args.query === 'string';