Skip to content

Commit 5aaf468

Browse files
committed
refactor: use jsonMapper when deserializing a CompleteRequest
1 parent eefce6b commit 5aaf468

File tree

2 files changed

+23
-61
lines changed

2 files changed

+23
-61
lines changed

mcp-core/src/main/java/io/modelcontextprotocol/server/McpAsyncServer.java

Lines changed: 11 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@
2525
import io.modelcontextprotocol.spec.McpSchema.CompleteResult.CompleteCompletion;
2626
import io.modelcontextprotocol.spec.McpSchema.ErrorCodes;
2727
import io.modelcontextprotocol.spec.McpSchema.LoggingLevel;
28-
import io.modelcontextprotocol.spec.McpSchema.LoggingMessageNotification;
2928
import io.modelcontextprotocol.spec.McpSchema.PromptReference;
3029
import io.modelcontextprotocol.spec.McpSchema.ResourceReference;
3130
import io.modelcontextprotocol.spec.McpSchema.SetLevelRequest;
@@ -995,47 +994,24 @@ private McpRequestHandler<McpSchema.CompleteResult> completionCompleteRequestHan
995994
}
996995

997996
/**
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}
999998
* object.
1000999
* <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.
10061005
* @return a {@link McpSchema.CompleteRequest} representing the structured completion
10071006
* request.
1008-
* @throws IllegalArgumentException if the "ref" type is not recognized.
1007+
* @throws IllegalArgumentException if the "ref" field is null.
10091008
*/
1010-
@SuppressWarnings("unchecked")
10111009
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");
10361013
}
1037-
1038-
return new McpSchema.CompleteRequest(ref, argument, meta, context);
1014+
return request;
10391015
}
10401016

10411017
/**

mcp-core/src/main/java/io/modelcontextprotocol/server/McpStatelessAsyncServer.java

Lines changed: 12 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -804,39 +804,25 @@ private McpStatelessRequestHandler<McpSchema.CompleteResult> completionCompleteR
804804
}
805805

806806
/**
807-
* Parses the raw JSON-RPC request parameters into a {@link McpSchema.CompleteRequest}
807+
* Converts raw JSON-RPC request parameters into a {@link McpSchema.CompleteRequest}
808808
* object.
809809
* <p>
810-
* This method manually extracts the `ref` and `argument` fields from the input map,
811-
* determines the correct reference type (either prompt or resource), and constructs a
812-
* fully-typed {@code CompleteRequest} instance.
813-
* @param object the raw request parameters, expected to be a Map containing "ref" and
814-
* "argument" entries.
810+
* This method extracts the `ref` field to ensure it is not null and validates the
811+
* structure of the request. It uses the {@code jsonMapper} to map the input object
812+
* into a {@code CompleteRequest} instance.
813+
* @param object the raw request parameters, expected to be a JSON-compatible object
814+
* containing "ref" and other fields.
815815
* @return a {@link McpSchema.CompleteRequest} representing the structured completion
816816
* request.
817-
* @throws IllegalArgumentException if the "ref" type is not recognized.
817+
* @throws IllegalArgumentException if the "ref" field is null.
818818
*/
819819
@SuppressWarnings("unchecked")
820820
private McpSchema.CompleteRequest parseCompletionParams(Object object) {
821-
Map<String, Object> params = (Map<String, Object>) object;
822-
Map<String, Object> refMap = (Map<String, Object>) params.get("ref");
823-
Map<String, Object> argMap = (Map<String, Object>) params.get("argument");
824-
825-
String refType = (String) refMap.get("type");
826-
827-
McpSchema.CompleteReference ref = switch (refType) {
828-
case PromptReference.TYPE -> new McpSchema.PromptReference(refType, (String) refMap.get("name"),
829-
refMap.get("title") != null ? (String) refMap.get("title") : null);
830-
case ResourceReference.TYPE -> new McpSchema.ResourceReference(refType, (String) refMap.get("uri"));
831-
default -> throw new IllegalArgumentException("Invalid ref type: " + refType);
832-
};
833-
834-
String argName = (String) argMap.get("name");
835-
String argValue = (String) argMap.get("value");
836-
McpSchema.CompleteRequest.CompleteArgument argument = new McpSchema.CompleteRequest.CompleteArgument(argName,
837-
argValue);
838-
839-
return new McpSchema.CompleteRequest(ref, argument);
821+
McpSchema.CompleteRequest request = jsonMapper.convertValue(object, McpSchema.CompleteRequest.class);
822+
if (request.ref() == null) {
823+
throw new IllegalArgumentException("Completion request ref must not be null");
824+
}
825+
return request;
840826
}
841827

842828
/**

0 commit comments

Comments
 (0)