Skip to content

Commit 898e8d5

Browse files
committed
fix: add support to set instructions as mentioned in modelcontextprotocol#98
1 parent 79ec5b5 commit 898e8d5

File tree

3 files changed

+49
-8
lines changed

3 files changed

+49
-8
lines changed

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

+4-1
Original file line numberDiff line numberDiff line change
@@ -247,6 +247,8 @@ private static class AsyncServerImpl extends McpAsyncServer {
247247

248248
private final McpSchema.Implementation serverInfo;
249249

250+
private final String instructions;
251+
250252
private final CopyOnWriteArrayList<McpServerFeatures.AsyncToolSpecification> tools = new CopyOnWriteArrayList<>();
251253

252254
private final CopyOnWriteArrayList<McpSchema.ResourceTemplate> resourceTemplates = new CopyOnWriteArrayList<>();
@@ -265,6 +267,7 @@ private static class AsyncServerImpl extends McpAsyncServer {
265267
this.objectMapper = objectMapper;
266268
this.serverInfo = features.serverInfo();
267269
this.serverCapabilities = features.serverCapabilities();
270+
this.instructions = features.instructions();
268271
this.tools.addAll(features.tools());
269272
this.resources.putAll(features.resources());
270273
this.resourceTemplates.addAll(features.resourceTemplates());
@@ -351,7 +354,7 @@ private Mono<McpSchema.InitializeResult> asyncInitializeRequestHandler(
351354
}
352355

353356
return Mono.just(new McpSchema.InitializeResult(serverProtocolVersion, this.serverCapabilities,
354-
this.serverInfo, null));
357+
this.serverInfo, this.instructions));
355358
});
356359
}
357360

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

+31-2
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,8 @@ class AsyncSpecification {
160160

161161
private McpSchema.ServerCapabilities serverCapabilities;
162162

163+
private String instructions;
164+
163165
/**
164166
* The Model Context Protocol (MCP) allows servers to expose tools that can be
165167
* invoked by language models. Tools enable models to interact with external
@@ -228,6 +230,18 @@ public AsyncSpecification serverInfo(String name, String version) {
228230
return this;
229231
}
230232

233+
/**
234+
* Sets the server instructions that will be shared with clients during connection
235+
* initialization. These instructions provide guidance to the client on how to
236+
* interact with this server.
237+
* @param instructions The instructions text. Can be null or empty.
238+
* @return This builder instance for method chaining
239+
*/
240+
public AsyncSpecification instructions(String instructions) {
241+
this.instructions = instructions;
242+
return this;
243+
}
244+
231245
/**
232246
* Sets the server capabilities that will be advertised to clients during
233247
* connection initialization. Capabilities define what features the server
@@ -549,7 +563,7 @@ public AsyncSpecification objectMapper(ObjectMapper objectMapper) {
549563
*/
550564
public McpAsyncServer build() {
551565
var features = new McpServerFeatures.Async(this.serverInfo, this.serverCapabilities, this.tools,
552-
this.resources, this.resourceTemplates, this.prompts, this.rootsChangeHandlers);
566+
this.resources, this.resourceTemplates, this.prompts, this.rootsChangeHandlers, this.instructions);
553567
var mapper = this.objectMapper != null ? this.objectMapper : new ObjectMapper();
554568
return new McpAsyncServer(this.transportProvider, mapper, features);
555569
}
@@ -572,6 +586,8 @@ class SyncSpecification {
572586

573587
private McpSchema.ServerCapabilities serverCapabilities;
574588

589+
private String instructions;
590+
575591
/**
576592
* The Model Context Protocol (MCP) allows servers to expose tools that can be
577593
* invoked by language models. Tools enable models to interact with external
@@ -640,6 +656,18 @@ public SyncSpecification serverInfo(String name, String version) {
640656
return this;
641657
}
642658

659+
/**
660+
* Sets the server instructions that will be shared with clients during connection
661+
* initialization. These instructions provide guidance to the client on how to
662+
* interact with this server.
663+
* @param instructions The instructions text. Can be null or empty.
664+
* @return This builder instance for method chaining
665+
*/
666+
public SyncSpecification instructions(String instructions) {
667+
this.instructions = instructions;
668+
return this;
669+
}
670+
643671
/**
644672
* Sets the server capabilities that will be advertised to clients during
645673
* connection initialization. Capabilities define what features the server
@@ -960,7 +988,8 @@ public SyncSpecification objectMapper(ObjectMapper objectMapper) {
960988
*/
961989
public McpSyncServer build() {
962990
McpServerFeatures.Sync syncFeatures = new McpServerFeatures.Sync(this.serverInfo, this.serverCapabilities,
963-
this.tools, this.resources, this.resourceTemplates, this.prompts, this.rootsChangeHandlers);
991+
this.tools, this.resources, this.resourceTemplates, this.prompts, this.rootsChangeHandlers,
992+
this.instructions);
964993
McpServerFeatures.Async asyncFeatures = McpServerFeatures.Async.fromSync(syncFeatures);
965994
var mapper = this.objectMapper != null ? this.objectMapper : new ObjectMapper();
966995
var asyncServer = new McpAsyncServer(this.transportProvider, mapper, asyncFeatures);

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

+14-5
Original file line numberDiff line numberDiff line change
@@ -35,12 +35,14 @@ public class McpServerFeatures {
3535
* @param prompts The map of prompt specifications
3636
* @param rootsChangeConsumers The list of consumers that will be notified when the
3737
* roots list changes
38+
* @param instructions The server instructions text
3839
*/
3940
record Async(McpSchema.Implementation serverInfo, McpSchema.ServerCapabilities serverCapabilities,
4041
List<McpServerFeatures.AsyncToolSpecification> tools, Map<String, AsyncResourceSpecification> resources,
4142
List<McpSchema.ResourceTemplate> resourceTemplates,
4243
Map<String, McpServerFeatures.AsyncPromptSpecification> prompts,
43-
List<BiFunction<McpAsyncServerExchange, List<McpSchema.Root>, Mono<Void>>> rootsChangeConsumers) {
44+
List<BiFunction<McpAsyncServerExchange, List<McpSchema.Root>, Mono<Void>>> rootsChangeConsumers,
45+
String instructions) {
4446

4547
/**
4648
* Create an instance and validate the arguments.
@@ -52,12 +54,14 @@ record Async(McpSchema.Implementation serverInfo, McpSchema.ServerCapabilities s
5254
* @param prompts The map of prompt specifications
5355
* @param rootsChangeConsumers The list of consumers that will be notified when
5456
* the roots list changes
57+
* @param instructions The server instructions text
5558
*/
5659
Async(McpSchema.Implementation serverInfo, McpSchema.ServerCapabilities serverCapabilities,
5760
List<McpServerFeatures.AsyncToolSpecification> tools, Map<String, AsyncResourceSpecification> resources,
5861
List<McpSchema.ResourceTemplate> resourceTemplates,
5962
Map<String, McpServerFeatures.AsyncPromptSpecification> prompts,
60-
List<BiFunction<McpAsyncServerExchange, List<McpSchema.Root>, Mono<Void>>> rootsChangeConsumers) {
63+
List<BiFunction<McpAsyncServerExchange, List<McpSchema.Root>, Mono<Void>>> rootsChangeConsumers,
64+
String instructions) {
6165

6266
Assert.notNull(serverInfo, "Server info must not be null");
6367

@@ -78,6 +82,7 @@ record Async(McpSchema.Implementation serverInfo, McpSchema.ServerCapabilities s
7882
this.resourceTemplates = (resourceTemplates != null) ? resourceTemplates : List.of();
7983
this.prompts = (prompts != null) ? prompts : Map.of();
8084
this.rootsChangeConsumers = (rootsChangeConsumers != null) ? rootsChangeConsumers : List.of();
85+
this.instructions = instructions;
8186
}
8287

8388
/**
@@ -113,7 +118,7 @@ static Async fromSync(Sync syncSpec) {
113118
}
114119

115120
return new Async(syncSpec.serverInfo(), syncSpec.serverCapabilities(), tools, resources,
116-
syncSpec.resourceTemplates(), prompts, rootChangeConsumers);
121+
syncSpec.resourceTemplates(), prompts, rootChangeConsumers, syncSpec.instructions());
117122
}
118123
}
119124

@@ -128,13 +133,14 @@ static Async fromSync(Sync syncSpec) {
128133
* @param prompts The map of prompt specifications
129134
* @param rootsChangeConsumers The list of consumers that will be notified when the
130135
* roots list changes
136+
* @param instructions The server instructions text
131137
*/
132138
record Sync(McpSchema.Implementation serverInfo, McpSchema.ServerCapabilities serverCapabilities,
133139
List<McpServerFeatures.SyncToolSpecification> tools,
134140
Map<String, McpServerFeatures.SyncResourceSpecification> resources,
135141
List<McpSchema.ResourceTemplate> resourceTemplates,
136142
Map<String, McpServerFeatures.SyncPromptSpecification> prompts,
137-
List<BiConsumer<McpSyncServerExchange, List<McpSchema.Root>>> rootsChangeConsumers) {
143+
List<BiConsumer<McpSyncServerExchange, List<McpSchema.Root>>> rootsChangeConsumers, String instructions) {
138144

139145
/**
140146
* Create an instance and validate the arguments.
@@ -146,13 +152,15 @@ record Sync(McpSchema.Implementation serverInfo, McpSchema.ServerCapabilities se
146152
* @param prompts The map of prompt specifications
147153
* @param rootsChangeConsumers The list of consumers that will be notified when
148154
* the roots list changes
155+
* @param instructions The server instructions text
149156
*/
150157
Sync(McpSchema.Implementation serverInfo, McpSchema.ServerCapabilities serverCapabilities,
151158
List<McpServerFeatures.SyncToolSpecification> tools,
152159
Map<String, McpServerFeatures.SyncResourceSpecification> resources,
153160
List<McpSchema.ResourceTemplate> resourceTemplates,
154161
Map<String, McpServerFeatures.SyncPromptSpecification> prompts,
155-
List<BiConsumer<McpSyncServerExchange, List<McpSchema.Root>>> rootsChangeConsumers) {
162+
List<BiConsumer<McpSyncServerExchange, List<McpSchema.Root>>> rootsChangeConsumers,
163+
String instructions) {
156164

157165
Assert.notNull(serverInfo, "Server info must not be null");
158166

@@ -173,6 +181,7 @@ record Sync(McpSchema.Implementation serverInfo, McpSchema.ServerCapabilities se
173181
this.resourceTemplates = (resourceTemplates != null) ? resourceTemplates : new ArrayList<>();
174182
this.prompts = (prompts != null) ? prompts : new HashMap<>();
175183
this.rootsChangeConsumers = (rootsChangeConsumers != null) ? rootsChangeConsumers : new ArrayList<>();
184+
this.instructions = instructions;
176185
}
177186

178187
}

0 commit comments

Comments
 (0)