{"record":{"id":"649df61851391c97","repo":"apache/shenyu","slug":"invalid-uri-construction-e-getmessage","errorCode":null,"errorMessage":"Invalid URI construction: ${e.getMessage()}","messagePattern":"Invalid URI construction: (.+?)","errorType":"exception","errorClass":"RuntimeException","httpStatus":null,"severity":"error","filePath":"shenyu-plugin/shenyu-plugin-mcp-server/src/main/java/org/apache/shenyu/plugin/mcp/server/callback/ShenyuToolCallback.java","lineNumber":589,"sourceCode":"        }\n    }\n\n    /**\n     * Sets the target URI for the request.\n     *\n     * @param requestBuilder the request builder\n     * @param originExchange the original exchange\n     * @param path           the target path\n     */\n    private void setTargetUri(final ServerHttpRequest.Builder requestBuilder,\n                              final ServerWebExchange originExchange,\n                              final String path) {\n        try {\n            final URI oldUri = originExchange.getRequest().getURI();\n            final String newUriStr = oldUri.getScheme() + \"://\" + oldUri.getAuthority() + path;\n            requestBuilder.uri(new URI(newUriStr));\n        } catch (URISyntaxException e) {\n            throw new RuntimeException(\"Invalid URI construction: \" + e.getMessage(), e);\n        }\n    }\n\n    /**\n     * Creates appropriate response decorator based on protocol type.\n     *\n     * @param originExchange the original exchange\n     * @param sessionId      the session identifier\n     * @param responseFuture the response future\n     * @param configStr      the configuration string (for response template)\n     * @return the appropriate response decorator\n     */\n    private ServerHttpResponseDecorator createResponseDecorator(final ServerWebExchange originExchange,\n                                                                final String sessionId,\n                                                                final CompletableFuture<String> responseFuture,\n                                                                final String configStr) {\n\n        final RequestConfigHelper configHelper = new RequestConfigHelper(configStr);","sourceCodeStart":571,"sourceCodeEnd":607,"githubUrl":"https://github.com/apache/shenyu/blob/567142e07261b3e615ae8850b30f4421f455cc5d/shenyu-plugin/shenyu-plugin-mcp-server/src/main/java/org/apache/shenyu/plugin/mcp/server/callback/ShenyuToolCallback.java#L571-L607","documentation":"setTargetUri rebuilds the request URI as scheme://authority + path and applies it to the request builder. If the resulting string is not a syntactically valid URI, URISyntaxException is wrapped in RuntimeException('Invalid URI construction: ...').","triggerScenarios":"The computed path argument contains illegal URI characters (spaces, unencoded braces '{ }', raw unicode, control chars) so that scheme://authority+path fails java.net.URI parsing.","commonSituations":"Path templates from requestConfig or MCP arguments contain '{param}' placeholders that were never substituted; path built from unescaped user input containing spaces or query fragments with raw characters.","solutions":["Inspect the wrapped cause message — java.net.URI reports the illegal character and index.","URL-encode the path before building: UriComponentsBuilder.fromPath(path).build().encode() or URLEncoder for segments.","Ensure all '{placeholder}' tokens in the path template are replaced before setTargetUri runs.","Strip illegal characters or convert them to a query string when appropriate."],"exampleFix":"// before\nrequestBuilder.uri(new URI(base + \"/users/{id}\".replace(\"{id}\", rawId)));\n// after\nString encoded = UriComponentsBuilder.fromPath(\"/users/{id}\").buildAndExpand(rawId).encode().toPathString();\nrequestBuilder.uri(new URI(base + encoded));","handlingStrategy":"validation","validationCode":"boolean isBuildableUri(String scheme, String authority, String path) {\n    try {\n        new java.net.URI(scheme + \"://\" + authority + path);\n        return true;\n    } catch (java.net.URISyntaxException e) {\n        return false;\n    }\n}","typeGuard":null,"tryCatchPattern":"try {\n    return callback.call(args, ctx);\n} catch (RuntimeException e) {\n    if (e.getMessage() != null && e.getMessage().startsWith(\"Invalid URI construction\")) {\n        log.error(\"Bad tool path, illegal characters: {}\", e.getCause().getMessage());\n        return \"Invalid target path configured for tool\";\n    }\n    throw e;\n}","preventionTips":["URL-encode path segments and substitute all '{placeholder}' tokens before the call.","Build URIs with Spring's UriComponentsBuilder(...).encode() instead of string concat.","Reject paths containing spaces, braces, or control characters at config time.","Test each tool's requestConfig path with a real request before publishing."],"tags":["mcp","uri","http","encoding","ai"],"backgroundTag":"invalid-url-format","analyzedSha":"567142e07261b3e615ae8850b30f4421f455cc5d","analyzedAt":"2026-09-12T10:08:21.293Z","contentChangedAt":"2026-09-12T10:08:21.293Z","schemaVersion":2},"datasetVersion":"2026-09-16T04:17:20.429Z"}