Skip to content

Commit a28e2c7

Browse files
committed
Augment McpServerTool/Prompt docs with details of parameter/result marshaling
1 parent e3aa54e commit a28e2c7

File tree

4 files changed

+437
-1
lines changed

4 files changed

+437
-1
lines changed

src/ModelContextProtocol/Server/McpServerPrompt.cs

Lines changed: 111 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,121 @@
11
using Microsoft.Extensions.AI;
2+
using Microsoft.Extensions.DependencyInjection;
3+
using ModelContextProtocol.Client;
4+
using ModelContextProtocol.Protocol.Messages;
25
using ModelContextProtocol.Protocol.Types;
36
using System.Diagnostics.CodeAnalysis;
47
using System.Reflection;
8+
using System.Text.Json;
59

610
namespace ModelContextProtocol.Server;
711

8-
/// <summary>Represents an invocable prompt used by Model Context Protocol servers.</summary>
12+
/// <summary>
13+
/// Represents an invocable prompt used by Model Context Protocol clients and servers.
14+
/// </summary>
15+
/// <remarks>
16+
/// <para>
17+
/// <see cref="McpServerPrompt"/> is an abstract base class that represents an MCP prompt for use in the server (as opposed
18+
/// to <see cref="Prompt"/>, which provides the protocol representation of a prompt, and <see cref="McpClientPrompt"/>, which
19+
/// provides a client-side representation of a prompt). Instances of <see cref="McpServerPrompt"/> can be added into a
20+
/// <see cref="IServiceCollection"/> to be picked up automatically when <see cref="McpServerFactory"/> is used to create
21+
/// an <see cref="IMcpServer"/>, or added into a <see cref="McpServerPrimitiveCollection{McpServerPrompt}"/> to be used as
22+
/// a <see cref="PromptsCapability.PromptCollection"/>.
23+
/// </para>
24+
/// <para>
25+
/// Most commonly, <see cref="McpServerPrompt"/> instances are created using the static <see cref="M:McpServerPrompt.Create"/> methods.
26+
/// These methods enable creating an <see cref="McpServerPrompt"/> for a method, specified via a <see cref="Delegate"/> or
27+
/// <see cref="MethodInfo"/>, and are what are used implicitly by <see cref="McpServerBuilderExtensions.WithPromptsFromAssembly"/> and
28+
/// <see cref="M:McpServerBuilderExtensions.WithPrompts"/>. The <see cref="M:McpServerPrompt.Create"/> methods
29+
/// create <see cref="McpServerPrompt"/> instances capable of working with a large variety of .NET method signatures, automatically handling
30+
/// how parameters are marshaled into the method from the JSON received from the MCP client, and how the return value is marshaled back
31+
/// into the <see cref="GetPromptResult"/> that's then serialized and sent back to the client.
32+
/// </para>
33+
/// <para>
34+
/// By default, parameters are sourced from the <see cref="GetPromptRequestParams.Arguments"/> dictionary, which is a collection
35+
/// of key/value pairs. Those parameters are deserialized from the
36+
/// <see cref="JsonElement"/> values in that collection. There are a few exceptions to this:
37+
/// <list type="bullet">
38+
/// <item>
39+
/// <description>
40+
/// <see cref="CancellationToken"/> parameters are automatically bound to a <see cref="CancellationToken"/> provided by the
41+
/// <see cref="IMcpServer"/> and that respects any <see cref="CancelledNotification"/>s sent by the client for this operation's
42+
/// <see cref="RequestId"/>.
43+
/// </description>
44+
/// </item>
45+
/// <item>
46+
/// <description>
47+
/// <see cref="IServiceProvider"/> parameters are bound from the <see cref="RequestContext{GetPromptRequestParams}"/> for this request.
48+
/// </description>
49+
/// </item>
50+
/// <item>
51+
/// <description>
52+
/// <see cref="IMcpServer"/> parameters are bound directly to the <see cref="IMcpServer"/> instance associated
53+
/// with this request's <see cref="RequestContext{CallPromptRequestParams}"/>. Such parameters may be used to understand
54+
/// what server is being used to process the request, and to interact with the client issuing the request to that server.
55+
/// </description>
56+
/// </item>
57+
/// <item>
58+
/// <description>
59+
/// <see cref="IProgress{ProgressNotificationValue}"/> parameters accepting <see cref="ProgressNotificationValue"/> values
60+
/// are bound to an <see cref="IProgress{ProgressNotificationValue}"/> instance manufactured to forward progress notifications
61+
/// from the prompt to the client. If the client included a <see cref="ProgressToken"/> in their request, progress reports issued
62+
/// to this instance will propagate to the client as <see cref="NotificationMethods.ProgressNotification"/> notifications with
63+
/// that token. If the client did not include a <see cref="ProgressToken"/>, the instance will ignore any progress reports issued to it.
64+
/// </description>
65+
/// </item>
66+
/// <item>
67+
/// <description>
68+
/// When the <see cref="McpServerPrompt"/> is constructed, it may be passed an <see cref="IServiceProvider"/> via
69+
/// <see cref="McpServerPromptCreateOptions.Services"/>. Any parameter that can be satisfied by that <see cref="IServiceProvider"/>
70+
/// will be resolved from the <see cref="IServiceProvider"/> provided to <see cref="GetAsync"/> rather than from the argument collection.
71+
/// </description>
72+
/// </item>
73+
/// <item>
74+
/// <description>
75+
/// Any parameter attributed with <see cref="FromKeyedServicesAttribute"/> will similarly be resolved from the
76+
/// <see cref="IServiceProvider"/> provided to <see cref="GetAsync"/> rather than from the argument collection.
77+
/// </description>
78+
/// </item>
79+
/// </list>
80+
/// </para>
81+
/// <para>
82+
/// All other parameters are deserialized from the <see cref="JsonElement"/>s in the <see cref="GetPromptRequestParams.Arguments"/> dictionary.
83+
/// </para>
84+
/// <para>
85+
/// In general, the data supplied via the <see cref="GetPromptRequestParams.Arguments"/>'s dictionary is passed along from the caller and
86+
/// should thus be considered unvalidated and untrusted. To provide validated and trusted data to the invocation of the prompt, consider having
87+
/// the prompt be an instance method, referring to data stored in the instance, or using an instance or parameters resolved from the <see cref="IServiceProvider"/>
88+
/// to provide data to the method.
89+
/// </para>
90+
/// <para>
91+
/// Return values from a method are used to create the <see cref="GetPromptResult"/> that is sent back to the client:
92+
/// </para>
93+
/// <list type="table">
94+
/// <item>
95+
/// <term><see cref="string"/></term>
96+
/// <description>Converted to a list containing a single <see cref="PromptMessage"/> with its <see cref="PromptMessage.Content"/> set to contain the <see cref="string"/>.</description>
97+
/// </item>
98+
/// <item>
99+
/// <term><see cref="PromptMessage"/></term>
100+
/// <description>Converted to a list containing the single <see cref="PromptMessage"/>.</description>
101+
/// </item>
102+
/// <item>
103+
/// <term><see cref="IEnumerable{PromptMessage}"/> of <see cref="PromptMessage"/></term>
104+
/// <description>Converted to a list containing all of the returned <see cref="PromptMessage"/> instances.</description>
105+
/// </item>
106+
/// <item>
107+
/// <term><see cref="ChatMessage"/></term>
108+
/// <description>Converted to a list of <see cref="PromptMessage"/> instances derived from the <see cref="ChatMessage"/> with <see cref="AIContentExtensions.ToPromptMessages"/>.</description>
109+
/// </item>
110+
/// <item>
111+
/// <term><see cref="IEnumerable{PromptMessage}"/> of <see cref="PromptMessage"/></term>
112+
/// <description>Converted to a list of <see cref="PromptMessage"/> instances derived from all of the <see cref="ChatMessage"/> instances with <see cref="AIContentExtensions.ToPromptMessages"/>.</description>
113+
/// </item>
114+
/// </list>
115+
/// <para>
116+
/// Other returned types will result in an <see cref="InvalidOperationException"/> being thrown.
117+
/// </para>
118+
/// </remarks>
9119
public abstract class McpServerPrompt : IMcpServerPrimitive
10120
{
11121
/// <summary>Initializes a new instance of the <see cref="McpServerPrompt"/> class.</summary>

src/ModelContextProtocol/Server/McpServerPromptAttribute.cs

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
1+
using Microsoft.Extensions.AI;
12
using Microsoft.Extensions.DependencyInjection;
3+
using ModelContextProtocol.Protocol.Messages;
4+
using ModelContextProtocol.Protocol.Types;
5+
using System.Text.Json;
26

37
namespace ModelContextProtocol.Server;
48

@@ -14,6 +18,91 @@ namespace ModelContextProtocol.Server;
1418
/// <para>
1519
/// When methods are provided directly to <see cref="M:McpServerPrompt.Create"/>, the attribute is not required.
1620
/// </para>
21+
/// <para>
22+
/// By default, parameters are sourced from the <see cref="GetPromptRequestParams.Arguments"/> dictionary, which is a collection
23+
/// of key/value pairs. Those parameters are deserialized from the
24+
/// <see cref="JsonElement"/> values in that collection. There are a few exceptions to this:
25+
/// <list type="bullet">
26+
/// <item>
27+
/// <description>
28+
/// <see cref="CancellationToken"/> parameters are automatically bound to a <see cref="CancellationToken"/> provided by the
29+
/// <see cref="IMcpServer"/> and that respects any <see cref="CancelledNotification"/>s sent by the client for this operation's
30+
/// <see cref="RequestId"/>.
31+
/// </description>
32+
/// </item>
33+
/// <item>
34+
/// <description>
35+
/// <see cref="IServiceProvider"/> parameters are bound from the <see cref="RequestContext{GetPromptRequestParams}"/> for this request.
36+
/// </description>
37+
/// </item>
38+
/// <item>
39+
/// <description>
40+
/// <see cref="IMcpServer"/> parameters are bound directly to the <see cref="IMcpServer"/> instance associated
41+
/// with this request's <see cref="RequestContext{CallPromptRequestParams}"/>. Such parameters may be used to understand
42+
/// what server is being used to process the request, and to interact with the client issuing the request to that server.
43+
/// </description>
44+
/// </item>
45+
/// <item>
46+
/// <description>
47+
/// <see cref="IProgress{ProgressNotificationValue}"/> parameters accepting <see cref="ProgressNotificationValue"/> values
48+
/// are bound to an <see cref="IProgress{ProgressNotificationValue}"/> instance manufactured to forward progress notifications
49+
/// from the prompt to the client. If the client included a <see cref="ProgressToken"/> in their request, progress reports issued
50+
/// to this instance will propagate to the client as <see cref="NotificationMethods.ProgressNotification"/> notifications with
51+
/// that token. If the client did not include a <see cref="ProgressToken"/>, the instance will ignore any progress reports issued to it.
52+
/// </description>
53+
/// </item>
54+
/// <item>
55+
/// <description>
56+
/// When the <see cref="McpServerPrompt"/> is constructed, it may be passed an <see cref="IServiceProvider"/> via
57+
/// <see cref="McpServerPromptCreateOptions.Services"/>. Any parameter that can be satisfied by that <see cref="IServiceProvider"/>
58+
/// will be resolved from the <see cref="IServiceProvider"/> provided to the prompt invocation rather than from the argument collection.
59+
/// </description>
60+
/// </item>
61+
/// <item>
62+
/// <description>
63+
/// Any parameter attributed with <see cref="FromKeyedServicesAttribute"/> will similarly be resolved from the
64+
/// <see cref="IServiceProvider"/> provided to the prompt invocation rather than from the argument collection.
65+
/// </description>
66+
/// </item>
67+
/// </list>
68+
/// </para>
69+
/// <para>
70+
/// All other parameters are deserialized from the <see cref="JsonElement"/>s in the <see cref="GetPromptRequestParams.Arguments"/> dictionary.
71+
/// </para>
72+
/// <para>
73+
/// In general, the data supplied via the <see cref="GetPromptRequestParams.Arguments"/>'s dictionary is passed along from the caller and
74+
/// should thus be considered unvalidated and untrusted. To provide validated and trusted data to the invocation of the prompt, consider having
75+
/// the prompt be an instance method, referring to data stored in the instance, or using an instance or parameters resolved from the <see cref="IServiceProvider"/>
76+
/// to provide data to the method.
77+
/// </para>
78+
/// <para>
79+
/// Return values from a method are used to create the <see cref="GetPromptResult"/> that is sent back to the client:
80+
/// </para>
81+
/// <list type="table">
82+
/// <item>
83+
/// <term><see cref="string"/></term>
84+
/// <description>Converted to a list containing a single <see cref="PromptMessage"/> with its <see cref="PromptMessage.Content"/> set to contain the <see cref="string"/>.</description>
85+
/// </item>
86+
/// <item>
87+
/// <term><see cref="PromptMessage"/></term>
88+
/// <description>Converted to a list containing the single <see cref="PromptMessage"/>.</description>
89+
/// </item>
90+
/// <item>
91+
/// <term><see cref="IEnumerable{PromptMessage}"/> of <see cref="PromptMessage"/></term>
92+
/// <description>Converted to a list containing all of the returned <see cref="PromptMessage"/> instances.</description>
93+
/// </item>
94+
/// <item>
95+
/// <term><see cref="ChatMessage"/></term>
96+
/// <description>Converted to a list of <see cref="PromptMessage"/> instances derived from the <see cref="ChatMessage"/> with <see cref="AIContentExtensions.ToPromptMessages"/>.</description>
97+
/// </item>
98+
/// <item>
99+
/// <term><see cref="IEnumerable{PromptMessage}"/> of <see cref="PromptMessage"/></term>
100+
/// <description>Converted to a list of <see cref="PromptMessage"/> instances derived from all of the <see cref="ChatMessage"/> instances with <see cref="AIContentExtensions.ToPromptMessages"/>.</description>
101+
/// </item>
102+
/// </list>
103+
/// <para>
104+
/// Other returned types will result in an <see cref="InvalidOperationException"/> being thrown.
105+
/// </para>
17106
/// </remarks>
18107
[AttributeUsage(AttributeTargets.Method)]
19108
public sealed class McpServerPromptAttribute : Attribute

0 commit comments

Comments
 (0)