|
| 1 | +using System.ComponentModel; |
| 2 | +using System.Text.Json; |
| 3 | +using System.Text.Json.Nodes; |
| 4 | +using System.Text.Json.Serialization; |
| 5 | +using System.Text.Json.Serialization.Metadata; |
| 6 | + |
| 7 | +namespace ModelContextProtocol; |
| 8 | + |
| 9 | +public static partial class McpJsonUtilities |
| 10 | +{ |
| 11 | + [ThreadStatic] |
| 12 | + private static McpSession? t_currentMcpSession; |
| 13 | + |
| 14 | + /// <summary> |
| 15 | + /// Serializes the given value to a <see cref="JsonNode"/> using the provided <see cref="JsonTypeInfo{T}"/>, |
| 16 | + /// </summary> |
| 17 | + internal static JsonNode? SerializeContextual<T>(T? value, JsonTypeInfo<T> typeInfo, McpSession session) |
| 18 | + { |
| 19 | + if (session is null) |
| 20 | + { |
| 21 | + Throw.IfNull(session); |
| 22 | + } |
| 23 | + |
| 24 | + if (t_currentMcpSession is not null) |
| 25 | + { |
| 26 | + throw new InvalidOperationException("Reentrant call to McpJsonUtilities.SerializeContextual detected."); |
| 27 | + } |
| 28 | + |
| 29 | + t_currentMcpSession = session; |
| 30 | + try |
| 31 | + { |
| 32 | + return JsonSerializer.SerializeToNode(value!, typeInfo); |
| 33 | + } |
| 34 | + finally |
| 35 | + { |
| 36 | + t_currentMcpSession = null; |
| 37 | + } |
| 38 | + } |
| 39 | + |
| 40 | + /// <summary> |
| 41 | + /// Deserializes the given value to a <see cref="JsonNode"/> using the provided <see cref="JsonTypeInfo{T}"/>, |
| 42 | + /// </summary> |
| 43 | + internal static T? DeserializeContextual<T>(JsonNode? node, JsonTypeInfo<T> typeInfo, McpSession session) |
| 44 | + { |
| 45 | + if (session is null) |
| 46 | + { |
| 47 | + Throw.IfNull(session); |
| 48 | + } |
| 49 | + |
| 50 | + if (t_currentMcpSession is not null) |
| 51 | + { |
| 52 | + throw new InvalidOperationException("Reentrant call to McpJsonUtilities.DeserializeContextual detected."); |
| 53 | + } |
| 54 | + |
| 55 | + t_currentMcpSession = session; |
| 56 | + try |
| 57 | + { |
| 58 | + return JsonSerializer.Deserialize(node, typeInfo); |
| 59 | + } |
| 60 | + finally |
| 61 | + { |
| 62 | + t_currentMcpSession = null; |
| 63 | + } |
| 64 | + } |
| 65 | + |
| 66 | + /// <summary> |
| 67 | + /// Defines an abstract JSON converter that has access to the current <see cref="IMcpEndpoint"/> context during serialization and deserialization. |
| 68 | + /// </summary> |
| 69 | + /// <typeparam name="T">The type being converted.</typeparam> |
| 70 | + [EditorBrowsable(EditorBrowsableState.Never)] |
| 71 | + public abstract class McpContextualJsonConverter<T> : JsonConverter<T> |
| 72 | + { |
| 73 | + /// <summary> |
| 74 | + /// Reads the JSON representation of the value. |
| 75 | + /// </summary> |
| 76 | + public abstract T? Read(ref Utf8JsonReader reader, McpSession? session, JsonSerializerOptions options); |
| 77 | + |
| 78 | + /// <summary> |
| 79 | + /// Writes the JSON representation of the value. |
| 80 | + /// </summary> |
| 81 | + public abstract void Write(Utf8JsonWriter writer, T value, McpSession? session, JsonSerializerOptions options); |
| 82 | + |
| 83 | + /// <inheritdoc/> |
| 84 | + public sealed override T? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) => |
| 85 | + Read(ref reader, t_currentMcpSession, options); |
| 86 | + |
| 87 | + /// <inheritdoc/> |
| 88 | + public sealed override void Write(Utf8JsonWriter writer, T value, JsonSerializerOptions options) => |
| 89 | + Write(writer, value, t_currentMcpSession, options); |
| 90 | + } |
| 91 | +} |
0 commit comments