Skip to content

Commit 237f84c

Browse files
committed
Add examples to README
1 parent b057aac commit 237f84c

File tree

1 file changed

+80
-4
lines changed

1 file changed

+80
-4
lines changed

README.md

Lines changed: 80 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1175,7 +1175,7 @@ await server.connect(transport);
11751175

11761176
### Eliciting User Input
11771177

1178-
MCP servers can request additional information from users through the elicitation feature. This is useful for interactive workflows where the server needs user input or confirmation:
1178+
MCP servers can request non-sensitive information from users through the form elicitation capability. This is useful for interactive workflows where the server needs user input or confirmation:
11791179

11801180
```typescript
11811181
// Server-side: Restaurant booking tool that asks for alternatives
@@ -1207,7 +1207,7 @@ server.registerTool(
12071207

12081208
if (!available) {
12091209
// Ask user if they want to try alternative dates
1210-
const result = await server.server.elicitInput({
1210+
const result = await server.server.elicitFormInput({
12111211
message: `No tables available at ${restaurant} on ${date}. Would you like to check alternative dates?`,
12121212
requestedSchema: {
12131213
type: 'object',
@@ -1274,7 +1274,7 @@ server.registerTool(
12741274
);
12751275
```
12761276

1277-
Client-side: Handle elicitation requests
1277+
On the client side, handle form elicitation requests:
12781278

12791279
```typescript
12801280
// This is a placeholder - implement based on your UI framework
@@ -1299,7 +1299,83 @@ client.setRequestHandler(ElicitRequestSchema, async request => {
12991299
});
13001300
```
13011301

1302-
**Note**: Elicitation requires client support. Clients must declare the `elicitation` capability during initialization.
1302+
Elicitation is a client capability. Clients must declare the `elicitation` capability during initialization:
1303+
1304+
```typescript
1305+
const client = new Client(
1306+
{
1307+
name: 'example-client',
1308+
version: '1.0.0'
1309+
},
1310+
{
1311+
capabilities: {
1312+
elicitation: {
1313+
form: {}
1314+
}
1315+
}
1316+
}
1317+
);
1318+
```
1319+
1320+
**Note**: Form elicitation **must** only be used to gather non-sensitive information. For sensitive information such as API keys or secrets, use URL elicitation instead.
1321+
1322+
### Eliciting URL Actions
1323+
1324+
MCP servers can prompt the user to perform a URL-based action through URL elicitation. This is useful for securely gathering sensitive information such as API keys or secrets, or for redirecting users to secure web-based flows.
1325+
1326+
```typescript
1327+
// Server-side: Prompt the user to navigate to a URL
1328+
const result = await server.server.elicitURLInput({
1329+
message: 'Please enter your API key',
1330+
requestedSchema: {
1331+
type: 'string'
1332+
}
1333+
});
1334+
1335+
// Alternative, return an error from within a tool:
1336+
throw new UrlElicitationRequiredError([
1337+
{
1338+
mode: 'url',
1339+
message: 'This tool requires a payment confirmation. Open the link to confirm payment!',
1340+
url: `http://localhost:${MCP_PORT}/confirm-payment?session=${sessionId}&elicitation=${elicitationId}&cartId=${encodeURIComponent(cartId)}`,
1341+
elicitationId
1342+
}
1343+
]);
1344+
```
1345+
1346+
On the client side, handle URL elicitation requests:
1347+
1348+
```typescript
1349+
client.setRequestHandler(ElicitRequestSchema, async request => {
1350+
if (request.params.mode !== 'url') {
1351+
throw new McpError(ErrorCode.InvalidParams, `Unsupported elicitation mode: ${request.params.mode}`);
1352+
}
1353+
1354+
// At a minimum, implement a UI that:
1355+
// - Display the full URL and server reason to prevent phishing
1356+
// - Explicitly ask the user for consent, with clear decline/cancel options
1357+
// - Open the URL in the system (not embedded) browser
1358+
// Optionally, listen for a `nofifications/elicitation/complete` message from the server
1359+
});
1360+
```
1361+
1362+
Elicitation is a client capability. Clients must declare the `elicitation` capability during initialization:
1363+
1364+
```typescript
1365+
const client = new Client(
1366+
{
1367+
name: 'example-client',
1368+
version: '1.0.0'
1369+
},
1370+
{
1371+
capabilities: {
1372+
elicitation: {
1373+
url: {}
1374+
}
1375+
}
1376+
}
1377+
);
1378+
```
13031379

13041380
### Writing MCP Clients
13051381

0 commit comments

Comments
 (0)