77
77
* @author Christian Tzolov
78
78
* @author Dariusz Jędrzejczyk
79
79
* @author Jihoon Kim
80
- * @see McpServer
80
+ * @see McpServerFactory
81
81
* @see McpSchema
82
82
* @see McpClientSession
83
83
*/
84
- public class McpAsyncServer {
84
+ public class McpAsyncServer implements McpServer {
85
85
86
86
private static final Logger logger = LoggerFactory .getLogger (McpAsyncServer .class );
87
87
@@ -144,7 +144,7 @@ public void close() {
144
144
* @param toolSpecification The tool specification to add
145
145
* @return Mono that completes when clients have been notified of the change
146
146
*/
147
- public Mono <Void > addTool (McpServerFeatures .AsyncToolSpecification toolSpecification ) {
147
+ public Mono <Void > addTool (McpServer .AsyncToolSpecification toolSpecification ) {
148
148
return this .delegate .addTool (toolSpecification );
149
149
}
150
150
@@ -173,7 +173,7 @@ public Mono<Void> notifyToolsListChanged() {
173
173
* @param resourceHandler The resource handler to add
174
174
* @return Mono that completes when clients have been notified of the change
175
175
*/
176
- public Mono <Void > addResource (McpServerFeatures .AsyncResourceSpecification resourceHandler ) {
176
+ public Mono <Void > addResource (McpServer .AsyncResourceSpecification resourceHandler ) {
177
177
return this .delegate .addResource (resourceHandler );
178
178
}
179
179
@@ -202,7 +202,7 @@ public Mono<Void> notifyResourcesListChanged() {
202
202
* @param promptSpecification The prompt handler to add
203
203
* @return Mono that completes when clients have been notified of the change
204
204
*/
205
- public Mono <Void > addPrompt (McpServerFeatures .AsyncPromptSpecification promptSpecification ) {
205
+ public Mono <Void > addPrompt (McpServer .AsyncPromptSpecification promptSpecification ) {
206
206
return this .delegate .addPrompt (promptSpecification );
207
207
}
208
208
@@ -251,7 +251,7 @@ public Mono<Void> loggingNotification(LoggingMessageNotification loggingMessageN
251
251
* code.
252
252
* @param protocolVersions the Client supported protocol versions.
253
253
*/
254
- void setProtocolVersions (List <String > protocolVersions ) {
254
+ public void setProtocolVersions (List <String > protocolVersions ) {
255
255
this .delegate .setProtocolVersions (protocolVersions );
256
256
}
257
257
@@ -267,13 +267,13 @@ private static class AsyncServerImpl extends McpAsyncServer {
267
267
268
268
private final String instructions ;
269
269
270
- private final CopyOnWriteArrayList <McpServerFeatures .AsyncToolSpecification > tools = new CopyOnWriteArrayList <>();
270
+ private final CopyOnWriteArrayList <McpServer .AsyncToolSpecification > tools = new CopyOnWriteArrayList <>();
271
271
272
272
private final CopyOnWriteArrayList <McpSchema .ResourceTemplate > resourceTemplates = new CopyOnWriteArrayList <>();
273
273
274
- private final ConcurrentHashMap <String , McpServerFeatures .AsyncResourceSpecification > resources = new ConcurrentHashMap <>();
274
+ private final ConcurrentHashMap <String , McpServer .AsyncResourceSpecification > resources = new ConcurrentHashMap <>();
275
275
276
- private final ConcurrentHashMap <String , McpServerFeatures .AsyncPromptSpecification > prompts = new ConcurrentHashMap <>();
276
+ private final ConcurrentHashMap <String , McpServer .AsyncPromptSpecification > prompts = new ConcurrentHashMap <>();
277
277
278
278
// FIXME: this field is deprecated and should be remvoed together with the
279
279
// broadcasting loggingNotification.
@@ -283,7 +283,7 @@ private static class AsyncServerImpl extends McpAsyncServer {
283
283
284
284
private List <String > protocolVersions = List .of (McpSchema .LATEST_PROTOCOL_VERSION );
285
285
286
- private McpUriTemplateManagerFactory uriTemplateManagerFactory = new DeafaultMcpUriTemplateManagerFactory () ;
286
+ private final McpUriTemplateManagerFactory uriTemplateManagerFactory ;
287
287
288
288
AsyncServerImpl (McpServerTransportProvider mcpTransportProvider , ObjectMapper objectMapper ,
289
289
Duration requestTimeout , McpServerFeatures .Async features ,
@@ -432,7 +432,7 @@ private McpServerSession.NotificationHandler asyncRootsListChangedNotificationHa
432
432
// ---------------------------------------
433
433
434
434
@ Override
435
- public Mono <Void > addTool (McpServerFeatures .AsyncToolSpecification toolSpecification ) {
435
+ public Mono <Void > addTool (McpServer .AsyncToolSpecification toolSpecification ) {
436
436
if (toolSpecification == null ) {
437
437
return Mono .error (new McpError ("Tool specification must not be null" ));
438
438
}
@@ -494,7 +494,7 @@ public Mono<Void> notifyToolsListChanged() {
494
494
495
495
private McpServerSession .RequestHandler <McpSchema .ListToolsResult > toolsListRequestHandler () {
496
496
return (exchange , params ) -> {
497
- List <Tool > tools = this .tools .stream ().map (McpServerFeatures .AsyncToolSpecification ::tool ).toList ();
497
+ List <Tool > tools = this .tools .stream ().map (McpServer .AsyncToolSpecification ::tool ).toList ();
498
498
499
499
return Mono .just (new McpSchema .ListToolsResult (tools , null ));
500
500
};
@@ -505,15 +505,16 @@ private McpServerSession.RequestHandler<CallToolResult> toolsCallRequestHandler(
505
505
McpSchema .CallToolRequest callToolRequest = schemaCodec .decodeResult (params ,
506
506
McpType .of (McpSchema .CallToolRequest .class ));
507
507
508
- Optional <McpServerFeatures .AsyncToolSpecification > toolSpecification = this .tools .stream ()
508
+ Optional <McpServer .AsyncToolSpecification > toolSpecification = this .tools .stream ()
509
509
.filter (tr -> callToolRequest .name ().equals (tr .tool ().name ()))
510
510
.findAny ();
511
511
512
512
if (toolSpecification .isEmpty ()) {
513
513
return Mono .error (new McpError ("Tool not found: " + callToolRequest .name ()));
514
514
}
515
515
516
- return toolSpecification .map (tool -> tool .call ().apply (exchange , callToolRequest .arguments ()))
516
+ return toolSpecification
517
+ .map (tool -> Mono .from (tool .call ().apply (exchange , callToolRequest .arguments ())))
517
518
.orElse (Mono .error (new McpError ("Tool not found: " + callToolRequest .name ())));
518
519
};
519
520
}
@@ -523,7 +524,7 @@ private McpServerSession.RequestHandler<CallToolResult> toolsCallRequestHandler(
523
524
// ---------------------------------------
524
525
525
526
@ Override
526
- public Mono <Void > addResource (McpServerFeatures .AsyncResourceSpecification resourceSpecification ) {
527
+ public Mono <Void > addResource (McpServer .AsyncResourceSpecification resourceSpecification ) {
527
528
if (resourceSpecification == null || resourceSpecification .resource () == null ) {
528
529
return Mono .error (new McpError ("Resource must not be null" ));
529
530
}
@@ -555,7 +556,7 @@ public Mono<Void> removeResource(String resourceUri) {
555
556
}
556
557
557
558
return Mono .defer (() -> {
558
- McpServerFeatures .AsyncResourceSpecification removed = this .resources .remove (resourceUri );
559
+ McpServer .AsyncResourceSpecification removed = this .resources .remove (resourceUri );
559
560
if (removed != null ) {
560
561
logger .debug ("Removed resource handler: {}" , resourceUri );
561
562
if (this .serverCapabilities .resources ().listChanged ()) {
@@ -577,7 +578,7 @@ private McpServerSession.RequestHandler<McpSchema.ListResourcesResult> resources
577
578
return (exchange , params ) -> {
578
579
var resourceList = this .resources .values ()
579
580
.stream ()
580
- .map (McpServerFeatures .AsyncResourceSpecification ::resource )
581
+ .map (McpServer .AsyncResourceSpecification ::resource )
581
582
.toList ();
582
583
return Mono .just (new McpSchema .ListResourcesResult (resourceList , null ));
583
584
};
@@ -613,15 +614,15 @@ private McpServerSession.RequestHandler<McpSchema.ReadResourceResult> resourcesR
613
614
McpType .of (McpSchema .ReadResourceRequest .class ));
614
615
var resourceUri = resourceRequest .uri ();
615
616
616
- McpServerFeatures .AsyncResourceSpecification specification = this .resources .values ()
617
+ McpServer .AsyncResourceSpecification specification = this .resources .values ()
617
618
.stream ()
618
619
.filter (resourceSpecification -> this .uriTemplateManagerFactory
619
620
.create (resourceSpecification .resource ().uri ())
620
621
.matches (resourceUri ))
621
622
.findFirst ()
622
623
.orElseThrow (() -> new McpError ("Resource not found: " + resourceUri ));
623
624
624
- return specification .readHandler ().apply (exchange , resourceRequest );
625
+ return Mono . from ( specification .readHandler ().apply (exchange , resourceRequest ) );
625
626
};
626
627
}
627
628
@@ -630,7 +631,7 @@ private McpServerSession.RequestHandler<McpSchema.ReadResourceResult> resourcesR
630
631
// ---------------------------------------
631
632
632
633
@ Override
633
- public Mono <Void > addPrompt (McpServerFeatures .AsyncPromptSpecification promptSpecification ) {
634
+ public Mono <Void > addPrompt (McpServer .AsyncPromptSpecification promptSpecification ) {
634
635
if (promptSpecification == null ) {
635
636
return Mono .error (new McpError ("Prompt specification must not be null" ));
636
637
}
@@ -639,7 +640,7 @@ public Mono<Void> addPrompt(McpServerFeatures.AsyncPromptSpecification promptSpe
639
640
}
640
641
641
642
return Mono .defer (() -> {
642
- McpServerFeatures .AsyncPromptSpecification specification = this .prompts
643
+ McpServer .AsyncPromptSpecification specification = this .prompts
643
644
.putIfAbsent (promptSpecification .prompt ().name (), promptSpecification );
644
645
if (specification != null ) {
645
646
return Mono .error (new McpError (
@@ -668,7 +669,7 @@ public Mono<Void> removePrompt(String promptName) {
668
669
}
669
670
670
671
return Mono .defer (() -> {
671
- McpServerFeatures .AsyncPromptSpecification removed = this .prompts .remove (promptName );
672
+ McpServer .AsyncPromptSpecification removed = this .prompts .remove (promptName );
672
673
673
674
if (removed != null ) {
674
675
logger .debug ("Removed prompt handler: {}" , promptName );
@@ -698,7 +699,7 @@ private McpServerSession.RequestHandler<McpSchema.ListPromptsResult> promptsList
698
699
699
700
var promptList = this .prompts .values ()
700
701
.stream ()
701
- .map (McpServerFeatures .AsyncPromptSpecification ::prompt )
702
+ .map (McpServer .AsyncPromptSpecification ::prompt )
702
703
.toList ();
703
704
704
705
return Mono .just (new McpSchema .ListPromptsResult (promptList , null ));
@@ -711,12 +712,12 @@ private McpServerSession.RequestHandler<McpSchema.GetPromptResult> promptsGetReq
711
712
McpType .of (McpSchema .GetPromptRequest .class ));
712
713
713
714
// Implement prompt retrieval logic here
714
- McpServerFeatures .AsyncPromptSpecification specification = this .prompts .get (promptRequest .name ());
715
+ McpServer .AsyncPromptSpecification specification = this .prompts .get (promptRequest .name ());
715
716
if (specification == null ) {
716
717
return Mono .error (new McpError ("Prompt not found: " + promptRequest .name ()));
717
718
}
718
719
719
- return specification .promptHandler ().apply (exchange , promptRequest );
720
+ return Mono . from ( specification .promptHandler ().apply (exchange , promptRequest ) );
720
721
};
721
722
}
722
723
@@ -775,25 +776,19 @@ private McpServerSession.RequestHandler<McpSchema.CompleteResult> completionComp
775
776
776
777
// check if the referenced resource exists
777
778
if (type .equals ("ref/prompt" ) && request .ref () instanceof McpSchema .PromptReference promptReference ) {
778
- McpServerFeatures .AsyncPromptSpecification promptSpec = this .prompts .get (promptReference .name ());
779
+ McpServer .AsyncPromptSpecification promptSpec = this .prompts .get (promptReference .name ());
779
780
if (promptSpec == null ) {
780
781
return Mono .error (new McpError ("Prompt not found: " + promptReference .name ()));
781
782
}
782
- if (!promptSpec .prompt ()
783
- .arguments ()
784
- .stream ()
785
- .filter (arg -> arg .name ().equals (argumentName ))
786
- .findFirst ()
787
- .isPresent ()) {
783
+ if (promptSpec .prompt ().arguments ().stream ().noneMatch (arg -> arg .name ().equals (argumentName ))) {
788
784
789
785
return Mono .error (new McpError ("Argument not found: " + argumentName ));
790
786
}
791
787
}
792
788
793
789
if (type .equals ("ref/resource" )
794
790
&& request .ref () instanceof McpSchema .ResourceReference resourceReference ) {
795
- McpServerFeatures .AsyncResourceSpecification resourceSpec = this .resources
796
- .get (resourceReference .uri ());
791
+ McpServer .AsyncResourceSpecification resourceSpec = this .resources .get (resourceReference .uri ());
797
792
if (resourceSpec == null ) {
798
793
return Mono .error (new McpError ("Resource not found: " + resourceReference .uri ()));
799
794
}
@@ -855,7 +850,7 @@ private McpSchema.CompleteRequest parseCompletionParams(Object object) {
855
850
// ---------------------------------------
856
851
857
852
@ Override
858
- void setProtocolVersions (List <String > protocolVersions ) {
853
+ public void setProtocolVersions (List <String > protocolVersions ) {
859
854
this .protocolVersions = protocolVersions ;
860
855
}
861
856
0 commit comments