Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -492,12 +492,9 @@ public McpSyncClient build() {

McpClientFeatures.Async asyncFeatures = McpClientFeatures.Async.fromSync(syncFeatures);

return new McpSyncClient(
new McpAsyncClient(transport, this.requestTimeout, this.initializationTimeout,
jsonSchemaValidator != null ? jsonSchemaValidator
: McpJsonDefaults.getDefaultJsonSchemaValidator(),
asyncFeatures),
this.contextProvider);
return new McpSyncClient(new McpAsyncClient(transport, this.requestTimeout, this.initializationTimeout,
jsonSchemaValidator != null ? jsonSchemaValidator : McpJsonDefaults.getSchemaValidator(),
asyncFeatures), this.contextProvider);
}

}
Expand Down Expand Up @@ -830,7 +827,7 @@ public AsyncSpec enableCallToolSchemaCaching(boolean enableCallToolSchemaCaching
*/
public McpAsyncClient build() {
var jsonSchemaValidator = (this.jsonSchemaValidator != null) ? this.jsonSchemaValidator
: McpJsonDefaults.getDefaultJsonSchemaValidator();
: McpJsonDefaults.getSchemaValidator();
return new McpAsyncClient(this.transport, this.requestTimeout, this.initializationTimeout,
jsonSchemaValidator,
new McpClientFeatures.Async(this.clientInfo, this.capabilities, this.roots,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -328,7 +328,7 @@ public Builder connectTimeout(Duration connectTimeout) {
public HttpClientSseClientTransport build() {
HttpClient httpClient = this.clientBuilder.connectTimeout(this.connectTimeout).build();
return new HttpClientSseClientTransport(httpClient, requestBuilder, baseUri, sseEndpoint,
jsonMapper == null ? McpJsonDefaults.getDefaultMcpJsonMapper() : jsonMapper, httpRequestCustomizer);
jsonMapper == null ? McpJsonDefaults.getMapper() : jsonMapper, httpRequestCustomizer);
}

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -843,10 +843,9 @@ public Builder supportedProtocolVersions(List<String> supportedProtocolVersions)
*/
public HttpClientStreamableHttpTransport build() {
HttpClient httpClient = this.clientBuilder.connectTimeout(this.connectTimeout).build();
return new HttpClientStreamableHttpTransport(
jsonMapper == null ? McpJsonDefaults.getDefaultMcpJsonMapper() : jsonMapper, httpClient,
requestBuilder, baseUri, endpoint, resumableStreams, openConnectionOnStartup, httpRequestCustomizer,
supportedProtocolVersions);
return new HttpClientStreamableHttpTransport(jsonMapper == null ? McpJsonDefaults.getMapper() : jsonMapper,
httpClient, requestBuilder, baseUri, endpoint, resumableStreams, openConnectionOnStartup,
httpRequestCustomizer, supportedProtocolVersions);
}

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,27 +8,29 @@
import io.modelcontextprotocol.util.McpServiceLoader;

/**
* This class is to be used to provide access to the default McpJsonMapper and to the
* default JsonSchemaValidator instances via the static methods: getDefaultMcpJsonMapper
* and getDefaultJsonSchemaValidator.
* This class is to be used to provide access to the default {@link McpJsonMapper} and to
* the default {@link JsonSchemaValidator} instances via the static methods:
* {@link #getMapper()} and {@link #getSchemaValidator()}.
* <p>
* The initialization of (singleton) instances of this class is different in non-OSGi
* environments and OSGi environments. Specifically, in non-OSGi environments The
* McpJsonDefaults class will be loaded by whatever classloader is used to call one of the
* existing static get methods for the first time. For servers, this will usually be in
* response to the creation of the first McpServer instance. At that first time, the
* mcpMapperServiceLoader and mcpValidatorServiceLoader will be null, and the
* McpJsonDefaults constructor will be called, creating/initializing the
* mcpMapperServiceLoader and the mcpValidatorServiceLoader...which will then be used to
* call the ServiceLoader.load method.
* environments and OSGi environments. Specifically, in non-OSGi environments the
* {@code McpJsonDefaults} class will be loaded by whatever classloader is used to call
* one of the existing static get methods for the first time. For servers, this will
* usually be in response to the creation of the first {@code McpServer} instance. At that
* first time, the {@code mcpMapperServiceLoader} and {@code mcpValidatorServiceLoader}
* will be null, and the {@code McpJsonDefaults} constructor will be called,
* creating/initializing the {@code mcpMapperServiceLoader} and the
* {@code mcpValidatorServiceLoader}...which will then be used to call the
* {@code ServiceLoader.load} method.
* <p>
* In OSGi environments, upon bundle activation SCR will create a new (singleton) instance
* of McpJsonDefaults (via the constructor), and then inject suppliers via the
* setMcpJsonMapperSupplier and setJsonSchemaValidatorSupplier methods with the
* SCR-discovered instances of those services. This does depend upon the jars/bundles
* providing those suppliers to be started/activated. This SCR behavior is dictated by xml
* files in OSGi-INF directory of mcp-core (this project/jar/bundle), and the jsonmapper
* and jsonschemvalidator provider jars/bundles (e.g. mcp-json-jackson2, 3, or others).
* of {@code McpJsonDefaults} (via the constructor), and then inject suppliers via the
* {@code setMcpJsonMapperSupplier} and {@code setJsonSchemaValidatorSupplier} methods
* with the SCR-discovered instances of those services. This does depend upon the
* jars/bundles providing those suppliers to be started/activated. This SCR behavior is
* dictated by xml files in {@code OSGi-INF} directory of {@code mcp-core} (this
* project/jar/bundle), and the jsonmapper and jsonschemavalidator provider jars/bundles
* (e.g. {@code mcp-json-jackson2}, {@code mcp-json-jackson3}, or others).
*/
public class McpJsonDefaults {

Expand All @@ -37,10 +39,8 @@ public class McpJsonDefaults {
protected static McpServiceLoader<JsonSchemaValidatorSupplier, JsonSchemaValidator> mcpValidatorServiceLoader;

public McpJsonDefaults() {
mcpMapperServiceLoader = new McpServiceLoader<McpJsonMapperSupplier, McpJsonMapper>(
McpJsonMapperSupplier.class);
mcpValidatorServiceLoader = new McpServiceLoader<JsonSchemaValidatorSupplier, JsonSchemaValidator>(
JsonSchemaValidatorSupplier.class);
mcpMapperServiceLoader = new McpServiceLoader<>(McpJsonMapperSupplier.class);
mcpValidatorServiceLoader = new McpServiceLoader<>(JsonSchemaValidatorSupplier.class);
}

void setMcpJsonMapperSupplier(McpJsonMapperSupplier supplier) {
Expand All @@ -51,7 +51,7 @@ void unsetMcpJsonMapperSupplier(McpJsonMapperSupplier supplier) {
mcpMapperServiceLoader.unsetSupplier(supplier);
}

public synchronized static McpJsonMapper getDefaultMcpJsonMapper() {
public synchronized static McpJsonMapper getMapper() {
if (mcpMapperServiceLoader == null) {
new McpJsonDefaults();
}
Expand All @@ -66,7 +66,7 @@ void unsetJsonSchemaValidatorSupplier(JsonSchemaValidatorSupplier supplier) {
mcpValidatorServiceLoader.unsetSupplier(supplier);
}

public synchronized static JsonSchemaValidator getDefaultJsonSchemaValidator() {
public synchronized static JsonSchemaValidator getSchemaValidator() {
if (mcpValidatorServiceLoader == null) {
new McpJsonDefaults();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -240,11 +240,10 @@ public McpAsyncServer build() {
this.instructions);

var jsonSchemaValidator = (this.jsonSchemaValidator != null) ? this.jsonSchemaValidator
: McpJsonDefaults.getDefaultJsonSchemaValidator();
: McpJsonDefaults.getSchemaValidator();

return new McpAsyncServer(transportProvider,
jsonMapper == null ? McpJsonDefaults.getDefaultMcpJsonMapper() : jsonMapper, features,
requestTimeout, uriTemplateManagerFactory, jsonSchemaValidator);
return new McpAsyncServer(transportProvider, jsonMapper == null ? McpJsonDefaults.getMapper() : jsonMapper,
features, requestTimeout, uriTemplateManagerFactory, jsonSchemaValidator);
}

}
Expand All @@ -268,10 +267,9 @@ public McpAsyncServer build() {
this.resources, this.resourceTemplates, this.prompts, this.completions, this.rootsChangeHandlers,
this.instructions);
var jsonSchemaValidator = this.jsonSchemaValidator != null ? this.jsonSchemaValidator
: McpJsonDefaults.getDefaultJsonSchemaValidator();
return new McpAsyncServer(transportProvider,
jsonMapper == null ? McpJsonDefaults.getDefaultMcpJsonMapper() : jsonMapper, features,
requestTimeout, uriTemplateManagerFactory, jsonSchemaValidator);
: McpJsonDefaults.getSchemaValidator();
return new McpAsyncServer(transportProvider, jsonMapper == null ? McpJsonDefaults.getMapper() : jsonMapper,
features, requestTimeout, uriTemplateManagerFactory, jsonSchemaValidator);
}

}
Expand Down Expand Up @@ -858,9 +856,9 @@ public McpSyncServer build() {
this.immediateExecution);

var asyncServer = new McpAsyncServer(transportProvider,
jsonMapper == null ? McpJsonDefaults.getDefaultMcpJsonMapper() : jsonMapper, asyncFeatures,
requestTimeout, uriTemplateManagerFactory, jsonSchemaValidator != null ? jsonSchemaValidator
: McpJsonDefaults.getDefaultJsonSchemaValidator());
jsonMapper == null ? McpJsonDefaults.getMapper() : jsonMapper, asyncFeatures, requestTimeout,
uriTemplateManagerFactory,
jsonSchemaValidator != null ? jsonSchemaValidator : McpJsonDefaults.getSchemaValidator());
return new McpSyncServer(asyncServer, this.immediateExecution);
}

Expand Down Expand Up @@ -888,10 +886,10 @@ public McpSyncServer build() {
McpServerFeatures.Async asyncFeatures = McpServerFeatures.Async.fromSync(syncFeatures,
this.immediateExecution);
var jsonSchemaValidator = this.jsonSchemaValidator != null ? this.jsonSchemaValidator
: McpJsonDefaults.getDefaultJsonSchemaValidator();
: McpJsonDefaults.getSchemaValidator();
var asyncServer = new McpAsyncServer(transportProvider,
jsonMapper == null ? McpJsonDefaults.getDefaultMcpJsonMapper() : jsonMapper, asyncFeatures,
this.requestTimeout, this.uriTemplateManagerFactory, jsonSchemaValidator);
jsonMapper == null ? McpJsonDefaults.getMapper() : jsonMapper, asyncFeatures, this.requestTimeout,
this.uriTemplateManagerFactory, jsonSchemaValidator);
return new McpSyncServer(asyncServer, this.immediateExecution);
}

Expand Down Expand Up @@ -1938,10 +1936,9 @@ public StatelessAsyncSpecification jsonSchemaValidator(JsonSchemaValidator jsonS
public McpStatelessAsyncServer build() {
var features = new McpStatelessServerFeatures.Async(this.serverInfo, this.serverCapabilities, this.tools,
this.resources, this.resourceTemplates, this.prompts, this.completions, this.instructions);
return new McpStatelessAsyncServer(transport,
jsonMapper == null ? McpJsonDefaults.getDefaultMcpJsonMapper() : jsonMapper, features,
requestTimeout, uriTemplateManagerFactory, jsonSchemaValidator != null ? jsonSchemaValidator
: McpJsonDefaults.getDefaultJsonSchemaValidator());
return new McpStatelessAsyncServer(transport, jsonMapper == null ? McpJsonDefaults.getMapper() : jsonMapper,
features, requestTimeout, uriTemplateManagerFactory,
jsonSchemaValidator != null ? jsonSchemaValidator : McpJsonDefaults.getSchemaValidator());
}

}
Expand Down Expand Up @@ -2440,9 +2437,9 @@ public McpStatelessSyncServer build() {
this.resources, this.resourceTemplates, this.prompts, this.completions, this.instructions);
var asyncFeatures = McpStatelessServerFeatures.Async.fromSync(syncFeatures, this.immediateExecution);
var asyncServer = new McpStatelessAsyncServer(transport,
jsonMapper == null ? McpJsonDefaults.getDefaultMcpJsonMapper() : jsonMapper, asyncFeatures,
requestTimeout, uriTemplateManagerFactory, this.jsonSchemaValidator != null
? this.jsonSchemaValidator : McpJsonDefaults.getDefaultJsonSchemaValidator());
jsonMapper == null ? McpJsonDefaults.getMapper() : jsonMapper, asyncFeatures, requestTimeout,
uriTemplateManagerFactory,
this.jsonSchemaValidator != null ? this.jsonSchemaValidator : McpJsonDefaults.getSchemaValidator());
return new McpStatelessSyncServer(asyncServer, this.immediateExecution);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -672,8 +672,8 @@ public HttpServletSseServerTransportProvider build() {
throw new IllegalStateException("MessageEndpoint must be set");
}
return new HttpServletSseServerTransportProvider(
jsonMapper == null ? McpJsonDefaults.getDefaultMcpJsonMapper() : jsonMapper, baseUrl,
messageEndpoint, sseEndpoint, keepAliveInterval, contextExtractor, securityValidator);
jsonMapper == null ? McpJsonDefaults.getMapper() : jsonMapper, baseUrl, messageEndpoint,
sseEndpoint, keepAliveInterval, contextExtractor, securityValidator);
}

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -331,8 +331,8 @@ public Builder securityValidator(ServerTransportSecurityValidator securityValida
public HttpServletStatelessServerTransport build() {
Assert.notNull(mcpEndpoint, "Message endpoint must be set");
return new HttpServletStatelessServerTransport(
jsonMapper == null ? McpJsonDefaults.getDefaultMcpJsonMapper() : jsonMapper, mcpEndpoint,
contextExtractor, securityValidator);
jsonMapper == null ? McpJsonDefaults.getMapper() : jsonMapper, mcpEndpoint, contextExtractor,
securityValidator);
}

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -895,8 +895,8 @@ public Builder securityValidator(ServerTransportSecurityValidator securityValida
public HttpServletStreamableServerTransportProvider build() {
Assert.notNull(this.mcpEndpoint, "MCP endpoint must be set");
return new HttpServletStreamableServerTransportProvider(
jsonMapper == null ? McpJsonDefaults.getDefaultMcpJsonMapper() : jsonMapper, mcpEndpoint,
disallowDelete, contextExtractor, keepAliveInterval, securityValidator);
jsonMapper == null ? McpJsonDefaults.getMapper() : jsonMapper, mcpEndpoint, disallowDelete,
contextExtractor, keepAliveInterval, securityValidator);
}

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,6 @@ public final class McpJsonMapperUtils {
private McpJsonMapperUtils() {
}

public static final McpJsonMapper JSON_MAPPER = McpJsonDefaults.getDefaultMcpJsonMapper();
public static final McpJsonMapper JSON_MAPPER = McpJsonDefaults.getMapper();

}
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class McpJsonMapperTest {

@Test
void shouldUseJackson2Mapper() {
assertThat(McpJsonDefaults.getDefaultMcpJsonMapper()).isInstanceOf(JacksonMcpJsonMapper.class);
assertThat(McpJsonDefaults.getMapper()).isInstanceOf(JacksonMcpJsonMapper.class);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class JsonSchemaValidatorTest {

@Test
void shouldUseJackson2Mapper() {
assertThat(McpJsonDefaults.getDefaultJsonSchemaValidator()).isInstanceOf(DefaultJsonSchemaValidator.class);
assertThat(McpJsonDefaults.getSchemaValidator()).isInstanceOf(DefaultJsonSchemaValidator.class);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class McpJsonMapperTest {

@Test
void shouldUseJackson2Mapper() {
assertThat(McpJsonDefaults.getDefaultMcpJsonMapper()).isInstanceOf(JacksonMcpJsonMapper.class);
assertThat(McpJsonDefaults.getMapper()).isInstanceOf(JacksonMcpJsonMapper.class);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class JsonSchemaValidatorTest {

@Test
void shouldUseJackson2Mapper() {
assertThat(McpJsonDefaults.getDefaultJsonSchemaValidator()).isInstanceOf(DefaultJsonSchemaValidator.class);
assertThat(McpJsonDefaults.getSchemaValidator()).isInstanceOf(DefaultJsonSchemaValidator.class);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -616,9 +616,8 @@ public Builder supportedProtocolVersions(List<String> supportedProtocolVersions)
* @return a new instance of {@link WebClientStreamableHttpTransport}
*/
public WebClientStreamableHttpTransport build() {
return new WebClientStreamableHttpTransport(
jsonMapper == null ? McpJsonDefaults.getDefaultMcpJsonMapper() : jsonMapper, webClientBuilder,
endpoint, resumableStreams, openConnectionOnStartup, supportedProtocolVersions);
return new WebClientStreamableHttpTransport(jsonMapper == null ? McpJsonDefaults.getMapper() : jsonMapper,
webClientBuilder, endpoint, resumableStreams, openConnectionOnStartup, supportedProtocolVersions);
}

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -405,7 +405,7 @@ public Builder jsonMapper(McpJsonMapper jsonMapper) {
*/
public WebFluxSseClientTransport build() {
return new WebFluxSseClientTransport(webClientBuilder,
jsonMapper == null ? McpJsonDefaults.getDefaultMcpJsonMapper() : jsonMapper, sseEndpoint);
jsonMapper == null ? McpJsonDefaults.getMapper() : jsonMapper, sseEndpoint);
}

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -562,9 +562,8 @@ public Builder securityValidator(ServerTransportSecurityValidator securityValida
*/
public WebFluxSseServerTransportProvider build() {
Assert.notNull(messageEndpoint, "Message endpoint must be set");
return new WebFluxSseServerTransportProvider(
jsonMapper == null ? McpJsonDefaults.getDefaultMcpJsonMapper() : jsonMapper, baseUrl,
messageEndpoint, sseEndpoint, keepAliveInterval, contextExtractor, securityValidator);
return new WebFluxSseServerTransportProvider(jsonMapper == null ? McpJsonDefaults.getMapper() : jsonMapper,
baseUrl, messageEndpoint, sseEndpoint, keepAliveInterval, contextExtractor, securityValidator);
}

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -245,9 +245,8 @@ public Builder securityValidator(ServerTransportSecurityValidator securityValida
*/
public WebFluxStatelessServerTransport build() {
Assert.notNull(mcpEndpoint, "Message endpoint must be set");
return new WebFluxStatelessServerTransport(
jsonMapper == null ? McpJsonDefaults.getDefaultMcpJsonMapper() : jsonMapper, mcpEndpoint,
contextExtractor, securityValidator);
return new WebFluxStatelessServerTransport(jsonMapper == null ? McpJsonDefaults.getMapper() : jsonMapper,
mcpEndpoint, contextExtractor, securityValidator);
}

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -533,8 +533,8 @@ public Builder securityValidator(ServerTransportSecurityValidator securityValida
public WebFluxStreamableServerTransportProvider build() {
Assert.notNull(mcpEndpoint, "Message endpoint must be set");
return new WebFluxStreamableServerTransportProvider(
jsonMapper == null ? McpJsonDefaults.getDefaultMcpJsonMapper() : jsonMapper, mcpEndpoint,
contextExtractor, disallowDelete, keepAliveInterval, securityValidator);
jsonMapper == null ? McpJsonDefaults.getMapper() : jsonMapper, mcpEndpoint, contextExtractor,
disallowDelete, keepAliveInterval, securityValidator);
}

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,7 @@ public Sse() {
public McpSyncClient createMcpClient(String baseUrl, TestHeaderExchangeFilterFunction exchangeFilterFunction) {
var transport = WebFluxSseClientTransport
.builder(WebClient.builder().baseUrl(baseUrl).filter(exchangeFilterFunction))
.jsonMapper(McpJsonDefaults.getDefaultMcpJsonMapper())
.jsonMapper(McpJsonDefaults.getMapper())
.build();
return McpClient.sync(transport).initializationTimeout(Duration.ofMillis(500)).build();
}
Expand Down Expand Up @@ -254,7 +254,7 @@ public StreamableHttp() {
public McpSyncClient createMcpClient(String baseUrl, TestHeaderExchangeFilterFunction exchangeFilterFunction) {
var transport = WebClientStreamableHttpTransport
.builder(WebClient.builder().baseUrl(baseUrl).filter(exchangeFilterFunction))
.jsonMapper(McpJsonDefaults.getDefaultMcpJsonMapper())
.jsonMapper(McpJsonDefaults.getMapper())
.openConnectionOnStartup(true)
.build();
return McpClient.sync(transport).initializationTimeout(Duration.ofMillis(500)).build();
Expand Down Expand Up @@ -288,7 +288,7 @@ public Stateless() {
public McpSyncClient createMcpClient(String baseUrl, TestHeaderExchangeFilterFunction exchangeFilterFunction) {
var transport = WebClientStreamableHttpTransport
.builder(WebClient.builder().baseUrl(baseUrl).filter(exchangeFilterFunction))
.jsonMapper(McpJsonDefaults.getDefaultMcpJsonMapper())
.jsonMapper(McpJsonDefaults.getMapper())
.openConnectionOnStartup(true)
.build();
return McpClient.sync(transport).initializationTimeout(Duration.ofMillis(500)).build();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,6 @@ public final class McpJsonMapperUtils {
private McpJsonMapperUtils() {
}

public static final McpJsonMapper JSON_MAPPER = McpJsonDefaults.getDefaultMcpJsonMapper();
public static final McpJsonMapper JSON_MAPPER = McpJsonDefaults.getMapper();

}
Loading