-
Notifications
You must be signed in to change notification settings - Fork 366
Expand file tree
/
Copy pathserver.py
More file actions
295 lines (257 loc) · 9.34 KB
/
server.py
File metadata and controls
295 lines (257 loc) · 9.34 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
#!/usr/bin/env python3
"""
MCP Server demonstrating elicitation capabilities.
This server provides tools that use MCP elicitation to request additional
information from the user during tool execution.
Usage with docker-agent:
docker agent run examples/elicitation/agent.yaml
Usage standalone:
uvx --with "mcp[cli]" mcp run examples/elicitation/server.py
"""
import json
from mcp.server.fastmcp import FastMCP
# Create the MCP server
mcp = FastMCP("elicitation-demo")
@mcp.tool()
async def confirm_action(action: str) -> str:
"""
Perform an action that requires user confirmation.
Use this when you need to confirm something with the user.
Args:
action: The action to confirm
"""
ctx = mcp.get_context()
# Request confirmation from the user
result = await ctx.session.elicit(
message=f"Are you sure you want to proceed with: {action}?",
requestedSchema={
"type": "object",
"properties": {
"confirmed": {
"type": "boolean",
"description": "Confirm this action",
"default": False,
},
"reason": {
"type": "string",
"description": "Optional reason for your decision",
},
},
},
)
if result.action == "accept":
content = result.content or {}
if content.get("confirmed", False):
reason = content.get("reason", "")
reason_text = f" Reason: {reason}" if reason else ""
return f"✅ Action confirmed: {action}.{reason_text}"
else:
return f"❌ Action declined: {action}"
else:
return f"⚠️ Confirmation cancelled for: {action}"
@mcp.tool()
async def create_user() -> str:
"""
Create a new user with interactive form input.
Demonstrates multi-field elicitation with validation.
"""
ctx = mcp.get_context()
result = await ctx.session.elicit(
message="Please provide the new user details:",
requestedSchema={
"type": "object",
"properties": {
"username": {
"type": "string",
"description": "Username (3-20 characters)",
"minLength": 3,
"maxLength": 20,
},
"email": {
"type": "string",
"description": "Email address",
"format": "email",
},
"role": {
"type": "string",
"description": "User role",
"enum": ["admin", "editor", "viewer"],
},
"bio": {
"type": "string",
"description": "Short bio (optional)",
},
"active": {
"type": "boolean",
"description": "Account active",
"default": True,
},
},
"required": ["username", "email", "role"],
},
)
if result.action == "accept":
content = result.content or {}
user_info = json.dumps(content, indent=2)
return f"✅ User created successfully!\n\nUser details:\n{user_info}"
else:
return "❌ User creation cancelled."
@mcp.tool()
async def configure_settings(preset: str = "default") -> str:
"""
Configure numeric settings with validation.
Demonstrates number field elicitation.
Args:
preset: Optional preset name to start from (default, performance, or reliable)
"""
ctx = mcp.get_context()
# Default values based on preset
defaults = {
"default": {"max_connections": 10, "timeout": 30, "retry_count": 3},
"performance": {"max_connections": 100, "timeout": 5, "retry_count": 1},
"reliable": {"max_connections": 5, "timeout": 60, "retry_count": 5},
}.get(preset, {"max_connections": 10, "timeout": 30, "retry_count": 3})
result = await ctx.session.elicit(
message=f"Configure settings (preset: {preset}):",
requestedSchema={
"type": "object",
"properties": {
"max_connections": {
"type": "integer",
"description": "Maximum concurrent connections (1-100)",
"minimum": 1,
"maximum": 100,
"default": defaults["max_connections"],
},
"timeout": {
"type": "number",
"description": "Request timeout in seconds (1-300)",
"minimum": 1,
"maximum": 300,
"default": defaults["timeout"],
},
"retry_count": {
"type": "integer",
"description": "Number of retries (0-10)",
"minimum": 0,
"maximum": 10,
"default": defaults["retry_count"],
},
},
"required": ["max_connections", "timeout"],
},
)
if result.action == "accept":
content = result.content or {}
settings_info = json.dumps(content, indent=2)
return f"✅ Settings configured!\n\nConfiguration:\n{settings_info}"
else:
return "❌ Settings configuration cancelled."
@mcp.tool()
async def setup_preferences() -> str:
"""
Set up user preferences with boolean toggles.
Demonstrates boolean field elicitation.
"""
ctx = mcp.get_context()
result = await ctx.session.elicit(
message="Set up your preferences:",
requestedSchema={
"type": "object",
"properties": {
"dark_mode": {
"type": "boolean",
"description": "Enable dark mode theme",
"default": False,
},
"notifications": {
"type": "boolean",
"description": "Enable notifications",
"default": True,
},
"auto_save": {
"type": "boolean",
"description": "Auto-save documents",
"default": True,
},
"telemetry": {
"type": "boolean",
"description": "Share anonymous usage data",
"default": False,
},
},
},
)
if result.action == "accept":
content = result.content or {}
prefs_info = json.dumps(content, indent=2)
return f"✅ Preferences saved!\n\nYour preferences:\n{prefs_info}"
else:
return "❌ Preferences setup cancelled."
@mcp.tool()
async def select_option() -> str:
"""
Select from a list of options.
Demonstrates enum field elicitation.
"""
ctx = mcp.get_context()
result = await ctx.session.elicit(
message="Make your selections:",
requestedSchema={
"type": "object",
"properties": {
"environment": {
"type": "string",
"description": "Deployment environment",
"enum": ["development", "staging", "production"],
},
"region": {
"type": "string",
"description": "Server region",
"enum": ["us-east", "us-west", "eu-west", "ap-south"],
},
"tier": {
"type": "string",
"description": "Service tier",
"enum": ["free", "starter", "professional", "enterprise"],
},
},
"required": ["environment", "region"],
},
)
if result.action == "accept":
content = result.content or {}
selection_info = json.dumps(content, indent=2)
return f"✅ Selection confirmed!\n\nYour choices:\n{selection_info}"
else:
return "❌ Selection cancelled."
@mcp.tool()
async def visit_documentation(topic: str = "getting-started") -> str:
"""
Open documentation in the browser.
Demonstrates URL-based elicitation where the user visits an external page.
Args:
topic: Documentation topic (getting-started, api-reference, tutorials, faq)
"""
import uuid
ctx = mcp.get_context()
# Map topics to documentation URLs
docs_urls = {
"getting-started": "https://docs.example.com/getting-started",
"api-reference": "https://docs.example.com/api",
"tutorials": "https://docs.example.com/tutorials",
"faq": "https://docs.example.com/faq",
}
url = docs_urls.get(topic, docs_urls["getting-started"])
elicitation_id = str(uuid.uuid4())
result = await ctx.session.elicit_url(
message=f"Please visit the {topic} documentation and confirm when you're done reading:",
url=url,
elicitation_id=elicitation_id,
)
if result.action == "accept":
return f"✅ Great! You've reviewed the {topic} documentation at:\n{url}\n\nLet me know if you have any questions!"
else:
return f"❌ Documentation review cancelled. The {topic} docs are available at {url} when you're ready."
if __name__ == "__main__":
mcp.run()