Skip to content
This repository was archived by the owner on Jun 3, 2026. It is now read-only.

Commit 8520272

Browse files
committed
docs: add TypeScript MCP elicitation examples
1 parent cde8d07 commit 8520272

2 files changed

Lines changed: 86 additions & 5 deletions

File tree

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
// @ts-nocheck
2+
3+
// --8<-- [start:elicitation_server]
4+
// server.ts
5+
import { McpServer } from '@modelcontextprotocol/sdk/server/mcp.js'
6+
import { StdioServerTransport } from '@modelcontextprotocol/sdk/server/stdio.js'
7+
import { z } from 'zod'
8+
9+
const server = new McpServer({
10+
name: 'File Tools',
11+
version: '1.0.0',
12+
})
13+
14+
server.tool(
15+
'delete_files',
16+
'Delete the given files after getting user approval.',
17+
{
18+
paths: z.array(z.string()),
19+
},
20+
async ({ paths }) => {
21+
const result = await server.server.elicitInput({
22+
message: `Do you want to delete ${paths.join(', ')}?`,
23+
requestedSchema: {
24+
type: 'object',
25+
properties: {
26+
username: { type: 'string', description: 'Who is approving?' },
27+
},
28+
required: ['username'],
29+
},
30+
})
31+
32+
if (result.action !== 'accept') {
33+
return { content: [{ type: 'text', text: `User ${result.action}ed deletion` }] }
34+
}
35+
36+
// Perform deletion...
37+
const username = (result.content as { username: string }).username
38+
return { content: [{ type: 'text', text: `User ${username} approved deletion` }] }
39+
}
40+
)
41+
42+
await server.connect(new StdioServerTransport())
43+
// --8<-- [end:elicitation_server]
44+
45+
// --8<-- [start:elicitation_client]
46+
// client.ts
47+
import { Agent, McpClient } from '@strands-agents/sdk'
48+
import type { ElicitationCallback } from '@strands-agents/sdk'
49+
import { StdioClientTransport } from '@modelcontextprotocol/sdk/client/stdio.js'
50+
51+
const elicitationCallback: ElicitationCallback = async (_context, params) => {
52+
console.log(`ELICITATION: ${params.message}`)
53+
// Get user confirmation...
54+
return {
55+
action: 'accept',
56+
content: { username: 'myname' },
57+
}
58+
}
59+
60+
const elicitClient = new McpClient({
61+
transport: new StdioClientTransport({
62+
command: 'npx',
63+
args: ['tsx', '/path/to/server.ts'],
64+
}),
65+
elicitationCallback,
66+
})
67+
68+
const agentElicit = new Agent({
69+
tools: [elicitClient],
70+
})
71+
72+
await agentElicit.invoke("Delete 'a/b/c.txt' and share the name of the approver")
73+
74+
await elicitClient.disconnect()
75+
// --8<-- [end:elicitation_client]

src/content/docs/user-guide/concepts/tools/mcp-tools.mdx

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -429,13 +429,13 @@ For more information on implementing MCP servers, see the [MCP documentation](ht
429429

430430
## Advanced Usage
431431

432-
<Tabs>
433-
<Tab label="Python">
434-
435432
### Elicitation
436433

437434
An MCP server can request additional information from the user by sending an elicitation request. Set up an elicitation callback to handle these requests:
438435

436+
<Tabs>
437+
<Tab label="Python">
438+
439439
```python
440440
# server.py
441441
from mcp.server import FastMCP
@@ -492,9 +492,15 @@ For more information on elicitation, see the [MCP specification](https://modelco
492492
</Tab>
493493
<Tab label="TypeScript">
494494

495-
```ts
496-
// Not supported in TypeScript
495+
```typescript
496+
--8<-- "user-guide/concepts/tools/mcp-tools-elicitation.ts:elicitation_server"
497+
```
498+
499+
```typescript
500+
--8<-- "user-guide/concepts/tools/mcp-tools-elicitation.ts:elicitation_client"
497501
```
502+
503+
For more information on elicitation, see the [MCP specification](https://modelcontextprotocol.io/specification/draft/client/elicitation).
498504
</Tab>
499505
</Tabs>
500506

0 commit comments

Comments
 (0)