alter_table
Modify existing MySQL table structures to add, drop, modify, or rename columns, enabling database schema adjustments without recreating tables.
Instructions
Modify an existing table structure
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| table | Yes | Table name | |
| operation | Yes | Type of alteration | |
| column | Yes | Column name | |
| definition | No | Column definition for ADD/MODIFY (e.g., 'VARCHAR(255) NOT NULL') | |
| newName | No | New name for RENAME operation | |
| database | No | Database name (optional) |
Implementation Reference
- src/index.ts:362-405 (handler)Handler function implementing the alter_table tool logic: constructs and executes SQL ALTER TABLE statements for ADD, DROP, MODIFY, or RENAME column operations.async ({ table, operation, column, definition, newName, database }) => { const p = await getPool(); const tableName = database ? `\`${database}\`.\`${table}\`` : `\`${table}\``; let sql: string; switch (operation) { case "ADD": sql = `ALTER TABLE ${tableName} ADD COLUMN \`${column}\` ${definition}`; break; case "DROP": sql = `ALTER TABLE ${tableName} DROP COLUMN \`${column}\``; break; case "MODIFY": sql = `ALTER TABLE ${tableName} MODIFY COLUMN \`${column}\` ${definition}`; break; case "RENAME": sql = `ALTER TABLE ${tableName} RENAME COLUMN \`${column}\` TO \`${newName}\``; break; default: throw new Error(`Unknown operation: ${operation}`); } await p.execute(sql); const output = { success: true, table, operation, column, newName: newName || null, database: database || null, }; return { content: [ { type: "text" as const, text: `Table ${table} altered successfully (${operation} ${column})`, }, ], structuredContent: output, }; }
- src/index.ts:354-361 (schema)Input schema using Zod for validating parameters of the alter_table tool.{ table: z.string().describe("Table name"), operation: z.enum(["ADD", "DROP", "MODIFY", "RENAME"]).describe("Type of alteration"), column: z.string().describe("Column name"), definition: z.string().optional().describe("Column definition for ADD/MODIFY (e.g., 'VARCHAR(255) NOT NULL')"), newName: z.string().optional().describe("New name for RENAME operation"), database: z.string().optional().describe("Database name (optional)"), },
- src/index.ts:351-406 (registration)Registration of the 'alter_table' tool using server.tool(), including name, description, input schema, and inline handler function.server.tool( "alter_table", "Modify an existing table structure", { table: z.string().describe("Table name"), operation: z.enum(["ADD", "DROP", "MODIFY", "RENAME"]).describe("Type of alteration"), column: z.string().describe("Column name"), definition: z.string().optional().describe("Column definition for ADD/MODIFY (e.g., 'VARCHAR(255) NOT NULL')"), newName: z.string().optional().describe("New name for RENAME operation"), database: z.string().optional().describe("Database name (optional)"), }, async ({ table, operation, column, definition, newName, database }) => { const p = await getPool(); const tableName = database ? `\`${database}\`.\`${table}\`` : `\`${table}\``; let sql: string; switch (operation) { case "ADD": sql = `ALTER TABLE ${tableName} ADD COLUMN \`${column}\` ${definition}`; break; case "DROP": sql = `ALTER TABLE ${tableName} DROP COLUMN \`${column}\``; break; case "MODIFY": sql = `ALTER TABLE ${tableName} MODIFY COLUMN \`${column}\` ${definition}`; break; case "RENAME": sql = `ALTER TABLE ${tableName} RENAME COLUMN \`${column}\` TO \`${newName}\``; break; default: throw new Error(`Unknown operation: ${operation}`); } await p.execute(sql); const output = { success: true, table, operation, column, newName: newName || null, database: database || null, }; return { content: [ { type: "text" as const, text: `Table ${table} altered successfully (${operation} ${column})`, }, ], structuredContent: output, }; } );