-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsupervisor.py
More file actions
751 lines (611 loc) · 27 KB
/
Copy pathsupervisor.py
File metadata and controls
751 lines (611 loc) · 27 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
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
"""
supervisor.py — Agentic supervisor that discovers and orchestrates MCP tools dynamically.
Usage:
python supervisor.py --prompt "plan a trip to spain"
python supervisor.py --prompt "what's the weather in madrid?" --test
How it works:
1. Discovers available MCP tools from Modal.Dict registry
2. Sends user prompt + available tools to LLM
3. Executes tool calls via MCP JSON-RPC over HTTP
4. Loops until LLM returns final answer (max 10 iterations)
5. Returns comprehensive response to user
Prerequisites:
- Modal.Dict named "mcp-tool-registry" with {mcp_name: endpoint_url} entries
- Deployed MCP servers registered in the dictionary
- ANTHROPIC_API_KEY or OPENAI_API_KEY set (based on LLM_PROVIDER)
"""
import json
import os
import sys
import subprocess
import time
from typing import Any
from pathlib import Path
import dotenv
import requests
dotenv.load_dotenv()
# Import modal only when needed for registry access
try:
import modal
MODAL_AVAILABLE = True
except ImportError:
MODAL_AVAILABLE = False
print("Warning: modal package not installed. Registry access will not work.")
# ── Configuration ──────────────────────────────────────────────────────────────
#LLM_PROVIDER = os.getenv("LLM_PROVIDER", "openai").lower()
LLM_PROVIDER = "anthropic"
if LLM_PROVIDER not in ("anthropic", "openai"):
print(f"Error: LLM_PROVIDER must be 'anthropic' or 'openai', got '{LLM_PROVIDER}'", file=sys.stderr)
sys.exit(1)
MAX_ITERATIONS = 10 # Prevent infinite loops
REGISTRY_NAME = "mcp-tool-registry"
REQUEST_TIMEOUT = 30 # seconds for MCP HTTP calls
# ── MCP Communication ──────────────────────────────────────────────────────────
def parse_sse_response(response_text: str) -> dict:
"""
Parse Server-Sent Events (SSE) response from MCP servers.
Args:
response_text: Raw SSE response text (e.g., "event: message\ndata: {...}")
Returns:
Parsed JSON data from the SSE data field.
"""
import json
# SSE format: "event: message\ndata: {...}\n\n"
lines = response_text.strip().split('\n')
for line in lines:
if line.startswith('data: '):
data_json = line[6:] # Remove "data: " prefix
return json.loads(data_json)
# If no SSE format, try parsing as plain JSON
return json.loads(response_text)
def list_mcp_tools(endpoint_url: str) -> list[dict]:
"""
Call the MCP server's tools/list endpoint to discover available tools.
Args:
endpoint_url: Base URL of the MCP server (e.g., https://...--my-mcp-web.modal.run)
Returns:
List of tool definitions in MCP format.
Raises:
requests.RequestException: On HTTP errors.
"""
response = requests.post(
f"{endpoint_url}/mcp/",
json={
"jsonrpc": "2.0",
"id": 1,
"method": "tools/list",
"params": {}
},
headers={
"Content-Type": "application/json",
"Accept": "application/json, text/event-stream"
},
timeout=REQUEST_TIMEOUT,
)
response.raise_for_status()
# Parse response (may be SSE or plain JSON)
data = parse_sse_response(response.text)
if "error" in data:
raise RuntimeError(f"MCP error from {endpoint_url}: {data['error']}")
return data.get("result", {}).get("tools", [])
def call_mcp_tool(endpoint_url: str, tool_name: str, arguments: dict) -> Any:
"""
Execute a tool on an MCP server via JSON-RPC.
Args:
endpoint_url: Base URL of the MCP server.
tool_name: Name of the tool to call.
arguments: Dictionary of arguments to pass to the tool.
Returns:
The tool's return value (parsed from JSON).
Raises:
requests.RequestException: On HTTP errors.
RuntimeError: On MCP protocol errors.
"""
response = requests.post(
f"{endpoint_url}/mcp/",
json={
"jsonrpc": "2.0",
"id": 1,
"method": "tools/call",
"params": {
"name": tool_name,
"arguments": arguments
}
},
headers={
"Content-Type": "application/json",
"Accept": "application/json, text/event-stream"
},
timeout=REQUEST_TIMEOUT,
)
response.raise_for_status()
# Parse response (may be SSE or plain JSON)
data = parse_sse_response(response.text)
if "error" in data:
error_msg = data["error"].get("message", str(data["error"]))
raise RuntimeError(f"MCP tool error ({tool_name}): {error_msg}")
# MCP returns result.content as a list of content blocks
result = data.get("result", {})
content = result.get("content", [])
if not content:
return None
# Extract text from first content block
if isinstance(content, list) and len(content) > 0:
return content[0].get("text", str(content))
return str(content)
# ── Tool Discovery & Format Conversion ─────────────────────────────────────────
def mcp_to_openai_tool(mcp_tool: dict, tool_id: str) -> dict:
"""
Convert MCP tool definition to OpenAI function calling format.
Args:
mcp_tool: Tool definition from MCP tools/list response.
tool_id: Unique tool ID for routing (format: "mcp_<server>_<tool>").
Returns:
OpenAI-compatible tool definition.
"""
return {
"type": "function",
"function": {
"name": tool_id,
"description": mcp_tool.get("description", ""),
"parameters": mcp_tool.get("inputSchema", {"type": "object", "properties": {}})
}
}
def mcp_to_anthropic_tool(mcp_tool: dict, tool_id: str) -> dict:
"""
Convert MCP tool definition to Anthropic tool calling format.
Args:
mcp_tool: Tool definition from MCP tools/list response.
tool_id: Unique tool ID for routing (format: "mcp_<server>_<tool>").
Returns:
Anthropic-compatible tool definition.
"""
return {
"name": tool_id,
"description": mcp_tool.get("description", ""),
"input_schema": mcp_tool.get("inputSchema", {"type": "object", "properties": {}})
}
# Tool routing is now done via endpoint_map dictionary (removed decode_tool_call)
def discover_tools_from_registry(test_mode: bool = False, allow_empty: bool = False) -> tuple[list[dict], dict[str, str]]:
"""
Query Modal.Dict registry and discover all available MCP tools.
Args:
test_mode: If True, uses mock registry for local testing.
allow_empty: If True, returns empty list instead of raising error when registry is empty.
Returns:
Tuple of (tools_list, endpoint_map) where:
- tools_list: List of tools in LLM-specific format (OpenAI or Anthropic)
- endpoint_map: Mapping of {encoded_function_name: endpoint_url} for routing
Raises:
RuntimeError: If registry is inaccessible (not if empty and allow_empty=True).
"""
if test_mode:
# Mock registry for local testing without Modal
registry_items = {
"test-mcp": "https://test--test-mcp-web.modal.run"
}
print(f"[TEST MODE] Using mock registry with {len(registry_items)} entries")
else:
if not MODAL_AVAILABLE:
raise RuntimeError(
"Modal package not installed. Install it with: pip install modal"
)
try:
registry = modal.Dict.from_name(REGISTRY_NAME, create_if_missing=True)
registry_items = dict(registry)
except Exception as e:
raise RuntimeError(f"Could not access registry '{REGISTRY_NAME}': {e}")
if not registry_items:
if allow_empty:
print(f"\n Registry '{REGISTRY_NAME}' is empty (no tools available yet)")
return [], {}
raise RuntimeError(f"Registry '{REGISTRY_NAME}' is empty. Deploy some MCP servers first.")
print(f"\n Discovered {len(registry_items)} MCP server(s) in registry:")
all_tools = []
endpoint_map = {} # Maps tool_id -> (endpoint_url, original_tool_name)
for mcp_name, endpoint_url in registry_items.items():
print(f" • {mcp_name}: {endpoint_url}")
try:
mcp_tools = list_mcp_tools(endpoint_url)
print(f" → {len(mcp_tools)} tool(s) available")
for mcp_tool in mcp_tools:
# Create shorter tool ID: mcp_<server-slug>_<tool-name>
# Extract server name from endpoint (e.g., barcelona-weather-forecast)
server_slug = mcp_name.replace("-", "_")[:20] # Limit to 20 chars
tool_name_slug = mcp_tool['name'].replace("-", "_").replace(" ", "_")[:30]
tool_id = f"mcp_{server_slug}_{tool_name_slug}"
# Store mapping: tool_id -> (endpoint_url, original_tool_name)
endpoint_map[tool_id] = (endpoint_url, mcp_tool['name'])
if LLM_PROVIDER == "openai":
tool_def = mcp_to_openai_tool(mcp_tool, tool_id)
all_tools.append(tool_def)
else: # anthropic
tool_def = mcp_to_anthropic_tool(mcp_tool, tool_id)
all_tools.append(tool_def)
except Exception as e:
print(f" ⚠ Error listing tools: {e}")
continue
if not all_tools:
raise RuntimeError("No tools available from any MCP server.")
return all_tools, endpoint_map
# ── LLM Call Abstraction ───────────────────────────────────────────────────────
def call_llm_with_tools(messages: list[dict], tools: list[dict]) -> dict:
"""
Call LLM (OpenAI or Anthropic) with tool support.
Args:
messages: Conversation history in LLM-specific format.
tools: Available tools in LLM-specific format.
Returns:
LLM response with normalized structure:
{
"finish_reason": "stop" | "tool_calls",
"content": str | None,
"tool_calls": [...] | None
}
"""
if LLM_PROVIDER == "openai":
import openai
client = openai.OpenAI()
response = client.chat.completions.create(
model="gpt-4-turbo",
messages=messages,
tools=tools if tools else None,
tool_choice="auto" if tools else None,
)
choice = response.choices[0]
return {
"finish_reason": choice.finish_reason,
"content": choice.message.content,
"tool_calls": choice.message.tool_calls,
"message": choice.message, # For appending to history
}
else: # anthropic
import anthropic
client = anthropic.Anthropic()
# Anthropic doesn't use system message in messages array
system_msgs = [m["content"] for m in messages if m["role"] == "system"]
system = system_msgs[0] if system_msgs else None
anthropic_messages = [m for m in messages if m["role"] != "system"]
create_kwargs = dict(
model="claude-opus-4-6",
max_tokens=4096,
messages=anthropic_messages,
)
if system:
create_kwargs["system"] = system
if tools:
create_kwargs["tools"] = tools
response = client.messages.create(**create_kwargs)
# Normalize Anthropic response to match OpenAI structure
finish_reason = "tool_calls" if response.stop_reason == "tool_use" else "stop"
# Extract text content
text_content = None
for block in response.content:
if block.type == "text":
text_content = block.text
break
# Extract tool calls
tool_calls = []
for block in response.content:
if block.type == "tool_use":
tool_calls.append({
"id": block.id,
"type": "function",
"function": {
"name": block.name,
"arguments": json.dumps(block.input)
}
})
return {
"finish_reason": finish_reason,
"content": text_content,
"tool_calls": tool_calls if tool_calls else None,
"raw_response": response, # For appending to history
}
# ── Tool Builder Integration ───────────────────────────────────────────────────
def call_tool_builder(user_goal: str, verbose: bool = True) -> bool:
"""
Call the tool builder to generate and deploy MCP servers for a goal.
Args:
user_goal: The high-level goal to build tools for.
verbose: If True, prints progress updates.
Returns:
True if tools were successfully built and deployed, False otherwise.
"""
if verbose:
print(f"\n{'='*70}")
print(f" TOOL BUILDER")
print(f"{'='*70}")
print(f" No suitable tools found in registry.")
print(f" Generating tools for: {user_goal}")
print(f"{'='*70}\n")
try:
# Check if modal CLI is available
try:
subprocess.run(
["modal", "--version"],
capture_output=True,
check=True,
timeout=5
)
except (subprocess.CalledProcessError, FileNotFoundError, subprocess.TimeoutExpired):
if verbose:
print(f"\n✗ Modal CLI not found. Please install it:")
print(f" pip install modal")
return False
# Run tools_builder.py via modal
result = subprocess.run(
["modal", "run", "tools_builder.py", "--goal", user_goal],
capture_output=not verbose,
text=True,
timeout=600, # 10 minute timeout
)
if result.returncode == 0:
if verbose:
print(f"\n{'='*70}")
print(f" ✓ Tool builder completed successfully")
print(f" Waiting 10 seconds for deployments to stabilize...")
print(f"{'='*70}\n")
# Wait for deployments to be ready
time.sleep(10)
return True
else:
if verbose:
print(f"\n{'='*70}")
print(f" ✗ Tool builder failed")
if result.stderr:
print(f" Error: {result.stderr}")
print(f"{'='*70}\n")
return False
except subprocess.TimeoutExpired:
if verbose:
print(f"\n✗ Tool builder timed out after 10 minutes")
return False
except Exception as e:
if verbose:
print(f"\n✗ Error calling tool builder: {e}")
return False
# ── Main Supervisor Loop ───────────────────────────────────────────────────────
def supervisor(user_prompt: str, test_mode: bool = False, verbose: bool = True, auto_build_tools: bool = True) -> str:
"""
Main supervisor orchestration loop.
Args:
user_prompt: The user's question/request.
test_mode: If True, uses mock data for local testing.
verbose: If True, prints progress updates.
auto_build_tools: If True, automatically calls tool builder when registry is empty.
Returns:
The LLM's final answer as a string.
Raises:
RuntimeError: On errors during tool discovery or execution.
"""
if verbose:
print(f"\n{'='*70}")
print(f" SUPERVISOR — {LLM_PROVIDER.upper()}")
print(f"{'='*70}")
print(f" Prompt: {user_prompt}")
# 1. Discover tools from registry (allow empty if auto-building)
tools, endpoint_map = discover_tools_from_registry(
test_mode=test_mode,
allow_empty=auto_build_tools
)
# 2. If no tools available and auto-build is enabled, call tool builder
if not tools and auto_build_tools and not test_mode:
if verbose:
print(f"\n No tools available in registry.")
print(f" Auto-building tools for this task...")
# Call tool builder with the user prompt as the goal
success = call_tool_builder(user_prompt, verbose=verbose)
if not success:
return (
"I couldn't generate the necessary tools to answer your question. "
"Please try running the tool builder manually:\n"
f" modal run tools_builder.py --goal \"{user_prompt}\""
)
# Re-discover tools after building
if verbose:
print(f"\n Re-discovering tools from registry...")
tools, endpoint_map = discover_tools_from_registry(test_mode=test_mode)
if not tools:
return (
"Tools were generated but couldn't be discovered in the registry. "
"The endpoints may still be cold-starting. Please wait 30 seconds and try again."
)
elif not tools:
# No tools and auto-build is disabled
return (
"No tools available in registry. Please generate tools first:\n"
f" modal run tools_builder.py --goal \"{user_prompt}\""
)
if verbose:
print(f"\n Total tools available: {len(tools)}")
# 3. Initialize conversation
if LLM_PROVIDER == "openai":
messages = [
{
"role": "system",
"content": (
"You are a helpful assistant with access to dynamic tools. "
"Use the available tools to answer the user's question comprehensively. "
"If you need information from multiple tools, call them in sequence."
)
},
{"role": "user", "content": user_prompt}
]
else: # anthropic
messages = [
{
"role": "user",
"content": user_prompt
}
]
# 4. Agentic loop
for iteration in range(1, MAX_ITERATIONS + 1):
if verbose:
print(f"\n Iteration {iteration}/{MAX_ITERATIONS}")
# Call LLM
response = call_llm_with_tools(messages, tools)
if verbose:
print(f" Finish reason: {response['finish_reason']}")
# If LLM is done, return final answer
if response["finish_reason"] == "stop":
final_answer = response["content"] or "(No response)"
if verbose:
print(f"\n {'='*70}")
print(f" FINAL ANSWER")
print(f" {'='*70}")
print(f" {final_answer}")
print(f" {'='*70}\n")
return final_answer
# If LLM wants to use tools, execute them
if response["finish_reason"] == "tool_calls" and response["tool_calls"]:
if LLM_PROVIDER == "openai":
# Append assistant message with tool calls
messages.append({
"role": "assistant",
"content": response["content"],
"tool_calls": [
{
"id": tc.id,
"type": "function",
"function": {
"name": tc.function.name,
"arguments": tc.function.arguments
}
}
for tc in response["tool_calls"]
]
})
# Execute each tool call
for tool_call in response["tool_calls"]:
function_name = tool_call.function.name
arguments = json.loads(tool_call.function.arguments)
if verbose:
print(f" 🔧 Calling: {function_name}")
print(f" Args: {arguments}")
try:
# Look up endpoint and original tool name from endpoint_map
if function_name not in endpoint_map:
raise ValueError(f"Unknown tool: {function_name}")
endpoint_url, original_tool_name = endpoint_map[function_name]
result = call_mcp_tool(endpoint_url, original_tool_name, arguments)
result_str = json.dumps(result) if not isinstance(result, str) else result
if verbose:
preview = result_str[:200] + "..." if len(result_str) > 200 else result_str
print(f" ✓ Result: {preview}")
messages.append({
"role": "tool",
"tool_call_id": tool_call.id,
"content": result_str
})
except Exception as e:
error_msg = f"Error executing {function_name}: {str(e)}"
if verbose:
print(f" ✗ {error_msg}")
messages.append({
"role": "tool",
"tool_call_id": tool_call.id,
"content": error_msg
})
else: # anthropic
# Append assistant message
messages.append({
"role": "assistant",
"content": response["raw_response"].content
})
# Execute each tool call and collect results
tool_results = []
for tool_call in response["tool_calls"]:
function_name = tool_call["function"]["name"]
arguments = json.loads(tool_call["function"]["arguments"])
if verbose:
print(f" 🔧 Calling: {function_name}")
print(f" Args: {arguments}")
try:
# Look up endpoint and original tool name from endpoint_map
if function_name not in endpoint_map:
raise ValueError(f"Unknown tool: {function_name}")
endpoint_url, original_tool_name = endpoint_map[function_name]
result = call_mcp_tool(endpoint_url, original_tool_name, arguments)
result_str = json.dumps(result) if not isinstance(result, str) else result
if verbose:
preview = result_str[:200] + "..." if len(result_str) > 200 else result_str
print(f" ✓ Result: {preview}")
tool_results.append({
"type": "tool_result",
"tool_use_id": tool_call["id"],
"content": result_str
})
except Exception as e:
error_msg = f"Error: {str(e)}"
if verbose:
print(f" ✗ {error_msg}")
tool_results.append({
"type": "tool_result",
"tool_use_id": tool_call["id"],
"content": error_msg,
"is_error": True
})
# Append tool results as user message
messages.append({
"role": "user",
"content": tool_results
})
else:
# Unexpected finish reason
if verbose:
print(f" ⚠ Unexpected finish reason, returning current content")
return response["content"] or "(No response)"
# Max iterations reached
final_msg = "Maximum iterations reached. Partial answer: " + (response["content"] or "(incomplete)")
if verbose:
print(f"\n ⚠ {final_msg}")
return final_msg
# ── CLI Entrypoint ─────────────────────────────────────────────────────────────
if __name__ == "__main__":
import argparse
parser = argparse.ArgumentParser(
description="Run supervisor agent to answer questions using dynamic MCP tools",
)
parser.add_argument("--test", action="store_true", help="Run in test mode (mock registry)")
parser.add_argument("--no-auto-build", action="store_true",
help="Disable automatic tool building (error if registry empty)")
parser.add_argument("--prompt", type=str, default=None, help="Run with this prompt and exit (non-interactive)")
args = parser.parse_args()
if args.prompt:
try:
supervisor(
args.prompt,
test_mode=args.test,
verbose=True,
auto_build_tools=not args.no_auto_build,
)
except Exception as e:
import traceback
print(f"\n ✗ Error: {e}", file=sys.stderr)
traceback.print_exc()
sys.exit(0)
print("\n Apollo Supervisor — type your prompt and press Enter, or 'quit' to exit.\n")
while True:
try:
prompt = input(" › ").strip()
except (EOFError, KeyboardInterrupt):
print("\n bye!")
sys.exit(0)
if not prompt:
continue
if prompt.lower() in ("quit", "exit", "q"):
print(" bye!")
sys.exit(0)
try:
supervisor(
prompt,
test_mode=args.test,
verbose=True,
auto_build_tools=not args.no_auto_build,
)
except Exception as e:
import traceback
print(f"\n ✗ Error: {e}", file=sys.stderr)
traceback.print_exc()
print()