Skip to content

Commit 767c512

Browse files
authored
Fix MCP server 500 error on unknown client capability fields (#26008)
* Use dedicated ObjectMapper for MCP protocol deserialization The global Graylog ObjectMapper (with SnakeCaseStrategy and custom modules) was being used to deserialize MCP SDK protocol types, which bypassed the SDK's @JsonIgnoreProperties(ignoreUnknown = true) annotations. This caused 500 errors when clients sent forward-compatible capability fields like capabilities.sampling.tools. Introduce a plain ObjectMapper qualified with @McpProtocolObjectMapper for deserializing McpSchema.* types, while keeping the global mapper for serializing Graylog domain objects in tool results. Fixes: #25956 Co-Authored-By: Claude Opus 4.6 <[EMAIL_ADDRESS_REDACTED]> * Disable FAIL_ON_UNKNOWN_PROPERTIES on the MCP protocol ObjectMapper The MCP Java SDK has a known bug (modelcontextprotocol/java-sdk#766) where capability sub-records like ClientCapabilities.Sampling lack @JsonIgnoreProperties(ignoreUnknown = true). As a temporary workaround we explicitly disable FAIL_ON_UNKNOWN_PROPERTIES to tolerate forward-compatible fields from newer MCP clients. Co-Authored-By: Claude Opus 4.6 <[EMAIL_ADDRESS_REDACTED]> * Add regression test for unknown MCP client capability fields Verifies that the protocol ObjectMapper tolerates forward-compatible fields like capabilities.sampling.tools that the SDK's empty Sampling record doesn't declare. Regression test for #25956 Co-Authored-By: Claude Opus 4.6 <[EMAIL_ADDRESS_REDACTED]>
1 parent 13d601f commit 767c512

7 files changed

Lines changed: 100 additions & 10 deletions

File tree

‎changelog/unreleased/pr-26008.toml‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
type = "fixed" # One of: a(dded), c(hanged), d(eprecated), r(emoved), f(ixed), s(ecurity)
2+
message = "Fix MCP server 500 error on unknown client capability fields"
3+
4+
issues = ["25956"]
5+
pulls = ["26008"]
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/*
2+
* Copyright (C) 2020 Graylog, Inc.
3+
*
4+
* This program is free software: you can redistribute it and/or modify
5+
* it under the terms of the Server Side Public License, version 1,
6+
* as published by MongoDB, Inc.
7+
*
8+
* This program is distributed in the hope that it will be useful,
9+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
10+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11+
* Server Side Public License for more details.
12+
*
13+
* You should have received a copy of the Server Side Public License
14+
* along with this program. If not, see
15+
* <http://www.mongodb.com/licensing/server-side-public-license>.
16+
*/
17+
package org.graylog.mcp.server;
18+
19+
import com.google.inject.BindingAnnotation;
20+
21+
import java.lang.annotation.Retention;
22+
import java.lang.annotation.Target;
23+
24+
import static java.lang.annotation.ElementType.FIELD;
25+
import static java.lang.annotation.ElementType.METHOD;
26+
import static java.lang.annotation.ElementType.PARAMETER;
27+
import static java.lang.annotation.RetentionPolicy.RUNTIME;
28+
29+
/**
30+
* Qualifier annotation for the ObjectMapper used to deserialize MCP protocol messages.
31+
* This mapper is a plain ObjectMapper configured with FAIL_ON_UNKNOWN_PROPERTIES=false,
32+
* unlike the global Graylog ObjectMapper which adds SnakeCaseStrategy and custom modules
33+
* that interfere with the MCP SDK's own Jackson annotations.
34+
*
35+
* Workaround for <a href="https://github.com/modelcontextprotocol/java-sdk/issues/766">java-sdk#766</a>.
36+
* Remove once the SDK adds @JsonIgnoreProperties(ignoreUnknown=true) to its capability records.
37+
*/
38+
@BindingAnnotation
39+
@Target({FIELD, PARAMETER, METHOD})
40+
@Retention(RUNTIME)
41+
public @interface McpProtocolObjectMapper {
42+
}

‎graylog2-server/src/main/java/org/graylog/mcp/server/McpRestResource.java‎

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ public class McpRestResource extends RestResource {
6464
private static final String HEADER_MCP_SESSION_ID = "Mcp-Session-Id";
6565
private static final String HEADER_MCP_PROTOCOL_VERSION = "MCP-Protocol-Version";
6666

67-
private final ObjectMapper objectMapper;
67+
private final ObjectMapper protocolObjectMapper;
6868

6969
private final McpService mcpService;
7070

@@ -73,10 +73,10 @@ public class McpRestResource extends RestResource {
7373
private final ClusterConfigService clusterConfig;
7474

7575
@Inject
76-
public McpRestResource(final ClusterConfigService clusterConfig, final McpService mcpService, final ObjectMapper objectMapper, final SecurityContext securityContext) {
76+
public McpRestResource(final ClusterConfigService clusterConfig, final McpService mcpService, @McpProtocolObjectMapper final ObjectMapper protocolObjectMapper, final SecurityContext securityContext) {
7777
this.clusterConfig = clusterConfig;
7878
this.mcpService = mcpService;
79-
this.objectMapper = objectMapper;
79+
this.protocolObjectMapper = protocolObjectMapper;
8080
this.securityContext = securityContext;
8181
}
8282

@@ -116,7 +116,7 @@ public Response post(@HeaderParam(HttpHeaders.ACCEPT) String acceptHeader,
116116
// Simple one-shot JSON reply
117117
Object id = null;
118118
try {
119-
final McpSchema.JSONRPCRequest request = objectMapper.convertValue(payload, McpSchema.JSONRPCRequest.class);
119+
final McpSchema.JSONRPCRequest request = protocolObjectMapper.convertValue(payload, McpSchema.JSONRPCRequest.class);
120120
LOG.trace("Received JSON-RPC request {}", request);
121121
id = request.id();
122122

@@ -140,7 +140,7 @@ public Response post(@HeaderParam(HttpHeaders.ACCEPT) String acceptHeader,
140140
final Optional<McpSchema.Result> result = mcpService.handle(permissionHelper, request, sessionId, protocolVersionHeader);
141141

142142
if (result.isPresent() && LOG.isTraceEnabled()) {
143-
LOG.trace("Successfully handled JSON-RPC request: {}", objectMapper.writeValueAsString(result));
143+
LOG.trace("Successfully handled JSON-RPC request: {}", protocolObjectMapper.writeValueAsString(result));
144144
}
145145
return Response.ok(new McpSchema.JSONRPCResponse("2.0", id, result.orElse(null), null))
146146
.header(HEADER_MCP_SESSION_ID, sessionId)

‎graylog2-server/src/main/java/org/graylog/mcp/server/McpServerModule.java‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616
*/
1717
package org.graylog.mcp.server;
1818

19+
import com.fasterxml.jackson.databind.DeserializationFeature;
20+
import com.fasterxml.jackson.databind.ObjectMapper;
1921
import org.graylog.mcp.resources.DashboardResourceProvider;
2022
import org.graylog.mcp.resources.EventDefinitionResourceProvider;
2123
import org.graylog.mcp.resources.StreamResourceProvider;
@@ -36,6 +38,12 @@
3638
public class McpServerModule extends PluginModule {
3739
@Override
3840
protected void configure() {
41+
// Workaround for https://github.com/modelcontextprotocol/java-sdk/issues/766
42+
// Remove once the SDK adds @JsonIgnoreProperties(ignoreUnknown=true) to capability records.
43+
bind(ObjectMapper.class).annotatedWith(McpProtocolObjectMapper.class)
44+
.toInstance(new ObjectMapper()
45+
.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false));
46+
3947
// Initialize schema module binder (empty set by default, plugins can contribute)
4048
schemaModuleBinder();
4149

‎graylog2-server/src/main/java/org/graylog/mcp/server/McpService.java‎

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ public class McpService {
5959
static final List<String> ALL_SUPPORTED_MCP_VERSIONS = List.of(LATEST_SUPPORTED_MCP_VERSION);
6060

6161
private final ObjectMapper objectMapper;
62+
private final ObjectMapper protocolObjectMapper;
6263
private final AuditEventSender auditEventSender;
6364
private final CustomizationConfig customizationConfig;
6465
private final GRNRegistry grnRegistry;
@@ -67,12 +68,14 @@ public class McpService {
6768

6869
@Inject
6970
protected McpService(ObjectMapper objectMapper,
71+
@McpProtocolObjectMapper ObjectMapper protocolObjectMapper,
7072
AuditEventSender auditEventSender,
7173
CustomizationConfig customizationConfig,
7274
GRNRegistry grnRegistry,
7375
Map<String, Tool<?, ?>> tools,
7476
Map<GRNType, ? extends ResourceProvider> resourceProviders) {
7577
this.objectMapper = objectMapper;
78+
this.protocolObjectMapper = protocolObjectMapper;
7679
this.auditEventSender = auditEventSender;
7780
this.customizationConfig = customizationConfig;
7881
this.grnRegistry = grnRegistry;
@@ -93,7 +96,7 @@ public Optional<McpSchema.Result> handle(PermissionHelper permissionHelper, McpS
9396

9497
switch (request.method()) {
9598
case McpSchema.METHOD_INITIALIZE -> {
96-
final McpSchema.InitializeRequest initializeRequest = objectMapper.convertValue(request.params(), new TypeReference<>() {});
99+
final McpSchema.InitializeRequest initializeRequest = protocolObjectMapper.convertValue(request.params(), new TypeReference<>() {});
97100
auditContext.put("request", initializeRequest);
98101
final McpSchema.InitializeResult result = new McpSchema.InitializeResult(
99102
// Version negotiation: https://modelcontextprotocol.io/specification/2025-06-18/basic/lifecycle#version-negotiation
@@ -130,7 +133,7 @@ public Optional<McpSchema.Result> handle(PermissionHelper permissionHelper, McpS
130133
return Optional.of(result);
131134
}
132135
case McpSchema.METHOD_RESOURCES_READ -> {
133-
final McpSchema.ReadResourceRequest readResourceRequest = objectMapper.convertValue(request.params(), new TypeReference<>() {});
136+
final McpSchema.ReadResourceRequest readResourceRequest = protocolObjectMapper.convertValue(request.params(), new TypeReference<>() {});
134137
auditContext.put("request", readResourceRequest);
135138
LOG.debug("Reading resource: {}", readResourceRequest);
136139
try {
@@ -181,7 +184,7 @@ public Optional<McpSchema.Result> handle(PermissionHelper permissionHelper, McpS
181184
return Optional.of(new McpSchema.ListToolsResult(toolList, null));
182185
}
183186
case McpSchema.METHOD_TOOLS_CALL -> {
184-
final McpSchema.CallToolRequest callToolRequest = objectMapper.convertValue(request.params(), new TypeReference<>() {});
187+
final McpSchema.CallToolRequest callToolRequest = protocolObjectMapper.convertValue(request.params(), new TypeReference<>() {});
185188
auditContext.put("request", callToolRequest);
186189

187190
LOG.debug("Calling MCP tool: {}", callToolRequest);
@@ -237,7 +240,7 @@ public Optional<McpSchema.Result> handle(PermissionHelper permissionHelper, McpS
237240
}
238241
case McpSchema.METHOD_PROMPT_GET -> {
239242
// disabled for now
240-
final McpSchema.GetPromptRequest promptRequest = objectMapper.convertValue(request.params(), new TypeReference<>() {});
243+
final McpSchema.GetPromptRequest promptRequest = protocolObjectMapper.convertValue(request.params(), new TypeReference<>() {});
241244
auditContext.put("request", promptRequest);
242245
LOG.debug("Getting prompt {}", promptRequest.name());
243246
auditEventSender.failure(auditActor, AuditEventType.create(MCP_PROMPT_GET), auditContext);

‎graylog2-server/src/test/java/org/graylog/mcp/server/McpRestResourceTest.java‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616
*/
1717
package org.graylog.mcp.server;
1818

19+
import com.fasterxml.jackson.databind.DeserializationFeature;
20+
import com.fasterxml.jackson.databind.ObjectMapper;
1921
import jakarta.ws.rs.core.Response;
2022
import org.graylog.mcp.config.McpConfiguration;
2123
import org.graylog.security.certutil.InMemoryClusterConfigService;
@@ -34,7 +36,7 @@ public static void setUp() throws Exception {
3436
// we have to create mocks for the rest, too
3537
resource = new McpRestResource(clusterConfigService,
3638
null,
37-
null,
39+
new ObjectMapper().configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false),
3840
null);
3941
clusterConfigService.write(McpConfiguration.create(false, false));
4042
}

‎graylog2-server/src/test/java/org/graylog/mcp/server/McpServiceTest.java‎

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
*/
1717
package org.graylog.mcp.server;
1818

19+
import com.fasterxml.jackson.databind.DeserializationFeature;
1920
import com.fasterxml.jackson.databind.ObjectMapper;
2021
import io.modelcontextprotocol.spec.McpError;
2122
import io.modelcontextprotocol.spec.McpSchema;
@@ -66,13 +67,16 @@ class McpServiceTest {
6667
private User user;
6768

6869
private ObjectMapper objectMapper;
70+
private ObjectMapper protocolObjectMapper;
6971
private McpService mcpService;
7072
private Map<String, Tool<?, ?>> tools;
7173
private Map<GRNType, ResourceProvider> resourceProviders;
7274

7375
@BeforeEach
7476
void setUp() {
7577
objectMapper = new ObjectMapper();
78+
protocolObjectMapper = new ObjectMapper()
79+
.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
7680
tools = new HashMap<>();
7781
resourceProviders = new HashMap<>();
7882

@@ -81,6 +85,7 @@ void setUp() {
8185

8286
mcpService = new McpService(
8387
objectMapper,
88+
protocolObjectMapper,
8489
auditEventSender,
8590
new CustomizationConfig(null),
8691
GRNRegistry.createWithBuiltinTypes(),
@@ -122,6 +127,31 @@ void testInitializeWithValidProtocolVersion() throws Exception {
122127
verify(auditEventSender).success(any(AuditActor.class), any(AuditEventType.class), anyMap());
123128
}
124129

130+
@Test
131+
void testInitializeToleratesUnknownCapabilityFields() throws Exception {
132+
// Regression test for https://github.com/Graylog2/graylog2-server/issues/25956
133+
// Newer MCP clients may send forward-compatible fields (e.g. capabilities.sampling.tools)
134+
// that the SDK's Sampling record doesn't declare.
135+
var params = Map.of(
136+
"protocolVersion", ProtocolVersions.MCP_2025_06_18,
137+
"capabilities", Map.of("sampling", Map.of("tools", Map.of())),
138+
"clientInfo", Map.of("name", "TestClient", "version", "1.0.0")
139+
);
140+
var request = new McpSchema.JSONRPCRequest(
141+
"2.0",
142+
McpSchema.METHOD_INITIALIZE,
143+
"1",
144+
params
145+
);
146+
147+
Optional<McpSchema.Result> result = mcpService.handle(permissionHelper, request, "session123");
148+
149+
assertThat(result).isPresent();
150+
assertThat(result.get()).isInstanceOf(McpSchema.InitializeResult.class);
151+
McpSchema.InitializeResult initResult = (McpSchema.InitializeResult) result.get();
152+
assertThat(initResult.protocolVersion()).isEqualTo(ProtocolVersions.MCP_2025_06_18);
153+
}
154+
125155
@Test
126156
void testInitializeWithInvalidProtocolVersion() {
127157
// Given

0 commit comments

Comments
 (0)