@@ -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