-
Notifications
You must be signed in to change notification settings - Fork 36
Fix blob serialization in MCP output adaptation #1275
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
DivyaMaddipudi
wants to merge
1
commit into
smithy-lang:main
Choose a base branch
from
DivyaMaddipudi:fix-blob-serialization
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+100
−2
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
93 changes: 93 additions & 0 deletions
93
mcp/mcp-server/src/test/java/software/amazon/smithy/java/mcp/server/McpServiceTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,93 @@ | ||
| /* | ||
| * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| */ | ||
|
|
||
| package software.amazon.smithy.java.mcp.server; | ||
|
|
||
| import static org.junit.jupiter.api.Assertions.assertEquals; | ||
| import static org.junit.jupiter.api.Assertions.assertNull; | ||
| import static org.junit.jupiter.api.Assertions.assertThrows; | ||
|
|
||
| import java.nio.charset.StandardCharsets; | ||
| import java.util.Base64; | ||
| import java.util.Collections; | ||
| import java.util.List; | ||
| import java.util.Map; | ||
| import org.junit.jupiter.api.BeforeEach; | ||
| import org.junit.jupiter.api.Test; | ||
| import software.amazon.smithy.java.core.schema.Schema; | ||
| import software.amazon.smithy.java.core.serde.document.Document; | ||
| import software.amazon.smithy.model.shapes.ShapeId; | ||
|
|
||
| class McpServiceTest { | ||
|
|
||
| private McpService service; | ||
|
|
||
| @BeforeEach | ||
| void setUp() { | ||
| service = new McpService( | ||
| Collections.emptyMap(), | ||
| List.of(), | ||
| "test", | ||
| "1.0", | ||
| null, | ||
| null, | ||
| null | ||
| ); | ||
| } | ||
|
|
||
| @Test | ||
| void testAdaptOutputBlobFromBinaryDocument() { | ||
| var schema = Schema.createBlob(ShapeId.from("smithy.test#Blob")); | ||
| var bytes = "Hello, World!".getBytes(StandardCharsets.UTF_8); | ||
|
|
||
| var result = service.adaptOutputDocument(Document.of(bytes), schema); | ||
|
|
||
| assertEquals(Base64.getEncoder().encodeToString(bytes), result.asString()); | ||
| } | ||
|
|
||
| @Test | ||
| void testAdaptOutputBlobFromStringDocument() { | ||
| var schema = Schema.createBlob(ShapeId.from("smithy.test#Blob")); | ||
| var base64 = Base64.getEncoder().encodeToString("payload".getBytes(StandardCharsets.UTF_8)); | ||
|
|
||
| var result = service.adaptOutputDocument(Document.of(base64), schema); | ||
|
|
||
| assertEquals(base64, result.asString()); | ||
| } | ||
|
|
||
| @Test | ||
| void testAdaptOutputBlobFromUnsupportedDocumentTypeThrows() { | ||
| var schema = Schema.createBlob(ShapeId.from("smithy.test#Blob")); | ||
|
|
||
| assertThrows(Exception.class, () -> service.adaptOutputDocument(Document.of(42), schema)); | ||
| } | ||
|
|
||
| @Test | ||
| void testAdaptOutputNullReturnsNull() { | ||
| var schema = Schema.createBlob(ShapeId.from("smithy.test#Blob")); | ||
|
|
||
| assertNull(service.adaptOutputDocument(null, schema)); | ||
| } | ||
|
|
||
| @Test | ||
| void testAdaptOutputStructureWithBlobFields() { | ||
| var blobSchema = Schema.createBlob(ShapeId.from("smithy.test#Blob")); | ||
| var structSchema = Schema.structureBuilder(ShapeId.from("smithy.test#Output")) | ||
| .putMember("binaryBlob", blobSchema) | ||
| .putMember("stringBlob", blobSchema) | ||
| .build(); | ||
|
|
||
| var bytes = "binary content".getBytes(StandardCharsets.UTF_8); | ||
| var base64 = Base64.getEncoder().encodeToString("string content".getBytes(StandardCharsets.UTF_8)); | ||
| var doc = Document.of(Map.of( | ||
| "binaryBlob", Document.of(bytes), | ||
| "stringBlob", Document.of(base64))); | ||
|
|
||
| var result = service.adaptOutputDocument(doc, structSchema); | ||
|
|
||
| assertEquals(Base64.getEncoder().encodeToString(bytes), result.getMember("binaryBlob").asString()); | ||
| assertEquals(base64, result.getMember("stringBlob").asString()); | ||
| } | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is a incorrect fix and in the wrong layer. The premise of this fix is also incorrect
That's the representation over the wire not after materialization.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The actual issue is Document.of(output) at McpService.java:704 round-trips JsonDocuments.StringDocument (has asBlob()) into core Documents.StringDocument (throws on asBlob()) before adaptOutputDocument resolves @OneOf document members containing blob fields. Should I fix it by skipping the round-trip (cast StructDocument directly since it implements Document), or add asBlob() to core Documents.StringDocument?