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

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +54 -58
app.py CHANGED
@@ -40,65 +40,57 @@ t2i_server_parameters = StdioServerParameters(
40
 
41
  server_parameters = [kgb_server_parameters, t2i_server_parameters]
42
 
43
- # --- 3. Main Execution Block ---
44
- # We use the MCPClient as a context manager to handle the lifecycle of the servers.
45
- with MCPClient(server_parameters) as mcp:
46
- print("Connecting to MCP servers and fetching tools...")
47
- all_tools = mcp.get_tools()
48
- print(f"Found {len(all_tools)} tools.")
49
-
50
- # --- 4. Tool Integration ---
51
- # Find our specific tools from the list provided by the MCP servers.
52
- # We will look for them by name.
53
- knowledge_tool = next((tool for tool in all_tools if "knowledge_graph" in tool.name.lower()), None)
54
- image_tool = next((tool for tool in all_tools if "text_to_image" in tool.name.lower()), None)
55
-
56
- if not knowledge_tool:
57
- print("Warning: Knowledge graph tool not found.")
58
- if not image_tool:
59
- print("Warning: Text-to-image tool not found.")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
60
 
61
- writer_tools = [knowledge_tool] if knowledge_tool else []
62
- illustrator_tools = [image_tool] if image_tool else []
63
-
64
- # --- 5. Agent Definitions ---
65
- # We define our agent team, now equipped with the tools from your MCP servers.
66
-
67
- # The Writer Agent
68
- writer_agent = ToolCallingAgent(
69
- tools=writer_tools,
70
- model=model,
71
- name="writer",
72
- description="A creative agent that writes short stories. It can use a knowledge graph tool to research topics for inspiration."
73
- )
74
-
75
- # The Illustrator Agent
76
- illustrator_agent = ToolCallingAgent(
77
- tools=illustrator_tools,
78
- model=model,
79
- name="illustrator",
80
- description="An artist agent that creates illustrations based on a descriptive prompt using a text-to-image tool."
81
- )
82
-
83
- # The Director Agent
84
- director_agent = CodeAgent(
85
- tools=[],
86
- model=model,
87
- managed_agents=[writer_agent, illustrator_agent],
88
- system_prompt="""
89
- You are the Director of Agentic Storycrafter, a creative team. Your job is to manage the writer and illustrator agents to create a story with an illustration.
90
-
91
- Here is your workflow:
92
- 1. Receive a user's prompt for a story.
93
- 2. Call the `writer` agent to write a story based on the user's prompt.
94
- 3. After the story is written, create a short, descriptive prompt for an illustration that captures the essence of the story.
95
- 4. Call the `illustrator` agent with this new prompt to generate an image. The result will be a dictionary containing image data.
96
- 5. Return a dictionary containing both the final 'story' and the 'image_data' from the illustrator.
97
- """
98
- )
99
-
100
- # --- 6. The Creative Workflow ---
101
- if __name__ == "__main__":
102
  user_prompt = "a story about a wise old owl living in a library of forgotten books"
103
 
104
  print(f"\n--- Director's Task ---")
@@ -133,3 +125,7 @@ with MCPClient(server_parameters) as mcp:
133
  print("\n--- ILLUSTRATION ---")
134
  print("No illustration was generated.")
135
 
 
 
 
 
 
40
 
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,
63
+ name="writer",
64
+ description="A creative agent that writes short stories. It can use a knowledge graph tool to research topics for inspiration."
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,
72
+ name="illustrator",
73
+ description="An artist agent that creates illustrations based on a descriptive prompt using a text-to-image tool."
74
+ )
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="""
82
+ You are the Director of Agentic Storycrafter, a creative team. Your job is to manage the writer and illustrator agents to create a story with an illustration.
83
+
84
+ Here is your workflow:
85
+ 1. Receive a user's prompt for a story.
86
+ 2. Call the `writer` agent to write a story based on the user's prompt.
87
+ 3. After the story is written, create a short, descriptive prompt for an illustration that captures the essence of the story.
88
+ 4. Call the `illustrator` agent with this new prompt to generate an image. The result will be a dictionary containing image data.
89
+ 5. Return a dictionary containing both the final 'story' and the 'image_data' from the illustrator.
90
+ """
91
+ )
92
 
93
+ # --- 5. The Creative Workflow ---
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
94
  user_prompt = "a story about a wise old owl living in a library of forgotten books"
95
 
96
  print(f"\n--- Director's Task ---")
 
125
  print("\n--- ILLUSTRATION ---")
126
  print("No illustration was generated.")
127
 
128
+
129
+ # --- 6. Execution Start ---
130
+ if __name__ == "__main__":
131
+ run_storycrafter()