You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/concepts/elicitation/elicitation.md
+170-9Lines changed: 170 additions & 9 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -9,13 +9,19 @@ uid: elicitation
9
9
10
10
The **elicitation** feature allows servers to request additional information from users during interactions. This enables more dynamic and interactive AI experiences, making it easier to gather necessary context before executing tasks.
11
11
12
+
The protocol supports two modes of elicitation:
13
+
-**Form (In-Band)**: The server requests structured data (strings, numbers, booleans, enums) which the client collects via a form interface and returns to the server.
14
+
-**URL Mode**: The server provides a URL for the user to visit (e.g., for OAuth, payments, or sensitive data entry). The interaction happens outside the MCP client.
15
+
12
16
### Server Support for Elicitation
13
17
14
-
Servers request structured data from users with the <xref:ModelContextProtocol.Server.McpServer.ElicitAsync*> extension method on <xref:ModelContextProtocol.Server.McpServer>.
18
+
Servers request information from users with the <xref:ModelContextProtocol.Server.McpServer.ElicitAsync*> extension method on <xref:ModelContextProtocol.Server.McpServer>.
15
19
The C# SDK registers an instance of <xref:ModelContextProtocol.Server.McpServer> with the dependency injection container,
16
20
so tools can simply add a parameter of type <xref:ModelContextProtocol.Server.McpServer> to their method signature to access it.
17
21
18
-
The MCP Server must specify the schema of each input value it's requesting from the user.
22
+
#### Form Mode Elicitation (In-Band)
23
+
24
+
For form-based elicitation, the MCP Server must specify the schema of each input value it is requesting from the user.
19
25
Primitive types (string, number, Boolean) and enum types are supported for elicitation requests.
20
26
The schema might include a description to help the user understand what's being requested.
21
27
@@ -34,19 +40,174 @@ The following example demonstrates how a server could request a Boolean response
For URL mode elicitation, the server provides a URL that the user must visit to complete an action. This is useful for scenarios like OAuth flows, payment processing, or collecting sensitive credentials that should not be exposed to the MCP client.
46
+
47
+
To request a URL mode interaction, set the `Mode` to "url" and provide a `Url` and `ElicitationId` in the `ElicitRequestParams`.
Message="Please authorize access to your account by logging in through your browser."
58
+
},
59
+
cancellationToken);
60
+
```
61
+
37
62
### Client Support for Elicitation
38
63
39
-
Elicitation is an optional feature so clients declare their support for it in their capabilities as part of the `initialize` request. In the MCP C# SDK, this is done by configuring an <xref:ModelContextProtocol.Client.McpClientHandlers.ElicitationHandler> in the <xref:ModelContextProtocol.Client.McpClientOptions>:
64
+
Clients declare their support for elicitation in their capabilities as part of the `initialize` request. Clients can support `Form` (in-band), `Url` (out-of-band), or both.
65
+
66
+
In the MCP C# SDK, this is done by configuring the capabilities and an <xref:ModelContextProtocol.Client.McpClientHandlers.ElicitationHandler> in the <xref:ModelContextProtocol.Client.McpClientOptions>:
The ElicitationHandler is an asynchronous method that will be called when the server requests additional information.
44
-
The ElicitationHandler must request input from the user and return the data in a format that matches the requested schema.
45
-
This will be highly dependent on the client application and how it interacts with the user.
86
+
The `ElicitationHandler` is an asynchronous method that will be called when the server requests additional information. The handler should check the `Mode` of the request:
46
87
47
-
If the user provides the requested information, the ElicitationHandler should return an <xref:ModelContextProtocol.Protocol.ElicitResult> with the action set to "accept" and the content containing the user's input.
48
-
If the user does not provide the requested information, the ElicitationHandler should return an [<xref:ModelContextProtocol.Protocol.ElicitResult> with the action set to "reject" and no content.
88
+
-**Form Mode**: Present the form defined by `RequestedSchema` to the user. Return the user's input in the `Content` of the result.
89
+
-**URL Mode**: Present the `Message` and `Url` to the user. Ask for consent to open the URL. If the user consents, open the URL and return `Action="accept"`. If the user declines, return `Action="decline"`.
90
+
91
+
If the user provides the requested information (or consents to URL mode), the ElicitationHandler should return an <xref:ModelContextProtocol.Protocol.ElicitResult> with the action set to "accept".
92
+
If the user does not provide the requested information, the ElicitationHandler should return an <xref:ModelContextProtocol.Protocol.ElicitResult> with the action set to "reject" (or "decline" / "cancel").
49
93
50
94
Here's an example implementation of how a console application might handle elicitation requests:
When a tool cannot proceed without first completing a URL-mode elicitation (for example, when third-party OAuth authorization is needed), and calling `ElicitAsync` is not practical (for example in <xref: ModelContextProtocol.AspNetCore.HttpServerTransportOptions.Stateless> is enabled disabling server-to-client requets), the server may throw a <xref:ModelContextProtocol.UrlElicitationRequiredException>. This is a specialized error (JSON-RPC error code `-32042`) that signals to the client that one or more URL-mode elicitations must be completed before the original request can be retried.
101
+
102
+
#### Throwing UrlElicitationRequiredException on the Server
103
+
104
+
A server tool can throw `UrlElicitationRequiredException` when it detects that authorization or other out-of-band interaction is required:
105
+
106
+
```csharp
107
+
[McpServerTool, Description("A tool that requires third-party authorization")]
Console.WriteLine($"Tool succeeded on retry: {retryResult.Content[0]}");
187
+
}
188
+
```
189
+
190
+
#### Listening for Elicitation Completion Notifications
191
+
192
+
Servers can optionally send a `notifications/elicitation/complete` notification when the out-of-band interaction is complete. Clients can register a handler to receive these notifications:
// Signal that the client can now retry the original request
206
+
}
207
+
});
208
+
```
209
+
210
+
This pattern is particularly useful for:
211
+
-**Third-party OAuth flows**: When the MCP server needs to obtain tokens from external services on behalf of the user
212
+
-**Payment processing**: When user confirmation is required through a secure payment interface
213
+
-**Sensitive credential collection**: When API keys or other secrets must be entered directly on a trusted server page rather than through the MCP client
0 commit comments