weiqingy commented on code in PR #531:
URL: https://github.com/apache/flink-agents/pull/531#discussion_r2825683020
##########
integrations/mcp/src/main/java/org/apache/flink/agents/integrations/mcp/MCPServer.java:
##########
@@ -257,28 +347,117 @@ private void validateHttpUrl() {
}
}
+ /**
+ * Execute an operation with retry logic.
+ *
+ * @param operation The operation to execute
+ * @param operationName Name of the operation for error messages
+ * @return The result of the operation
+ * @throws RuntimeException if all retries fail
+ */
+ private <T> T executeWithRetry(Callable<T> operation, String
operationName) {
+ int attempt = 0;
+ long backoff = initialBackoffMs;
+ Exception lastException = null;
+
+ while (attempt <= maxRetries) {
+ try {
+ return operation.call();
+
+ } catch (Exception e) {
+ lastException = e;
+ attempt++;
+
+ if (!isRetryable(e)) {
+ throw new RuntimeException(
+ String.format(
+ "MCP operation '%s' failed: %s",
operationName, e.getMessage()),
+ e);
+ }
+
+ if (attempt > maxRetries) {
+ break;
+ }
+
+ // Exponential backoff with jitter
+ try {
+ long jitter = RANDOM.nextInt((int) (backoff / 10) + 1);
+ long sleepTime = backoff + jitter;
+ Thread.sleep(sleepTime);
+ } catch (InterruptedException ie) {
+ Thread.currentThread().interrupt();
+ throw new RuntimeException(
+ "Interrupted while retrying MCP operation: " +
operationName, ie);
+ }
+
+ backoff = Math.min(backoff * 2, maxBackoffMs);
+ }
+ }
+
+ // All retries exhausted
+ throw new RuntimeException(
+ String.format(
+ "MCP operation '%s' failed after %d retries: %s",
+ operationName, maxRetries, lastException.getMessage()),
+ lastException);
+ }
+
+ /**
+ * Check if an exception is retryable.
+ *
+ * @param e The exception to check
+ * @return true if the operation should be retried
+ */
+ private boolean isRetryable(Exception e) {
+ // Network-related errors are retryable
+ if (e instanceof SocketTimeoutException || e instanceof
ConnectException) {
+ return true;
+ }
+ String message = e.getMessage();
+ if (message != null) {
+ if (message.contains("503")) {
Review Comment:
The string-contains check for "503" and "429" could match unrelated messages
- e.g., a tool named "room-503-monitor" or an error like "processed 429 items".
Would a more specific pattern like "HTTP 503" or a word-boundary check work
here? Alternatively, if the MCP SDK throws typed exceptions for HTTP errors,
checking the exception type would be more robust.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]