Skip to content

Commit 69c1038

Browse files
committed
Incorporate dynamic addition of tools, resources, and prompts
Signed-off-by: Dariusz Jędrzejczyk <[email protected]>
1 parent 9f50a23 commit 69c1038

File tree

2 files changed

+112
-43
lines changed

2 files changed

+112
-43
lines changed

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

Lines changed: 79 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -185,11 +185,23 @@ public Mono<McpSchema.ListRootsResult> listRoots(String cursor) {
185185
* Add a new tool registration at runtime.
186186
* @param toolRegistration The tool registration to add
187187
* @return Mono that completes when clients have been notified of the change
188+
* @deprecated This method will be removed in 0.9.0. Use
189+
* {@link #addTool(McpServerFeatures.AsyncToolSpecification)}.
188190
*/
191+
@Deprecated
189192
public Mono<Void> addTool(McpServerFeatures.AsyncToolRegistration toolRegistration) {
190193
return this.delegate.addTool(toolRegistration);
191194
}
192195

196+
/**
197+
* Add a new tool specification at runtime.
198+
* @param toolSpecification The tool specification to add
199+
* @return Mono that completes when clients have been notified of the change
200+
*/
201+
public Mono<Void> addTool(McpServerFeatures.AsyncToolSpecification toolSpecification) {
202+
return this.delegate.addTool(toolSpecification);
203+
}
204+
193205
/**
194206
* Remove a tool handler at runtime.
195207
* @param toolName The name of the tool handler to remove
@@ -215,11 +227,23 @@ public Mono<Void> notifyToolsListChanged() {
215227
* Add a new resource handler at runtime.
216228
* @param resourceHandler The resource handler to add
217229
* @return Mono that completes when clients have been notified of the change
230+
* @deprecated This method will be removed in 0.9.0. Use
231+
* {@link #addResource(McpServerFeatures.AsyncResourceSpecification)}.
218232
*/
233+
@Deprecated
219234
public Mono<Void> addResource(McpServerFeatures.AsyncResourceRegistration resourceHandler) {
220235
return this.delegate.addResource(resourceHandler);
221236
}
222237

238+
/**
239+
* Add a new resource handler at runtime.
240+
* @param resourceHandler The resource handler to add
241+
* @return Mono that completes when clients have been notified of the change
242+
*/
243+
public Mono<Void> addResource(McpServerFeatures.AsyncResourceSpecification resourceHandler) {
244+
return this.delegate.addResource(resourceHandler);
245+
}
246+
223247
/**
224248
* Remove a resource handler at runtime.
225249
* @param resourceUri The URI of the resource handler to remove
@@ -245,11 +269,23 @@ public Mono<Void> notifyResourcesListChanged() {
245269
* Add a new prompt handler at runtime.
246270
* @param promptRegistration The prompt handler to add
247271
* @return Mono that completes when clients have been notified of the change
272+
* @deprecated This method will be removed in 0.9.0. Use
273+
* {@link #addPrompt(McpServerFeatures.AsyncPromptSpecification)}.
248274
*/
275+
@Deprecated
249276
public Mono<Void> addPrompt(McpServerFeatures.AsyncPromptRegistration promptRegistration) {
250277
return this.delegate.addPrompt(promptRegistration);
251278
}
252279

280+
/**
281+
* Add a new prompt handler at runtime.
282+
* @param promptSpecification The prompt handler to add
283+
* @return Mono that completes when clients have been notified of the change
284+
*/
285+
public Mono<Void> addPrompt(McpServerFeatures.AsyncPromptSpecification promptSpecification) {
286+
return this.delegate.addPrompt(promptSpecification);
287+
}
288+
253289
/**
254290
* Remove a prompt handler at runtime.
255291
* @param promptName The name of the prompt handler to remove
@@ -536,20 +572,8 @@ private McpServerSession.NotificationHandler asyncRootsListChangedNotificationHa
536572
// ---------------------------------------
537573

538574
/**
539-
* Add a new tool registration at runtime.
540-
* @param toolRegistration The tool registration to add
541-
* @return Mono that completes when clients have been notified of the change
542-
* @deprecated This method will be removed in 0.9.0. Use
543-
* {@link #addTool(McpServerFeatures.AsyncToolSpecification)}.
544-
*/
545-
@Deprecated
546-
public Mono<Void> addTool(McpServerFeatures.AsyncToolRegistration toolRegistration) {
547-
return this.addTool(toolRegistration.toSpecification());
548-
}
549-
550-
/**
551-
* Add a new tool registration at runtime.
552-
* @param toolSpecification The tool registration to add
575+
* Add a new tool specification at runtime.
576+
* @param toolSpecification The tool specification to add
553577
* @return Mono that completes when clients have been notified of the change
554578
*/
555579
public Mono<Void> addTool(McpServerFeatures.AsyncToolSpecification toolSpecification) {
@@ -583,6 +607,11 @@ public Mono<Void> addTool(McpServerFeatures.AsyncToolSpecification toolSpecifica
583607
});
584608
}
585609

610+
@Override
611+
public Mono<Void> addTool(McpServerFeatures.AsyncToolRegistration toolRegistration) {
612+
return this.addTool(toolRegistration.toSpecification());
613+
}
614+
586615
/**
587616
* Remove a tool handler at runtime.
588617
* @param toolName The name of the tool handler to remove
@@ -598,7 +627,7 @@ public Mono<Void> removeTool(String toolName) {
598627

599628
return Mono.defer(() -> {
600629
boolean removed = this.tools
601-
.removeIf(toolRegistration -> toolRegistration.tool().name().equals(toolName));
630+
.removeIf(toolSpecification -> toolSpecification.tool().name().equals(toolName));
602631
if (removed) {
603632
logger.debug("Removed tool handler: {}", toolName);
604633
if (this.serverCapabilities.tools().listChanged()) {
@@ -649,18 +678,6 @@ private McpServerSession.RequestHandler<CallToolResult> toolsCallRequestHandler(
649678
// Resource Management
650679
// ---------------------------------------
651680

652-
/**
653-
* Add a new resource handler at runtime.
654-
* @param resourceHandler The resource handler to add
655-
* @return Mono that completes when clients have been notified of the change
656-
* @deprecated This method will be removed in 0.9.0. Use
657-
* {@link #addResource(McpServerFeatures.AsyncResourceSpecification)}.
658-
*/
659-
@Deprecated
660-
public Mono<Void> addResource(McpServerFeatures.AsyncResourceRegistration resourceHandler) {
661-
return this.addResource(resourceHandler.toSpecification());
662-
}
663-
664681
/**
665682
* Add a new resource handler at runtime.
666683
* @param resourceSpecification The resource handler to add
@@ -688,6 +705,11 @@ public Mono<Void> addResource(McpServerFeatures.AsyncResourceSpecification resou
688705
});
689706
}
690707

708+
@Override
709+
public Mono<Void> addResource(McpServerFeatures.AsyncResourceRegistration resourceHandler) {
710+
return this.addResource(resourceHandler.toSpecification());
711+
}
712+
691713
/**
692714
* Remove a resource handler at runtime.
693715
* @param resourceUri The URI of the resource handler to remove
@@ -744,9 +766,9 @@ private McpServerSession.RequestHandler<McpSchema.ReadResourceResult> resourcesR
744766
new TypeReference<McpSchema.ReadResourceRequest>() {
745767
});
746768
var resourceUri = resourceRequest.uri();
747-
McpServerFeatures.AsyncResourceSpecification registration = this.resources.get(resourceUri);
748-
if (registration != null) {
749-
return registration.readHandler().apply(exchange, resourceRequest);
769+
McpServerFeatures.AsyncResourceSpecification specification = this.resources.get(resourceUri);
770+
if (specification != null) {
771+
return specification.readHandler().apply(exchange, resourceRequest);
750772
}
751773
return Mono.error(new McpError("Resource not found: " + resourceUri));
752774
};
@@ -756,26 +778,14 @@ private McpServerSession.RequestHandler<McpSchema.ReadResourceResult> resourcesR
756778
// Prompt Management
757779
// ---------------------------------------
758780

759-
/**
760-
* Add a new prompt handler at runtime.
761-
* @param promptRegistration The prompt handler to add
762-
* @return Mono that completes when clients have been notified of the change
763-
* @deprecated This method will be removed in 0.9.0. Use
764-
* {@link #addPrompt(McpServerFeatures.AsyncPromptSpecification)}.
765-
*/
766-
@Deprecated
767-
public Mono<Void> addPrompt(McpServerFeatures.AsyncPromptRegistration promptRegistration) {
768-
return this.addPrompt(promptRegistration.toSpecification());
769-
}
770-
771781
/**
772782
* Add a new prompt handler at runtime.
773783
* @param promptSpecification The prompt handler to add
774784
* @return Mono that completes when clients have been notified of the change
775785
*/
776786
public Mono<Void> addPrompt(McpServerFeatures.AsyncPromptSpecification promptSpecification) {
777787
if (promptSpecification == null) {
778-
return Mono.error(new McpError("Prompt registration must not be null"));
788+
return Mono.error(new McpError("Prompt specification must not be null"));
779789
}
780790
if (this.serverCapabilities.prompts() == null) {
781791
return Mono.error(new McpError("Server must be configured with prompt capabilities"));
@@ -801,6 +811,11 @@ public Mono<Void> addPrompt(McpServerFeatures.AsyncPromptSpecification promptSpe
801811
});
802812
}
803813

814+
@Override
815+
public Mono<Void> addPrompt(McpServerFeatures.AsyncPromptRegistration promptRegistration) {
816+
return this.addPrompt(promptRegistration.toSpecification());
817+
}
818+
804819
/**
805820
* Remove a prompt handler at runtime.
806821
* @param promptName The name of the prompt handler to remove
@@ -1056,6 +1071,24 @@ private static final class LegacyAsyncServer extends McpAsyncServer {
10561071
notificationHandlers);
10571072
}
10581073

1074+
@Override
1075+
public Mono<Void> addTool(McpServerFeatures.AsyncToolSpecification toolSpecification) {
1076+
throw new IllegalArgumentException(
1077+
"McpAsyncServer configured with legacy " + "transport. Use McpServerTransportProvider instead.");
1078+
}
1079+
1080+
@Override
1081+
public Mono<Void> addResource(McpServerFeatures.AsyncResourceSpecification resourceHandler) {
1082+
throw new IllegalArgumentException(
1083+
"McpAsyncServer configured with legacy " + "transport. Use McpServerTransportProvider instead.");
1084+
}
1085+
1086+
@Override
1087+
public Mono<Void> addPrompt(McpServerFeatures.AsyncPromptSpecification promptSpecification) {
1088+
throw new IllegalArgumentException(
1089+
"McpAsyncServer configured with legacy " + "transport. Use McpServerTransportProvider instead.");
1090+
}
1091+
10591092
// ---------------------------------------
10601093
// Lifecycle Management
10611094
// ---------------------------------------
@@ -1182,6 +1215,7 @@ private DefaultMcpSession.NotificationHandler asyncRootsListChangedNotificationH
11821215
* @param toolRegistration The tool registration to add
11831216
* @return Mono that completes when clients have been notified of the change
11841217
*/
1218+
@Override
11851219
public Mono<Void> addTool(McpServerFeatures.AsyncToolRegistration toolRegistration) {
11861220
if (toolRegistration == null) {
11871221
return Mono.error(new McpError("Tool registration must not be null"));
@@ -1284,6 +1318,7 @@ private DefaultMcpSession.RequestHandler<CallToolResult> toolsCallRequestHandler
12841318
* @param resourceHandler The resource handler to add
12851319
* @return Mono that completes when clients have been notified of the change
12861320
*/
1321+
@Override
12871322
public Mono<Void> addResource(McpServerFeatures.AsyncResourceRegistration resourceHandler) {
12881323
if (resourceHandler == null || resourceHandler.resource() == null) {
12891324
return Mono.error(new McpError("Resource must not be null"));
@@ -1379,6 +1414,7 @@ private DefaultMcpSession.RequestHandler<McpSchema.ReadResourceResult> resources
13791414
* @param promptRegistration The prompt handler to add
13801415
* @return Mono that completes when clients have been notified of the change
13811416
*/
1417+
@Override
13821418
public Mono<Void> addPrompt(McpServerFeatures.AsyncPromptRegistration promptRegistration) {
13831419
if (promptRegistration == null) {
13841420
return Mono.error(new McpError("Prompt registration must not be null"));

mcp/src/main/java/io/modelcontextprotocol/server/McpSyncServer.java

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,11 +87,22 @@ public McpSchema.ListRootsResult listRoots(String cursor) {
8787
/**
8888
* Add a new tool handler.
8989
* @param toolHandler The tool handler to add
90+
* @deprecated This method will be removed in 0.9.0. Use
91+
* {@link #addTool(McpServerFeatures.SyncToolSpecification)}.
9092
*/
93+
@Deprecated
9194
public void addTool(McpServerFeatures.SyncToolRegistration toolHandler) {
9295
this.asyncServer.addTool(McpServerFeatures.AsyncToolRegistration.fromSync(toolHandler)).block();
9396
}
9497

98+
/**
99+
* Add a new tool handler.
100+
* @param toolHandler The tool handler to add
101+
*/
102+
public void addTool(McpServerFeatures.SyncToolSpecification toolHandler) {
103+
this.asyncServer.addTool(McpServerFeatures.AsyncToolSpecification.fromSync(toolHandler)).block();
104+
}
105+
95106
/**
96107
* Remove a tool handler.
97108
* @param toolName The name of the tool handler to remove
@@ -103,11 +114,22 @@ public void removeTool(String toolName) {
103114
/**
104115
* Add a new resource handler.
105116
* @param resourceHandler The resource handler to add
117+
* @deprecated This method will be removed in 0.9.0. Use
118+
* {@link #addResource(McpServerFeatures.SyncResourceSpecification)}.
106119
*/
120+
@Deprecated
107121
public void addResource(McpServerFeatures.SyncResourceRegistration resourceHandler) {
108122
this.asyncServer.addResource(McpServerFeatures.AsyncResourceRegistration.fromSync(resourceHandler)).block();
109123
}
110124

125+
/**
126+
* Add a new resource handler.
127+
* @param resourceHandler The resource handler to add
128+
*/
129+
public void addResource(McpServerFeatures.SyncResourceSpecification resourceHandler) {
130+
this.asyncServer.addResource(McpServerFeatures.AsyncResourceSpecification.fromSync(resourceHandler)).block();
131+
}
132+
111133
/**
112134
* Remove a resource handler.
113135
* @param resourceUri The URI of the resource handler to remove
@@ -119,11 +141,22 @@ public void removeResource(String resourceUri) {
119141
/**
120142
* Add a new prompt handler.
121143
* @param promptRegistration The prompt registration to add
144+
* @deprecated This method will be removed in 0.9.0. Use
145+
* {@link #addPrompt(McpServerFeatures.SyncPromptSpecification)}.
122146
*/
147+
@Deprecated
123148
public void addPrompt(McpServerFeatures.SyncPromptRegistration promptRegistration) {
124149
this.asyncServer.addPrompt(McpServerFeatures.AsyncPromptRegistration.fromSync(promptRegistration)).block();
125150
}
126151

152+
/**
153+
* Add a new prompt handler.
154+
* @param promptSpecification The prompt specification to add
155+
*/
156+
public void addPrompt(McpServerFeatures.SyncPromptSpecification promptSpecification) {
157+
this.asyncServer.addPrompt(McpServerFeatures.AsyncPromptSpecification.fromSync(promptSpecification)).block();
158+
}
159+
127160
/**
128161
* Remove a prompt handler.
129162
* @param promptName The name of the prompt handler to remove

0 commit comments

Comments
 (0)