conductor-oss/conductor · error · RuntimeException

MCP server exceeded the redirect limit

Error message

MCP server exceeded the redirect limit

What it means

Thrown by execute() when the redirect loop counter exceeds 5 (the for loop runs redirects 0..5 inclusive, so up to 6 redirects total are followed before this fires). Protects against redirect loops/cycles between the client and server. Indicates either a loop (A->B->A) or a redirect chain longer than the cap.

Source

Thrown at ai/src/main/java/org/conductoross/conductor/ai/mcp/MCPService.java:400

                            response.code(),
                            response.header("Content-Type", "application/json"),
                            readBoundedBody(response.body()));
                }
                String location = response.header("Location");
                if (location == null || request.url().resolve(location) == null) {
                    throw new RuntimeException(
                            "MCP server returned a redirect without a valid Location");
                }
                String target = request.url().resolve(location).toString();
                if (hasSensitiveHeaders(request)
                        && !isSameOrigin(request.url().toString(), target)) {
                    throw new RuntimeException(
                            "Refusing to forward credentials across an MCP redirect");
                }
                request = request.newBuilder().url(target).build();
            }
        }
        throw new RuntimeException("MCP server exceeded the redirect limit");
    }

    private void addHeaders(Request.Builder builder, Map<String, String> headers) {
        if (headers == null || headers.isEmpty()) {
            return;
        }
        headers.forEach(
                (name, value) -> {
                    if (name == null
                            || value == null
                            || name.indexOf('\r') >= 0
                            || name.indexOf('\n') >= 0
                            || value.indexOf('\r') >= 0
                            || value.indexOf('\n') >= 0) {
                        throw new IllegalArgumentException(
                                "MCP headers must not contain CR or LF characters");
                    }
                    builder.header(name, value);

View on GitHub (pinned to cf7c3e4a8a)

Solutions

  1. Point serverUrl at the canonical final endpoint to collapse the chain.
  2. Fix the server/proxy redirect loop (check trailing slashes, scheme, host config).
  3. Capture the sequence of Location values to identify the cycle.
  4. If a long chain is legitimate, redesign so the client targets the terminal host directly.
Defensive patterns

Strategy: try-catch

Try / catch

try {
    mcpService.listTools(serverUrl, headers);
} catch (RuntimeException e) {
    if (e.getMessage().contains("exceeded the redirect limit")) {
        // loop or very long chain; fix server/proxy config or use the final URL
    }
    throw e;
}

Prevention

When it happens

Trigger: Server returns a redirect on every request (loop A->B->A); a chain of gateways each redirecting to the next beyond 6 hops; URL is wrong and the server keeps redirecting to a login page that redirects back.

Common situations: Auth gateway redirect loop (logged-out -> login -> back to resource -> logged-out); trailing-slash vs no-slash loop; http->https->http misconfiguration; CDN origin pull loop.

Related errors


AI-assisted analysis of conductor-oss/conductor@cf7c3e4a8a (2026-08-14). Data as JSON: /api/errors/59da298d766d598b. Report an issue: GitHub.