write_query
Execute SQL modification queries (INSERT, UPDATE, DELETE, REPLACE) and retrieve the number of affected rows.
Instructions
Execute a data modification query (INSERT, UPDATE, DELETE, REPLACE). Returns affected row count.
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| query | Yes | The SQL modification query to execute |
Implementation Reference
- src/index.ts:210-220 (registration)Registration of the 'write_query' tool definition with name, description, and inputSchema.
{ name: 'write_query', description: 'Execute a data modification query (INSERT, UPDATE, DELETE, REPLACE). Returns affected row count.', inputSchema: { type: 'object' as const, properties: { query: { type: 'string', description: 'The SQL modification query to execute' }, }, required: ['query'], }, }, - src/index.ts:287-294 (handler)Handler for 'write_query' tool call: extracts query, validates it, runs it via db.run(), and returns affected row count.
case 'write_query': { const { query } = toolArgs as { query: string }; validateWriteQuery(query); const result = db.run(query); return { content: [{ type: 'text', text: JSON.stringify({ affected_rows: result.changes }, null, 2) }], }; } - src/index.ts:46-55 (helper)Validation helper function that checks the query starts with INSERT, UPDATE, DELETE, or REPLACE, and disallows multiple statements.
function validateWriteQuery(query: string): void { const normalized = query.trim().toLowerCase(); const allowed = ['insert', 'update', 'delete', 'replace']; if (!allowed.some(prefix => normalized.startsWith(prefix))) { throw new McpError(ErrorCode.InvalidParams, 'Only INSERT, UPDATE, DELETE, and REPLACE queries are allowed for write_query'); } if (queryHasMultipleStatements(query)) { throw new McpError(ErrorCode.InvalidParams, 'Multiple statements are not allowed'); } }