Skip to content

Commit 121929d

Browse files
Audit protocol types (#892)
1 parent 66c084c commit 121929d

File tree

63 files changed

+262
-168
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

63 files changed

+262
-168
lines changed

src/ModelContextProtocol.AspNetCore/StreamableHttpHandler.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -250,6 +250,7 @@ private static Task WriteJsonRpcErrorAsync(HttpContext context, string errorMess
250250
{
251251
var jsonRpcError = new JsonRpcError
252252
{
253+
Id = default,
253254
Error = new()
254255
{
255256
Code = errorCode,

src/ModelContextProtocol.Core/AIContentExtensions.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -226,6 +226,7 @@ internal static ContentBlock ToContent(this AIContent content) =>
226226
{
227227
Blob = dataContent.Base64Data.ToString(),
228228
MimeType = dataContent.MediaType,
229+
Uri = string.Empty,
229230
}
230231
},
231232

src/ModelContextProtocol.Core/Protocol/Annotations.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ public sealed class Annotations
1515
/// Gets or sets the intended audience for this content as an array of <see cref="Role"/> values.
1616
/// </summary>
1717
[JsonPropertyName("audience")]
18-
public IList<Role>? Audience { get; init; }
18+
public IList<Role>? Audience { get; set; }
1919

2020
/// <summary>
2121
/// Gets or sets a value indicating how important this data is for operating the server.
@@ -25,7 +25,7 @@ public sealed class Annotations
2525
/// 1 represents highest priority.
2626
/// </remarks>
2727
[JsonPropertyName("priority")]
28-
public float? Priority { get; init; }
28+
public float? Priority { get; set; }
2929

3030
/// <summary>
3131
/// Gets or sets the moment the resource was last modified.

src/ModelContextProtocol.Core/Protocol/Argument.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ public sealed class Argument
1515
/// Gets or sets the name of the argument being completed.
1616
/// </summary>
1717
[JsonPropertyName("name")]
18-
public string Name { get; set; } = string.Empty;
18+
public required string Name { get; set; }
1919

2020
/// <summary>
2121
/// Gets or sets the current partial text value for which completion suggestions are requested.
@@ -25,5 +25,5 @@ public sealed class Argument
2525
/// options should be generated.
2626
/// </remarks>
2727
[JsonPropertyName("value")]
28-
public string Value { get; set; } = string.Empty;
29-
}
28+
public required string Value { get; set; }
29+
}

src/ModelContextProtocol.Core/Protocol/BlobResourceContents.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,5 +26,5 @@ public sealed class BlobResourceContents : ResourceContents
2626
/// Gets or sets the base64-encoded string representing the binary data of the item.
2727
/// </summary>
2828
[JsonPropertyName("blob")]
29-
public string Blob { get; set; } = string.Empty;
30-
}
29+
public required string Blob { get; set; }
30+
}

src/ModelContextProtocol.Core/Protocol/CallToolRequestParams.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ public sealed class CallToolRequestParams : RequestParams
1414
{
1515
/// <summary>Gets or sets the name of the tool to invoke.</summary>
1616
[JsonPropertyName("name")]
17-
public required string Name { get; init; }
17+
public required string Name { get; set; }
1818

1919
/// <summary>
2020
/// Gets or sets optional arguments to pass to the tool when invoking it on the server.
@@ -24,5 +24,5 @@ public sealed class CallToolRequestParams : RequestParams
2424
/// a parameter name and its corresponding argument value.
2525
/// </remarks>
2626
[JsonPropertyName("arguments")]
27-
public IReadOnlyDictionary<string, JsonElement>? Arguments { get; init; }
27+
public IDictionary<string, JsonElement>? Arguments { get; set; }
2828
}

src/ModelContextProtocol.Core/Protocol/CancelledNotificationParams.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ public sealed class CancelledNotificationParams : NotificationParams
2020
/// This must match the ID of an in-flight request that the sender wishes to cancel.
2121
/// </remarks>
2222
[JsonPropertyName("requestId")]
23-
public RequestId RequestId { get; set; }
23+
public required RequestId RequestId { get; set; }
2424

2525
/// <summary>
2626
/// Gets or sets an optional string describing the reason for the cancellation request.

src/ModelContextProtocol.Core/Protocol/CompleteContext.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,5 +15,5 @@ public sealed class CompleteContext
1515
/// Gets or sets previously-resolved variables in a URI template or prompt.
1616
/// </summary>
1717
[JsonPropertyName("arguments")]
18-
public IDictionary<string, string>? Arguments { get; init; }
18+
public IDictionary<string, string>? Arguments { get; set; }
1919
}

src/ModelContextProtocol.Core/Protocol/CompleteRequestParams.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,18 +23,18 @@ public sealed class CompleteRequestParams : RequestParams
2323
/// Gets or sets the reference's information.
2424
/// </summary>
2525
[JsonPropertyName("ref")]
26-
public required Reference Ref { get; init; }
26+
public required Reference Ref { get; set; }
2727

2828
/// <summary>
2929
/// Gets or sets the argument information for the completion request, specifying what is being completed
3030
/// and the current partial input.
3131
/// </summary>
3232
[JsonPropertyName("argument")]
33-
public required Argument Argument { get; init; }
33+
public required Argument Argument { get; set; }
3434

3535
/// <summary>
3636
/// Gets or sets additional, optional context for completions.
3737
/// </summary>
3838
[JsonPropertyName("context")]
39-
public CompleteContext? Context { get; init; }
39+
public CompleteContext? Context { get; set; }
4040
}

src/ModelContextProtocol.Core/Protocol/ContentBlock.cs

Lines changed: 21 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using Microsoft.Extensions.AI;
22
using System.ComponentModel;
33
using System.Diagnostics;
4+
using System.Diagnostics.CodeAnalysis;
45
using System.Text.Json;
56
using System.Text.Json.Nodes;
67
using System.Text.Json.Serialization;
@@ -33,13 +34,13 @@ private protected ContentBlock()
3334
}
3435

3536
/// <summary>
36-
/// Gets or sets the type of content.
37+
/// When overridden in a derived class, gets the type of content.
3738
/// </summary>
3839
/// <remarks>
3940
/// This determines the structure of the content object. Valid values include "image", "audio", "text", "resource", and "resource_link".
4041
/// </remarks>
4142
[JsonPropertyName("type")]
42-
public string Type { get; set; } = string.Empty;
43+
public abstract string Type { get; }
4344

4445
/// <summary>
4546
/// Gets or sets optional annotations for the content.
@@ -49,7 +50,7 @@ private protected ContentBlock()
4950
/// and the priority level of the content. Clients can use this information to filter or prioritize content for different roles.
5051
/// </remarks>
5152
[JsonPropertyName("annotations")]
52-
public Annotations? Annotations { get; init; }
53+
public Annotations? Annotations { get; set; }
5354

5455
/// <summary>
5556
/// Provides a <see cref="JsonConverter"/> for <see cref="ContentBlock"/>.
@@ -276,8 +277,8 @@ public override void Write(Utf8JsonWriter writer, ContentBlock value, JsonSerial
276277
/// <summary>Represents text provided to or from an LLM.</summary>
277278
public sealed class TextContentBlock : ContentBlock
278279
{
279-
/// <summary>Initializes the instance of the <see cref="TextContentBlock"/> class.</summary>
280-
public TextContentBlock() => Type = "text";
280+
/// <inheritdoc/>
281+
public override string Type => "text";
281282

282283
/// <summary>
283284
/// Gets or sets the text content of the message.
@@ -298,8 +299,8 @@ public sealed class TextContentBlock : ContentBlock
298299
/// <summary>Represents an image provided to or from an LLM.</summary>
299300
public sealed class ImageContentBlock : ContentBlock
300301
{
301-
/// <summary>Initializes the instance of the <see cref="ImageContentBlock"/> class.</summary>
302-
public ImageContentBlock() => Type = "image";
302+
/// <inheritdoc/>
303+
public override string Type => "image";
303304

304305
/// <summary>
305306
/// Gets or sets the base64-encoded image data.
@@ -331,8 +332,8 @@ public sealed class ImageContentBlock : ContentBlock
331332
/// <summary>Represents audio provided to or from an LLM.</summary>
332333
public sealed class AudioContentBlock : ContentBlock
333334
{
334-
/// <summary>Initializes the instance of the <see cref="AudioContentBlock"/> class.</summary>
335-
public AudioContentBlock() => Type = "audio";
335+
/// <inheritdoc/>
336+
public override string Type => "audio";
336337

337338
/// <summary>
338339
/// Gets or sets the base64-encoded audio data.
@@ -367,8 +368,8 @@ public sealed class AudioContentBlock : ContentBlock
367368
/// </remarks>
368369
public sealed class EmbeddedResourceBlock : ContentBlock
369370
{
370-
/// <summary>Initializes the instance of the <see cref="ResourceLinkBlock"/> class.</summary>
371-
public EmbeddedResourceBlock() => Type = "resource";
371+
/// <inheritdoc/>
372+
public override string Type => "resource";
372373

373374
/// <summary>
374375
/// Gets or sets the resource content of the message when <see cref="Type"/> is "resource".
@@ -399,20 +400,21 @@ public sealed class EmbeddedResourceBlock : ContentBlock
399400
/// </remarks>
400401
public sealed class ResourceLinkBlock : ContentBlock
401402
{
402-
/// <summary>Initializes the instance of the <see cref="ResourceLinkBlock"/> class.</summary>
403-
public ResourceLinkBlock() => Type = "resource_link";
403+
/// <inheritdoc/>
404+
public override string Type => "resource_link";
404405

405406
/// <summary>
406407
/// Gets or sets the URI of this resource.
407408
/// </summary>
408409
[JsonPropertyName("uri")]
409-
public required string Uri { get; init; }
410+
[StringSyntax(StringSyntaxAttribute.Uri)]
411+
public required string Uri { get; set; }
410412

411413
/// <summary>
412414
/// Gets or sets a human-readable name for this resource.
413415
/// </summary>
414416
[JsonPropertyName("name")]
415-
public required string Name { get; init; }
417+
public required string Name { get; set; }
416418

417419
/// <summary>
418420
/// Gets or sets a description of what this resource represents.
@@ -431,7 +433,7 @@ public sealed class ResourceLinkBlock : ContentBlock
431433
/// </para>
432434
/// </remarks>
433435
[JsonPropertyName("description")]
434-
public string? Description { get; init; }
436+
public string? Description { get; set; }
435437

436438
/// <summary>
437439
/// Gets or sets the MIME type of this resource.
@@ -447,7 +449,7 @@ public sealed class ResourceLinkBlock : ContentBlock
447449
/// </para>
448450
/// </remarks>
449451
[JsonPropertyName("mimeType")]
450-
public string? MimeType { get; init; }
452+
public string? MimeType { get; set; }
451453

452454
/// <summary>
453455
/// Gets or sets the size of the raw resource content (before base64 encoding), in bytes, if known.
@@ -456,5 +458,5 @@ public sealed class ResourceLinkBlock : ContentBlock
456458
/// This can be used by applications to display file sizes and estimate context window usage.
457459
/// </remarks>
458460
[JsonPropertyName("size")]
459-
public long? Size { get; init; }
460-
}
461+
public long? Size { get; set; }
462+
}

0 commit comments

Comments
 (0)