You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@shardingsphere.apache.org by zh...@apache.org on 2026/10/29 16:18:54 UTC
(shardingsphere) branch master updated: Unify MCP payload JSON handling with JsonEngine (#40416)
This is an automated email from the ASF dual-hosted git repository.
terrymanu pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/shardingsphere.git
The following commit(s) were added to refs/heads/master by this push:
new 5e39fda6d9b Unify MCP payload JSON handling with JsonEngine (#40416)
5e39fda6d9b is described below
commit 5e39fda6d9bc254145c95b5969f1cf1f5381ece2
Author: Liang Zhang <zh...@apache.org>
AuthorDate: Fri Oct 30 00:18:47 2026 +0800
Unify MCP payload JSON handling with JsonEngine (#40416)
---
.../resource/MCPResourceSpecificationFactory.java | 7 ++--
.../capability/tool/MCPCallToolResultFactory.java | 7 ++--
.../MCPResourceSpecificationFactoryTest.java | 31 ++++++++++++++++++
.../tool/MCPCallToolResultFactoryTest.java | 37 ++++++++++++++++++++++
.../server/http/StreamableHttpMCPServerIT.java | 15 ++++-----
.../support/transport/MCPInteractionPayloads.java | 17 ++++------
.../transport/MCPInteractionProtocolSupport.java | 11 +++----
.../AbstractProcessMCPStdioInteractionClient.java | 23 +++++++++-----
8 files changed, 106 insertions(+), 42 deletions(-)
diff --git a/mcp/bootstrap/src/main/java/org/apache/shardingsphere/mcp/bootstrap/transport/capability/resource/MCPResourceSpecificationFactory.java b/mcp/bootstrap/src/main/java/org/apache/shardingsphere/mcp/bootstrap/transport/capability/resource/MCPResourceSpecificationFactory.java
index 0a897e2c73c..ded05107d6a 100644
--- a/mcp/bootstrap/src/main/java/org/apache/shardingsphere/mcp/bootstrap/transport/capability/resource/MCPResourceSpecificationFactory.java
+++ b/mcp/bootstrap/src/main/java/org/apache/shardingsphere/mcp/bootstrap/transport/capability/resource/MCPResourceSpecificationFactory.java
@@ -17,16 +17,15 @@
package org.apache.shardingsphere.mcp.bootstrap.transport.capability.resource;
-import io.modelcontextprotocol.json.McpJsonMapper;
import io.modelcontextprotocol.server.McpServerFeatures.SyncResourceSpecification;
import io.modelcontextprotocol.server.McpServerFeatures.SyncResourceTemplateSpecification;
import io.modelcontextprotocol.spec.McpSchema;
import io.modelcontextprotocol.spec.McpSchema.ReadResourceResult;
import io.modelcontextprotocol.spec.McpSchema.TextResourceContents;
+import org.apache.shardingsphere.infra.util.json.JsonEngine;
import org.apache.shardingsphere.mcp.api.capability.resource.MCPResourceAnnotations;
import org.apache.shardingsphere.mcp.api.capability.resource.MCPResourceDescriptor;
import org.apache.shardingsphere.mcp.bootstrap.transport.MCPTransportErrorFactory;
-import org.apache.shardingsphere.mcp.bootstrap.transport.MCPTransportJsonMapperFactory;
import org.apache.shardingsphere.mcp.core.context.MCPRuntimeContext;
import org.apache.shardingsphere.mcp.core.resource.MCPResourceController;
import org.apache.shardingsphere.mcp.core.resource.handler.ResourceDefinitionRegistry;
@@ -44,8 +43,6 @@ public final class MCPResourceSpecificationFactory {
private static final String JSON_CONTENT_TYPE = "application/json";
- private final McpJsonMapper jsonMapper = MCPTransportJsonMapperFactory.create();
-
private final Collection<MCPResourceDescriptor> descriptors;
private final MCPResourceController controller;
@@ -117,7 +114,7 @@ public final class MCPResourceSpecificationFactory {
private ReadResourceResult readResource(final String sessionId, final McpSchema.ReadResourceRequest request) {
try {
Map<String, Object> payload = controller.handle(sessionId, request.uri()).toPayload();
- return new ReadResourceResult(Collections.singletonList(new TextResourceContents(request.uri(), JSON_CONTENT_TYPE, jsonMapper.writeValueAsString(payload))));
+ return new ReadResourceResult(Collections.singletonList(new TextResourceContents(request.uri(), JSON_CONTENT_TYPE, JsonEngine.marshal(payload))));
// CHECKSTYLE:OFF
} catch (final Exception ex) {
// CHECKSTYLE:ON
diff --git a/mcp/bootstrap/src/main/java/org/apache/shardingsphere/mcp/bootstrap/transport/capability/tool/MCPCallToolResultFactory.java b/mcp/bootstrap/src/main/java/org/apache/shardingsphere/mcp/bootstrap/transport/capability/tool/MCPCallToolResultFactory.java
index 348af79b8f1..c88407b6607 100644
--- a/mcp/bootstrap/src/main/java/org/apache/shardingsphere/mcp/bootstrap/transport/capability/tool/MCPCallToolResultFactory.java
+++ b/mcp/bootstrap/src/main/java/org/apache/shardingsphere/mcp/bootstrap/transport/capability/tool/MCPCallToolResultFactory.java
@@ -24,6 +24,8 @@ import io.modelcontextprotocol.json.schema.JsonSchemaValidator.ValidationRespons
import io.modelcontextprotocol.json.schema.jackson2.DefaultJsonSchemaValidator;
import io.modelcontextprotocol.spec.McpSchema;
import io.modelcontextprotocol.spec.McpSchema.CallToolResult;
+import org.apache.shardingsphere.infra.util.json.JsonEngine;
+import org.apache.shardingsphere.infra.util.json.JsonException;
import org.apache.shardingsphere.mcp.api.capability.tool.MCPToolDescriptor;
import org.apache.shardingsphere.mcp.api.payload.MCPSuccessPayload;
import org.apache.shardingsphere.mcp.bootstrap.transport.MCPTransportErrorFactory;
@@ -31,7 +33,6 @@ import org.apache.shardingsphere.mcp.bootstrap.transport.MCPTransportJsonMapperF
import org.apache.shardingsphere.mcp.core.protocol.error.MCPErrorPayload;
import org.apache.shardingsphere.mcp.support.descriptor.MCPShardingSphereMetadataKeys;
-import java.io.IOException;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Objects;
@@ -71,8 +72,8 @@ final class MCPCallToolResultFactory {
private String toJsonString(final Object value) {
try {
- return jsonMapper.writeValueAsString(value);
- } catch (final IOException ex) {
+ return JsonEngine.marshal(value);
+ } catch (final JsonException ex) {
throw MCPTransportErrorFactory.createError(ex);
}
}
diff --git a/mcp/bootstrap/src/test/java/org/apache/shardingsphere/mcp/bootstrap/transport/capability/resource/MCPResourceSpecificationFactoryTest.java b/mcp/bootstrap/src/test/java/org/apache/shardingsphere/mcp/bootstrap/transport/capability/resource/MCPResourceSpecificationFactoryTest.java
index ac06b5ef8cd..aea38baaf26 100644
--- a/mcp/bootstrap/src/test/java/org/apache/shardingsphere/mcp/bootstrap/transport/capability/resource/MCPResourceSpecificationFactoryTest.java
+++ b/mcp/bootstrap/src/test/java/org/apache/shardingsphere/mcp/bootstrap/transport/capability/resource/MCPResourceSpecificationFactoryTest.java
@@ -26,6 +26,8 @@ import io.modelcontextprotocol.spec.McpSchema;
import io.modelcontextprotocol.spec.McpSchema.ReadResourceRequest;
import io.modelcontextprotocol.spec.McpSchema.ReadResourceResult;
import io.modelcontextprotocol.spec.McpSchema.TextResourceContents;
+import org.apache.shardingsphere.infra.util.json.JsonEngine;
+import org.apache.shardingsphere.infra.util.json.JsonException;
import org.apache.shardingsphere.mcp.api.capability.resource.MCPResourceAnnotations;
import org.apache.shardingsphere.mcp.api.capability.resource.MCPResourceDescriptor;
import org.apache.shardingsphere.mcp.api.exception.MCPInvalidRequestException;
@@ -180,6 +182,35 @@ class MCPResourceSpecificationFactoryTest {
}
}
+ @Test
+ void assertReadResourceSanitizesSerializationFailure(final LogCaptureAssertion logCaptureAssertion) {
+ MCPResourceDescriptor descriptor = new MCPResourceDescriptor("shardingsphere://foo", "foo", "Foo", "Read foo.", "application/json", MCPResourceAnnotations.EMPTY, Map.of());
+ Map<String, Object> payload = Map.of("foo_field", "foo_value");
+ IllegalStateException expectedCause = new IllegalStateException("foo_sensitive_detail");
+ JsonException expectedException = mock(JsonException.class);
+ when(expectedException.getMessage()).thenReturn("foo_sensitive_detail");
+ when(expectedException.getCause()).thenReturn(expectedCause);
+ try (
+ MockedStatic<ResourceDefinitionRegistry> mockedRegistry = mockStatic(ResourceDefinitionRegistry.class);
+ MockedStatic<JsonEngine> mockedJsonEngine = mockStatic(JsonEngine.class)) {
+ mockedRegistry.when(ResourceDefinitionRegistry::getSupportedResourceDescriptors).thenReturn(List.of(descriptor));
+ mockedRegistry.when(() -> ResourceDefinitionRegistry.dispatch(any(MCPFeatureRuntimeRequestContext.class), eq("shardingsphere://foo")))
+ .thenReturn(Optional.of(new MCPMapPayload(payload)));
+ mockedJsonEngine.when(() -> JsonEngine.marshal(payload)).thenThrow(expectedException);
+ SyncResourceSpecification actualSpecification = new MCPResourceSpecificationFactory(createRuntimeContext()).createResourceSpecifications().getFirst();
+ McpError actual = assertThrows(McpError.class, () -> actualSpecification.readHandler().apply(createExchange(), new ReadResourceRequest("shardingsphere://foo")));
+ assertThat(actual.getJsonRpcError().code(), is(McpSchema.ErrorCodes.INTERNAL_ERROR));
+ assertThat(actual.getJsonRpcError().message(), is("Service is temporarily unavailable."));
+ Map<?, ?> actualData = (Map<?, ?>) actual.getJsonRpcError().data();
+ assertThat(actualData.get("error_id"), isA(String.class));
+ assertThat(actualData, is(Map.of("response_mode", "recovery", "summary", "Service is temporarily unavailable.", "error_id", actualData.get("error_id"))));
+ logCaptureAssertion.assertErrorLog(actualException -> {
+ assertThat(((ThrowableProxy) actualException).getThrowable(), sameInstance(expectedException));
+ assertThat(((ThrowableProxy) actualException).getThrowable().getCause(), sameInstance(expectedCause));
+ });
+ }
+ }
+
@Test
void assertCreateResourceTemplateSpecifications() {
Collection<SyncResourceTemplateSpecification> actual = new MCPResourceSpecificationFactory(mock(MCPRuntimeContext.class)).createResourceTemplateSpecifications();
diff --git a/mcp/bootstrap/src/test/java/org/apache/shardingsphere/mcp/bootstrap/transport/capability/tool/MCPCallToolResultFactoryTest.java b/mcp/bootstrap/src/test/java/org/apache/shardingsphere/mcp/bootstrap/transport/capability/tool/MCPCallToolResultFactoryTest.java
index 5e51e8b991b..b0c75f770bc 100644
--- a/mcp/bootstrap/src/test/java/org/apache/shardingsphere/mcp/bootstrap/transport/capability/tool/MCPCallToolResultFactoryTest.java
+++ b/mcp/bootstrap/src/test/java/org/apache/shardingsphere/mcp/bootstrap/transport/capability/tool/MCPCallToolResultFactoryTest.java
@@ -17,14 +17,18 @@
package org.apache.shardingsphere.mcp.bootstrap.transport.capability.tool;
+import ch.qos.logback.classic.spi.ThrowableProxy;
import io.modelcontextprotocol.json.schema.JsonSchemaValidator;
import io.modelcontextprotocol.json.schema.JsonSchemaValidator.ValidationResponse;
import io.modelcontextprotocol.json.schema.jackson2.DefaultJsonSchemaValidator;
+import io.modelcontextprotocol.spec.McpError;
+import io.modelcontextprotocol.spec.McpSchema;
import io.modelcontextprotocol.spec.McpSchema.CallToolRequest;
import io.modelcontextprotocol.spec.McpSchema.CallToolResult;
import io.modelcontextprotocol.spec.McpSchema.ResourceLink;
import io.modelcontextprotocol.spec.McpSchema.TextContent;
import org.apache.shardingsphere.infra.util.json.JsonEngine;
+import org.apache.shardingsphere.infra.util.json.JsonException;
import org.apache.shardingsphere.mcp.api.capability.tool.MCPToolDescriptor;
import org.apache.shardingsphere.mcp.api.payload.MCPSuccessPayload;
import org.apache.shardingsphere.mcp.api.transport.MCPTransportType;
@@ -57,7 +61,11 @@ import org.apache.shardingsphere.mcp.support.workflow.model.WorkflowLifecycle;
import org.apache.shardingsphere.mcp.support.workflow.model.WorkflowRequest;
import org.apache.shardingsphere.mcp.support.workflow.service.WorkflowArtifactMaskUtils;
import org.apache.shardingsphere.mcp.support.workflow.service.WorkflowPlanPayloadBuilder;
+import org.apache.shardingsphere.test.infra.framework.extension.log.LogCaptureAssertion;
+import org.apache.shardingsphere.test.infra.framework.extension.log.LogCaptureExtension;
+import org.apache.shardingsphere.test.infra.framework.extension.log.LogCaptureSettings;
import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.MockedStatic;
import java.time.OffsetDateTime;
@@ -67,11 +75,17 @@ import java.util.Map;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.isA;
+import static org.hamcrest.Matchers.sameInstance;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNull;
+import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
+import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.mockStatic;
+import static org.mockito.Mockito.when;
+@ExtendWith(LogCaptureExtension.class)
+@LogCaptureSettings(value = "org.apache.shardingsphere.mcp.bootstrap.transport.MCPTransportErrorFactory", suppressOutput = true)
class MCPCallToolResultFactoryTest extends AbstractMCPToolSpecificationFactoryTest {
@Test
@@ -105,6 +119,29 @@ class MCPCallToolResultFactoryTest extends AbstractMCPToolSpecificationFactoryTe
assertFalse(actual.isError());
}
+ @Test
+ void assertCreateSanitizesSerializationFailure(final LogCaptureAssertion logCaptureAssertion) {
+ Map<String, Object> payload = Map.of("foo_field", "foo_value");
+ IllegalStateException expectedCause = new IllegalStateException("foo_sensitive_detail");
+ JsonException expectedException = mock(JsonException.class);
+ when(expectedException.getMessage()).thenReturn("foo_sensitive_detail");
+ when(expectedException.getCause()).thenReturn(expectedCause);
+ MCPToolDescriptor descriptor = createToolDescriptorWithoutOutputSchema("fixture_ping");
+ try (MockedStatic<JsonEngine> mockedJsonEngine = mockStatic(JsonEngine.class)) {
+ mockedJsonEngine.when(() -> JsonEngine.marshal(payload)).thenThrow(expectedException);
+ McpError actual = assertThrows(McpError.class, () -> new MCPCallToolResultFactory().create(descriptor, new MCPMapPayload(payload)));
+ assertThat(actual.getJsonRpcError().code(), is(McpSchema.ErrorCodes.INTERNAL_ERROR));
+ assertThat(actual.getJsonRpcError().message(), is("Service is temporarily unavailable."));
+ Map<?, ?> actualData = (Map<?, ?>) actual.getJsonRpcError().data();
+ assertThat(actualData.get("error_id"), isA(String.class));
+ assertThat(actualData, is(Map.of("response_mode", "recovery", "summary", "Service is temporarily unavailable.", "error_id", actualData.get("error_id"))));
+ logCaptureAssertion.assertErrorLog(actualException -> {
+ assertThat(((ThrowableProxy) actualException).getThrowable(), sameInstance(expectedException));
+ assertThat(((ThrowableProxy) actualException).getThrowable().getCause(), sameInstance(expectedCause));
+ });
+ }
+ }
+
@Test
void assertCreateWithRuntimeValidationDiagnostic() {
MCPSuccessPayload response = RuntimeDatabaseValidationPayload.from(RuntimeDatabaseValidationResult.failed("",
diff --git a/mcp/bootstrap/src/test/java/org/apache/shardingsphere/mcp/bootstrap/transport/server/http/StreamableHttpMCPServerIT.java b/mcp/bootstrap/src/test/java/org/apache/shardingsphere/mcp/bootstrap/transport/server/http/StreamableHttpMCPServerIT.java
index f5bbe7460c7..c28e19afaf1 100644
--- a/mcp/bootstrap/src/test/java/org/apache/shardingsphere/mcp/bootstrap/transport/server/http/StreamableHttpMCPServerIT.java
+++ b/mcp/bootstrap/src/test/java/org/apache/shardingsphere/mcp/bootstrap/transport/server/http/StreamableHttpMCPServerIT.java
@@ -17,12 +17,11 @@
package org.apache.shardingsphere.mcp.bootstrap.transport.server.http;
-import io.modelcontextprotocol.json.McpJsonMapper;
+import org.apache.shardingsphere.infra.util.json.JsonEngine;
import org.apache.shardingsphere.mcp.api.transport.MCPTransportType;
import org.apache.shardingsphere.mcp.bootstrap.config.HttpTransportConfiguration;
import org.apache.shardingsphere.mcp.bootstrap.config.SessionAttributionSourceConfiguration;
import org.apache.shardingsphere.mcp.bootstrap.transport.MCPTransportConstants;
-import org.apache.shardingsphere.mcp.bootstrap.transport.MCPTransportJsonMapperFactory;
import org.apache.shardingsphere.mcp.core.context.MCPRuntimeContext;
import org.apache.shardingsphere.mcp.core.session.MCPSessionManager;
import org.apache.shardingsphere.mcp.support.database.capability.MCPDatabaseCapabilityProvider;
@@ -46,8 +45,6 @@ import static org.junit.jupiter.api.Assertions.assertTrue;
class StreamableHttpMCPServerIT {
- private static final McpJsonMapper JSON_MAPPER = MCPTransportJsonMapperFactory.create();
-
private static final String CONTENT_TYPE_HEADER = "Content-Type";
private static final String ACCEPT_HEADER = "Accept";
@@ -226,7 +223,7 @@ class StreamableHttpMCPServerIT {
}
private HttpResponse<String> sendPost(final Map<String, Object> payload, final Map<String, String> headers) throws IOException, InterruptedException {
- HttpRequest.Builder requestBuilder = HttpRequest.newBuilder(endpoint).POST(HttpRequest.BodyPublishers.ofString(JSON_MAPPER.writeValueAsString(payload)));
+ HttpRequest.Builder requestBuilder = HttpRequest.newBuilder(endpoint).POST(HttpRequest.BodyPublishers.ofString(JsonEngine.marshal(payload)));
headers.forEach(requestBuilder::header);
return httpClient.send(requestBuilder.build(), HttpResponse.BodyHandlers.ofString());
}
@@ -268,8 +265,8 @@ class StreamableHttpMCPServerIT {
"params", Map.of("uri", "shardingsphere://capabilities"));
}
- private Map<?, ?> parseBody(final HttpResponse<String> response) throws IOException {
- return JSON_MAPPER.readValue(normalizeJsonBody(response.body()), Map.class);
+ private Map<?, ?> parseBody(final HttpResponse<String> response) {
+ return JsonEngine.unmarshal(normalizeJsonBody(response.body()), Map.class);
}
private String normalizeJsonBody(final String responseBody) {
@@ -293,7 +290,7 @@ class StreamableHttpMCPServerIT {
return hasDataLine ? stringBuilder.toString() : result;
}
- private Map<?, ?> assertJsonRpcError(final HttpResponse<String> response, final String requestId) throws IOException {
+ private Map<?, ?> assertJsonRpcError(final HttpResponse<String> response, final String requestId) {
Map<?, ?> actual = parseBody(response);
assertThat(actual.get("jsonrpc"), is("2.0"));
assertThat(actual.get("id"), is(requestId));
@@ -303,7 +300,7 @@ class StreamableHttpMCPServerIT {
return error;
}
- private String getRecoveryCategory(final HttpResponse<String> response) throws IOException {
+ private String getRecoveryCategory(final HttpResponse<String> response) {
Map<?, ?> error = (Map<?, ?>) parseBody(response).get("error");
Map<?, ?> data = (Map<?, ?>) error.get("data");
return String.valueOf(((Map<?, ?>) data.get("recovery")).get("category"));
diff --git a/test/e2e/mcp/src/test/java/org/apache/shardingsphere/test/e2e/mcp/support/transport/MCPInteractionPayloads.java b/test/e2e/mcp/src/test/java/org/apache/shardingsphere/test/e2e/mcp/support/transport/MCPInteractionPayloads.java
index 4c6af9e2ef0..0493645957f 100644
--- a/test/e2e/mcp/src/test/java/org/apache/shardingsphere/test/e2e/mcp/support/transport/MCPInteractionPayloads.java
+++ b/test/e2e/mcp/src/test/java/org/apache/shardingsphere/test/e2e/mcp/support/transport/MCPInteractionPayloads.java
@@ -17,13 +17,12 @@
package org.apache.shardingsphere.test.e2e.mcp.support.transport;
-import io.modelcontextprotocol.json.McpJsonMapper;
-import io.modelcontextprotocol.json.TypeRef;
import lombok.AccessLevel;
import lombok.NoArgsConstructor;
-import org.apache.shardingsphere.mcp.bootstrap.transport.MCPTransportJsonMapperFactory;
+import org.apache.shardingsphere.infra.util.json.JsonEngine;
+import org.apache.shardingsphere.infra.util.json.JsonException;
+import org.apache.shardingsphere.infra.util.json.JsonTypeReference;
-import java.io.IOException;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
@@ -34,8 +33,6 @@ import java.util.Map;
@NoArgsConstructor(access = AccessLevel.PRIVATE)
public final class MCPInteractionPayloads {
- private static final McpJsonMapper JSON_MAPPER = MCPTransportJsonMapperFactory.create();
-
/**
* Parse one MCP HTTP or STDIO JSON payload.
*
@@ -45,9 +42,9 @@ public final class MCPInteractionPayloads {
*/
public static Map<String, Object> parseJsonPayload(final String responseBody) {
try {
- return JSON_MAPPER.readValue(normalizeJsonBody(responseBody), new TypeRef<Map<String, Object>>() {
+ return JsonEngine.unmarshal(normalizeJsonBody(responseBody), new JsonTypeReference<Map<String, Object>>() {
});
- } catch (final IOException ex) {
+ } catch (final JsonException ex) {
throw new IllegalStateException("Failed to parse MCP response body.", ex);
}
}
@@ -202,9 +199,9 @@ public final class MCPInteractionPayloads {
private static Map<String, Object> parseJsonText(final String value) {
try {
- return JSON_MAPPER.readValue(value, new TypeRef<Map<String, Object>>() {
+ return JsonEngine.unmarshal(value, new JsonTypeReference<Map<String, Object>>() {
});
- } catch (final IOException ex) {
+ } catch (final JsonException ex) {
throw new IllegalStateException("Failed to parse MCP JSON text payload.", ex);
}
}
diff --git a/test/e2e/mcp/src/test/java/org/apache/shardingsphere/test/e2e/mcp/support/transport/MCPInteractionProtocolSupport.java b/test/e2e/mcp/src/test/java/org/apache/shardingsphere/test/e2e/mcp/support/transport/MCPInteractionProtocolSupport.java
index 1f7e0a90c2e..d2dc318d34f 100644
--- a/test/e2e/mcp/src/test/java/org/apache/shardingsphere/test/e2e/mcp/support/transport/MCPInteractionProtocolSupport.java
+++ b/test/e2e/mcp/src/test/java/org/apache/shardingsphere/test/e2e/mcp/support/transport/MCPInteractionProtocolSupport.java
@@ -17,13 +17,12 @@
package org.apache.shardingsphere.test.e2e.mcp.support.transport;
-import io.modelcontextprotocol.json.McpJsonMapper;
import io.modelcontextprotocol.spec.ProtocolVersions;
import lombok.AccessLevel;
import lombok.NoArgsConstructor;
-import org.apache.shardingsphere.mcp.bootstrap.transport.MCPTransportJsonMapperFactory;
+import org.apache.shardingsphere.infra.util.json.JsonEngine;
+import org.apache.shardingsphere.infra.util.json.JsonException;
-import java.io.IOException;
import java.util.LinkedHashMap;
import java.util.Map;
@@ -35,8 +34,6 @@ public final class MCPInteractionProtocolSupport {
public static final String PROTOCOL_VERSION = ProtocolVersions.MCP_2025_11_25;
- private static final McpJsonMapper JSON_MAPPER = MCPTransportJsonMapperFactory.create();
-
/**
* Create initialize request parameters for the given client name.
*
@@ -108,8 +105,8 @@ public final class MCPInteractionProtocolSupport {
private static String toJsonString(final Object value) {
try {
- return JSON_MAPPER.writeValueAsString(value);
- } catch (final IOException ex) {
+ return JsonEngine.marshal(value);
+ } catch (final JsonException ex) {
throw new IllegalStateException("Failed to serialize MCP JSON-RPC payload.", ex);
}
}
diff --git a/test/e2e/mcp/src/test/java/org/apache/shardingsphere/test/e2e/mcp/support/transport/client/AbstractProcessMCPStdioInteractionClient.java b/test/e2e/mcp/src/test/java/org/apache/shardingsphere/test/e2e/mcp/support/transport/client/AbstractProcessMCPStdioInteractionClient.java
index adbf94967b9..c4ba1196e11 100644
--- a/test/e2e/mcp/src/test/java/org/apache/shardingsphere/test/e2e/mcp/support/transport/client/AbstractProcessMCPStdioInteractionClient.java
+++ b/test/e2e/mcp/src/test/java/org/apache/shardingsphere/test/e2e/mcp/support/transport/client/AbstractProcessMCPStdioInteractionClient.java
@@ -17,9 +17,9 @@
package org.apache.shardingsphere.test.e2e.mcp.support.transport.client;
-import io.modelcontextprotocol.json.McpJsonMapper;
-import io.modelcontextprotocol.json.TypeRef;
-import org.apache.shardingsphere.mcp.bootstrap.transport.MCPTransportJsonMapperFactory;
+import org.apache.shardingsphere.infra.util.json.JsonEngine;
+import org.apache.shardingsphere.infra.util.json.JsonException;
+import org.apache.shardingsphere.infra.util.json.JsonTypeReference;
import org.apache.shardingsphere.test.e2e.mcp.support.artifact.MCPArtifactUtils;
import org.apache.shardingsphere.test.e2e.mcp.support.transport.MCPInteractionPayloads;
import org.apache.shardingsphere.test.e2e.mcp.support.transport.MCPInteractionProtocolSupport;
@@ -53,8 +53,6 @@ abstract class AbstractProcessMCPStdioInteractionClient extends AbstractMCPInter
private static final String INITIALIZE_REQUEST_ID = "init-1";
- private static final McpJsonMapper JSON_MAPPER = MCPTransportJsonMapperFactory.create();
-
private final List<String> stdErrorMessages = new CopyOnWriteArrayList<>();
private Process process;
@@ -154,7 +152,11 @@ abstract class AbstractProcessMCPStdioInteractionClient extends AbstractMCPInter
}
private void writeJsonRpcMessage(final Map<String, Object> payload) throws IOException {
- writer.write(JSON_MAPPER.writeValueAsString(payload));
+ try {
+ writer.write(JsonEngine.marshal(payload));
+ } catch (final JsonException ex) {
+ throw new IOException(ex.getMessage(), ex);
+ }
writer.newLine();
writer.flush();
}
@@ -166,8 +168,13 @@ abstract class AbstractProcessMCPStdioInteractionClient extends AbstractMCPInter
if (line.isBlank()) {
continue;
}
- Map<String, Object> result = JSON_MAPPER.readValue(line, new TypeRef<>() {
- });
+ Map<String, Object> result;
+ try {
+ result = JsonEngine.unmarshal(line, new JsonTypeReference<Map<String, Object>>() {
+ });
+ } catch (final JsonException ex) {
+ throw new IOException(ex.getMessage(), ex);
+ }
if (requestId.equals(String.valueOf(result.get("id")))) {
return result;
}