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.Immutable; 023 024import static java.util.Objects.requireNonNull; 025 026/** 027 * Immutable resource-read result payload. 028 * 029 * @author <a href="https://www.revetkn.com">Mark Allen</a> 030 */ 031@Immutable 032public record McpResourceContents( 033 @NonNull String uri, 034 @NonNull String mimeType, 035 @Nullable String text, 036 @Nullable String blobBase64 037) { 038 public McpResourceContents { 039 requireNonNull(uri); 040 requireNonNull(mimeType); 041 042 if ((text == null) == (blobBase64 == null)) 043 throw new IllegalArgumentException("Exactly one of text or blobBase64 must be present."); 044 } 045 046 /** 047 * Creates a text-backed resource-contents payload. 048 * 049 * @param uri the resource URI 050 * @param text the text payload 051 * @param mimeType the MIME type 052 * @return a text-backed resource contents value 053 */ 054 @NonNull 055 public static McpResourceContents fromText(@NonNull String uri, 056 @NonNull String text, 057 @NonNull String mimeType) { 058 requireNonNull(uri); 059 requireNonNull(text); 060 requireNonNull(mimeType); 061 return new McpResourceContents(uri, mimeType, text, null); 062 } 063 064 /** 065 * Creates a blob-backed resource-contents payload. 066 * 067 * @param uri the resource URI 068 * @param blobBase64 the base64-encoded blob payload 069 * @param mimeType the MIME type 070 * @return a blob-backed resource contents value 071 */ 072 @NonNull 073 public static McpResourceContents fromBlob(@NonNull String uri, 074 @NonNull String blobBase64, 075 @NonNull String mimeType) { 076 requireNonNull(uri); 077 requireNonNull(blobBase64); 078 requireNonNull(mimeType); 079 return new McpResourceContents(uri, mimeType, null, blobBase64); 080 } 081}