-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathMcpToolResult.cs
More file actions
109 lines (95 loc) · 4.06 KB
/
McpToolResult.cs
File metadata and controls
109 lines (95 loc) · 4.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
// The Sisk Framework source code
// Copyright (c) 2024- PROJECT PRINCIPIUM and all Sisk contributors
//
// The code below is licensed under the MIT license as
// of the date of its publication, available at
//
// File name: McpToolResult.cs
// Repository: https://github.com/sisk-http/core
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Sisk.ModelContextProtocol;
/// <summary>
/// Represents the result of executing an MCP tool.
/// </summary>
public sealed class McpToolResult {
/// <summary>
/// Gets the JSON representation of the tool result.
/// </summary>
public JsonValue Result { get; }
/// <summary>
/// Creates a text-based result for an MCP tool.
/// </summary>
/// <param name="text">The text content of the result.</param>
/// <returns>An <see cref="McpToolResult"/> representing a text result.</returns>
public static McpToolResult CreateText ( string text ) {
return new McpToolResult ( new JsonObject () {
[ "type" ] = "text",
[ "text" ] = text
} );
}
/// <summary>
/// Creates an image-based result for an MCP tool.
/// </summary>
/// <param name="imageBytes">The byte array representing the image data.</param>
/// <param name="mimeType">The MIME type of the image (e.g., "image/png"). Defaults to "image/png".</param>
/// <returns>An <see cref="McpToolResult"/> representing an image result.</returns>
public static McpToolResult CreateImage ( ReadOnlySpan<byte> imageBytes, string mimeType = "image/png" ) {
return new McpToolResult ( new JsonObject () {
[ "type" ] = "image",
[ "data" ] = Convert.ToBase64String ( imageBytes, Base64FormattingOptions.None ),
[ "mimeType" ] = mimeType
} );
}
/// <summary>
/// Creates an audio-based result for an MCP tool.
/// </summary>
/// <param name="audioBytes">The byte array representing the audio data.</param>
/// <param name="mimeType">The MIME type of the audio (e.g., "audio/wav"). Defaults to "audio/wav".</param>
/// <returns>An <see cref="McpToolResult"/> representing an audio result.</returns>
public static McpToolResult CreateAudio ( ReadOnlySpan<byte> audioBytes, string mimeType = "audio/wav" ) {
return new McpToolResult ( new JsonObject () {
[ "type" ] = "audio",
[ "data" ] = Convert.ToBase64String ( audioBytes, Base64FormattingOptions.None ),
[ "mimeType" ] = mimeType
} );
}
/// <summary>
/// Combines multiple <see cref="McpToolResult"/> objects into a single result.
/// If the input contains a single result, it is returned directly. Otherwise,
/// all individual results are unpacked and combined into a JSON array.
/// </summary>
/// <param name="results">A collection of <see cref="McpToolResult"/> objects to combine.</param>
/// <returns>A single <see cref="McpToolResult"/> containing the combined results.</returns>
public static McpToolResult Combine ( params McpToolResult [] results ) {
if (results.Length == 1)
return results [ 0 ];
JsonArray resultsValue = [];
void Unpack ( JsonValue value ) {
if (value.Type == JsonValueType.Object) {
resultsValue.Add ( value );
}
else if (value.Type == JsonValueType.Array) {
foreach (var item in value.GetJsonArray ()) {
Unpack ( item );
}
}
}
foreach (var item in results) {
Unpack ( item.Result );
}
return new McpToolResult ( resultsValue );
}
/// <summary>
/// Initializes a new instance of the <see cref="McpToolResult"/> class.
/// </summary>
/// <param name="result">The JSON value representing the tool result.</param>
public McpToolResult ( JsonValue result ) {
Result = result;
}
/// <inheritdoc/>
public static implicit operator McpToolResult ( string text ) => CreateText ( text );
}