feat: add context support to CompleteRequest for dependent completions

- Add CompleteContext record to support contextual completion scenarios
- Update CompleteRequest to include context and meta fields with backward-compatible constructors
- Modify McpAsyncServer to extract and pass context/meta from request parameters
- Add tests for context-aware completions including dependent scenarios
- Maintain backward compatibility for existing completion handlers

Signed-off-by: Christian Tzolov <christian.tzolov@broadcom.com>
This commit is contained in:
bzsurbhi 2025-07-02 13:31:18 -07:00 • committed by Christian Tzolov
commit dc477fc522
2 changed files with 336 additions and 12 deletions

View file

@ -721,6 +721,8 @@ public class McpAsyncServer {
Map<String, Object> params = (Map<String, Object>) object;
Map<String, Object> refMap = (Map<String, Object>) params.get("ref");
Map<String, Object> argMap = (Map<String, Object>) params.get("argument");
Map<String, Object> contextMap = (Map<String, Object>) params.get("context");
Map<String, Object> meta = (Map<String, Object>) params.get("_meta");
String refType = (String) refMap.get("type");
@ -736,7 +738,13 @@ public class McpAsyncServer {
McpSchema.CompleteRequest.CompleteArgument argument = new McpSchema.CompleteRequest.CompleteArgument(argName,
argValue);
return new McpSchema.CompleteRequest(ref, argument);
McpSchema.CompleteRequest.CompleteContext context = null;
if (contextMap != null) {
Map<String, String> arguments = (Map<String, String>) contextMap.get("arguments");
context = new McpSchema.CompleteRequest.CompleteContext(arguments);
}
return new McpSchema.CompleteRequest(ref, argument, meta, context);
}
/**

View file

@ -1755,18 +1755,31 @@ public final class McpSchema {
@JsonInclude(JsonInclude.Include.NON_ABSENT)
@JsonIgnoreProperties(ignoreUnknown = true)
public record CompleteRequest(// @formatter:off
@JsonProperty("ref") McpSchema.CompleteReference ref,
@JsonProperty("argument") CompleteArgument argument,
@JsonProperty("_meta") Map<String, Object> meta) implements Request {
@JsonProperty("ref") McpSchema.CompleteReference ref,
@JsonProperty("argument") CompleteArgument argument,
@JsonProperty("_meta") Map<String, Object> meta,
@JsonProperty("context") CompleteContext context) implements Request {
public CompleteRequest(McpSchema.CompleteReference ref, CompleteArgument argument) {
this(ref, argument, null);
}
public CompleteRequest(McpSchema.CompleteReference ref, CompleteArgument argument, Map<String, Object> meta) {
this(ref, argument, meta, null);
}
public record CompleteArgument(
@JsonProperty("name") String name,
@JsonProperty("value") String value) {
}// @formatter:on
public CompleteRequest(McpSchema.CompleteReference ref, CompleteArgument argument, CompleteContext context) {
this(ref, argument, null, context);
}
public CompleteRequest(McpSchema.CompleteReference ref, CompleteArgument argument) {
this(ref, argument, null, null);
}
public record CompleteArgument(
@JsonProperty("name") String name,
@JsonProperty("value") String value) {
}
public record CompleteContext(
@JsonProperty("arguments") Map<String, String> arguments) {
}// @formatter:on
}
@JsonInclude(JsonInclude.Include.NON_ABSENT)