refactor: change notification params type from Map<String,Object> to Object (#137)

* refactor: change notification params type from Map<String,Object> to Object

This change generalizes the parameter type for notification methods across the MCP framework,
allowing for more flexible parameter passing. Instead of requiring parameters to be structured
as a Map<String,Object>, the API now accepts any Object as parameters.

The primary motivation is to simplify client usage by allowing direct passing of strongly-typed
 objects without requiring conversion to a Map first, as demonstrated in the McpAsyncServer
logging notification implementation.

Affected components:

- McpSession interface and implementations
- McpServerTransportProvider interface and implementations
- McpSchema JSONRPCNotification record
---------

Signed-off-by: Christian Tzolov <christian.tzolov@broadcom.com>
This commit is contained in:
Christian Tzolov 2025-04-10 12:26:29 +02:00 • committed by GitHub
commit 391ec19fdc
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
11 changed files with 14 additions and 17 deletions

View file

@ -188,7 +188,7 @@ public class WebFluxSseServerTransportProvider implements McpServerTransportProv
* errors if any session fails to receive the message * errors if any session fails to receive the message
*/ */
@Override @Override
public Mono<Void> notifyClients(String method, Map<String, Object> params) { public Mono<Void> notifyClients(String method, Object params) {
if (sessions.isEmpty()) { if (sessions.isEmpty()) {
logger.debug("No active sessions to broadcast message to"); logger.debug("No active sessions to broadcast message to");
return Mono.empty(); return Mono.empty();

View file

@ -179,7 +179,7 @@ public class WebMvcSseServerTransportProvider implements McpServerTransportProvi
* @return A Mono that completes when the broadcast attempt is finished * @return A Mono that completes when the broadcast attempt is finished
*/ */
@Override @Override
public Mono<Void> notifyClients(String method, Map<String, Object> params) { public Mono<Void> notifyClients(String method, Object params) {
if (sessions.isEmpty()) { if (sessions.isEmpty()) {
logger.debug("No active sessions to broadcast message to"); logger.debug("No active sessions to broadcast message to");
return Mono.empty(); return Mono.empty();

View file

@ -669,15 +669,12 @@ public class McpAsyncServer {
return Mono.error(new McpError("Logging message must not be null")); return Mono.error(new McpError("Logging message must not be null"));
} }
Map<String, Object> params = this.objectMapper.convertValue(loggingMessageNotification,
new TypeReference<Map<String, Object>>() {
});
if (loggingMessageNotification.level().level() < minLoggingLevel.level()) { if (loggingMessageNotification.level().level() < minLoggingLevel.level()) {
return Mono.empty(); return Mono.empty();
} }
return this.mcpTransportProvider.notifyClients(McpSchema.METHOD_NOTIFICATION_MESSAGE, params); return this.mcpTransportProvider.notifyClients(McpSchema.METHOD_NOTIFICATION_MESSAGE,
loggingMessageNotification);
} }
private McpServerSession.RequestHandler<Void> setLoggerRequestHandler() { private McpServerSession.RequestHandler<Void> setLoggerRequestHandler() {

View file

@ -160,7 +160,7 @@ public class HttpServletSseServerTransportProvider extends HttpServlet implement
* @return A Mono that completes when the broadcast attempt is finished * @return A Mono that completes when the broadcast attempt is finished
*/ */
@Override @Override
public Mono<Void> notifyClients(String method, Map<String, Object> params) { public Mono<Void> notifyClients(String method, Object params) {
if (sessions.isEmpty()) { if (sessions.isEmpty()) {
logger.debug("No active sessions to broadcast message to"); logger.debug("No active sessions to broadcast message to");
return Mono.empty(); return Mono.empty();

View file

@ -99,7 +99,7 @@ public class StdioServerTransportProvider implements McpServerTransportProvider
} }
@Override @Override
public Mono<Void> notifyClients(String method, Map<String, Object> params) { public Mono<Void> notifyClients(String method, Object params) {
if (this.session == null) { if (this.session == null) {
return Mono.error(new McpError("No session to close")); return Mono.error(new McpError("No session to close"));
} }

View file

@ -258,7 +258,7 @@ public class McpClientSession implements McpSession {
* @return A Mono that completes when the notification is sent * @return A Mono that completes when the notification is sent
*/ */
@Override @Override
public Mono<Void> sendNotification(String method, Map<String, Object> params) { public Mono<Void> sendNotification(String method, Object params) {
McpSchema.JSONRPCNotification jsonrpcNotification = new McpSchema.JSONRPCNotification(McpSchema.JSONRPC_VERSION, McpSchema.JSONRPCNotification jsonrpcNotification = new McpSchema.JSONRPCNotification(McpSchema.JSONRPC_VERSION,
method, params); method, params);
return this.transport.sendMessage(jsonrpcNotification); return this.transport.sendMessage(jsonrpcNotification);

View file

@ -191,7 +191,7 @@ public final class McpSchema {
public record JSONRPCNotification( // @formatter:off public record JSONRPCNotification( // @formatter:off
@JsonProperty("jsonrpc") String jsonrpc, @JsonProperty("jsonrpc") String jsonrpc,
@JsonProperty("method") String method, @JsonProperty("method") String method,
@JsonProperty("params") Map<String, Object> params) implements JSONRPCMessage { @JsonProperty("params") Object params) implements JSONRPCMessage {
} // @formatter:on } // @formatter:on
@JsonInclude(JsonInclude.Include.NON_ABSENT) @JsonInclude(JsonInclude.Include.NON_ABSENT)

View file

@ -132,7 +132,7 @@ public class McpServerSession implements McpSession {
} }
@Override @Override
public Mono<Void> sendNotification(String method, Map<String, Object> params) { public Mono<Void> sendNotification(String method, Object params) {
McpSchema.JSONRPCNotification jsonrpcNotification = new McpSchema.JSONRPCNotification(McpSchema.JSONRPC_VERSION, McpSchema.JSONRPCNotification jsonrpcNotification = new McpSchema.JSONRPCNotification(McpSchema.JSONRPC_VERSION,
method, params); method, params);
return this.transport.sendMessage(jsonrpcNotification); return this.transport.sendMessage(jsonrpcNotification);

View file

@ -42,11 +42,11 @@ public interface McpServerTransportProvider {
/** /**
* Sends a notification to all connected clients. * Sends a notification to all connected clients.
* @param method the name of the notification method to be called on the clients * @param method the name of the notification method to be called on the clients
* @param params a map of parameters to be sent with the notification * @param params parameters to be sent with the notification
* @return a Mono that completes when the notification has been broadcast * @return a Mono that completes when the notification has been broadcast
* @see McpSession#sendNotification(String, Map) * @see McpSession#sendNotification(String, Map)
*/ */
Mono<Void> notifyClients(String method, Map<String, Object> params); Mono<Void> notifyClients(String method, Object params);
/** /**
* Immediately closes all the transports with connected clients and releases any * Immediately closes all the transports with connected clients and releases any

View file

@ -63,10 +63,10 @@ public interface McpSession {
* parameters with the notification. * parameters with the notification.
* </p> * </p>
* @param method the name of the notification method to be sent to the counterparty * @param method the name of the notification method to be sent to the counterparty
* @param params a map of parameters to be sent with the notification * @param params parameters to be sent with the notification
* @return a Mono that completes when the notification has been sent * @return a Mono that completes when the notification has been sent
*/ */
Mono<Void> sendNotification(String method, Map<String, Object> params); Mono<Void> sendNotification(String method, Object params);
/** /**
* Closes the session and releases any associated resources asynchronously. * Closes the session and releases any associated resources asynchronously.

View file

@ -47,7 +47,7 @@ public class MockMcpServerTransportProvider implements McpServerTransportProvide
} }
@Override @Override
public Mono<Void> notifyClients(String method, Map<String, Object> params) { public Mono<Void> notifyClients(String method, Object params) {
return session.sendNotification(method, params); return session.sendNotification(method, params);
} }