create_table
Create new tables in MSSQL databases by defining table names and column specifications with SQL types and constraints.
Instructions
Creates a new table in the MSSQL Database with the specified columns.
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| columns | Yes | Array of column definitions (e.g., [{ name: 'id', type: 'INT PRIMARY KEY' }, ...]) | |
| tableName | Yes | Name of the table to create |
Implementation Reference
- src/tools/CreateTableTool.ts:28-48 (handler)The `run` method executes the tool: validates input, builds CREATE TABLE query from tableName and columns, executes it via mssql, returns success/error.async run(params: any) { try { const { tableName, columns } = params; if (!Array.isArray(columns) || columns.length === 0) { throw new Error("'columns' must be a non-empty array"); } const columnDefs = columns.map((col: any) => `[${col.name}] ${col.type}`).join(", "); const query = `CREATE TABLE [${tableName}] (${columnDefs})`; await new sql.Request().query(query); return { success: true, message: `Table '${tableName}' created successfully.` }; } catch (error) { console.error("Error creating table:", error); return { success: false, message: `Failed to create table: ${error}` }; } }
- src/tools/CreateTableTool.ts:8-26 (schema)Input schema defining required parameters: `tableName` (string) and `columns` (array of {name, type} objects).inputSchema = { type: "object", properties: { tableName: { type: "string", description: "Name of the table to create" }, columns: { type: "array", description: "Array of column definitions (e.g., [{ name: 'id', type: 'INT PRIMARY KEY' }, ...])", items: { type: "object", properties: { name: { type: "string", description: "Column name" }, type: { type: "string", description: "SQL type and constraints (e.g., 'INT PRIMARY KEY', 'NVARCHAR(255) NOT NULL')" } }, required: ["name", "type"] } } }, required: ["tableName", "columns"], } as any;
- src/index.ts:115-119 (registration)In ListToolsRequestHandler, `createTableTool` is added to the list of available tools (in non-readonly mode).server.setRequestHandler(ListToolsRequestSchema, async () => ({ tools: isReadOnly ? [listTableTool, readDataTool, describeTableTool] // todo: add searchDataTool to the list of tools available in readonly mode once implemented : [insertDataTool, readDataTool, describeTableTool, updateDataTool, createTableTool, createIndexTool, dropTableTool, listTableTool], // add all new tools here }));
- src/index.ts:135-137 (registration)In CallToolRequestHandler switch statement, dispatches to `createTableTool.run(args)` when name matches.case createTableTool.name: result = await createTableTool.run(args); break;
- src/index.ts:92-92 (registration)Instantiation of the CreateTableTool instance used throughout the server.const createTableTool = new CreateTableTool();