Skip to main content
Glama
nilsir

MCP Server MySQL

by nilsir

alter_table

Modify MySQL table structure by adding, dropping, modifying, or renaming columns to adapt database schema to changing requirements.

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

  • The handler function for the 'alter_table' tool. It constructs and executes an ALTER TABLE SQL statement based on the operation (ADD, DROP, MODIFY, RENAME) using the provided parameters, then returns a success response with structured output.
    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, }; }
  • Zod schema defining the input parameters for the 'alter_table' tool: table, operation (enum), column, optional definition, newName, and database.
    { 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