Skip to content

Commit a559bfb

Browse files
Copilotstephentoub
andauthored
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>
1 parent 9395394 commit a559bfb

File tree

6 files changed

+48
-48
lines changed

6 files changed

+48
-48
lines changed

docs/concepts/elicitation/samples/client/Program.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -78,16 +78,16 @@ async ValueTask<ElicitResult> HandleElicitationAsync(ElicitRequestParams? reques
7878
// Try standard boolean parsing first
7979
if (bool.TryParse(clientInput, out parsedBool))
8080
{
81-
content[property.Key] = JsonSerializer.Deserialize<JsonElement>(JsonSerializer.Serialize(parsedBool));
81+
content[property.Key] = JsonElement.Parse(JsonSerializer.Serialize(parsedBool));
8282
}
8383
// Also accept "yes"/"no" as valid boolean inputs
8484
else if (string.Equals(clientInput?.Trim(), "yes", StringComparison.OrdinalIgnoreCase))
8585
{
86-
content[property.Key] = JsonSerializer.Deserialize<JsonElement>(JsonSerializer.Serialize(true));
86+
content[property.Key] = JsonElement.Parse(JsonSerializer.Serialize(true));
8787
}
8888
else if (string.Equals(clientInput?.Trim(), "no", StringComparison.OrdinalIgnoreCase))
8989
{
90-
content[property.Key] = JsonSerializer.Deserialize<JsonElement>(JsonSerializer.Serialize(false));
90+
content[property.Key] = JsonElement.Parse(JsonSerializer.Serialize(false));
9191
}
9292
}
9393
else if (property.Value is ElicitRequestParams.NumberSchema numberSchema)
@@ -97,14 +97,14 @@ async ValueTask<ElicitResult> HandleElicitationAsync(ElicitRequestParams? reques
9797
double parsedNumber;
9898
if (double.TryParse(clientInput, out parsedNumber))
9999
{
100-
content[property.Key] = JsonSerializer.Deserialize<JsonElement>(JsonSerializer.Serialize(parsedNumber));
100+
content[property.Key] = JsonElement.Parse(JsonSerializer.Serialize(parsedNumber));
101101
}
102102
}
103103
else if (property.Value is ElicitRequestParams.StringSchema stringSchema)
104104
{
105105
Console.Write($"{stringSchema.Description}: ");
106106
var clientInput = Console.ReadLine();
107-
content[property.Key] = JsonSerializer.Deserialize<JsonElement>(JsonSerializer.Serialize(clientInput));
107+
content[property.Key] = JsonElement.Parse(JsonSerializer.Serialize(clientInput));
108108
}
109109
}
110110

tests/ModelContextProtocol.TestServer/Program.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ await server.SendMessageAsync(new JsonRpcNotification
8181
Params = JsonSerializer.SerializeToNode(new LoggingMessageNotificationParams
8282
{
8383
Level = logLevel,
84-
Data = JsonSerializer.Deserialize<JsonElement>("\"Random log message\"")
84+
Data = JsonElement.Parse("\"Random log message\"")
8585
})
8686
}, cancellationToken);
8787
}
@@ -117,7 +117,7 @@ private static void ConfigureTools(McpServerOptions options, string? cliArg)
117117
{
118118
Name = "echo",
119119
Description = "Echoes the input back to the client.",
120-
InputSchema = JsonSerializer.Deserialize<JsonElement>("""
120+
InputSchema = JsonElement.Parse("""
121121
{
122122
"type": "object",
123123
"properties": {
@@ -134,17 +134,17 @@ private static void ConfigureTools(McpServerOptions options, string? cliArg)
134134
{
135135
Name = "echoSessionId",
136136
Description = "Echoes the session id back to the client.",
137-
InputSchema = JsonSerializer.Deserialize<JsonElement>("""
137+
InputSchema = JsonElement.Parse("""
138138
{
139139
"type": "object"
140140
}
141-
""", McpJsonUtilities.DefaultOptions),
141+
"""),
142142
},
143143
new Tool
144144
{
145145
Name = "sampleLLM",
146146
Description = "Samples from an LLM using MCP's sampling feature.",
147-
InputSchema = JsonSerializer.Deserialize<JsonElement>("""
147+
InputSchema = JsonElement.Parse("""
148148
{
149149
"type": "object",
150150
"properties": {

tests/ModelContextProtocol.TestSseServer/Program.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ static CreateMessageRequestParams CreateRequestSamplingParams(string context, st
107107
{
108108
Name = "echo",
109109
Description = "Echoes the input back to the client.",
110-
InputSchema = JsonSerializer.Deserialize<JsonElement>("""
110+
InputSchema = JsonElement.Parse("""
111111
{
112112
"type": "object",
113113
"properties": {
@@ -118,23 +118,23 @@ static CreateMessageRequestParams CreateRequestSamplingParams(string context, st
118118
},
119119
"required": ["message"]
120120
}
121-
""", McpJsonUtilities.DefaultOptions),
121+
"""),
122122
},
123123
new Tool
124124
{
125125
Name = "echoSessionId",
126126
Description = "Echoes the session id back to the client.",
127-
InputSchema = JsonSerializer.Deserialize<JsonElement>("""
127+
InputSchema = JsonElement.Parse("""
128128
{
129129
"type": "object"
130130
}
131-
""", McpJsonUtilities.DefaultOptions),
131+
"""),
132132
},
133133
new Tool
134134
{
135135
Name = "sampleLLM",
136136
Description = "Samples from an LLM using MCP's sampling feature.",
137-
InputSchema = JsonSerializer.Deserialize<JsonElement>("""
137+
InputSchema = JsonElement.Parse("""
138138
{
139139
"type": "object",
140140
"properties": {
@@ -149,7 +149,7 @@ static CreateMessageRequestParams CreateRequestSamplingParams(string context, st
149149
},
150150
"required": ["prompt", "maxTokens"]
151151
}
152-
""", McpJsonUtilities.DefaultOptions),
152+
"""),
153153
}
154154
]
155155
};

tests/ModelContextProtocol.Tests/Configuration/McpServerBuilderExtensionsToolsTests.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -43,13 +43,13 @@ protected override void ConfigureServices(ServiceCollection services, IMcpServer
4343
{
4444
Name = "FirstCustomTool",
4545
Description = "First tool returned by custom handler",
46-
InputSchema = JsonSerializer.Deserialize<JsonElement>("""
46+
InputSchema = JsonElement.Parse("""
4747
{
4848
"type": "object",
4949
"properties": {},
5050
"required": []
5151
}
52-
""", McpJsonUtilities.DefaultOptions),
52+
"""),
5353
}],
5454
};
5555

@@ -61,13 +61,13 @@ protected override void ConfigureServices(ServiceCollection services, IMcpServer
6161
{
6262
Name = "SecondCustomTool",
6363
Description = "Second tool returned by custom handler",
64-
InputSchema = JsonSerializer.Deserialize<JsonElement>("""
64+
InputSchema = JsonElement.Parse("""
6565
{
6666
"type": "object",
6767
"properties": {},
6868
"required": []
6969
}
70-
""", McpJsonUtilities.DefaultOptions),
70+
"""),
7171
}],
7272
};
7373

@@ -79,13 +79,13 @@ protected override void ConfigureServices(ServiceCollection services, IMcpServer
7979
{
8080
Name = "FinalCustomTool",
8181
Description = "Third tool returned by custom handler",
82-
InputSchema = JsonSerializer.Deserialize<JsonElement>("""
82+
InputSchema = JsonElement.Parse("""
8383
{
8484
"type": "object",
8585
"properties": {},
8686
"required": []
8787
}
88-
""", McpJsonUtilities.DefaultOptions),
88+
"""),
8989
}],
9090
};
9191

tests/ModelContextProtocol.Tests/Protocol/ElicitationTests.cs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -120,18 +120,18 @@ public async Task Can_Elicit_Information()
120120
Action = "accept",
121121
Content = new Dictionary<string, JsonElement>
122122
{
123-
["prop1"] = (JsonElement)JsonSerializer.Deserialize("""
123+
["prop1"] = JsonElement.Parse("""
124124
"string result"
125-
""", McpJsonUtilities.DefaultOptions.GetTypeInfo(typeof(JsonElement)))!,
126-
["prop2"] = (JsonElement)JsonSerializer.Deserialize("""
125+
"""),
126+
["prop2"] = JsonElement.Parse("""
127127
42
128-
""", McpJsonUtilities.DefaultOptions.GetTypeInfo(typeof(JsonElement)))!,
129-
["prop3"] = (JsonElement)JsonSerializer.Deserialize("""
128+
"""),
129+
["prop3"] = JsonElement.Parse("""
130130
true
131-
""", McpJsonUtilities.DefaultOptions.GetTypeInfo(typeof(JsonElement)))!,
132-
["prop4"] = (JsonElement)JsonSerializer.Deserialize("""
131+
"""),
132+
["prop4"] = JsonElement.Parse("""
133133
"option2"
134-
""", McpJsonUtilities.DefaultOptions.GetTypeInfo(typeof(JsonElement)))!,
134+
"""),
135135
},
136136
};
137137
}

tests/ModelContextProtocol.Tests/Protocol/ElicitationTypedTests.cs

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -177,24 +177,24 @@ public async Task Can_Elicit_Typed_Information()
177177
Action = "accept",
178178
Content = new Dictionary<string, JsonElement>
179179
{
180-
[nameof(SampleForm.Name)] = (JsonElement)JsonSerializer.Deserialize("""
180+
[nameof(SampleForm.Name)] = JsonElement.Parse("""
181181
"Alice"
182-
""", McpJsonUtilities.DefaultOptions.GetTypeInfo(typeof(JsonElement)))!,
183-
[nameof(SampleForm.Age)] = (JsonElement)JsonSerializer.Deserialize("""
182+
"""),
183+
[nameof(SampleForm.Age)] = JsonElement.Parse("""
184184
30
185-
""", McpJsonUtilities.DefaultOptions.GetTypeInfo(typeof(JsonElement)))!,
186-
[nameof(SampleForm.Active)] = (JsonElement)JsonSerializer.Deserialize("""
185+
"""),
186+
[nameof(SampleForm.Active)] = JsonElement.Parse("""
187187
true
188-
""", McpJsonUtilities.DefaultOptions.GetTypeInfo(typeof(JsonElement)))!,
189-
[nameof(SampleForm.Role)] = (JsonElement)JsonSerializer.Deserialize("""
188+
"""),
189+
[nameof(SampleForm.Role)] = JsonElement.Parse("""
190190
"Admin"
191-
""", McpJsonUtilities.DefaultOptions.GetTypeInfo(typeof(JsonElement)))!,
192-
[nameof(SampleForm.Score)] = (JsonElement)JsonSerializer.Deserialize("""
191+
"""),
192+
[nameof(SampleForm.Score)] = JsonElement.Parse("""
193193
99.5
194-
""", McpJsonUtilities.DefaultOptions.GetTypeInfo(typeof(JsonElement)))!,
195-
[nameof(SampleForm.Created)] = (JsonElement)JsonSerializer.Deserialize("""
194+
"""),
195+
[nameof(SampleForm.Created)] = JsonElement.Parse("""
196196
"2023-08-27T03:05:00"
197-
""", McpJsonUtilities.DefaultOptions.GetTypeInfo(typeof(JsonElement)))!,
197+
"""),
198198
},
199199
};
200200
},
@@ -228,15 +228,15 @@ public async Task Elicit_Typed_Respects_NamingPolicy()
228228
Action = "accept",
229229
Content = new Dictionary<string, JsonElement>
230230
{
231-
["firstName"] = (JsonElement)JsonSerializer.Deserialize("""
231+
["firstName"] = JsonElement.Parse("""
232232
"Bob"
233-
""", McpJsonUtilities.DefaultOptions.GetTypeInfo(typeof(JsonElement)))!,
234-
["zipCode"] = (JsonElement)JsonSerializer.Deserialize("""
233+
"""),
234+
["zipCode"] = JsonElement.Parse("""
235235
90210
236-
""", McpJsonUtilities.DefaultOptions.GetTypeInfo(typeof(JsonElement)))!,
237-
["isAdmin"] = (JsonElement)JsonSerializer.Deserialize("""
236+
"""),
237+
["isAdmin"] = JsonElement.Parse("""
238238
false
239-
""", McpJsonUtilities.DefaultOptions.GetTypeInfo(typeof(JsonElement)))!,
239+
"""),
240240
},
241241
};
242242
},

0 commit comments

Comments
 (0)