|
| 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] |
0 commit comments