Skip to main content
Glama
nilsir

MCP Server MySQL

by nilsir

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
NameRequiredDescriptionDefault
tableYesTable name
operationYesType of alteration
columnYesColumn name
definitionNoColumn definition for ADD/MODIFY (e.g., 'VARCHAR(255) NOT NULL')
newNameNoNew name for RENAME operation
databaseNoDatabase name (optional)

Implementation Reference

  • 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, }; }
  • 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, }; } );

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/nilsir/mcp-server-mysql'

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