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
@@ -95,13 +95,13 @@ public class McpAsyncServer {
95
95
96
96
private final String instructions ;
97
97
98
- private final CopyOnWriteArrayList <McpServerFeatures .AsyncToolSpecification > tools = new CopyOnWriteArrayList <>();
98
+ private final CopyOnWriteArrayList <McpServer .AsyncToolSpecification > tools = new CopyOnWriteArrayList <>();
99
99
100
100
private final CopyOnWriteArrayList <McpSchema .ResourceTemplate > resourceTemplates = new CopyOnWriteArrayList <>();
101
101
102
- private final ConcurrentHashMap <String , McpServerFeatures .AsyncResourceSpecification > resources = new ConcurrentHashMap <>();
102
+ private final ConcurrentHashMap <String , McpServer .AsyncResourceSpecification > resources = new ConcurrentHashMap <>();
103
103
104
- private final ConcurrentHashMap <String , McpServerFeatures .AsyncPromptSpecification > prompts = new ConcurrentHashMap <>();
104
+ private final ConcurrentHashMap <String , McpServer .AsyncPromptSpecification > prompts = new ConcurrentHashMap <>();
105
105
106
106
// FIXME: this field is deprecated and should be remvoed together with the
107
107
// broadcasting loggingNotification.
@@ -111,7 +111,7 @@ public class McpAsyncServer {
111
111
112
112
private List <String > protocolVersions = List .of (McpSchema .LATEST_PROTOCOL_VERSION );
113
113
114
- private McpUriTemplateManagerFactory uriTemplateManagerFactory = new DeafaultMcpUriTemplateManagerFactory () ;
114
+ private final McpUriTemplateManagerFactory uriTemplateManagerFactory ;
115
115
116
116
/**
117
117
* Create a new McpAsyncServer with the given transport provider and capabilities.
@@ -282,7 +282,7 @@ private McpServerSession.NotificationHandler asyncRootsListChangedNotificationHa
282
282
* @param toolSpecification The tool specification to add
283
283
* @return Mono that completes when clients have been notified of the change
284
284
*/
285
- public Mono <Void > addTool (McpServerFeatures .AsyncToolSpecification toolSpecification ) {
285
+ public Mono <Void > addTool (McpServer .AsyncToolSpecification toolSpecification ) {
286
286
if (toolSpecification == null ) {
287
287
return Mono .error (new McpError ("Tool specification must not be null" ));
288
288
}
@@ -351,7 +351,7 @@ public Mono<Void> notifyToolsListChanged() {
351
351
352
352
private McpServerSession .RequestHandler <McpSchema .ListToolsResult > toolsListRequestHandler () {
353
353
return (exchange , params ) -> {
354
- List <Tool > tools = this .tools .stream ().map (McpServerFeatures .AsyncToolSpecification ::tool ).toList ();
354
+ List <Tool > tools = this .tools .stream ().map (McpServer .AsyncToolSpecification ::tool ).toList ();
355
355
356
356
return Mono .just (new McpSchema .ListToolsResult (tools , null ));
357
357
};
@@ -362,15 +362,16 @@ private McpServerSession.RequestHandler<CallToolResult> toolsCallRequestHandler(
362
362
McpSchema .CallToolRequest callToolRequest = schemaCodec .decodeResult (params ,
363
363
McpType .of (McpSchema .CallToolRequest .class ));
364
364
365
- Optional <McpServerFeatures .AsyncToolSpecification > toolSpecification = this .tools .stream ()
365
+ Optional <McpServer .AsyncToolSpecification > toolSpecification = this .tools .stream ()
366
366
.filter (tr -> callToolRequest .name ().equals (tr .tool ().name ()))
367
367
.findAny ();
368
368
369
369
if (toolSpecification .isEmpty ()) {
370
370
return Mono .error (new McpError ("Tool not found: " + callToolRequest .name ()));
371
371
}
372
372
373
- return toolSpecification .map (tool -> tool .call ().apply (exchange , callToolRequest .arguments ()))
373
+ return toolSpecification
374
+ .map (tool -> Mono .from (tool .call ().apply (exchange , callToolRequest .arguments ())))
374
375
.orElse (Mono .error (new McpError ("Tool not found: " + callToolRequest .name ())));
375
376
};
376
377
}
@@ -384,7 +385,7 @@ private McpServerSession.RequestHandler<CallToolResult> toolsCallRequestHandler(
384
385
* @param resourceSpecification The resource handler to add
385
386
* @return Mono that completes when clients have been notified of the change
386
387
*/
387
- public Mono <Void > addResource (McpServerFeatures .AsyncResourceSpecification resourceSpecification ) {
388
+ public Mono <Void > addResource (McpServer .AsyncResourceSpecification resourceSpecification ) {
388
389
if (resourceSpecification == null || resourceSpecification .resource () == null ) {
389
390
return Mono .error (new McpError ("Resource must not be null" ));
390
391
}
@@ -420,7 +421,7 @@ public Mono<Void> removeResource(String resourceUri) {
420
421
}
421
422
422
423
return Mono .defer (() -> {
423
- McpServerFeatures .AsyncResourceSpecification removed = this .resources .remove (resourceUri );
424
+ McpServer .AsyncResourceSpecification removed = this .resources .remove (resourceUri );
424
425
if (removed != null ) {
425
426
logger .debug ("Removed resource handler: {}" , resourceUri );
426
427
if (this .serverCapabilities .resources ().listChanged ()) {
@@ -445,7 +446,7 @@ private McpServerSession.RequestHandler<McpSchema.ListResourcesResult> resources
445
446
return (exchange , params ) -> {
446
447
var resourceList = this .resources .values ()
447
448
.stream ()
448
- .map (McpServerFeatures .AsyncResourceSpecification ::resource )
449
+ .map (McpServer .AsyncResourceSpecification ::resource )
449
450
.toList ();
450
451
return Mono .just (new McpSchema .ListResourcesResult (resourceList , null ));
451
452
};
@@ -481,15 +482,15 @@ private McpServerSession.RequestHandler<McpSchema.ReadResourceResult> resourcesR
481
482
McpType .of (McpSchema .ReadResourceRequest .class ));
482
483
var resourceUri = resourceRequest .uri ();
483
484
484
- McpServerFeatures .AsyncResourceSpecification specification = this .resources .values ()
485
+ McpServer .AsyncResourceSpecification specification = this .resources .values ()
485
486
.stream ()
486
487
.filter (resourceSpecification -> this .uriTemplateManagerFactory
487
488
.create (resourceSpecification .resource ().uri ())
488
489
.matches (resourceUri ))
489
490
.findFirst ()
490
491
.orElseThrow (() -> new McpError ("Resource not found: " + resourceUri ));
491
492
492
- return specification .readHandler ().apply (exchange , resourceRequest );
493
+ return Mono . from ( specification .readHandler ().apply (exchange , resourceRequest ) );
493
494
};
494
495
}
495
496
@@ -502,7 +503,7 @@ private McpServerSession.RequestHandler<McpSchema.ReadResourceResult> resourcesR
502
503
* @param promptSpecification The prompt handler to add
503
504
* @return Mono that completes when clients have been notified of the change
504
505
*/
505
- public Mono <Void > addPrompt (McpServerFeatures .AsyncPromptSpecification promptSpecification ) {
506
+ public Mono <Void > addPrompt (McpServer .AsyncPromptSpecification promptSpecification ) {
506
507
if (promptSpecification == null ) {
507
508
return Mono .error (new McpError ("Prompt specification must not be null" ));
508
509
}
@@ -511,7 +512,7 @@ public Mono<Void> addPrompt(McpServerFeatures.AsyncPromptSpecification promptSpe
511
512
}
512
513
513
514
return Mono .defer (() -> {
514
- McpServerFeatures .AsyncPromptSpecification specification = this .prompts
515
+ McpServer .AsyncPromptSpecification specification = this .prompts
515
516
.putIfAbsent (promptSpecification .prompt ().name (), promptSpecification );
516
517
if (specification != null ) {
517
518
return Mono .error (
@@ -544,7 +545,7 @@ public Mono<Void> removePrompt(String promptName) {
544
545
}
545
546
546
547
return Mono .defer (() -> {
547
- McpServerFeatures .AsyncPromptSpecification removed = this .prompts .remove (promptName );
548
+ McpServer .AsyncPromptSpecification removed = this .prompts .remove (promptName );
548
549
549
550
if (removed != null ) {
550
551
logger .debug ("Removed prompt handler: {}" , promptName );
@@ -577,7 +578,7 @@ private McpServerSession.RequestHandler<McpSchema.ListPromptsResult> promptsList
577
578
578
579
var promptList = this .prompts .values ()
579
580
.stream ()
580
- .map (McpServerFeatures .AsyncPromptSpecification ::prompt )
581
+ .map (McpServer .AsyncPromptSpecification ::prompt )
581
582
.toList ();
582
583
583
584
return Mono .just (new McpSchema .ListPromptsResult (promptList , null ));
@@ -590,12 +591,12 @@ private McpServerSession.RequestHandler<McpSchema.GetPromptResult> promptsGetReq
590
591
McpType .of (McpSchema .GetPromptRequest .class ));
591
592
592
593
// Implement prompt retrieval logic here
593
- McpServerFeatures .AsyncPromptSpecification specification = this .prompts .get (promptRequest .name ());
594
+ McpServer .AsyncPromptSpecification specification = this .prompts .get (promptRequest .name ());
594
595
if (specification == null ) {
595
596
return Mono .error (new McpError ("Prompt not found: " + promptRequest .name ()));
596
597
}
597
598
598
- return specification .promptHandler ().apply (exchange , promptRequest );
599
+ return Mono . from ( specification .promptHandler ().apply (exchange , promptRequest ) );
599
600
};
600
601
}
601
602
@@ -665,31 +666,26 @@ private McpServerSession.RequestHandler<McpSchema.CompleteResult> completionComp
665
666
666
667
// check if the referenced resource exists
667
668
if (type .equals ("ref/prompt" ) && request .ref () instanceof McpSchema .PromptReference promptReference ) {
668
- McpServerFeatures .AsyncPromptSpecification promptSpec = this .prompts .get (promptReference .name ());
669
+ McpServer .AsyncPromptSpecification promptSpec = this .prompts .get (promptReference .name ());
669
670
if (promptSpec == null ) {
670
671
return Mono .error (new McpError ("Prompt not found: " + promptReference .name ()));
671
672
}
672
- if (!promptSpec .prompt ()
673
- .arguments ()
674
- .stream ()
675
- .filter (arg -> arg .name ().equals (argumentName ))
676
- .findFirst ()
677
- .isPresent ()) {
673
+ if (promptSpec .prompt ().arguments ().stream ().noneMatch (arg -> arg .name ().equals (argumentName ))) {
678
674
679
675
return Mono .error (new McpError ("Argument not found: " + argumentName ));
680
676
}
681
677
}
682
678
683
679
if (type .equals ("ref/resource" ) && request .ref () instanceof McpSchema .ResourceReference resourceReference ) {
684
- McpServerFeatures .AsyncResourceSpecification resourceSpec = this .resources .get (resourceReference .uri ());
685
- if (resourceSpec == null ) {
686
- return Mono .error (new McpError ("Resource not found: " + resourceReference .uri ()));
687
- }
688
- if (!uriTemplateManagerFactory .create (resourceSpec .resource ().uri ())
689
- .getVariableNames ()
690
- .contains (argumentName )) {
691
- return Mono .error (new McpError ("Argument not found: " + argumentName ));
692
- }
680
+ McpServer .AsyncResourceSpecification resourceSpec = this .resources .get (resourceReference .uri ());
681
+ if (resourceSpec == null ) {
682
+ return Mono .error (new McpError ("Resource not found: " + resourceReference .uri ()));
683
+ }
684
+ if (!uriTemplateManagerFactory .create (resourceSpec .resource ().uri ())
685
+ .getVariableNames ()
686
+ .contains (argumentName )) {
687
+ return Mono .error (new McpError ("Argument not found: " + argumentName ));
688
+ }
693
689
694
690
}
695
691
@@ -743,8 +739,8 @@ private McpSchema.CompleteRequest parseCompletionParams(Object object) {
743
739
* code.
744
740
* @param protocolVersions the Client supported protocol versions.
745
741
*/
746
- void setProtocolVersions (List <String > protocolVersions ) {
747
- this .protocolVersions = protocolVersions ;
748
- }
742
+ public void setProtocolVersions (List <String > protocolVersions ) {
743
+ this .protocolVersions = protocolVersions ;
744
+ }
749
745
750
746
}
0 commit comments