|
25 | 25 | import io.modelcontextprotocol.spec.McpSchema.CompleteResult.CompleteCompletion; |
26 | 26 | import io.modelcontextprotocol.spec.McpSchema.ErrorCodes; |
27 | 27 | import io.modelcontextprotocol.spec.McpSchema.LoggingLevel; |
28 | | -import io.modelcontextprotocol.spec.McpSchema.LoggingMessageNotification; |
29 | 28 | import io.modelcontextprotocol.spec.McpSchema.PromptReference; |
30 | 29 | import io.modelcontextprotocol.spec.McpSchema.ResourceReference; |
31 | 30 | import io.modelcontextprotocol.spec.McpSchema.SetLevelRequest; |
@@ -995,47 +994,24 @@ private McpRequestHandler<McpSchema.CompleteResult> completionCompleteRequestHan |
995 | 994 | } |
996 | 995 |
|
997 | 996 | /** |
998 | | - * Parses the raw JSON-RPC request parameters into a {@link McpSchema.CompleteRequest} |
| 997 | + * Converts raw JSON-RPC request parameters into a {@link McpSchema.CompleteRequest} |
999 | 998 | * object. |
1000 | 999 | * <p> |
1001 | | - * This method manually extracts the `ref` and `argument` fields from the input map, |
1002 | | - * determines the correct reference type (either prompt or resource), and constructs a |
1003 | | - * fully-typed {@code CompleteRequest} instance. |
1004 | | - * @param object the raw request parameters, expected to be a Map containing "ref" and |
1005 | | - * "argument" entries. |
| 1000 | + * This method extracts the `ref` field to ensure it is not null and validates the |
| 1001 | + * structure of the request. It uses the {@code jsonMapper} to map the input object |
| 1002 | + * into a {@code CompleteRequest} instance. |
| 1003 | + * @param object the raw request parameters, expected to be a JSON-compatible object |
| 1004 | + * containing "ref" and other fields. |
1006 | 1005 | * @return a {@link McpSchema.CompleteRequest} representing the structured completion |
1007 | 1006 | * request. |
1008 | | - * @throws IllegalArgumentException if the "ref" type is not recognized. |
| 1007 | + * @throws IllegalArgumentException if the "ref" field is null. |
1009 | 1008 | */ |
1010 | | - @SuppressWarnings("unchecked") |
1011 | 1009 | private McpSchema.CompleteRequest parseCompletionParams(Object object) { |
1012 | | - Map<String, Object> params = (Map<String, Object>) object; |
1013 | | - Map<String, Object> refMap = (Map<String, Object>) params.get("ref"); |
1014 | | - Map<String, Object> argMap = (Map<String, Object>) params.get("argument"); |
1015 | | - Map<String, Object> contextMap = (Map<String, Object>) params.get("context"); |
1016 | | - Map<String, Object> meta = (Map<String, Object>) params.get("_meta"); |
1017 | | - |
1018 | | - String refType = (String) refMap.get("type"); |
1019 | | - |
1020 | | - McpSchema.CompleteReference ref = switch (refType) { |
1021 | | - case PromptReference.TYPE -> new McpSchema.PromptReference(refType, (String) refMap.get("name"), |
1022 | | - refMap.get("title") != null ? (String) refMap.get("title") : null); |
1023 | | - case ResourceReference.TYPE -> new McpSchema.ResourceReference(refType, (String) refMap.get("uri")); |
1024 | | - default -> throw new IllegalArgumentException("Invalid ref type: " + refType); |
1025 | | - }; |
1026 | | - |
1027 | | - String argName = (String) argMap.get("name"); |
1028 | | - String argValue = (String) argMap.get("value"); |
1029 | | - McpSchema.CompleteRequest.CompleteArgument argument = new McpSchema.CompleteRequest.CompleteArgument(argName, |
1030 | | - argValue); |
1031 | | - |
1032 | | - McpSchema.CompleteRequest.CompleteContext context = null; |
1033 | | - if (contextMap != null) { |
1034 | | - Map<String, String> arguments = (Map<String, String>) contextMap.get("arguments"); |
1035 | | - context = new McpSchema.CompleteRequest.CompleteContext(arguments); |
| 1010 | + McpSchema.CompleteRequest request = jsonMapper.convertValue(object, McpSchema.CompleteRequest.class); |
| 1011 | + if (request.ref() == null) { |
| 1012 | + throw new IllegalArgumentException("Completion request ref must not be null"); |
1036 | 1013 | } |
1037 | | - |
1038 | | - return new McpSchema.CompleteRequest(ref, argument, meta, context); |
| 1014 | + return request; |
1039 | 1015 | } |
1040 | 1016 |
|
1041 | 1017 | /** |
|
0 commit comments