create_gsi
Add a global secondary index to a DynamoDB table by specifying the index name, partition key, projection type, and read/write capacities for optimized query performance.
Instructions
Creates a global secondary index on a table
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| indexName | Yes | Name of the new index | |
| nonKeyAttributes | No | Non-key attributes to project (optional) | |
| partitionKey | Yes | Partition key for the index | |
| partitionKeyType | Yes | Type of partition key | |
| projectionType | Yes | Type of projection | |
| readCapacity | Yes | Provisioned read capacity units | |
| sortKey | No | Sort key for the index (optional) | |
| sortKeyType | No | Type of sort key (optional) | |
| tableName | Yes | Name of the table | |
| writeCapacity | Yes | Provisioned write capacity units |
Implementation Reference
- src/index.ts:290-332 (handler)The handler function that implements the core logic of the 'create_gsi' tool by constructing and sending an UpdateTableCommand to DynamoDB to create a Global Secondary Index.async function createGSI(params: any) { try { const command = new UpdateTableCommand({ TableName: params.tableName, AttributeDefinitions: [ { AttributeName: params.partitionKey, AttributeType: params.partitionKeyType }, ...(params.sortKey ? [{ AttributeName: params.sortKey, AttributeType: params.sortKeyType }] : []), ], GlobalSecondaryIndexUpdates: [ { Create: { IndexName: params.indexName, KeySchema: [ { AttributeName: params.partitionKey, KeyType: "HASH" as const }, ...(params.sortKey ? [{ AttributeName: params.sortKey, KeyType: "RANGE" as const }] : []), ], Projection: { ProjectionType: params.projectionType, ...(params.projectionType === "INCLUDE" ? { NonKeyAttributes: params.nonKeyAttributes } : {}), }, ProvisionedThroughput: { ReadCapacityUnits: params.readCapacity, WriteCapacityUnits: params.writeCapacity, }, }, }, ], }); const response = await dynamoClient.send(command); return { success: true, message: `GSI ${params.indexName} creation initiated on table ${params.tableName}`, details: response.TableDescription, }; } catch (error) { console.error("Error creating GSI:", error); return { success: false, message: `Failed to create GSI: ${error}`, }; } }
- src/index.ts:73-92 (schema)The tool object definition for 'create_gsi', including name, description, and detailed inputSchema for parameter validation.const CREATE_GSI_TOOL: Tool = { name: "create_gsi", description: "Creates a global secondary index on a table", inputSchema: { type: "object", properties: { tableName: { type: "string", description: "Name of the table" }, indexName: { type: "string", description: "Name of the new index" }, partitionKey: { type: "string", description: "Partition key for the index" }, partitionKeyType: { type: "string", enum: ["S", "N", "B"], description: "Type of partition key" }, sortKey: { type: "string", description: "Sort key for the index (optional)" }, sortKeyType: { type: "string", enum: ["S", "N", "B"], description: "Type of sort key (optional)" }, projectionType: { type: "string", enum: ["ALL", "KEYS_ONLY", "INCLUDE"], description: "Type of projection" }, nonKeyAttributes: { type: "array", items: { type: "string" }, description: "Non-key attributes to project (optional)" }, readCapacity: { type: "number", description: "Provisioned read capacity units" }, writeCapacity: { type: "number", description: "Provisioned write capacity units" }, }, required: ["tableName", "indexName", "partitionKey", "partitionKeyType", "projectionType", "readCapacity", "writeCapacity"], }, };
- src/index.ts:598-600 (registration)Registers the CREATE_GSI_TOOL in the list of available tools served by the MCP server in response to ListToolsRequest.server.setRequestHandler(ListToolsRequestSchema, async () => ({ tools: [CREATE_TABLE_TOOL, UPDATE_CAPACITY_TOOL, PUT_ITEM_TOOL, GET_ITEM_TOOL, QUERY_TABLE_TOOL, SCAN_TABLE_TOOL, DESCRIBE_TABLE_TOOL, LIST_TABLES_TOOL, CREATE_GSI_TOOL, UPDATE_GSI_TOOL, CREATE_LSI_TOOL, UPDATE_ITEM_TOOL], }));
- src/index.ts:614-616 (registration)Dispatch/registration case in the CallToolRequestHandler switch statement that invokes the createGSI handler for the 'create_gsi' tool.case "create_gsi": result = await createGSI(args); break;