forked from modelcontextprotocol/csharp-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMcpJsonUtilities.McpJsonConverter.cs
More file actions
91 lines (79 loc) · 3 KB
/
McpJsonUtilities.McpJsonConverter.cs
File metadata and controls
91 lines (79 loc) · 3 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
using System.ComponentModel;
using System.Text.Json;
using System.Text.Json.Nodes;
using System.Text.Json.Serialization;
using System.Text.Json.Serialization.Metadata;
namespace ModelContextProtocol;
public static partial class McpJsonUtilities
{
[ThreadStatic]
private static McpSession? t_currentMcpSession;
/// <summary>
/// Serializes the given value to a <see cref="JsonNode"/> using the provided <see cref="JsonTypeInfo{T}"/>,
/// </summary>
internal static JsonNode? SerializeContextual<T>(T? value, JsonTypeInfo<T> typeInfo, McpSession session)
{
if (session is null)
{
Throw.IfNull(session);
}
if (t_currentMcpSession is not null)
{
throw new InvalidOperationException("Reentrant call to McpJsonUtilities.SerializeContextual detected.");
}
t_currentMcpSession = session;
try
{
return JsonSerializer.SerializeToNode(value!, typeInfo);
}
finally
{
t_currentMcpSession = null;
}
}
/// <summary>
/// Deserializes the given value to a <see cref="JsonNode"/> using the provided <see cref="JsonTypeInfo{T}"/>,
/// </summary>
internal static T? DeserializeContextual<T>(JsonNode? node, JsonTypeInfo<T> typeInfo, McpSession session)
{
if (session is null)
{
Throw.IfNull(session);
}
if (t_currentMcpSession is not null)
{
throw new InvalidOperationException("Reentrant call to McpJsonUtilities.DeserializeContextual detected.");
}
t_currentMcpSession = session;
try
{
return JsonSerializer.Deserialize(node, typeInfo);
}
finally
{
t_currentMcpSession = null;
}
}
/// <summary>
/// Defines an abstract JSON converter that has access to the current <see cref="IMcpEndpoint"/> context during serialization and deserialization.
/// </summary>
/// <typeparam name="T">The type being converted.</typeparam>
[EditorBrowsable(EditorBrowsableState.Never)]
public abstract class McpContextualJsonConverter<T> : JsonConverter<T>
{
/// <summary>
/// Reads the JSON representation of the value.
/// </summary>
public abstract T? Read(ref Utf8JsonReader reader, McpSession? session, JsonSerializerOptions options);
/// <summary>
/// Writes the JSON representation of the value.
/// </summary>
public abstract void Write(Utf8JsonWriter writer, T value, McpSession? session, JsonSerializerOptions options);
/// <inheritdoc/>
public sealed override T? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) =>
Read(ref reader, t_currentMcpSession, options);
/// <inheritdoc/>
public sealed override void Write(Utf8JsonWriter writer, T value, JsonSerializerOptions options) =>
Write(writer, value, t_currentMcpSession, options);
}
}