Change type of Tool.InputSchema to be JsonElement (#4)
* Change type of Tool.InputSchema to be JsonElement * Add schema validation in the InputSchema property setter. * Simplify AIFunction tool adapter. --------- Co-authored-by: Eirik Tsarpalis <eirik.tsarpalis@gmail.com>
This commit is contained in:
parent
d23e065945
commit
3de19d38e4
1 changed files with 175 additions and 128 deletions
|
|
@ -77,6 +77,31 @@ public static partial class McpJsonUtilities
|
|||
internal static JsonTypeInfo<T> GetTypeInfo<T>(this JsonSerializerOptions options) =>
|
||||
(JsonTypeInfo<T>)options.GetTypeInfo(typeof(T));
|
||||
|
||||
internal static JsonElement DefaultMcpToolSchema = ParseJsonElement("{\"type\":\"object\"}"u8);
|
||||
internal static bool IsValidMcpToolSchema(JsonElement element)
|
||||
{
|
||||
if (element.ValueKind is not JsonValueKind.Object)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
foreach (JsonProperty property in element.EnumerateObject())
|
||||
{
|
||||
if (property.NameEquals("type"))
|
||||
{
|
||||
if (property.Value.ValueKind is not JsonValueKind.String ||
|
||||
!property.Value.ValueEquals("object"))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
return true; // No need to check other properties
|
||||
}
|
||||
}
|
||||
|
||||
return false; // No type keyword found.
|
||||
}
|
||||
|
||||
// Keep in sync with CreateDefaultOptions above.
|
||||
[JsonSourceGenerationOptions(JsonSerializerDefaults.Web,
|
||||
UseStringEnumConverter = true,
|
||||
|
|
@ -96,7 +121,13 @@ public static partial class McpJsonUtilities
|
|||
[JsonSerializable(typeof(CreateMessageResult))]
|
||||
[JsonSerializable(typeof(ListRootsResult))]
|
||||
[JsonSerializable(typeof(InitializeResult))]
|
||||
[JsonSerializable(typeof(JsonSchema))]
|
||||
[JsonSerializable(typeof(CallToolResponse))]
|
||||
internal sealed partial class JsonContext : JsonSerializerContext;
|
||||
|
||||
private static JsonElement ParseJsonElement(ReadOnlySpan<byte> utf8Json)
|
||||
{
|
||||
Utf8JsonReader reader = new(utf8Json);
|
||||
return JsonElement.ParseValue(ref reader);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue