Skip to content

Commit 5617302

Browse files
committed
feat: Add customizable URI template manager factory to MCP server
Adds the ability to customize the URI template manager factory in both synchronous and asynchronous MCP servers. - Adding a configurable uriTemplateManagerFactory field to both AsyncSpecification and SyncSpecification classes - Adding builder methods to allow setting a custom URI template manager factory - Modifying constructors to pass the URI template manager factory to the server implementation - Updating the server implementation to use the provided factory Signed-off-by: Christian Tzolov <christian.tzolov@broadcom.com>
1 parent 780f5dd commit 5617302

File tree

2 files changed

+44
-6
lines changed

2 files changed

+44
-6
lines changed

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

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -96,8 +96,10 @@ public class McpAsyncServer {
9696
* @param objectMapper The ObjectMapper to use for JSON serialization/deserialization
9797
*/
9898
McpAsyncServer(McpServerTransportProvider mcpTransportProvider, ObjectMapper objectMapper,
99-
McpServerFeatures.Async features, Duration requestTimeout) {
100-
this.delegate = new AsyncServerImpl(mcpTransportProvider, objectMapper, requestTimeout, features);
99+
McpServerFeatures.Async features, Duration requestTimeout,
100+
McpUriTemplateManagerFactory uriTemplateManagerFactory) {
101+
this.delegate = new AsyncServerImpl(mcpTransportProvider, objectMapper, requestTimeout, features,
102+
uriTemplateManagerFactory);
101103
}
102104

103105
/**
@@ -278,10 +280,11 @@ private static class AsyncServerImpl extends McpAsyncServer {
278280

279281
private List<String> protocolVersions = List.of(McpSchema.LATEST_PROTOCOL_VERSION);
280282

281-
private final McpUriTemplateManagerFactory uriTemplateManagerFactory = new DeafaultMcpUriTemplateManagerFactory();
283+
private McpUriTemplateManagerFactory uriTemplateManagerFactory = new DeafaultMcpUriTemplateManagerFactory();
282284

283285
AsyncServerImpl(McpServerTransportProvider mcpTransportProvider, ObjectMapper objectMapper,
284-
Duration requestTimeout, McpServerFeatures.Async features) {
286+
Duration requestTimeout, McpServerFeatures.Async features,
287+
McpUriTemplateManagerFactory uriTemplateManagerFactory) {
285288
this.mcpTransportProvider = mcpTransportProvider;
286289
this.objectMapper = objectMapper;
287290
this.serverInfo = features.serverInfo();
@@ -292,6 +295,7 @@ private static class AsyncServerImpl extends McpAsyncServer {
292295
this.resourceTemplates.addAll(features.resourceTemplates());
293296
this.prompts.putAll(features.prompts());
294297
this.completions.putAll(features.completions());
298+
this.uriTemplateManagerFactory = uriTemplateManagerFactory;
295299

296300
Map<String, McpServerSession.RequestHandler<?>> requestHandlers = new HashMap<>();
297301

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

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919
import io.modelcontextprotocol.spec.McpSchema.ResourceTemplate;
2020
import io.modelcontextprotocol.spec.McpServerTransportProvider;
2121
import io.modelcontextprotocol.util.Assert;
22+
import io.modelcontextprotocol.util.DeafaultMcpUriTemplateManagerFactory;
23+
import io.modelcontextprotocol.util.McpUriTemplateManagerFactory;
2224
import reactor.core.publisher.Mono;
2325

2426
/**
@@ -156,6 +158,8 @@ class AsyncSpecification {
156158

157159
private final McpServerTransportProvider transportProvider;
158160

161+
private McpUriTemplateManagerFactory uriTemplateManagerFactory = new DeafaultMcpUriTemplateManagerFactory();
162+
159163
private ObjectMapper objectMapper;
160164

161165
private McpSchema.Implementation serverInfo = DEFAULT_SERVER_INFO;
@@ -204,6 +208,19 @@ private AsyncSpecification(McpServerTransportProvider transportProvider) {
204208
this.transportProvider = transportProvider;
205209
}
206210

211+
/**
212+
* Sets the URI template manager factory to use for creating URI templates. This
213+
* allows for custom URI template parsing and variable extraction.
214+
* @param uriTemplateManagerFactory The factory to use. Must not be null.
215+
* @return This builder instance for method chaining
216+
* @throws IllegalArgumentException if uriTemplateManagerFactory is null
217+
*/
218+
public AsyncSpecification uriTemplateManagerFactory(McpUriTemplateManagerFactory uriTemplateManagerFactory) {
219+
Assert.notNull(uriTemplateManagerFactory, "URI template manager factory must not be null");
220+
this.uriTemplateManagerFactory = uriTemplateManagerFactory;
221+
return this;
222+
}
223+
207224
/**
208225
* Sets the duration to wait for server responses before timing out requests. This
209226
* timeout applies to all requests made through the client, including tool calls,
@@ -617,7 +634,8 @@ public McpAsyncServer build() {
617634
this.resources, this.resourceTemplates, this.prompts, this.completions, this.rootsChangeHandlers,
618635
this.instructions);
619636
var mapper = this.objectMapper != null ? this.objectMapper : new ObjectMapper();
620-
return new McpAsyncServer(this.transportProvider, mapper, features, this.requestTimeout);
637+
return new McpAsyncServer(this.transportProvider, mapper, features, this.requestTimeout,
638+
this.uriTemplateManagerFactory);
621639
}
622640

623641
}
@@ -630,6 +648,8 @@ class SyncSpecification {
630648
private static final McpSchema.Implementation DEFAULT_SERVER_INFO = new McpSchema.Implementation("mcp-server",
631649
"1.0.0");
632650

651+
private McpUriTemplateManagerFactory uriTemplateManagerFactory = new DeafaultMcpUriTemplateManagerFactory();
652+
633653
private final McpServerTransportProvider transportProvider;
634654

635655
private ObjectMapper objectMapper;
@@ -680,6 +700,19 @@ private SyncSpecification(McpServerTransportProvider transportProvider) {
680700
this.transportProvider = transportProvider;
681701
}
682702

703+
/**
704+
* Sets the URI template manager factory to use for creating URI templates. This
705+
* allows for custom URI template parsing and variable extraction.
706+
* @param uriTemplateManagerFactory The factory to use. Must not be null.
707+
* @return This builder instance for method chaining
708+
* @throws IllegalArgumentException if uriTemplateManagerFactory is null
709+
*/
710+
public SyncSpecification uriTemplateManagerFactory(McpUriTemplateManagerFactory uriTemplateManagerFactory) {
711+
Assert.notNull(uriTemplateManagerFactory, "URI template manager factory must not be null");
712+
this.uriTemplateManagerFactory = uriTemplateManagerFactory;
713+
return this;
714+
}
715+
683716
/**
684717
* Sets the duration to wait for server responses before timing out requests. This
685718
* timeout applies to all requests made through the client, including tool calls,
@@ -1094,7 +1127,8 @@ public McpSyncServer build() {
10941127
this.rootsChangeHandlers, this.instructions);
10951128
McpServerFeatures.Async asyncFeatures = McpServerFeatures.Async.fromSync(syncFeatures);
10961129
var mapper = this.objectMapper != null ? this.objectMapper : new ObjectMapper();
1097-
var asyncServer = new McpAsyncServer(this.transportProvider, mapper, asyncFeatures, this.requestTimeout);
1130+
var asyncServer = new McpAsyncServer(this.transportProvider, mapper, asyncFeatures, this.requestTimeout,
1131+
this.uriTemplateManagerFactory);
10981132

10991133
return new McpSyncServer(asyncServer);
11001134
}

0 commit comments

Comments
 (0)