create_table
Define a new database table structure by specifying column names, data types, and optional constraints like primary keys or nullability.
Instructions
Create a new table with specified columns
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| table | Yes | Table name | |
| columns | Yes | Column definitions | |
| database | No | Database name (optional) |
Implementation Reference
- src/index.ts:319-347 (handler)The anonymous async handler function for the create_table tool. It builds the column definitions string, constructs the CREATE TABLE SQL query, executes it on the MySQL pool, and returns a success response with structured output.async ({ table, columns, database }) => { const p = await getPool(); const columnDefs = columns.map((col) => { let def = `\`${col.name}\` ${col.type}`; if (col.nullable === false) def += " NOT NULL"; if (col.autoIncrement) def += " AUTO_INCREMENT"; if (col.default !== undefined) def += ` DEFAULT ${col.default}`; if (col.primaryKey) def += " PRIMARY KEY"; return def; }); const tableName = database ? `\`${database}\`.\`${table}\`` : `\`${table}\``; const sql = `CREATE TABLE ${tableName} (${columnDefs.join(", ")})`; await p.execute(sql); const output = { success: true, table, database: database || null }; return { content: [ { type: "text" as const, text: `Table ${table} created successfully`, }, ], structuredContent: output, }; }
- src/index.ts:301-308 (schema)Zod schema defining the structure of a single column for use in the create_table tool's input parameters.const columnSchema = z.object({ name: z.string().describe("Column name"), type: z.string().describe("Column type (e.g., VARCHAR(255), INT, TEXT)"), nullable: z.boolean().optional().describe("Whether column can be null"), primaryKey: z.boolean().optional().describe("Whether this is the primary key"), autoIncrement: z.boolean().optional().describe("Whether to auto increment"), default: z.string().optional().describe("Default value"), });
- src/index.ts:311-348 (registration)Registration of the 'create_table' tool on the MCP server, including the tool name, description, Zod input schema (referencing columnSchema), and inline handler function.server.tool( "create_table", "Create a new table with specified columns", { table: z.string().describe("Table name"), columns: z.array(columnSchema).describe("Column definitions"), database: z.string().optional().describe("Database name (optional)"), }, async ({ table, columns, database }) => { const p = await getPool(); const columnDefs = columns.map((col) => { let def = `\`${col.name}\` ${col.type}`; if (col.nullable === false) def += " NOT NULL"; if (col.autoIncrement) def += " AUTO_INCREMENT"; if (col.default !== undefined) def += ` DEFAULT ${col.default}`; if (col.primaryKey) def += " PRIMARY KEY"; return def; }); const tableName = database ? `\`${database}\`.\`${table}\`` : `\`${table}\``; const sql = `CREATE TABLE ${tableName} (${columnDefs.join(", ")})`; await p.execute(sql); const output = { success: true, table, database: database || null }; return { content: [ { type: "text" as const, text: `Table ${table} created successfully`, }, ], structuredContent: output, }; } );