Skip to content

Commit 2550491

Browse files
committed
fix: attribute merge optimization
1 parent 9cb60eb commit 2550491

File tree

2 files changed

+28
-10
lines changed

2 files changed

+28
-10
lines changed

mcp/src/main/java/io/modelcontextprotocol/client/McpAsyncClient.java

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -612,17 +612,17 @@ private McpSchema.ListToolsResult mergeToolsAnnotations(McpSchema.ListToolsResul
612612
return new McpSchema.ListToolsResult(mergedTools, listToolsResult.nextCursor(), listToolsResult.meta());
613613
}
614614

615-
private McpSchema.ToolAnnotations mergeAnnotations(McpSchema.ToolAnnotations original,
616-
McpSchema.ToolAnnotations newer) {
617-
if (original == null) {
618-
return newer;
615+
private McpSchema.ToolAnnotations mergeAnnotations(McpSchema.ToolAnnotations remoteAnnotations,
616+
McpSchema.ToolAnnotations clientAnnotations) {
617+
if (remoteAnnotations == null) {
618+
return clientAnnotations;
619619
}
620-
return new McpSchema.ToolAnnotations(Utils.preferFirst(newer.title(), original.title()),
621-
Utils.preferFirst(newer.readOnlyHint(), original.readOnlyHint()),
622-
Utils.preferFirst(newer.destructiveHint(), original.destructiveHint()),
623-
Utils.preferFirst(newer.idempotentHint(), original.idempotentHint()),
624-
Utils.preferFirst(newer.openWorldHint(), original.openWorldHint()),
625-
Utils.preferFirst(newer.returnDirect(), original.returnDirect()));
620+
return new McpSchema.ToolAnnotations(Utils.preferFirst(clientAnnotations.title(), remoteAnnotations.title()),
621+
Utils.mergeBoolean(clientAnnotations.readOnlyHint(), remoteAnnotations.readOnlyHint()),
622+
Utils.mergeBoolean(clientAnnotations.destructiveHint(), remoteAnnotations.destructiveHint()),
623+
Utils.mergeBoolean(clientAnnotations.idempotentHint(), remoteAnnotations.idempotentHint()),
624+
Utils.mergeBoolean(clientAnnotations.openWorldHint(), remoteAnnotations.openWorldHint()),
625+
Utils.mergeBoolean(clientAnnotations.returnDirect(), remoteAnnotations.returnDirect()));
626626
}
627627

628628
private NotificationHandler asyncToolsChangeNotificationHandler(

mcp/src/main/java/io/modelcontextprotocol/util/Utils.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,4 +117,22 @@ public static <T> T preferFirst(T first, T second) {
117117
return second != null ? second : first;
118118
}
119119

120+
121+
/**
122+
* Merges two boolean values. If the first value is null, the second value is returned.
123+
* If the second value is null, false is returned.
124+
* @param first The first boolean value
125+
* @param second The second boolean value
126+
* @return The merged boolean value
127+
*/
128+
public static Boolean mergeBoolean(Boolean first, Boolean second) {
129+
if (first == null) {
130+
return second;
131+
}
132+
if (second == null) {
133+
return false;
134+
}
135+
return first && second;
136+
}
137+
120138
}

0 commit comments

Comments
 (0)