forked from modelcontextprotocol/csharp-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathReference.cs
More file actions
89 lines (80 loc) · 3.13 KB
/
Reference.cs
File metadata and controls
89 lines (80 loc) · 3.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
using ModelContextProtocol.Client;
using System.Diagnostics.CodeAnalysis;
using System.Text.Json.Serialization;
namespace ModelContextProtocol.Protocol;
/// <summary>
/// Represents a reference to a resource or prompt in the Model Context Protocol.
/// </summary>
/// <remarks>
/// <para>
/// A Reference object identifies either a resource or a prompt:
/// </para>
/// <list type="bullet">
/// <item><description>For resource references, set <see cref="Type"/> to "ref/resource" and provide the <see cref="Uri"/> property.</description></item>
/// <item><description>For prompt references, set <see cref="Type"/> to "ref/prompt" and provide the <see cref="Name"/> property.</description></item>
/// </list>
/// <para>
/// References are commonly used with <see cref="McpClientExtensions.CompleteAsync"/> to request completion suggestions for arguments,
/// and with other methods that need to reference resources or prompts.
/// </para>
/// <para>
/// See the <see href="https://github.com/modelcontextprotocol/specification/blob/main/schema/">schema</see> for details.
/// </para>
/// </remarks>
public class Reference
{
/// <summary>
/// Gets or sets the type of content.
/// </summary>
/// <remarks>
/// This can be "ref/resource" or "ref/prompt".
/// </remarks>
[JsonPropertyName("type")]
public string Type { get; set; } = string.Empty;
/// <summary>
/// Gets or sets the URI or URI template of the resource.
/// </summary>
[JsonPropertyName("uri")]
public string? Uri { get; set; }
/// <summary>
/// Gets or sets the name of the prompt or prompt template.
/// </summary>
[JsonPropertyName("name")]
public string? Name { get; set; }
/// <inheritdoc />
public override string ToString() => $"\"{Type}\": \"{Uri ?? Name}\"";
/// <summary>
/// Validates the reference object to ensure it contains the required properties for its type.
/// </summary>
/// <param name="validationMessage">When this method returns false, contains a message explaining why validation failed; otherwise, null.</param>
/// <returns>True if the reference is valid; otherwise, false.</returns>
/// <remarks>
/// For "ref/resource" type, the <see cref="Uri"/> property must not be null or empty.
/// For "ref/prompt" type, the <see cref="Name"/> property must not be null or empty.
/// </remarks>
public bool Validate([NotNullWhen(false)] out string? validationMessage)
{
switch (Type)
{
case "ref/resource":
if (string.IsNullOrEmpty(Uri))
{
validationMessage = "Uri is required for ref/resource";
return false;
}
break;
case "ref/prompt":
if (string.IsNullOrEmpty(Name))
{
validationMessage = "Name is required for ref/prompt";
return false;
}
break;
default:
validationMessage = $"Unknown reference type: {Type}";
return false;
}
validationMessage = null;
return true;
}
}