execute_graph_batch
Execute multiple Microsoft Graph API requests in a single batch operation to improve performance and efficiency.
Instructions
Execute multiple Microsoft Graph API requests in a single batch operation for improved performance and efficiency.
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| requests | Yes |
Implementation Reference
- Core handler function that executes multiple Microsoft Graph API requests in a single batch call using the /$batch endpoint. Validates input (1-20 requests), constructs batch payload, sends POST to /$batch, processes responses, and returns structured results with success/error counts.async executeBatch(requests: BatchRequest[]): Promise<BatchResponse> { if (requests.length === 0) { throw new McpError(ErrorCode.InvalidParams, 'At least one request is required for batch operation'); } if (requests.length > 20) { throw new McpError(ErrorCode.InvalidParams, 'Maximum 20 requests allowed per batch'); } const batchPayload = { requests: requests.map((req, index) => ({ id: req.id || index.toString(), method: req.method.toUpperCase(), url: req.url, headers: req.headers || {}, body: req.body })) }; try { const response = await this.graphClient .api('/$batch') .post(batchPayload); return { responses: response.responses, executedAt: new Date().toISOString(), totalRequests: requests.length, successCount: response.responses.filter((r: any) => r.status >= 200 && r.status < 300).length, errorCount: response.responses.filter((r: any) => r.status >= 400).length }; } catch (error) { throw new McpError( ErrorCode.InternalError, `Batch operation failed: ${error instanceof Error ? error.message : 'Unknown error'}` ); } }
- src/server.ts:1388-1409 (registration)MCP server tool registration for 'execute_graph_batch'. Instantiates GraphAdvancedFeatures class, validates credentials, calls executeBatch with args.requests, formats response as MCP content, handles errors.this.server.tool( "execute_graph_batch", "Execute multiple Microsoft Graph API requests in a single batch operation for improved performance and efficiency.", batchRequestSchema.shape, {"readOnlyHint":false,"destructiveHint":false,"idempotentHint":false}, wrapToolHandler(async (args: any) => { this.validateCredentials(); try { const advancedFeatures = new GraphAdvancedFeatures(this.getGraphClient(), this.getAccessToken.bind(this)); const result = await advancedFeatures.executeBatch(args.requests); return { content: [{ type: 'text', text: JSON.stringify(result, null, 2) }] }; } catch (error) { if (error instanceof McpError) { throw error; } throw new McpError( ErrorCode.InternalError, `Error executing batch operation: ${error instanceof Error ? error.message : 'Unknown error'}` ); } }) );
- src/tool-metadata.ts:208-212 (schema)Tool metadata including description, title, and annotations (readOnlyHint, destructiveHint, idempotentHint, openWorldHint) used by MCP server for tool discovery and UI hints.execute_graph_batch: { description: "Execute multiple Microsoft Graph API requests in a single batch operation for improved performance and efficiency.", title: "Graph Batch Executor", annotations: { title: "Graph Batch Executor", readOnlyHint: false, destructiveHint: false, idempotentHint: false, openWorldHint: true } },
- Zod input schema for batch requests. Validates array of 1-20 requests, each with optional id, required method (enum), url, optional headers/body. Passed to server.tool() as input schema.export const batchRequestSchema = z.object({ requests: z.array(z.object({ id: z.string().optional(), method: z.enum(['GET', 'POST', 'PATCH', 'PUT', 'DELETE']), url: z.string(), headers: z.record(z.string(), z.string()).optional(), body: z.any().optional() })).min(1).max(20) });
- src/server.ts:15-15 (helper)Import of GraphAdvancedFeatures class and schemas (batchRequestSchema etc.) from graph-advanced-features.ts, enabling their use in tool registrations.import { GraphAdvancedFeatures, batchRequestSchema, deltaQuerySchema, webhookSubscriptionSchema, searchQuerySchema } from './utils/graph-advanced-features.js';