fix: add support to set instructions as mentioned in #98 (#99)

This commit is contained in:
codezjx 2025-04-05 19:13:15 +08:00 • committed by GitHub
commit 15a55b6b62
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 49 additions and 8 deletions

View file

@ -247,6 +247,8 @@ public class McpAsyncServer {
private final McpSchema.Implementation serverInfo; private final McpSchema.Implementation serverInfo;
private final String instructions;
private final CopyOnWriteArrayList<McpServerFeatures.AsyncToolSpecification> tools = new CopyOnWriteArrayList<>(); private final CopyOnWriteArrayList<McpServerFeatures.AsyncToolSpecification> tools = new CopyOnWriteArrayList<>();
private final CopyOnWriteArrayList<McpSchema.ResourceTemplate> resourceTemplates = new CopyOnWriteArrayList<>(); private final CopyOnWriteArrayList<McpSchema.ResourceTemplate> resourceTemplates = new CopyOnWriteArrayList<>();
@ -265,6 +267,7 @@ public class McpAsyncServer {
this.objectMapper = objectMapper; this.objectMapper = objectMapper;
this.serverInfo = features.serverInfo(); this.serverInfo = features.serverInfo();
this.serverCapabilities = features.serverCapabilities(); this.serverCapabilities = features.serverCapabilities();
this.instructions = features.instructions();
this.tools.addAll(features.tools()); this.tools.addAll(features.tools());
this.resources.putAll(features.resources()); this.resources.putAll(features.resources());
this.resourceTemplates.addAll(features.resourceTemplates()); this.resourceTemplates.addAll(features.resourceTemplates());
@ -351,7 +354,7 @@ public class McpAsyncServer {
} }
return Mono.just(new McpSchema.InitializeResult(serverProtocolVersion, this.serverCapabilities, return Mono.just(new McpSchema.InitializeResult(serverProtocolVersion, this.serverCapabilities,
this.serverInfo, null)); this.serverInfo, this.instructions));
}); });
} }

View file

@ -160,6 +160,8 @@ public interface McpServer {
private McpSchema.ServerCapabilities serverCapabilities; private McpSchema.ServerCapabilities serverCapabilities;
private String instructions;
/** /**
* The Model Context Protocol (MCP) allows servers to expose tools that can be * The Model Context Protocol (MCP) allows servers to expose tools that can be
* invoked by language models. Tools enable models to interact with external * invoked by language models. Tools enable models to interact with external
@ -228,6 +230,18 @@ public interface McpServer {
return this; return this;
} }
/**
* Sets the server instructions that will be shared with clients during connection
* initialization. These instructions provide guidance to the client on how to
* interact with this server.
* @param instructions The instructions text. Can be null or empty.
* @return This builder instance for method chaining
*/
public AsyncSpecification instructions(String instructions) {
this.instructions = instructions;
return this;
}
/** /**
* Sets the server capabilities that will be advertised to clients during * Sets the server capabilities that will be advertised to clients during
* connection initialization. Capabilities define what features the server * connection initialization. Capabilities define what features the server
@ -549,7 +563,7 @@ public interface McpServer {
*/ */
public McpAsyncServer build() { public McpAsyncServer build() {
var features = new McpServerFeatures.Async(this.serverInfo, this.serverCapabilities, this.tools, var features = new McpServerFeatures.Async(this.serverInfo, this.serverCapabilities, this.tools,
this.resources, this.resourceTemplates, this.prompts, this.rootsChangeHandlers); this.resources, this.resourceTemplates, this.prompts, this.rootsChangeHandlers, this.instructions);
var mapper = this.objectMapper != null ? this.objectMapper : new ObjectMapper(); var mapper = this.objectMapper != null ? this.objectMapper : new ObjectMapper();
return new McpAsyncServer(this.transportProvider, mapper, features); return new McpAsyncServer(this.transportProvider, mapper, features);
} }
@ -572,6 +586,8 @@ public interface McpServer {
private McpSchema.ServerCapabilities serverCapabilities; private McpSchema.ServerCapabilities serverCapabilities;
private String instructions;
/** /**
* The Model Context Protocol (MCP) allows servers to expose tools that can be * The Model Context Protocol (MCP) allows servers to expose tools that can be
* invoked by language models. Tools enable models to interact with external * invoked by language models. Tools enable models to interact with external
@ -640,6 +656,18 @@ public interface McpServer {
return this; return this;
} }
/**
* Sets the server instructions that will be shared with clients during connection
* initialization. These instructions provide guidance to the client on how to
* interact with this server.
* @param instructions The instructions text. Can be null or empty.
* @return This builder instance for method chaining
*/
public SyncSpecification instructions(String instructions) {
this.instructions = instructions;
return this;
}
/** /**
* Sets the server capabilities that will be advertised to clients during * Sets the server capabilities that will be advertised to clients during
* connection initialization. Capabilities define what features the server * connection initialization. Capabilities define what features the server
@ -960,7 +988,8 @@ public interface McpServer {
*/ */
public McpSyncServer build() { public McpSyncServer build() {
McpServerFeatures.Sync syncFeatures = new McpServerFeatures.Sync(this.serverInfo, this.serverCapabilities, McpServerFeatures.Sync syncFeatures = new McpServerFeatures.Sync(this.serverInfo, this.serverCapabilities,
this.tools, this.resources, this.resourceTemplates, this.prompts, this.rootsChangeHandlers); this.tools, this.resources, this.resourceTemplates, this.prompts, this.rootsChangeHandlers,
this.instructions);
McpServerFeatures.Async asyncFeatures = McpServerFeatures.Async.fromSync(syncFeatures); McpServerFeatures.Async asyncFeatures = McpServerFeatures.Async.fromSync(syncFeatures);
var mapper = this.objectMapper != null ? this.objectMapper : new ObjectMapper(); var mapper = this.objectMapper != null ? this.objectMapper : new ObjectMapper();
var asyncServer = new McpAsyncServer(this.transportProvider, mapper, asyncFeatures); var asyncServer = new McpAsyncServer(this.transportProvider, mapper, asyncFeatures);

View file

@ -35,12 +35,14 @@ public class McpServerFeatures {
* @param prompts The map of prompt specifications * @param prompts The map of prompt specifications
* @param rootsChangeConsumers The list of consumers that will be notified when the * @param rootsChangeConsumers The list of consumers that will be notified when the
* roots list changes * roots list changes
* @param instructions The server instructions text
*/ */
record Async(McpSchema.Implementation serverInfo, McpSchema.ServerCapabilities serverCapabilities, record Async(McpSchema.Implementation serverInfo, McpSchema.ServerCapabilities serverCapabilities,
List<McpServerFeatures.AsyncToolSpecification> tools, Map<String, AsyncResourceSpecification> resources, List<McpServerFeatures.AsyncToolSpecification> tools, Map<String, AsyncResourceSpecification> resources,
List<McpSchema.ResourceTemplate> resourceTemplates, List<McpSchema.ResourceTemplate> resourceTemplates,
Map<String, McpServerFeatures.AsyncPromptSpecification> prompts, Map<String, McpServerFeatures.AsyncPromptSpecification> prompts,
List<BiFunction<McpAsyncServerExchange, List<McpSchema.Root>, Mono<Void>>> rootsChangeConsumers) { List<BiFunction<McpAsyncServerExchange, List<McpSchema.Root>, Mono<Void>>> rootsChangeConsumers,
String instructions) {
/** /**
* Create an instance and validate the arguments. * Create an instance and validate the arguments.
@ -52,12 +54,14 @@ public class McpServerFeatures {
* @param prompts The map of prompt specifications * @param prompts The map of prompt specifications
* @param rootsChangeConsumers The list of consumers that will be notified when * @param rootsChangeConsumers The list of consumers that will be notified when
* the roots list changes * the roots list changes
* @param instructions The server instructions text
*/ */
Async(McpSchema.Implementation serverInfo, McpSchema.ServerCapabilities serverCapabilities, Async(McpSchema.Implementation serverInfo, McpSchema.ServerCapabilities serverCapabilities,
List<McpServerFeatures.AsyncToolSpecification> tools, Map<String, AsyncResourceSpecification> resources, List<McpServerFeatures.AsyncToolSpecification> tools, Map<String, AsyncResourceSpecification> resources,
List<McpSchema.ResourceTemplate> resourceTemplates, List<McpSchema.ResourceTemplate> resourceTemplates,
Map<String, McpServerFeatures.AsyncPromptSpecification> prompts, Map<String, McpServerFeatures.AsyncPromptSpecification> prompts,
List<BiFunction<McpAsyncServerExchange, List<McpSchema.Root>, Mono<Void>>> rootsChangeConsumers) { List<BiFunction<McpAsyncServerExchange, List<McpSchema.Root>, Mono<Void>>> rootsChangeConsumers,
String instructions) {
Assert.notNull(serverInfo, "Server info must not be null"); Assert.notNull(serverInfo, "Server info must not be null");
@ -78,6 +82,7 @@ public class McpServerFeatures {
this.resourceTemplates = (resourceTemplates != null) ? resourceTemplates : List.of(); this.resourceTemplates = (resourceTemplates != null) ? resourceTemplates : List.of();
this.prompts = (prompts != null) ? prompts : Map.of(); this.prompts = (prompts != null) ? prompts : Map.of();
this.rootsChangeConsumers = (rootsChangeConsumers != null) ? rootsChangeConsumers : List.of(); this.rootsChangeConsumers = (rootsChangeConsumers != null) ? rootsChangeConsumers : List.of();
this.instructions = instructions;
} }
/** /**
@ -113,7 +118,7 @@ public class McpServerFeatures {
} }
return new Async(syncSpec.serverInfo(), syncSpec.serverCapabilities(), tools, resources, return new Async(syncSpec.serverInfo(), syncSpec.serverCapabilities(), tools, resources,
syncSpec.resourceTemplates(), prompts, rootChangeConsumers); syncSpec.resourceTemplates(), prompts, rootChangeConsumers, syncSpec.instructions());
} }
} }
@ -128,13 +133,14 @@ public class McpServerFeatures {
* @param prompts The map of prompt specifications * @param prompts The map of prompt specifications
* @param rootsChangeConsumers The list of consumers that will be notified when the * @param rootsChangeConsumers The list of consumers that will be notified when the
* roots list changes * roots list changes
* @param instructions The server instructions text
*/ */
record Sync(McpSchema.Implementation serverInfo, McpSchema.ServerCapabilities serverCapabilities, record Sync(McpSchema.Implementation serverInfo, McpSchema.ServerCapabilities serverCapabilities,
List<McpServerFeatures.SyncToolSpecification> tools, List<McpServerFeatures.SyncToolSpecification> tools,
Map<String, McpServerFeatures.SyncResourceSpecification> resources, Map<String, McpServerFeatures.SyncResourceSpecification> resources,
List<McpSchema.ResourceTemplate> resourceTemplates, List<McpSchema.ResourceTemplate> resourceTemplates,
Map<String, McpServerFeatures.SyncPromptSpecification> prompts, Map<String, McpServerFeatures.SyncPromptSpecification> prompts,
List<BiConsumer<McpSyncServerExchange, List<McpSchema.Root>>> rootsChangeConsumers) { List<BiConsumer<McpSyncServerExchange, List<McpSchema.Root>>> rootsChangeConsumers, String instructions) {
/** /**
* Create an instance and validate the arguments. * Create an instance and validate the arguments.
@ -146,13 +152,15 @@ public class McpServerFeatures {
* @param prompts The map of prompt specifications * @param prompts The map of prompt specifications
* @param rootsChangeConsumers The list of consumers that will be notified when * @param rootsChangeConsumers The list of consumers that will be notified when
* the roots list changes * the roots list changes
* @param instructions The server instructions text
*/ */
Sync(McpSchema.Implementation serverInfo, McpSchema.ServerCapabilities serverCapabilities, Sync(McpSchema.Implementation serverInfo, McpSchema.ServerCapabilities serverCapabilities,
List<McpServerFeatures.SyncToolSpecification> tools, List<McpServerFeatures.SyncToolSpecification> tools,
Map<String, McpServerFeatures.SyncResourceSpecification> resources, Map<String, McpServerFeatures.SyncResourceSpecification> resources,
List<McpSchema.ResourceTemplate> resourceTemplates, List<McpSchema.ResourceTemplate> resourceTemplates,
Map<String, McpServerFeatures.SyncPromptSpecification> prompts, Map<String, McpServerFeatures.SyncPromptSpecification> prompts,
List<BiConsumer<McpSyncServerExchange, List<McpSchema.Root>>> rootsChangeConsumers) { List<BiConsumer<McpSyncServerExchange, List<McpSchema.Root>>> rootsChangeConsumers,
String instructions) {
Assert.notNull(serverInfo, "Server info must not be null"); Assert.notNull(serverInfo, "Server info must not be null");
@ -173,6 +181,7 @@ public class McpServerFeatures {
this.resourceTemplates = (resourceTemplates != null) ? resourceTemplates : new ArrayList<>(); this.resourceTemplates = (resourceTemplates != null) ? resourceTemplates : new ArrayList<>();
this.prompts = (prompts != null) ? prompts : new HashMap<>(); this.prompts = (prompts != null) ? prompts : new HashMap<>();
this.rootsChangeConsumers = (rootsChangeConsumers != null) ? rootsChangeConsumers : new ArrayList<>(); this.rootsChangeConsumers = (rootsChangeConsumers != null) ? rootsChangeConsumers : new ArrayList<>();
this.instructions = instructions;
} }
} }