create_index
Create database indexes on MSSQL table columns to improve query performance and enforce data uniqueness.
Instructions
Creates an index on a specified column or columns in an MSSQL Database table
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| schemaName | No | Name of the schema containing the table | |
| tableName | Yes | Name of the table to create index on | |
| indexName | Yes | Name for the new index | |
| columns | Yes | Array of column names to include in the index | |
| isUnique | No | Whether the index should enforce uniqueness (default: false) | |
| isClustered | No | Whether the index should be clustered (default: false) |
Implementation Reference
- src/tools/CreateIndexTool.ts:33-66 (handler)The async run method implements the core logic of the create_index tool: constructs and executes a CREATE INDEX SQL query on the specified table columns with optional unique and clustered modifiers.async run(params: any) { try { const { schemaName, tableName, indexName, columns, isUnique = false, isClustered = false } = params; let indexType = isClustered ? "CLUSTERED" : "NONCLUSTERED"; if (isUnique) { indexType = `UNIQUE ${indexType}`; } const columnNames = columns.join(", "); const request = new sql.Request(); const query = `CREATE ${indexType} INDEX ${indexName} ON ${schemaName}.${tableName} (${columnNames})`; await request.query(query); return { success: true, message: `Index [${indexName}] created successfully on table [${schemaName}.${tableName}]`, details: { schemaName, tableName, indexName, columnNames, isUnique, isClustered } }; } catch (error) { console.error("Error creating index:", error); return { success: false, message: `Failed to create index: ${error}`, }; } }
- src/tools/CreateIndexTool.ts:8-31 (schema)Input schema defining the parameters for the create_index tool: schemaName, tableName, indexName, columns (required array), isUnique, isClustered.inputSchema = { type: "object", properties: { schemaName: { type: "string", description: "Name of the schema containing the table" }, tableName: { type: "string", description: "Name of the table to create index on" }, indexName: { type: "string", description: "Name for the new index" }, columns: { type: "array", items: { type: "string" }, description: "Array of column names to include in the index" }, isUnique: { type: "boolean", description: "Whether the index should enforce uniqueness (default: false)", default: false }, isClustered: { type: "boolean", description: "Whether the index should be clustered (default: false)", default: false }, }, required: ["tableName", "indexName", "columns"], } as any;
- src/index.ts:110-112 (registration)Registers createIndexTool in the list of available tools for the ListToolsRequestSchema handler (included in non-readonly mode).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:132-133 (registration)Registers the dispatch to createIndexTool.run() in the switch statement of the CallToolRequestSchema handler.case createIndexTool.name: result = await createIndexTool.run(args);
- src/index.ts:87-87 (registration)Instantiates the CreateIndexTool instance used throughout the server.const createIndexTool = new CreateIndexTool();