{"record":{"id":"bbf2548fb1c2ab25","repo":"apache/shenyu","slug":"invalid-json-format-e-getmessage","errorCode":null,"errorMessage":"Invalid JSON format: ${e.getMessage()}","messagePattern":"Invalid JSON format: (.+?)","errorType":"validation","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"shenyu-plugin/shenyu-plugin-mcp-server/src/main/java/org/apache/shenyu/plugin/mcp/server/callback/ShenyuToolCallback.java","lineNumber":348,"sourceCode":"     * Parses the input JSON string into a JsonObject.\n     *\n     * @param input the input JSON string\n     * @return the parsed JsonObject\n     * @throws IllegalArgumentException if input is not valid JSON\n     */\n    private JsonObject parseInput(final String input) {\n        try {\n            if (org.apache.commons.lang3.StringUtils.isBlank(input)) {\n                return new JsonObject();\n            }\n            final JsonObject inputJson = GsonUtils.getInstance().fromJson(input, JsonObject.class);\n            if (Objects.isNull(inputJson)) {\n                throw new IllegalArgumentException(\"Invalid input JSON format\");\n            }\n            return inputJson;\n        } catch (Exception e) {\n            LOG.error(\"Failed to parse input JSON: {}\", e.getMessage());\n            throw new IllegalArgumentException(\"Invalid JSON format: \" + e.getMessage(), e);\n        }\n    }\n\n    /**\n     * Builds request configuration from configuration string and input parameters.\n     *\n     * @param configStr the configuration string\n     * @param inputJson the input parameters\n     * @return the built request configuration\n     */\n    private RequestConfig buildRequestConfig(final String configStr, final JsonObject inputJson) {\n        final RequestConfigHelper configHelper = new RequestConfigHelper(configStr);\n        final JsonObject requestTemplate = configHelper.getRequestTemplate();\n        final JsonObject argsPosition = configHelper.getArgsPosition();\n        final String urlTemplate = configHelper.getUrlTemplate();\n        final String method = configHelper.getMethod();\n        final boolean argsToJsonBody = configHelper.isArgsToJsonBody();\n","sourceCodeStart":330,"sourceCodeEnd":366,"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#L330-L366","documentation":"In parseInput, any exception thrown while converting the input string to a JsonObject (Gson syntax error, unexpected token, or the null-result IllegalArgumentException from the same method) is caught, logged, and rethrown as IllegalArgumentException('Invalid JSON format: <cause message>'). This tells the MCP caller its arguments string was not valid JSON.","triggerScenarios":"Calling the tool with an arguments string that Gson cannot parse, e.g. \"{name: }\", trailing commas, single quotes, or non-JSON text emitted by the LLM.","commonSituations":"LLM-generated tool arguments that are malformed JSON; clients doing naive string concatenation instead of JSON serialization; encoding issues truncating the payload.","solutions":["Read the wrapped cause message to see the exact Gson parse error and position.","Fix the client to serialize arguments with a proper JSON library instead of string building.","Validate the arguments string with JSON.parse (or equivalent) before the tool call.","Constrain/prompt the LLM or use structured output so emitted tool arguments are valid JSON."],"exampleFix":"// before\ncallTool(\"t\", \"{name:'x'}\");\n// after\ncallTool(\"t\", new Gson().toJson(Map.of(\"name\", \"x\"))); // {\"name\":\"x\"}","handlingStrategy":"validation","validationCode":"boolean isValidJsonObject(String s) {\n    try {\n        JsonObject o = com.google.gson.JsonParser.parseString(s).getAsJsonObject();\n        return o != null;\n    } catch (Exception e) { return false; }\n}","typeGuard":null,"tryCatchPattern":"try {\n    return callback.call(args, ctx);\n} catch (IllegalArgumentException e) {\n    if (e.getMessage().startsWith(\"Invalid JSON format\")) {\n        log.warn(\"Tool arguments not valid JSON: {}\", e.getMessage());\n        return \"Please provide arguments as a valid JSON object\";\n    }\n    throw e;\n}","preventionTips":["Pre-validate arguments with JSON.parse / Gson before the call.","Use structured/JSON-mode output from the LLM to avoid malformed tool arguments.","Log the raw arguments string alongside the parse error for debugging.","Encode payloads consistently (UTF-8) to avoid truncation artifacts."],"tags":["mcp","json","validation","ai"],"backgroundTag":"json-parse-error","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"}