{"record":{"id":"59da298d766d598b","repo":"conductor-oss/conductor","slug":"mcp-server-exceeded-the-redirect-limit","errorCode":null,"errorMessage":"MCP server exceeded the redirect limit","messagePattern":"MCP server exceeded the redirect limit","errorType":"exception","errorClass":"RuntimeException","httpStatus":null,"severity":"error","filePath":"ai/src/main/java/org/conductoross/conductor/ai/mcp/MCPService.java","lineNumber":400,"sourceCode":"                            response.code(),\n                            response.header(\"Content-Type\", \"application/json\"),\n                            readBoundedBody(response.body()));\n                }\n                String location = response.header(\"Location\");\n                if (location == null || request.url().resolve(location) == null) {\n                    throw new RuntimeException(\n                            \"MCP server returned a redirect without a valid Location\");\n                }\n                String target = request.url().resolve(location).toString();\n                if (hasSensitiveHeaders(request)\n                        && !isSameOrigin(request.url().toString(), target)) {\n                    throw new RuntimeException(\n                            \"Refusing to forward credentials across an MCP redirect\");\n                }\n                request = request.newBuilder().url(target).build();\n            }\n        }\n        throw new RuntimeException(\"MCP server exceeded the redirect limit\");\n    }\n\n    private void addHeaders(Request.Builder builder, Map<String, String> headers) {\n        if (headers == null || headers.isEmpty()) {\n            return;\n        }\n        headers.forEach(\n                (name, value) -> {\n                    if (name == null\n                            || value == null\n                            || name.indexOf('\\r') >= 0\n                            || name.indexOf('\\n') >= 0\n                            || value.indexOf('\\r') >= 0\n                            || value.indexOf('\\n') >= 0) {\n                        throw new IllegalArgumentException(\n                                \"MCP headers must not contain CR or LF characters\");\n                    }\n                    builder.header(name, value);","sourceCodeStart":382,"sourceCodeEnd":418,"githubUrl":"https://github.com/conductor-oss/conductor/blob/cf7c3e4a8adfb158be778ab1ec525323c363cd3a/ai/src/main/java/org/conductoross/conductor/ai/mcp/MCPService.java#L382-L418","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Point serverUrl at the canonical final endpoint to collapse the chain.","Fix the server/proxy redirect loop (check trailing slashes, scheme, host config).","Capture the sequence of Location values to identify the cycle.","If a long chain is legitimate, redesign so the client targets the terminal host directly."],"exampleFix":null,"handlingStrategy":"try-catch","validationCode":null,"typeGuard":null,"tryCatchPattern":"try {\n    mcpService.listTools(serverUrl, headers);\n} catch (RuntimeException e) {\n    if (e.getMessage().contains(\"exceeded the redirect limit\")) {\n        // loop or very long chain; fix server/proxy config or use the final URL\n    }\n    throw e;\n}","preventionTips":["Use the canonical final URL to collapse redirect chains.","Fix trailing-slash / scheme / host redirect loops at the server or proxy.","Capture the Location sequence to diagnose cycles."],"tags":["mcp","network","http-redirect","redirect-loop"],"backgroundTag":null,"analyzedSha":"cf7c3e4a8adfb158be778ab1ec525323c363cd3a","analyzedAt":"2026-08-14T03:33:19.897Z","schemaVersion":2},"datasetVersion":"2026-08-14T05:17:29.042Z"}