batch_create_elements
Create multiple diagram elements simultaneously in Excalidraw to save time when building complex drawings.
Instructions
Create multiple elements at once (max 100)
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| elements | Yes |
Implementation Reference
- src/mcp/index.ts:215-237 (handler)Main MCP tool registration and handler for batch_create_elements. Validates input using schema, calls client.batchCreate(elements), and returns the created elements with count. This is the primary entry point that handles the tool's request-response logic.// --- Tool: batch_create_elements --- server.tool( 'batch_create_elements', `Create multiple elements at once (max ${LIMITS.MAX_BATCH_SIZE})`, { elements: z.array(z.object(elementFields)).min(1).max(LIMITS.MAX_BATCH_SIZE), }, async ({ elements }) => { try { const created = await client.batchCreate( elements as unknown as Record<string, unknown>[] ); return { content: [{ type: 'text', text: JSON.stringify({ elements: created, count: created.length }, null, 2), }], }; } catch (err) { return { content: [{ type: 'text', text: `Error: ${(err as Error).message}` }], isError: true }; } } );
- src/mcp/schemas/element.ts:117-124 (schema)BatchCreateSchema definition that validates the input for batch_create_elements. Requires an 'elements' array with minimum 1 and maximum LIMITS.MAX_BATCH_SIZE elements. Each element must match CreateElementSchema.export const BatchCreateSchema = z .object({ elements: z .array(CreateElementSchema) .min(1) .max(LIMITS.MAX_BATCH_SIZE), }) .strict();
- src/mcp/canvas-client.ts:100-116 (handler)CanvasClient.batchCreate method implementation for connected mode. Makes a POST request to /api/elements/batch endpoint with the elements array, handles errors, and returns the created ServerElement array.async batchCreate( elements: Record<string, unknown>[] ): Promise<ServerElement[]> { const res = await fetch(`${this.baseUrl}/api/elements/batch`, { method: 'POST', headers: this.headers(), body: JSON.stringify({ elements }), }); if (!res.ok) { const body = await res.json().catch(() => ({})) as ApiResponse; throw new Error(body.error ?? `Canvas error: ${res.status}`); } const body = await res.json() as { elements?: ServerElement[] }; return body.elements ?? []; }
- CanvasClientAdapter.batchCreate method implementation for standalone mode. Iterates through elements array and calls createElement for each one, collecting and returning all created ServerElements.async batchCreate( elements: Record<string, unknown>[] ): Promise<ServerElement[]> { const results: ServerElement[] = []; for (const data of elements) { results.push(await this.createElement(data)); } return results; }
- src/mcp/schemas/limits.ts:1-22 (helper)LIMITS constant defining validation constraints including MAX_BATCH_SIZE: 100, which is used to enforce the maximum number of elements that can be created in a single batch_create_elements call.export const LIMITS = { MAX_ID_LENGTH: 64, MAX_COLOR_LENGTH: 32, MAX_TEXT_LENGTH: 10_000, MAX_FONT_FAMILY_LENGTH: 64, MAX_GROUP_ID_LENGTH: 64, MAX_MERMAID_LENGTH: 50_000, MAX_COORDINATE: 1_000_000, MIN_COORDINATE: -1_000_000, MAX_DIMENSION: 100_000, MAX_FONT_SIZE: 1000, MAX_STROKE_WIDTH: 100, MIN_OPACITY: 0, MAX_OPACITY: 100, MAX_ROUGHNESS: 3, MAX_BATCH_SIZE: 100, MAX_ELEMENT_IDS: 500, MAX_POINTS: 10_000, MAX_GROUP_IDS: 50, MAX_ELEMENTS_TOTAL: 10_000, } as const;