create_table
Define new database tables with specific columns, data types, and constraints for structured data storage in MySQL.
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 handler function that executes the create_table tool logic: constructs a CREATE TABLE SQL statement from column definitions and executes it.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 for column definitions used in the input schema of create_table tool.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 using server.tool, including name, description, input schema, and reference to handler.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, }; } );