diff --git a/mcp/src/main/java/io/modelcontextprotocol/server/McpAsyncServer.java b/mcp/src/main/java/io/modelcontextprotocol/server/McpAsyncServer.java index 188b0f48..df938668 100644 --- a/mcp/src/main/java/io/modelcontextprotocol/server/McpAsyncServer.java +++ b/mcp/src/main/java/io/modelcontextprotocol/server/McpAsyncServer.java @@ -247,6 +247,8 @@ private static class AsyncServerImpl extends McpAsyncServer { private final McpSchema.Implementation serverInfo; + private final String instructions; + private final CopyOnWriteArrayList tools = new CopyOnWriteArrayList<>(); private final CopyOnWriteArrayList resourceTemplates = new CopyOnWriteArrayList<>(); @@ -265,6 +267,7 @@ private static class AsyncServerImpl extends McpAsyncServer { this.objectMapper = objectMapper; this.serverInfo = features.serverInfo(); this.serverCapabilities = features.serverCapabilities(); + this.instructions = features.instructions(); this.tools.addAll(features.tools()); this.resources.putAll(features.resources()); this.resourceTemplates.addAll(features.resourceTemplates()); @@ -351,7 +354,7 @@ private Mono asyncInitializeRequestHandler( } return Mono.just(new McpSchema.InitializeResult(serverProtocolVersion, this.serverCapabilities, - this.serverInfo, null)); + this.serverInfo, this.instructions)); }); } diff --git a/mcp/src/main/java/io/modelcontextprotocol/server/McpServer.java b/mcp/src/main/java/io/modelcontextprotocol/server/McpServer.java index 091efac2..d5427335 100644 --- a/mcp/src/main/java/io/modelcontextprotocol/server/McpServer.java +++ b/mcp/src/main/java/io/modelcontextprotocol/server/McpServer.java @@ -160,6 +160,8 @@ class AsyncSpecification { private McpSchema.ServerCapabilities serverCapabilities; + private String instructions; + /** * The Model Context Protocol (MCP) allows servers to expose tools that can be * invoked by language models. Tools enable models to interact with external @@ -228,6 +230,18 @@ public AsyncSpecification serverInfo(String name, String version) { 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 * connection initialization. Capabilities define what features the server @@ -549,7 +563,7 @@ public AsyncSpecification objectMapper(ObjectMapper objectMapper) { */ public McpAsyncServer build() { 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(); return new McpAsyncServer(this.transportProvider, mapper, features); } @@ -572,6 +586,8 @@ class SyncSpecification { private McpSchema.ServerCapabilities serverCapabilities; + private String instructions; + /** * The Model Context Protocol (MCP) allows servers to expose tools that can be * invoked by language models. Tools enable models to interact with external @@ -640,6 +656,18 @@ public SyncSpecification serverInfo(String name, String version) { 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 * connection initialization. Capabilities define what features the server @@ -960,7 +988,8 @@ public SyncSpecification objectMapper(ObjectMapper objectMapper) { */ public McpSyncServer build() { 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); var mapper = this.objectMapper != null ? this.objectMapper : new ObjectMapper(); var asyncServer = new McpAsyncServer(this.transportProvider, mapper, asyncFeatures); diff --git a/mcp/src/main/java/io/modelcontextprotocol/server/McpServerFeatures.java b/mcp/src/main/java/io/modelcontextprotocol/server/McpServerFeatures.java index 8c110027..e0f337b7 100644 --- a/mcp/src/main/java/io/modelcontextprotocol/server/McpServerFeatures.java +++ b/mcp/src/main/java/io/modelcontextprotocol/server/McpServerFeatures.java @@ -35,12 +35,14 @@ public class McpServerFeatures { * @param prompts The map of prompt specifications * @param rootsChangeConsumers The list of consumers that will be notified when the * roots list changes + * @param instructions The server instructions text */ record Async(McpSchema.Implementation serverInfo, McpSchema.ServerCapabilities serverCapabilities, List tools, Map resources, List resourceTemplates, Map prompts, - List, Mono>> rootsChangeConsumers) { + List, Mono>> rootsChangeConsumers, + String instructions) { /** * Create an instance and validate the arguments. @@ -52,12 +54,14 @@ record Async(McpSchema.Implementation serverInfo, McpSchema.ServerCapabilities s * @param prompts The map of prompt specifications * @param rootsChangeConsumers The list of consumers that will be notified when * the roots list changes + * @param instructions The server instructions text */ Async(McpSchema.Implementation serverInfo, McpSchema.ServerCapabilities serverCapabilities, List tools, Map resources, List resourceTemplates, Map prompts, - List, Mono>> rootsChangeConsumers) { + List, Mono>> rootsChangeConsumers, + String instructions) { Assert.notNull(serverInfo, "Server info must not be null"); @@ -78,6 +82,7 @@ record Async(McpSchema.Implementation serverInfo, McpSchema.ServerCapabilities s this.resourceTemplates = (resourceTemplates != null) ? resourceTemplates : List.of(); this.prompts = (prompts != null) ? prompts : Map.of(); this.rootsChangeConsumers = (rootsChangeConsumers != null) ? rootsChangeConsumers : List.of(); + this.instructions = instructions; } /** @@ -113,7 +118,7 @@ static Async fromSync(Sync syncSpec) { } return new Async(syncSpec.serverInfo(), syncSpec.serverCapabilities(), tools, resources, - syncSpec.resourceTemplates(), prompts, rootChangeConsumers); + syncSpec.resourceTemplates(), prompts, rootChangeConsumers, syncSpec.instructions()); } } @@ -128,13 +133,14 @@ static Async fromSync(Sync syncSpec) { * @param prompts The map of prompt specifications * @param rootsChangeConsumers The list of consumers that will be notified when the * roots list changes + * @param instructions The server instructions text */ record Sync(McpSchema.Implementation serverInfo, McpSchema.ServerCapabilities serverCapabilities, List tools, Map resources, List resourceTemplates, Map prompts, - List>> rootsChangeConsumers) { + List>> rootsChangeConsumers, String instructions) { /** * Create an instance and validate the arguments. @@ -146,13 +152,15 @@ record Sync(McpSchema.Implementation serverInfo, McpSchema.ServerCapabilities se * @param prompts The map of prompt specifications * @param rootsChangeConsumers The list of consumers that will be notified when * the roots list changes + * @param instructions The server instructions text */ Sync(McpSchema.Implementation serverInfo, McpSchema.ServerCapabilities serverCapabilities, List tools, Map resources, List resourceTemplates, Map prompts, - List>> rootsChangeConsumers) { + List>> rootsChangeConsumers, + String instructions) { Assert.notNull(serverInfo, "Server info must not be null"); @@ -173,6 +181,7 @@ record Sync(McpSchema.Implementation serverInfo, McpSchema.ServerCapabilities se this.resourceTemplates = (resourceTemplates != null) ? resourceTemplates : new ArrayList<>(); this.prompts = (prompts != null) ? prompts : new HashMap<>(); this.rootsChangeConsumers = (rootsChangeConsumers != null) ? rootsChangeConsumers : new ArrayList<>(); + this.instructions = instructions; } }