Initial commit
This commit is contained in:
commit
fdbd94b3f9
4 changed files with 2833 additions and 0 deletions
51
src/mcpdotnet/Utils/Json/ContentJsonConverter.cs
Normal file
51
src/mcpdotnet/Utils/Json/ContentJsonConverter.cs
Normal file
|
|
@ -0,0 +1,51 @@
|
|||
// Utils/Json/ContentJsonConverter.cs
|
||||
namespace McpDotNet.Utils.Json;
|
||||
|
||||
using System.Text.Json;
|
||||
using System.Text.Json.Serialization;
|
||||
using global::McpDotNet.Protocol.Types;
|
||||
|
||||
/// <summary>
|
||||
/// JSON converter for IContent that handles polymorphic deserialization based on the "type" property.
|
||||
/// </summary>
|
||||
public class ContentJsonConverter : JsonConverter<IContent>
|
||||
{
|
||||
public override IContent? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
|
||||
{
|
||||
if (reader.TokenType != JsonTokenType.StartObject)
|
||||
{
|
||||
throw new JsonException("Expected StartObject token");
|
||||
}
|
||||
|
||||
using var doc = JsonDocument.ParseValue(ref reader);
|
||||
var root = doc.RootElement;
|
||||
|
||||
if (!root.TryGetProperty("type", out var typeProperty))
|
||||
{
|
||||
throw new JsonException("Missing 'type' property");
|
||||
}
|
||||
|
||||
var type = typeProperty.GetString();
|
||||
return type switch
|
||||
{
|
||||
"text" => JsonSerializer.Deserialize<TextContent>(root.GetRawText(), options),
|
||||
"image" => JsonSerializer.Deserialize<ImageContent>(root.GetRawText(), options),
|
||||
_ => throw new JsonException($"Unknown content type: {type}")
|
||||
};
|
||||
}
|
||||
|
||||
public override void Write(Utf8JsonWriter writer, IContent value, JsonSerializerOptions options)
|
||||
{
|
||||
switch (value)
|
||||
{
|
||||
case TextContent text:
|
||||
JsonSerializer.Serialize(writer, text, options);
|
||||
break;
|
||||
case ImageContent image:
|
||||
JsonSerializer.Serialize(writer, image, options);
|
||||
break;
|
||||
default:
|
||||
throw new JsonException($"Unknown content type: {value.GetType()}");
|
||||
}
|
||||
}
|
||||
}
|
||||
86
src/mcpdotnet/Utils/Json/JsonRpcMessageConverter.cs
Normal file
86
src/mcpdotnet/Utils/Json/JsonRpcMessageConverter.cs
Normal file
|
|
@ -0,0 +1,86 @@
|
|||
// Utils/Json/JsonRpcMessageConverter.cs (continued)
|
||||
namespace McpDotNet.Utils.Json;
|
||||
|
||||
using System.Text.Json;
|
||||
using System.Text.Json.Serialization;
|
||||
using global::McpDotNet.Protocol.Messages;
|
||||
|
||||
/// <summary>
|
||||
/// JSON converter for IJsonRpcMessage that handles polymorphic deserialization of different message types.
|
||||
/// </summary>
|
||||
public class JsonRpcMessageConverter : JsonConverter<IJsonRpcMessage>
|
||||
{
|
||||
public override IJsonRpcMessage? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
|
||||
{
|
||||
if (reader.TokenType != JsonTokenType.StartObject)
|
||||
{
|
||||
throw new JsonException("Expected StartObject token");
|
||||
}
|
||||
|
||||
using var doc = JsonDocument.ParseValue(ref reader);
|
||||
var root = doc.RootElement;
|
||||
|
||||
// All JSON-RPC messages must have a jsonrpc property with value "2.0"
|
||||
if (!root.TryGetProperty("jsonrpc", out var versionProperty) ||
|
||||
versionProperty.GetString() != "2.0")
|
||||
{
|
||||
throw new JsonException("Invalid or missing jsonrpc version");
|
||||
}
|
||||
|
||||
// Determine the message type based on the presence of id, method, and error properties
|
||||
bool hasId = root.TryGetProperty("id", out _);
|
||||
bool hasMethod = root.TryGetProperty("method", out _);
|
||||
bool hasError = root.TryGetProperty("error", out _);
|
||||
|
||||
var rawText = root.GetRawText();
|
||||
|
||||
// Messages with an id but no method are responses
|
||||
if (hasId && !hasMethod)
|
||||
{
|
||||
// Messages with an error property are error responses
|
||||
if (hasError)
|
||||
{
|
||||
return JsonSerializer.Deserialize<JsonRpcError>(rawText, options);
|
||||
}
|
||||
// Messages with a result property are success responses
|
||||
else if (root.TryGetProperty("result", out _))
|
||||
{
|
||||
return JsonSerializer.Deserialize<JsonRpcResponse>(rawText, options);
|
||||
}
|
||||
throw new JsonException("Response must have either result or error");
|
||||
}
|
||||
// Messages with a method but no id are notifications
|
||||
else if (hasMethod && !hasId)
|
||||
{
|
||||
return JsonSerializer.Deserialize<JsonRpcNotification>(rawText, options);
|
||||
}
|
||||
// Messages with both method and id are requests
|
||||
else if (hasMethod && hasId)
|
||||
{
|
||||
return JsonSerializer.Deserialize<JsonRpcRequest>(rawText, options);
|
||||
}
|
||||
|
||||
throw new JsonException("Invalid JSON-RPC message format");
|
||||
}
|
||||
|
||||
public override void Write(Utf8JsonWriter writer, IJsonRpcMessage value, JsonSerializerOptions options)
|
||||
{
|
||||
switch (value)
|
||||
{
|
||||
case JsonRpcRequest request:
|
||||
JsonSerializer.Serialize(writer, request, options);
|
||||
break;
|
||||
case JsonRpcNotification notification:
|
||||
JsonSerializer.Serialize(writer, notification, options);
|
||||
break;
|
||||
case JsonRpcResponse response:
|
||||
JsonSerializer.Serialize(writer, response, options);
|
||||
break;
|
||||
case JsonRpcError error:
|
||||
JsonSerializer.Serialize(writer, error, options);
|
||||
break;
|
||||
default:
|
||||
throw new JsonException($"Unknown JSON-RPC message type: {value.GetType()}");
|
||||
}
|
||||
}
|
||||
}
|
||||
31
src/mcpdotnet/Utils/Json/JsonSerializerOptionsExtensions.cs
Normal file
31
src/mcpdotnet/Utils/Json/JsonSerializerOptionsExtensions.cs
Normal file
|
|
@ -0,0 +1,31 @@
|
|||
namespace McpDotNet.Utils.Json;
|
||||
|
||||
using System.Text.Json;
|
||||
using System.Text.Json.Serialization;
|
||||
|
||||
/// <summary>
|
||||
/// Extensions for configuring System.Text.Json serialization options for MCP.
|
||||
/// </summary>
|
||||
public static class JsonSerializerOptionsExtensions
|
||||
{
|
||||
/// <summary>
|
||||
/// Configures JsonSerializerOptions with MCP-specific settings and converters.
|
||||
/// </summary>
|
||||
/// <param name="options">The options to configure.</param>
|
||||
/// <returns>The configured options.</returns>
|
||||
public static JsonSerializerOptions ConfigureForMcp(this JsonSerializerOptions options)
|
||||
{
|
||||
// Add custom converters
|
||||
options.Converters.Add(new JsonRpcMessageConverter());
|
||||
options.Converters.Add(new ContentJsonConverter());
|
||||
options.Converters.Add(new ResourceContentsJsonConverter());
|
||||
options.Converters.Add(new JsonStringEnumConverter());
|
||||
|
||||
// Configure general options
|
||||
options.PropertyNameCaseInsensitive = true;
|
||||
options.DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull;
|
||||
options.NumberHandling = JsonNumberHandling.AllowReadingFromString;
|
||||
|
||||
return options;
|
||||
}
|
||||
}
|
||||
49
src/mcpdotnet/Utils/Json/ResourceContentsJsonConverter.cs
Normal file
49
src/mcpdotnet/Utils/Json/ResourceContentsJsonConverter.cs
Normal file
|
|
@ -0,0 +1,49 @@
|
|||
namespace McpDotNet.Utils.Json;
|
||||
|
||||
using System.Text.Json;
|
||||
using System.Text.Json.Serialization;
|
||||
using global::McpDotNet.Protocol.Types;
|
||||
|
||||
/// <summary>
|
||||
/// JSON converter for IResourceContents that handles polymorphic deserialization based on content properties.
|
||||
/// </summary>
|
||||
public class ResourceContentsJsonConverter : JsonConverter<IResourceContents>
|
||||
{
|
||||
public override IResourceContents? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
|
||||
{
|
||||
if (reader.TokenType != JsonTokenType.StartObject)
|
||||
{
|
||||
throw new JsonException("Expected StartObject token");
|
||||
}
|
||||
|
||||
using var doc = JsonDocument.ParseValue(ref reader);
|
||||
var root = doc.RootElement;
|
||||
|
||||
// Determine the type based on whether it has a 'text' or 'blob' property
|
||||
if (root.TryGetProperty("text", out _))
|
||||
{
|
||||
return JsonSerializer.Deserialize<TextResourceContents>(root.GetRawText(), options);
|
||||
}
|
||||
else if (root.TryGetProperty("blob", out _))
|
||||
{
|
||||
return JsonSerializer.Deserialize<BlobResourceContents>(root.GetRawText(), options);
|
||||
}
|
||||
|
||||
throw new JsonException("Resource contents must have either 'text' or 'blob' property");
|
||||
}
|
||||
|
||||
public override void Write(Utf8JsonWriter writer, IResourceContents value, JsonSerializerOptions options)
|
||||
{
|
||||
switch (value)
|
||||
{
|
||||
case TextResourceContents text:
|
||||
JsonSerializer.Serialize(writer, text, options);
|
||||
break;
|
||||
case BlobResourceContents blob:
|
||||
JsonSerializer.Serialize(writer, blob, options);
|
||||
break;
|
||||
default:
|
||||
throw new JsonException($"Unknown resource contents type: {value.GetType()}");
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Reference in a new issue