Skip to content

Commit 414f707

Browse files
rhernandez35adwsingh
authored andcommitted
Add structured content to tool responses when possible
1 parent 98f9605 commit 414f707

2 files changed

Lines changed: 24 additions & 7 deletions

File tree

mcp/mcp-schemas/model/main.smithy

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,8 @@ structure CallToolResult {
171171
// TODO add others
172172
content: TextContentList
173173

174+
structuredContent: Document
175+
174176
@default(false)
175177
isError: Boolean
176178
}

mcp/mcp-server/src/main/java/software/amazon/smithy/java/mcp/server/McpServer.java

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -170,8 +170,7 @@ private void handleRequest(JsonRpcRequest req) {
170170
writeResponse(req.getId(), result);
171171
}
172172
case "tools/list" -> {
173-
boolean supportsOutputSchema = protocolVersion != null
174-
&& protocolVersion.compareTo(ProtocolVersion.v2025_06_18.INSTANCE) >= 0;
173+
var supportsOutputSchema = supportsOutputSchema();
175174
writeResponse(req.getId(),
176175
ListToolsResult.builder()
177176
.tools(tools.values()
@@ -223,11 +222,7 @@ private void handleRequest(JsonRpcRequest req) {
223222
var adaptedDoc = adaptDocument(argumentsDoc, operation.getApiOperation().inputSchema());
224223
var input = adaptedDoc.asShape(operation.getApiOperation().inputBuilder());
225224
var output = operation.function().apply(input, null);
226-
var result = CallToolResult.builder()
227-
.content(List.of(TextContent.builder()
228-
.text(CODEC.serializeToString((SerializableShape) output))
229-
.build()))
230-
.build();
225+
var result = formatStructuredContent(tool, (SerializableShape) output);
231226
writeResponse(req.getId(), result);
232227
}
233228
}
@@ -240,6 +235,26 @@ private void handleRequest(JsonRpcRequest req) {
240235
}
241236
}
242237

238+
private boolean supportsOutputSchema() {
239+
return protocolVersion != null && protocolVersion.compareTo(ProtocolVersion.v2025_06_18.INSTANCE) >= 0;
240+
}
241+
242+
private CallToolResult formatStructuredContent(Tool tool, SerializableShape output) {
243+
var result = CallToolResult.builder()
244+
.content(List.of(TextContent.builder()
245+
.text(CODEC.serializeToString(output))
246+
.build()));
247+
248+
if (supportsOutputSchema()) {
249+
var outputSchema = tool.toolInfo().getOutputSchema();
250+
if (outputSchema != null) {
251+
result.structuredContent(Document.of(output));
252+
}
253+
}
254+
255+
return result.build();
256+
}
257+
243258
private ToolInfo extractToolInfo(Tool tool, boolean supportsOutput) {
244259
var toolInfo = tool.toolInfo();
245260
if (supportsOutput || toolInfo.getOutputSchema() == null) {

0 commit comments

Comments
 (0)