retry_operation
Execute operations with exponential backoff to handle transient failures like API calls or network requests. Configure max retries, initial delay, and operation details.
Instructions
Retry an operation with exponential backoff. Use this for operations that might fail temporarily (API calls, network requests, etc.)
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| initial_delay_ms | No | ||
| max_retries | No | ||
| operation_data | Yes | Data specific to the operation (e.g., URL for HTTP, query for DB) | |
| operation_id | Yes | Unique identifier for this operation (used for tracking retries) | |
| operation_type | Yes | Type of operation being retried | |
| should_execute | No | If false, just returns retry metadata without executing |
Implementation Reference
- src/index-v2.ts:289-391 (handler)The core handler function for the 'retry_operation' tool, managing retry metadata, exponential backoff delays, execution conditions, and returning appropriate status responses.case "retry_operation": { const { operation_id, operation_type, operation_data, max_retries = 3, initial_delay_ms = 1000, should_execute = true } = args as any; // Get or create retry metadata let metadata = retryMetadata.get(operation_id) || { attempts: 0, lastAttempt: 0, success: false }; // Check if we should retry if (metadata.success) { return { content: [{ type: "text", text: JSON.stringify({ operation_id, status: "already_succeeded", attempts: metadata.attempts, message: "Operation already completed successfully" }) }] }; } if (metadata.attempts >= max_retries) { return { content: [{ type: "text", text: JSON.stringify({ operation_id, status: "max_retries_exceeded", attempts: metadata.attempts, message: "Maximum retry attempts reached" }) }] }; } // Calculate delay with exponential backoff const timeSinceLastAttempt = Date.now() - metadata.lastAttempt; const requiredDelay = initial_delay_ms * Math.pow(2, metadata.attempts); if (metadata.attempts > 0 && timeSinceLastAttempt < requiredDelay) { const waitTime = requiredDelay - timeSinceLastAttempt; return { content: [{ type: "text", text: JSON.stringify({ operation_id, status: "retry_delayed", attempts: metadata.attempts, wait_ms: waitTime, message: `Retry delayed. Wait ${waitTime}ms before next attempt` }) }] }; } if (!should_execute) { return { content: [{ type: "text", text: JSON.stringify({ operation_id, status: "ready_to_execute", attempts: metadata.attempts, next_delay_ms: requiredDelay, operation_type, operation_data }) }] }; } // Update metadata for this attempt metadata.attempts += 1; metadata.lastAttempt = Date.now(); retryMetadata.set(operation_id, metadata); // Here we return instructions for what should be retried // In practice, the calling system would execute the actual operation return { content: [{ type: "text", text: JSON.stringify({ operation_id, status: "execute_attempt", attempt_number: metadata.attempts, operation_type, operation_data, instructions: "Execute the operation and call retry_operation again with the result" }) }] }; }
- src/index-v2.ts:56-95 (schema)Tool registration including name, description, and input schema definition for 'retry_operation', used in the listTools response.{ name: "retry_operation", description: "Retry an operation with exponential backoff. Use this for operations that might fail temporarily (API calls, network requests, etc.)", inputSchema: { type: "object", properties: { operation_id: { type: "string", description: "Unique identifier for this operation (used for tracking retries)" }, operation_type: { type: "string", enum: ["http_request", "database_query", "file_operation", "custom"], description: "Type of operation being retried" }, operation_data: { type: "object", description: "Data specific to the operation (e.g., URL for HTTP, query for DB)" }, max_retries: { type: "number", default: 3, minimum: 1, maximum: 10 }, initial_delay_ms: { type: "number", default: 1000, minimum: 100, maximum: 60000 }, should_execute: { type: "boolean", description: "If false, just returns retry metadata without executing", default: true } }, required: ["operation_id", "operation_type", "operation_data"] } },