001/* 002 * Copyright 2022-2026 Revetware LLC. 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016 017package com.soklet; 018 019import org.jspecify.annotations.NonNull; 020import org.jspecify.annotations.Nullable; 021 022import javax.annotation.concurrent.ThreadSafe; 023 024import static java.util.Objects.requireNonNull; 025 026/** 027 * Converts app-owned structured content objects into {@link McpValue}. 028 * 029 * @author <a href="https://www.revetkn.com">Mark Allen</a> 030 */ 031@ThreadSafe 032public interface McpResponseMarshaler { 033 /** 034 * Converts app-owned structured content into an {@link McpValue} tree that Soklet can embed into the MCP response. 035 * 036 * @param value the app-owned structured content value, possibly {@code null} 037 * @param context the marshaling context for the current tool call 038 * @return the marshaled MCP value 039 */ 040 @NonNull 041 McpValue marshalStructuredContent(@Nullable Object value, 042 @NonNull McpStructuredContentContext context); 043 044 /** 045 * Acquires Soklet's default structured-content marshaler. 046 * 047 * @return the default marshaler instance 048 */ 049 @NonNull 050 static McpResponseMarshaler defaultInstance() { 051 return DefaultMcpResponseMarshaler.INSTANCE; 052 } 053} 054 055enum DefaultMcpResponseMarshaler implements McpResponseMarshaler { 056 INSTANCE; 057 058 @NonNull 059 @Override 060 public McpValue marshalStructuredContent(@Nullable Object value, 061 @NonNull McpStructuredContentContext context) { 062 requireNonNull(context); 063 064 if (value == null) 065 return McpNull.INSTANCE; 066 067 if (value instanceof McpValue mcpValue) 068 return mcpValue; 069 070 throw new IllegalArgumentException("Structured content value '%s' is not an McpValue. Supply a custom McpResponseMarshaler to handle arbitrary objects.".formatted(value.getClass().getName())); 071 } 072}