create_table
Build and define new database tables on MCP-MySQL Server by specifying table names, fields, indexes, and column properties for structured data storage.
Instructions
Create a new table in the database
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| fields | Yes | ||
| indexes | No | ||
| table | Yes | Table name |
Implementation Reference
- src/index.ts:686-717 (handler)The handler function that implements the core logic for the 'create_table' tool. It builds a CREATE TABLE SQL statement based on the input fields and optional indexes, executes it using executeQuery, and returns a success message.private async handleCreateTable(args: any) { const fields = args.fields.map((field: SchemaField) => { let def = `\`${field.name}\` ${field.type.toUpperCase()}`; if (field.length) def += `(${field.length})`; if (field.nullable === false) def += ' NOT NULL'; if (field.default !== undefined) { def += ` DEFAULT ${field.default === null ? 'NULL' : `'${field.default}'`}`; } if (field.autoIncrement) def += ' AUTO_INCREMENT'; if (field.primary) def += ' PRIMARY KEY'; return def; }); const indexes = args.indexes?.map((idx: IndexDefinition) => { const type = idx.unique ? 'UNIQUE INDEX' : 'INDEX'; return `${type} \`${idx.name}\` (\`${idx.columns.join('`, `')}\`)`; }) || []; const sql = `CREATE TABLE \`${args.table}\` ( ${[...fields, ...indexes].join(',\n ')} )`; await this.executeQuery(sql); return { content: [ { type: 'text', text: `Table ${args.table} created successfully` } ] }; }
- src/index.ts:490-539 (registration)Registration of the 'create_table' tool in the ListToolsRequestSchema handler, including its name, description, and detailed input schema definition.{ name: 'create_table', description: 'Create a new table in the database', inputSchema: { type: 'object', properties: { table: { type: 'string', description: 'Table name', }, fields: { type: 'array', items: { type: 'object', properties: { name: { type: 'string' }, type: { type: 'string' }, length: { type: 'number', optional: true }, nullable: { type: 'boolean', optional: true }, default: { type: 'string', description: 'Default value for the column (as string).', optional: true }, autoIncrement: { type: 'boolean', optional: true }, primary: { type: 'boolean', optional: true } }, required: ['name', 'type'] } }, indexes: { type: 'array', items: { type: 'object', properties: { name: { type: 'string' }, columns: { type: 'array', items: { type: 'string' } }, unique: { type: 'boolean', optional: true } }, required: ['name', 'columns'] }, optional: true } }, required: ['table', 'fields'] } },
- src/index.ts:581-582 (registration)Registration of the 'create_table' handler in the CallToolRequestSchema switch statement, dispatching to handleCreateTable.case 'create_table': return await this.handleCreateTable(request.params.arguments);
- src/index.ts:42-50 (schema)TypeScript interface defining the structure of table fields used in create_table input and handler.interface SchemaField { name: string; type: string; length?: number; nullable?: boolean; default?: string | number | null; autoIncrement?: boolean; primary?: boolean; }
- src/index.ts:52-56 (schema)TypeScript interface defining indexes for table creation in create_table tool.interface IndexDefinition { name: string; columns: string[]; unique?: boolean; }