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

Commit a85dbaf

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

2 files changed

Lines changed: 85 additions & 2 deletions

File tree

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

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -492,9 +492,19 @@ 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+
### Elicitation
496+
497+
An MCP server can request additional information from the user by sending an elicitation request. Set up an elicitation callback to handle these requests:
498+
499+
```typescript
500+
--8<-- "user-guide/concepts/tools/mcp-tools.ts:elicitation_server"
497501
```
502+
503+
```typescript
504+
--8<-- "user-guide/concepts/tools/mcp-tools.ts:elicitation"
505+
```
506+
507+
For more information on elicitation, see the [MCP specification](https://modelcontextprotocol.io/specification/draft/client/elicitation).
498508
</Tab>
499509
</Tabs>
500510

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

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,79 @@ const transport = new StdioServerTransport()
143143
await server.connect(transport)
144144
// --8<-- [end:mcp_server]
145145

146+
// --8<-- [start:elicitation_server]
147+
// @ts-nocheck — standalone snippet; duplicates imports from mcp_server snippet above
148+
// server.ts
149+
import { McpServer } from '@modelcontextprotocol/sdk/server/mcp.js'
150+
import { StdioServerTransport } from '@modelcontextprotocol/sdk/server/stdio.js'
151+
import { z } from 'zod'
152+
153+
const elicitServer = new McpServer({
154+
name: 'File Tools',
155+
version: '1.0.0',
156+
})
157+
158+
elicitServer.tool(
159+
'delete_files',
160+
'Delete the given files after getting user approval.',
161+
{
162+
paths: z.array(z.string()),
163+
},
164+
async ({ paths }) => {
165+
const result = await elicitServer.server.elicitInput({
166+
message: `Do you want to delete ${paths.join(', ')}?`,
167+
requestedSchema: {
168+
type: 'object',
169+
properties: {
170+
username: { type: 'string', description: 'Who is approving?' },
171+
},
172+
required: ['username'],
173+
},
174+
})
175+
176+
if (result.action !== 'accept') {
177+
return { content: [{ type: 'text', text: `User ${result.action}ed deletion` }] }
178+
}
179+
180+
// Perform deletion...
181+
const username = (result.content as { username: string }).username
182+
return { content: [{ type: 'text', text: `User ${username} approved deletion` }] }
183+
}
184+
)
185+
186+
await elicitServer.connect(new StdioServerTransport())
187+
// --8<-- [end:elicitation_server]
188+
189+
// --8<-- [start:elicitation]
190+
// client.ts
191+
import type { ElicitationCallback } from '@strands-agents/sdk'
192+
193+
const elicitationCallback: ElicitationCallback = async (_context, params) => {
194+
console.log(`ELICITATION: ${params.message}`)
195+
// Get user confirmation...
196+
return {
197+
action: 'accept',
198+
content: { username: 'myname' },
199+
}
200+
}
201+
202+
const elicitClient = new McpClient({
203+
transport: new StdioClientTransport({
204+
command: 'npx',
205+
args: ['tsx', '/path/to/server.ts'],
206+
}),
207+
elicitationCallback,
208+
})
209+
210+
const agentElicit = new Agent({
211+
tools: [elicitClient],
212+
})
213+
214+
await agentElicit.invoke("Delete 'a/b/c.txt' and share the name of the approver")
215+
216+
await elicitClient.disconnect()
217+
// --8<-- [end:elicitation]
218+
146219
// --8<-- [start:tools_overview_example]
147220
// Create MCP client with stdio transport
148221
const mcpClientOverview = new McpClient({

0 commit comments

Comments
 (0)