Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
Prompt.cs
2using System.Diagnostics.CodeAnalysis;
3using System.Reflection;
4using System.Threading.Tasks;
8
10{
14 public class Prompt : ProtectedMethod
15 {
16 private Icons? icons = null;
17
31 public Prompt(MethodInfo Method, string Title, string Description,
32 string IconsMethod, params KeyValuePair<string, object>[] MetaData)
33 : base(Method, false)
34 {
35 this.Title = Title;
36 this.Description = Description;
37 this.IconsMethod = IconsMethod;
38 this.MetaData = MetaData;
39 this.ReturnAttributes = Method.ReturnParameter?.GetCustomAttribute<McpParameterAttribute>();
40 }
41
45 public string Title { get; }
46
53 public string Description { get; }
54
60 public string IconsMethod { get; }
61
65 public KeyValuePair<string, object>[] MetaData { get; }
66
71
77 public async Task<Dictionary<string, object>> ToJson(HttpMcpServerResource Resource)
78 {
79 this.icons ??= await Tool.GetIcons(Resource, this.IconsMethod);
80
81 Dictionary<string, object> Result = new Dictionary<string, object>()
82 {
83 { "name", this.Method.Name }
84 };
85
86 PopulateArguments(this.Method, Result);
87
88 if (!string.IsNullOrEmpty(this.Title))
89 Result.Add("title", this.Title);
90
91 if (!string.IsNullOrEmpty(this.Description))
92 Result.Add("description", this.Description);
93
94 if (!this.icons.Empty)
95 Result.Add("icons", this.icons.ToJson());
96
97 if ((this.MetaData?.Length ?? 0) > 0)
98 {
99 Dictionary<string, object> MetaData = new Dictionary<string, object>();
100
101 foreach (KeyValuePair<string, object> P in this.MetaData!)
102 MetaData[P.Key] = P.Value;
103
104 Result["_meta"] = MetaData;
105 }
106
107 return Result;
108 }
109
110 private static void PopulateArguments(MethodInfo Method,
111 Dictionary<string, object> Result)
112 {
113 ParameterInfo[] Parameters = Method.GetParameters();
114
116
117 foreach (ParameterInfo Parameter in Parameters)
118 {
119 Dictionary<string, object?> PromptArgument = new Dictionary<string, object?>()
120 {
121 { "name", Parameter.Name },
122 { "required", !Parameter.HasDefaultValue }
123 };
124
125 McpParameterAttribute? Attribute = Parameter.GetCustomAttribute<McpParameterAttribute>();
126 Attribute?.Annotate(PromptArgument);
127
129 Arguments.Add(PromptArgument);
130 }
131
132 if (!(Arguments is null))
133 Result["arguments"] = Arguments.ToArray();
134 }
135
148 public bool TryBuildRequest(object? Id, Dictionary<string, object?> Parameters,
149 HttpRequest Request, HttpResponse Response,
150 Dictionary<string, object?>? MetaData,
151 [NotNullWhen(false)] out string? Reason,
152 [NotNullWhen(true)] out object?[]? Arguments)
153 {
154 if (!this.TryBuildRequest(Id, Parameters, MetaData, out Reason, out Arguments))
155 return false;
156
157 if (this.RequestArgument.HasValue)
158 Arguments[this.RequestArgument.Value] = Request;
159
160 if (this.ResponseArgument.HasValue)
161 Arguments[this.ResponseArgument.Value] = Response;
162
163 if (this.IdArgument.HasValue)
164 Arguments[this.IdArgument.Value] = Id;
165
166 return true;
167 }
168
169 }
170}
Represents an HTTP request.
Definition: HttpRequest.cs:22
Represets a response of an HTTP client request.
Definition: HttpResponse.cs:23
Information about a protected method.
ProtectedMethodArgumentInfo[] Arguments
Arguments
int? ResponseArgument
Response argument index
Abstract base class for HTTP-based Model Context Protocol (MCP) server resource, as defined in:
virtual void Annotate(Dictionary< string, object?> Schema)
Annotates a schema object with information in the attribute.
Base interface to add icons property.
Definition: Icons.cs:11
bool Empty
If there are icons defined.
Definition: Icons.cs:44
Dictionary< string, object >[] ToJson()
Converts object to a generic representation.
Definition: Icons.cs:81
Contains information about an MCP Server Prompt
Definition: Prompt.cs:15
string IconsMethod
Name of method that returns an Icon?, an an Icon[]? or an Icons? resource representing the prompt....
Definition: Prompt.cs:60
string Title
A human-readable title for the prompt.
Definition: Prompt.cs:45
McpParameterAttribute? ReturnAttributes
Any MCP attributes declared for the return value.
Definition: Prompt.cs:70
KeyValuePair< string, object >[] MetaData
Meta-data associated with prompt.
Definition: Prompt.cs:65
bool TryBuildRequest(object? Id, Dictionary< string, object?> Parameters, HttpRequest Request, HttpResponse Response, Dictionary< string, object?>? MetaData, [NotNullWhen(false)] out string? Reason, [NotNullWhen(true)] out object?[]? Arguments)
Tries to build a request for the method, based on the provided named parameters.
Definition: Prompt.cs:148
Prompt(MethodInfo Method, string Title, string Description, string IconsMethod, params KeyValuePair< string, object >[] MetaData)
Contains information about an MCP Server Prompt
Definition: Prompt.cs:31
async Task< Dictionary< string, object > > ToJson(HttpMcpServerResource Resource)
Converts object to a generic representation.
Definition: Prompt.cs:77
string Description
A human-readable description of the prompt.
Definition: Prompt.cs:53
Contains information about an MCP Server Resource
Definition: Resource.cs:14
Contains information about an MCP Server Tool
Definition: Tool.cs:22
A chunked list is a linked list of chunks of objects of type T .
Definition: ChunkedList.cs:54