Use JsonElement.Parse for string-to-JsonElement deserialization (#1002)
Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com> Co-authored-by: stephentoub <2642209+stephentoub@users.noreply.github.com>
This commit is contained in:
parent
9395394c1b
commit
a559bfb891
6 changed files with 48 additions and 48 deletions
|
|
@ -78,16 +78,16 @@ async ValueTask<ElicitResult> HandleElicitationAsync(ElicitRequestParams? reques
|
|||
// Try standard boolean parsing first
|
||||
if (bool.TryParse(clientInput, out parsedBool))
|
||||
{
|
||||
content[property.Key] = JsonSerializer.Deserialize<JsonElement>(JsonSerializer.Serialize(parsedBool));
|
||||
content[property.Key] = JsonElement.Parse(JsonSerializer.Serialize(parsedBool));
|
||||
}
|
||||
// Also accept "yes"/"no" as valid boolean inputs
|
||||
else if (string.Equals(clientInput?.Trim(), "yes", StringComparison.OrdinalIgnoreCase))
|
||||
{
|
||||
content[property.Key] = JsonSerializer.Deserialize<JsonElement>(JsonSerializer.Serialize(true));
|
||||
content[property.Key] = JsonElement.Parse(JsonSerializer.Serialize(true));
|
||||
}
|
||||
else if (string.Equals(clientInput?.Trim(), "no", StringComparison.OrdinalIgnoreCase))
|
||||
{
|
||||
content[property.Key] = JsonSerializer.Deserialize<JsonElement>(JsonSerializer.Serialize(false));
|
||||
content[property.Key] = JsonElement.Parse(JsonSerializer.Serialize(false));
|
||||
}
|
||||
}
|
||||
else if (property.Value is ElicitRequestParams.NumberSchema numberSchema)
|
||||
|
|
@ -97,14 +97,14 @@ async ValueTask<ElicitResult> HandleElicitationAsync(ElicitRequestParams? reques
|
|||
double parsedNumber;
|
||||
if (double.TryParse(clientInput, out parsedNumber))
|
||||
{
|
||||
content[property.Key] = JsonSerializer.Deserialize<JsonElement>(JsonSerializer.Serialize(parsedNumber));
|
||||
content[property.Key] = JsonElement.Parse(JsonSerializer.Serialize(parsedNumber));
|
||||
}
|
||||
}
|
||||
else if (property.Value is ElicitRequestParams.StringSchema stringSchema)
|
||||
{
|
||||
Console.Write($"{stringSchema.Description}: ");
|
||||
var clientInput = Console.ReadLine();
|
||||
content[property.Key] = JsonSerializer.Deserialize<JsonElement>(JsonSerializer.Serialize(clientInput));
|
||||
content[property.Key] = JsonElement.Parse(JsonSerializer.Serialize(clientInput));
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -81,7 +81,7 @@ internal static class Program
|
|||
Params = JsonSerializer.SerializeToNode(new LoggingMessageNotificationParams
|
||||
{
|
||||
Level = logLevel,
|
||||
Data = JsonSerializer.Deserialize<JsonElement>("\"Random log message\"")
|
||||
Data = JsonElement.Parse("\"Random log message\"")
|
||||
})
|
||||
}, cancellationToken);
|
||||
}
|
||||
|
|
@ -117,7 +117,7 @@ internal static class Program
|
|||
{
|
||||
Name = "echo",
|
||||
Description = "Echoes the input back to the client.",
|
||||
InputSchema = JsonSerializer.Deserialize<JsonElement>("""
|
||||
InputSchema = JsonElement.Parse("""
|
||||
{
|
||||
"type": "object",
|
||||
"properties": {
|
||||
|
|
@ -134,17 +134,17 @@ internal static class Program
|
|||
{
|
||||
Name = "echoSessionId",
|
||||
Description = "Echoes the session id back to the client.",
|
||||
InputSchema = JsonSerializer.Deserialize<JsonElement>("""
|
||||
InputSchema = JsonElement.Parse("""
|
||||
{
|
||||
"type": "object"
|
||||
}
|
||||
""", McpJsonUtilities.DefaultOptions),
|
||||
"""),
|
||||
},
|
||||
new Tool
|
||||
{
|
||||
Name = "sampleLLM",
|
||||
Description = "Samples from an LLM using MCP's sampling feature.",
|
||||
InputSchema = JsonSerializer.Deserialize<JsonElement>("""
|
||||
InputSchema = JsonElement.Parse("""
|
||||
{
|
||||
"type": "object",
|
||||
"properties": {
|
||||
|
|
|
|||
|
|
@ -107,7 +107,7 @@ public class Program
|
|||
{
|
||||
Name = "echo",
|
||||
Description = "Echoes the input back to the client.",
|
||||
InputSchema = JsonSerializer.Deserialize<JsonElement>("""
|
||||
InputSchema = JsonElement.Parse("""
|
||||
{
|
||||
"type": "object",
|
||||
"properties": {
|
||||
|
|
@ -118,23 +118,23 @@ public class Program
|
|||
},
|
||||
"required": ["message"]
|
||||
}
|
||||
""", McpJsonUtilities.DefaultOptions),
|
||||
"""),
|
||||
},
|
||||
new Tool
|
||||
{
|
||||
Name = "echoSessionId",
|
||||
Description = "Echoes the session id back to the client.",
|
||||
InputSchema = JsonSerializer.Deserialize<JsonElement>("""
|
||||
InputSchema = JsonElement.Parse("""
|
||||
{
|
||||
"type": "object"
|
||||
}
|
||||
""", McpJsonUtilities.DefaultOptions),
|
||||
"""),
|
||||
},
|
||||
new Tool
|
||||
{
|
||||
Name = "sampleLLM",
|
||||
Description = "Samples from an LLM using MCP's sampling feature.",
|
||||
InputSchema = JsonSerializer.Deserialize<JsonElement>("""
|
||||
InputSchema = JsonElement.Parse("""
|
||||
{
|
||||
"type": "object",
|
||||
"properties": {
|
||||
|
|
@ -149,7 +149,7 @@ public class Program
|
|||
},
|
||||
"required": ["prompt", "maxTokens"]
|
||||
}
|
||||
""", McpJsonUtilities.DefaultOptions),
|
||||
"""),
|
||||
}
|
||||
]
|
||||
};
|
||||
|
|
|
|||
|
|
@ -43,13 +43,13 @@ public partial class McpServerBuilderExtensionsToolsTests : ClientServerTestBase
|
|||
{
|
||||
Name = "FirstCustomTool",
|
||||
Description = "First tool returned by custom handler",
|
||||
InputSchema = JsonSerializer.Deserialize<JsonElement>("""
|
||||
InputSchema = JsonElement.Parse("""
|
||||
{
|
||||
"type": "object",
|
||||
"properties": {},
|
||||
"required": []
|
||||
}
|
||||
""", McpJsonUtilities.DefaultOptions),
|
||||
"""),
|
||||
}],
|
||||
};
|
||||
|
||||
|
|
@ -61,13 +61,13 @@ public partial class McpServerBuilderExtensionsToolsTests : ClientServerTestBase
|
|||
{
|
||||
Name = "SecondCustomTool",
|
||||
Description = "Second tool returned by custom handler",
|
||||
InputSchema = JsonSerializer.Deserialize<JsonElement>("""
|
||||
InputSchema = JsonElement.Parse("""
|
||||
{
|
||||
"type": "object",
|
||||
"properties": {},
|
||||
"required": []
|
||||
}
|
||||
""", McpJsonUtilities.DefaultOptions),
|
||||
"""),
|
||||
}],
|
||||
};
|
||||
|
||||
|
|
@ -79,13 +79,13 @@ public partial class McpServerBuilderExtensionsToolsTests : ClientServerTestBase
|
|||
{
|
||||
Name = "FinalCustomTool",
|
||||
Description = "Third tool returned by custom handler",
|
||||
InputSchema = JsonSerializer.Deserialize<JsonElement>("""
|
||||
InputSchema = JsonElement.Parse("""
|
||||
{
|
||||
"type": "object",
|
||||
"properties": {},
|
||||
"required": []
|
||||
}
|
||||
""", McpJsonUtilities.DefaultOptions),
|
||||
"""),
|
||||
}],
|
||||
};
|
||||
|
||||
|
|
|
|||
|
|
@ -120,18 +120,18 @@ public partial class ElicitationTests : ClientServerTestBase
|
|||
Action = "accept",
|
||||
Content = new Dictionary<string, JsonElement>
|
||||
{
|
||||
["prop1"] = (JsonElement)JsonSerializer.Deserialize("""
|
||||
["prop1"] = JsonElement.Parse("""
|
||||
"string result"
|
||||
""", McpJsonUtilities.DefaultOptions.GetTypeInfo(typeof(JsonElement)))!,
|
||||
["prop2"] = (JsonElement)JsonSerializer.Deserialize("""
|
||||
"""),
|
||||
["prop2"] = JsonElement.Parse("""
|
||||
42
|
||||
""", McpJsonUtilities.DefaultOptions.GetTypeInfo(typeof(JsonElement)))!,
|
||||
["prop3"] = (JsonElement)JsonSerializer.Deserialize("""
|
||||
"""),
|
||||
["prop3"] = JsonElement.Parse("""
|
||||
true
|
||||
""", McpJsonUtilities.DefaultOptions.GetTypeInfo(typeof(JsonElement)))!,
|
||||
["prop4"] = (JsonElement)JsonSerializer.Deserialize("""
|
||||
"""),
|
||||
["prop4"] = JsonElement.Parse("""
|
||||
"option2"
|
||||
""", McpJsonUtilities.DefaultOptions.GetTypeInfo(typeof(JsonElement)))!,
|
||||
"""),
|
||||
},
|
||||
};
|
||||
}
|
||||
|
|
|
|||
|
|
@ -177,24 +177,24 @@ public partial class ElicitationTypedTests : ClientServerTestBase
|
|||
Action = "accept",
|
||||
Content = new Dictionary<string, JsonElement>
|
||||
{
|
||||
[nameof(SampleForm.Name)] = (JsonElement)JsonSerializer.Deserialize("""
|
||||
[nameof(SampleForm.Name)] = JsonElement.Parse("""
|
||||
"Alice"
|
||||
""", McpJsonUtilities.DefaultOptions.GetTypeInfo(typeof(JsonElement)))!,
|
||||
[nameof(SampleForm.Age)] = (JsonElement)JsonSerializer.Deserialize("""
|
||||
"""),
|
||||
[nameof(SampleForm.Age)] = JsonElement.Parse("""
|
||||
30
|
||||
""", McpJsonUtilities.DefaultOptions.GetTypeInfo(typeof(JsonElement)))!,
|
||||
[nameof(SampleForm.Active)] = (JsonElement)JsonSerializer.Deserialize("""
|
||||
"""),
|
||||
[nameof(SampleForm.Active)] = JsonElement.Parse("""
|
||||
true
|
||||
""", McpJsonUtilities.DefaultOptions.GetTypeInfo(typeof(JsonElement)))!,
|
||||
[nameof(SampleForm.Role)] = (JsonElement)JsonSerializer.Deserialize("""
|
||||
"""),
|
||||
[nameof(SampleForm.Role)] = JsonElement.Parse("""
|
||||
"Admin"
|
||||
""", McpJsonUtilities.DefaultOptions.GetTypeInfo(typeof(JsonElement)))!,
|
||||
[nameof(SampleForm.Score)] = (JsonElement)JsonSerializer.Deserialize("""
|
||||
"""),
|
||||
[nameof(SampleForm.Score)] = JsonElement.Parse("""
|
||||
99.5
|
||||
""", McpJsonUtilities.DefaultOptions.GetTypeInfo(typeof(JsonElement)))!,
|
||||
[nameof(SampleForm.Created)] = (JsonElement)JsonSerializer.Deserialize("""
|
||||
"""),
|
||||
[nameof(SampleForm.Created)] = JsonElement.Parse("""
|
||||
"2023-08-27T03:05:00"
|
||||
""", McpJsonUtilities.DefaultOptions.GetTypeInfo(typeof(JsonElement)))!,
|
||||
"""),
|
||||
},
|
||||
};
|
||||
},
|
||||
|
|
@ -228,15 +228,15 @@ public partial class ElicitationTypedTests : ClientServerTestBase
|
|||
Action = "accept",
|
||||
Content = new Dictionary<string, JsonElement>
|
||||
{
|
||||
["firstName"] = (JsonElement)JsonSerializer.Deserialize("""
|
||||
["firstName"] = JsonElement.Parse("""
|
||||
"Bob"
|
||||
""", McpJsonUtilities.DefaultOptions.GetTypeInfo(typeof(JsonElement)))!,
|
||||
["zipCode"] = (JsonElement)JsonSerializer.Deserialize("""
|
||||
"""),
|
||||
["zipCode"] = JsonElement.Parse("""
|
||||
90210
|
||||
""", McpJsonUtilities.DefaultOptions.GetTypeInfo(typeof(JsonElement)))!,
|
||||
["isAdmin"] = (JsonElement)JsonSerializer.Deserialize("""
|
||||
"""),
|
||||
["isAdmin"] = JsonElement.Parse("""
|
||||
false
|
||||
""", McpJsonUtilities.DefaultOptions.GetTypeInfo(typeof(JsonElement)))!,
|
||||
"""),
|
||||
},
|
||||
};
|
||||
},
|
||||
|
|
|
|||
Loading…
Reference in a new issue