VirtualOasis commited on
Commit
989ef70
·
verified ·
1 Parent(s): f795f54

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +9 -15
app.py CHANGED
@@ -19,7 +19,6 @@ model = LiteLLMModel(
19
 
20
  # --- 2. MCP Server Configuration ---
21
  # Define the connection parameters for your MCP servers.
22
- # These commands will run in the background to connect to your deployed tools.
23
  kgb_server_parameters = StdioServerParameters(
24
  command="npx",
25
  args=[
@@ -41,22 +40,21 @@ t2i_server_parameters = StdioServerParameters(
41
  server_parameters = [kgb_server_parameters, t2i_server_parameters]
42
 
43
  # --- 3. Main Application Logic ---
44
- # We define the main function to encapsulate the agent setup and workflow.
45
  def run_storycrafter():
46
- # We use the MCPClient as a context manager to handle the lifecycle of the servers.
47
- with MCPClient(server_parameters) as mcp:
 
 
 
48
  print("Connecting to MCP servers and fetching tools...")
49
  # Get all available tools from all connected MCP servers.
50
  all_tools = mcp.get_tools()
51
  print(f"Found {len(all_tools)} tools.")
52
  if not all_tools:
53
  print("Warning: No tools were loaded from the MCP servers. Agents will have limited capabilities.")
54
-
55
  # --- 4. Agent Definitions ---
56
- # We define our agent team inside the 'with' block to give them access to the loaded tools.
57
-
58
  # The Writer Agent is given all available tools.
59
- # It will intelligently select the correct tool (e.g., knowledge_graph) when needed.
60
  writer_agent = ToolCallingAgent(
61
  tools=all_tools,
62
  model=model,
@@ -65,7 +63,6 @@ def run_storycrafter():
65
  )
66
 
67
  # The Illustrator Agent is also given all available tools.
68
- # It will select the 'text_to_image' tool when its services are required.
69
  illustrator_agent = ToolCallingAgent(
70
  tools=all_tools,
71
  model=model,
@@ -75,7 +72,7 @@ def run_storycrafter():
75
 
76
  # The Director Agent orchestrates the other two agents.
77
  director_agent = CodeAgent(
78
- tools=[], # The director itself doesn't call external tools, it directs other agents.
79
  model=model,
80
  managed_agents=[writer_agent, illustrator_agent],
81
  system_prompt="""
@@ -96,12 +93,10 @@ def run_storycrafter():
96
  print(f"\n--- Director's Task ---")
97
  print(f"Prompt: {user_prompt}\n")
98
 
99
- # The director now runs the full workflow.
100
  final_output = director_agent.run(f"Create a story and illustration for the following prompt: {user_prompt}")
101
 
102
  print("\n--- Agentic Storycrafter Result ---")
103
 
104
- # The output from the director is code that needs to be executed to get the result
105
  result_dict = eval(final_output)
106
 
107
  story = result_dict.get("story")
@@ -113,7 +108,6 @@ def run_storycrafter():
113
  if image_data and 'b64_json' in image_data:
114
  print("\n--- ILLUSTRATION ---")
115
  print("Illustration created. Saving to 'story_illustration.png'")
116
- # Decode the base64 string and save it as an image file
117
  try:
118
  img_bytes = base64.b64decode(image_data['b64_json'])
119
  img = Image.open(io.BytesIO(img_bytes))
@@ -125,7 +119,7 @@ def run_storycrafter():
125
  print("\n--- ILLUSTRATION ---")
126
  print("No illustration was generated.")
127
 
128
-
129
  # --- 6. Execution Start ---
130
  if __name__ == "__main__":
131
- run_storycrafter()
 
 
19
 
20
  # --- 2. MCP Server Configuration ---
21
  # Define the connection parameters for your MCP servers.
 
22
  kgb_server_parameters = StdioServerParameters(
23
  command="npx",
24
  args=[
 
40
  server_parameters = [kgb_server_parameters, t2i_server_parameters]
41
 
42
  # --- 3. Main Application Logic ---
 
43
  def run_storycrafter():
44
+ # Instantiate the MCPClient *before* the 'with' block, as per the working example.
45
+ mcp = MCPClient(server_parameters)
46
+
47
+ # Use the created MCPClient instance as a context manager.
48
+ with mcp:
49
  print("Connecting to MCP servers and fetching tools...")
50
  # Get all available tools from all connected MCP servers.
51
  all_tools = mcp.get_tools()
52
  print(f"Found {len(all_tools)} tools.")
53
  if not all_tools:
54
  print("Warning: No tools were loaded from the MCP servers. Agents will have limited capabilities.")
55
+
56
  # --- 4. Agent Definitions ---
 
 
57
  # The Writer Agent is given all available tools.
 
58
  writer_agent = ToolCallingAgent(
59
  tools=all_tools,
60
  model=model,
 
63
  )
64
 
65
  # The Illustrator Agent is also given all available tools.
 
66
  illustrator_agent = ToolCallingAgent(
67
  tools=all_tools,
68
  model=model,
 
72
 
73
  # The Director Agent orchestrates the other two agents.
74
  director_agent = CodeAgent(
75
+ tools=[],
76
  model=model,
77
  managed_agents=[writer_agent, illustrator_agent],
78
  system_prompt="""
 
93
  print(f"\n--- Director's Task ---")
94
  print(f"Prompt: {user_prompt}\n")
95
 
 
96
  final_output = director_agent.run(f"Create a story and illustration for the following prompt: {user_prompt}")
97
 
98
  print("\n--- Agentic Storycrafter Result ---")
99
 
 
100
  result_dict = eval(final_output)
101
 
102
  story = result_dict.get("story")
 
108
  if image_data and 'b64_json' in image_data:
109
  print("\n--- ILLUSTRATION ---")
110
  print("Illustration created. Saving to 'story_illustration.png'")
 
111
  try:
112
  img_bytes = base64.b64decode(image_data['b64_json'])
113
  img = Image.open(io.BytesIO(img_bytes))
 
119
  print("\n--- ILLUSTRATION ---")
120
  print("No illustration was generated.")
121
 
 
122
  # --- 6. Execution Start ---
123
  if __name__ == "__main__":
124
+ run_storycrafter()
125
+