-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfull_showcase.py
More file actions
358 lines (307 loc) · 12.9 KB
/
full_showcase.py
File metadata and controls
358 lines (307 loc) · 12.9 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
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
# examples/python/full_showcase.py
# A project management assistant — the full protomcp showcase.
#
# Tools: login, create_task, list_tasks, assign_task, complete_task, search_tasks
# Resources: project://config, project://tasks/{id}, project://team
# Prompts: standup_report, sprint_review, task_breakdown
# Completions: task IDs, team members, task statuses
#
# Run: pmcp dev examples/python/full_showcase.py
import json
import time
from protomcp import (
tool, ToolResult, ToolContext, log,
resource, resource_template, ResourceContent,
prompt, PromptArg, PromptMessage,
completion, CompletionResult,
tool_manager,
)
# ─── In-Memory Data ─────────────────────────────────────────────────────────
PROJECT = {
"name": "protomcp v2",
"sprint": "Sprint 14",
"started": "2026-03-10",
"ends": "2026-03-24",
}
TEAM = {
"alice": {"name": "Alice Chen", "role": "Engineering Lead"},
"bob": {"name": "Bob Park", "role": "Backend Engineer"},
"carol": {"name": "Carol Diaz", "role": "Frontend Engineer"},
"dave": {"name": "Dave Kim", "role": "QA Engineer"},
}
TASKS: dict[str, dict] = {
"PROJ-101": {
"title": "Implement WebSocket hub",
"status": "in_progress",
"assignee": "bob",
"priority": "high",
"description": "Build the WebSocket event broadcasting system for the playground backend.",
},
"PROJ-102": {
"title": "Design trace panel UI",
"status": "in_progress",
"assignee": "carol",
"priority": "high",
"description": "Create the protocol trace visualization panel with color-coded entries and expand/collapse.",
},
"PROJ-103": {
"title": "Write e2e test suite",
"status": "todo",
"assignee": "dave",
"priority": "medium",
"description": "End-to-end tests covering tool calls, resource reads, and prompt gets through the full stack.",
},
"PROJ-104": {
"title": "Add sampling support",
"status": "done",
"assignee": "alice",
"priority": "high",
"description": "Bidirectional sampling: SDK process requests LLM calls from the MCP client.",
},
"PROJ-105": {
"title": "Performance benchmarks",
"status": "todo",
"assignee": None,
"priority": "low",
"description": "Measure latency overhead of the bridge layer vs direct SDK calls.",
},
}
_next_id = 106
_authenticated = False
# ─── Resources ───────────────────────────────────────────────────────────────
@resource(
uri="project://config",
description="Current project and sprint configuration",
mime_type="application/json",
)
def project_config(uri: str) -> ResourceContent:
return ResourceContent(uri=uri, text=json.dumps(PROJECT, indent=2), mime_type="application/json")
@resource(
uri="project://team",
description="Team members and their roles",
mime_type="application/json",
)
def team_info(uri: str) -> ResourceContent:
team_list = [{"id": k, **v} for k, v in TEAM.items()]
return ResourceContent(uri=uri, text=json.dumps(team_list, indent=2), mime_type="application/json")
@resource_template(
uri_template="project://tasks/{task_id}",
description="Read a specific task by ID (e.g. PROJ-101)",
mime_type="application/json",
)
def read_task(uri: str) -> ResourceContent:
task_id = uri.replace("project://tasks/", "")
task = TASKS.get(task_id)
if not task:
return ResourceContent(uri=uri, text=json.dumps({"error": f"Task {task_id} not found"}))
return ResourceContent(
uri=uri,
text=json.dumps({"id": task_id, **task}, indent=2),
mime_type="application/json",
)
# ─── Prompts ─────────────────────────────────────────────────────────────────
@prompt(
description="Generate a standup report for a team member",
arguments=[
PromptArg(name="member", description="Team member ID (e.g. alice, bob)", required=True),
],
)
def standup_report(member: str) -> list[PromptMessage]:
person = TEAM.get(member, {"name": member, "role": "Unknown"})
their_tasks = [
f"- [{t['status']}] {tid}: {t['title']}"
for tid, t in TASKS.items()
if t.get("assignee") == member
]
task_list = "\n".join(their_tasks) if their_tasks else " (no tasks assigned)"
return [
PromptMessage(
role="user",
content=f"Generate a standup report for {person['name']} ({person['role']}).\n\nTheir current tasks:\n{task_list}\n\nInclude: what they did yesterday, what they're doing today, any blockers.",
),
]
@prompt(
description="Write a sprint review summary",
arguments=[
PromptArg(name="include_metrics", description="Include velocity metrics (true/false)"),
],
)
def sprint_review(include_metrics: str = "true") -> list[PromptMessage]:
done = [f"- {tid}: {t['title']}" for tid, t in TASKS.items() if t["status"] == "done"]
in_progress = [f"- {tid}: {t['title']}" for tid, t in TASKS.items() if t["status"] == "in_progress"]
todo = [f"- {tid}: {t['title']}" for tid, t in TASKS.items() if t["status"] == "todo"]
summary = f"Sprint: {PROJECT['sprint']} ({PROJECT['started']} to {PROJECT['ends']})\n\n"
summary += f"Done ({len(done)}):\n" + ("\n".join(done) or " (none)") + "\n\n"
summary += f"In Progress ({len(in_progress)}):\n" + ("\n".join(in_progress) or " (none)") + "\n\n"
summary += f"Todo ({len(todo)}):\n" + ("\n".join(todo) or " (none)")
messages = [PromptMessage(role="user", content=f"Write a sprint review summary based on this data:\n\n{summary}")]
if include_metrics.lower() == "true":
total = len(TASKS)
velocity = len(done) / max(total, 1) * 100
messages.append(
PromptMessage(role="user", content=f"Also include: velocity is {velocity:.0f}% ({len(done)}/{total} tasks complete)")
)
return messages
@prompt(
description="Break down a feature into implementation tasks",
arguments=[
PromptArg(name="feature", description="Feature to break down", required=True),
PromptArg(name="complexity", description="Expected complexity: small, medium, large"),
],
)
def task_breakdown(feature: str, complexity: str = "medium") -> list[PromptMessage]:
return [
PromptMessage(
role="user",
content=f"Break down this feature into implementation tasks:\n\nFeature: {feature}\nComplexity: {complexity}\n\nFor each task, provide: title, description, estimated effort, and suggested assignee from the team (alice=lead, bob=backend, carol=frontend, dave=qa).",
),
]
# ─── Completions ─────────────────────────────────────────────────────────────
@completion("ref/prompt", "standup_report", "member")
def complete_member(value: str) -> CompletionResult:
matches = [m for m in TEAM if m.startswith(value)]
return CompletionResult(values=matches, total=len(matches))
@completion("ref/prompt", "sprint_review", "include_metrics")
def complete_bool(value: str) -> list[str]:
return [v for v in ["true", "false"] if v.startswith(value)]
@completion("ref/prompt", "task_breakdown", "complexity")
def complete_complexity(value: str) -> list[str]:
return [v for v in ["small", "medium", "large"] if v.startswith(value)]
@completion("ref/resource", "project://tasks/{task_id}", "task_id")
def complete_task_id(value: str) -> CompletionResult:
matches = [tid for tid in TASKS if tid.startswith(value)]
return CompletionResult(values=matches, total=len(matches))
# ─── Tools ───────────────────────────────────────────────────────────────────
@tool(
"Authenticate to unlock project management tools",
title="Login",
read_only=True,
)
def login(token: str) -> ToolResult:
global _authenticated
if token == "admin":
_authenticated = True
log.info("User authenticated successfully")
return ToolResult(
result="Authenticated. Project management tools are now available.",
enable_tools=["create_task", "assign_task", "complete_task"],
)
return ToolResult(
result="Invalid token. Use 'admin' to authenticate.",
is_error=True,
error_code="AUTH_FAILED",
suggestion="Try: login with token 'admin'",
)
@tool(
"List all tasks in the current sprint, optionally filtered by status",
title="List Tasks",
read_only=True,
)
def list_tasks(status: str = "") -> ToolResult:
log.debug(f"Listing tasks with filter: status={status or 'all'}")
filtered = {
tid: t for tid, t in TASKS.items()
if not status or t["status"] == status
}
result = []
for tid, t in filtered.items():
assignee = TEAM.get(t["assignee"], {}).get("name", "Unassigned") if t["assignee"] else "Unassigned"
result.append({
"id": tid,
"title": t["title"],
"status": t["status"],
"assignee": assignee,
"priority": t["priority"],
})
return ToolResult(result=json.dumps(result, indent=2))
@tool(
"Search tasks by keyword in title or description",
title="Search Tasks",
read_only=True,
)
def search_tasks(ctx: ToolContext, query: str) -> ToolResult:
log.info(f"Searching tasks for: {query}")
matches = {}
items = list(TASKS.items())
for i, (tid, t) in enumerate(items):
ctx.report_progress(i + 1, len(items), f"Searching {tid}...")
if query.lower() in t["title"].lower() or query.lower() in t["description"].lower():
matches[tid] = {
"title": t["title"],
"status": t["status"],
"match_in": "title" if query.lower() in t["title"].lower() else "description",
}
time.sleep(0.05) # Simulate work
return ToolResult(result=json.dumps(matches, indent=2))
@tool(
"Create a new task in the current sprint",
title="Create Task",
destructive=True,
hidden=True,
)
def create_task(title: str, description: str, priority: str = "medium") -> ToolResult:
global _next_id
if not _authenticated:
return ToolResult(result="Not authenticated", is_error=True, error_code="UNAUTHORIZED")
task_id = f"PROJ-{_next_id}"
_next_id += 1
TASKS[task_id] = {
"title": title,
"description": description,
"status": "todo",
"assignee": None,
"priority": priority,
}
log.info(f"Created task {task_id}: {title}")
return ToolResult(result=json.dumps({"id": task_id, "title": title, "status": "todo"}))
@tool(
"Assign a task to a team member",
title="Assign Task",
hidden=True,
)
def assign_task(task_id: str, member: str) -> ToolResult:
if not _authenticated:
return ToolResult(result="Not authenticated", is_error=True, error_code="UNAUTHORIZED")
if task_id not in TASKS:
return ToolResult(result=f"Task {task_id} not found", is_error=True, error_code="NOT_FOUND")
if member not in TEAM:
return ToolResult(
result=f"Unknown member '{member}'",
is_error=True,
error_code="INVALID_MEMBER",
suggestion=f"Valid members: {', '.join(TEAM.keys())}",
)
TASKS[task_id]["assignee"] = member
log.info(f"Assigned {task_id} to {TEAM[member]['name']}")
return ToolResult(result=json.dumps({
"task": task_id,
"assignee": TEAM[member]["name"],
"role": TEAM[member]["role"],
}))
@tool(
"Mark a task as complete",
title="Complete Task",
destructive=True,
hidden=True,
)
def complete_task(ctx: ToolContext, task_id: str) -> ToolResult:
if not _authenticated:
return ToolResult(result="Not authenticated", is_error=True, error_code="UNAUTHORIZED")
if task_id not in TASKS:
return ToolResult(result=f"Task {task_id} not found", is_error=True, error_code="NOT_FOUND")
ctx.report_progress(1, 3, "Validating task...")
time.sleep(0.2)
ctx.report_progress(2, 3, "Running completion checks...")
time.sleep(0.2)
ctx.report_progress(3, 3, "Marking complete")
old_status = TASKS[task_id]["status"]
TASKS[task_id]["status"] = "done"
log.info(f"Completed {task_id} (was: {old_status})")
return ToolResult(result=json.dumps({
"task": task_id,
"previous_status": old_status,
"new_status": "done",
}))
if __name__ == "__main__":
from protomcp.runner import run
run()