create_table
Create a new database table by defining table name, field specifications, and optional indexes for structured data storage.
Instructions
Create a new table in the database
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| table | Yes | Table name | |
| fields | Yes | ||
| indexes | No |
Implementation Reference
- src/index.ts:801-841 (handler)The main handler function for the 'create_table' tool. It constructs a CREATE TABLE SQL statement using the provided table name, fields (with type, length, nullable, default, autoIncrement, primary), and optional indexes, then executes the query on the MySQL connection.private async handleCreateTable(args: any) { await this.ensureConnection(); 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 ')} )`; try { await this.connection!.query(sql); return { content: [ { type: 'text', text: `Table ${args.table} created successfully` } ] }; } catch (error) { throw new McpError( ErrorCode.InternalError, `Failed to create table: ${getErrorMessage(error)}` ); } }
- src/index.ts:327-371 (schema)Input schema definition for the 'create_table' tool, specifying the structure for table name, fields array (each with name, type, optional length, nullable, default, autoIncrement, primary), and optional indexes array (with name, columns, unique).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', 'number', 'null'], 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:324-372 (registration)Tool registration in the ListToolsRequestSchema handler, defining the 'create_table' tool with its name, description, and inputSchema.{ 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', 'number', 'null'], 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:547-548 (registration)Dispatch case in the CallToolRequestSchema handler that routes 'create_table' tool calls to the handleCreateTable method.case 'create_table': return await this.handleCreateTable(request.params.arguments);
- src/index.ts:50-58 (helper)TypeScript interface defining the structure of a schema field used in the create_table tool handler.interface SchemaField { name: string; type: string; length?: number; nullable?: boolean; default?: string | number | null; autoIncrement?: boolean; primary?: boolean; }
- src/index.ts:60-64 (helper)TypeScript interface defining the structure of an index used in the create_table tool handler.interface IndexDefinition { name: string; columns: string[]; unique?: boolean; }