fast-agent-mcp 0.1.13__py3-none-any.whl → 0.2.0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (147) hide show
  1. {fast_agent_mcp-0.1.13.dist-info → fast_agent_mcp-0.2.0.dist-info}/METADATA +3 -4
  2. fast_agent_mcp-0.2.0.dist-info/RECORD +123 -0
  3. mcp_agent/__init__.py +75 -0
  4. mcp_agent/agents/agent.py +59 -371
  5. mcp_agent/agents/base_agent.py +522 -0
  6. mcp_agent/agents/workflow/__init__.py +1 -0
  7. mcp_agent/agents/workflow/chain_agent.py +173 -0
  8. mcp_agent/agents/workflow/evaluator_optimizer.py +362 -0
  9. mcp_agent/agents/workflow/orchestrator_agent.py +591 -0
  10. mcp_agent/{workflows/orchestrator → agents/workflow}/orchestrator_models.py +27 -11
  11. mcp_agent/agents/workflow/parallel_agent.py +182 -0
  12. mcp_agent/agents/workflow/router_agent.py +307 -0
  13. mcp_agent/app.py +3 -1
  14. mcp_agent/cli/commands/bootstrap.py +18 -7
  15. mcp_agent/cli/commands/setup.py +12 -4
  16. mcp_agent/cli/main.py +1 -1
  17. mcp_agent/cli/terminal.py +1 -1
  18. mcp_agent/config.py +24 -35
  19. mcp_agent/context.py +3 -1
  20. mcp_agent/context_dependent.py +3 -1
  21. mcp_agent/core/agent_types.py +10 -7
  22. mcp_agent/core/direct_agent_app.py +179 -0
  23. mcp_agent/core/direct_decorators.py +443 -0
  24. mcp_agent/core/direct_factory.py +476 -0
  25. mcp_agent/core/enhanced_prompt.py +15 -20
  26. mcp_agent/core/fastagent.py +151 -337
  27. mcp_agent/core/interactive_prompt.py +424 -0
  28. mcp_agent/core/mcp_content.py +19 -11
  29. mcp_agent/core/prompt.py +6 -2
  30. mcp_agent/core/validation.py +89 -16
  31. mcp_agent/executor/decorator_registry.py +6 -2
  32. mcp_agent/executor/temporal.py +35 -11
  33. mcp_agent/executor/workflow_signal.py +8 -2
  34. mcp_agent/human_input/handler.py +3 -1
  35. mcp_agent/llm/__init__.py +2 -0
  36. mcp_agent/{workflows/llm → llm}/augmented_llm.py +131 -256
  37. mcp_agent/{workflows/llm → llm}/augmented_llm_passthrough.py +35 -107
  38. mcp_agent/llm/augmented_llm_playback.py +83 -0
  39. mcp_agent/{workflows/llm → llm}/model_factory.py +26 -8
  40. mcp_agent/llm/providers/__init__.py +8 -0
  41. mcp_agent/{workflows/llm → llm/providers}/anthropic_utils.py +5 -1
  42. mcp_agent/{workflows/llm → llm/providers}/augmented_llm_anthropic.py +37 -141
  43. mcp_agent/llm/providers/augmented_llm_deepseek.py +53 -0
  44. mcp_agent/{workflows/llm → llm/providers}/augmented_llm_openai.py +112 -148
  45. mcp_agent/{workflows/llm → llm}/providers/multipart_converter_anthropic.py +78 -35
  46. mcp_agent/{workflows/llm → llm}/providers/multipart_converter_openai.py +73 -44
  47. mcp_agent/{workflows/llm → llm}/providers/openai_multipart.py +18 -4
  48. mcp_agent/{workflows/llm → llm/providers}/openai_utils.py +3 -3
  49. mcp_agent/{workflows/llm → llm}/providers/sampling_converter_anthropic.py +3 -3
  50. mcp_agent/{workflows/llm → llm}/providers/sampling_converter_openai.py +3 -3
  51. mcp_agent/{workflows/llm → llm}/sampling_converter.py +0 -21
  52. mcp_agent/{workflows/llm → llm}/sampling_format_converter.py +16 -1
  53. mcp_agent/logging/logger.py +2 -2
  54. mcp_agent/mcp/gen_client.py +9 -3
  55. mcp_agent/mcp/interfaces.py +67 -45
  56. mcp_agent/mcp/logger_textio.py +97 -0
  57. mcp_agent/mcp/mcp_agent_client_session.py +12 -4
  58. mcp_agent/mcp/mcp_agent_server.py +3 -1
  59. mcp_agent/mcp/mcp_aggregator.py +124 -93
  60. mcp_agent/mcp/mcp_connection_manager.py +21 -7
  61. mcp_agent/mcp/prompt_message_multipart.py +59 -1
  62. mcp_agent/mcp/prompt_render.py +77 -0
  63. mcp_agent/mcp/prompt_serialization.py +20 -13
  64. mcp_agent/mcp/prompts/prompt_constants.py +18 -0
  65. mcp_agent/mcp/prompts/prompt_helpers.py +327 -0
  66. mcp_agent/mcp/prompts/prompt_load.py +15 -5
  67. mcp_agent/mcp/prompts/prompt_server.py +154 -87
  68. mcp_agent/mcp/prompts/prompt_template.py +26 -35
  69. mcp_agent/mcp/resource_utils.py +3 -1
  70. mcp_agent/mcp/sampling.py +24 -15
  71. mcp_agent/mcp_server/agent_server.py +8 -5
  72. mcp_agent/mcp_server_registry.py +22 -9
  73. mcp_agent/resources/examples/{workflows → in_dev}/agent_build.py +1 -1
  74. mcp_agent/resources/examples/{data-analysis → in_dev}/slides.py +1 -1
  75. mcp_agent/resources/examples/internal/agent.py +4 -2
  76. mcp_agent/resources/examples/internal/fastagent.config.yaml +8 -2
  77. mcp_agent/resources/examples/prompting/image_server.py +3 -1
  78. mcp_agent/resources/examples/prompting/work_with_image.py +19 -0
  79. mcp_agent/ui/console_display.py +27 -7
  80. fast_agent_mcp-0.1.13.dist-info/RECORD +0 -164
  81. mcp_agent/core/agent_app.py +0 -570
  82. mcp_agent/core/agent_utils.py +0 -69
  83. mcp_agent/core/decorators.py +0 -448
  84. mcp_agent/core/factory.py +0 -422
  85. mcp_agent/core/proxies.py +0 -278
  86. mcp_agent/core/types.py +0 -22
  87. mcp_agent/eval/__init__.py +0 -0
  88. mcp_agent/mcp/stdio.py +0 -114
  89. mcp_agent/resources/examples/data-analysis/analysis-campaign.py +0 -188
  90. mcp_agent/resources/examples/data-analysis/analysis.py +0 -65
  91. mcp_agent/resources/examples/data-analysis/fastagent.config.yaml +0 -41
  92. mcp_agent/resources/examples/data-analysis/mount-point/WA_Fn-UseC_-HR-Employee-Attrition.csv +0 -1471
  93. mcp_agent/resources/examples/mcp_researcher/researcher-eval.py +0 -53
  94. mcp_agent/resources/examples/researcher/fastagent.config.yaml +0 -66
  95. mcp_agent/resources/examples/researcher/researcher-eval.py +0 -53
  96. mcp_agent/resources/examples/researcher/researcher-imp.py +0 -189
  97. mcp_agent/resources/examples/researcher/researcher.py +0 -39
  98. mcp_agent/resources/examples/workflows/chaining.py +0 -45
  99. mcp_agent/resources/examples/workflows/evaluator.py +0 -79
  100. mcp_agent/resources/examples/workflows/fastagent.config.yaml +0 -24
  101. mcp_agent/resources/examples/workflows/human_input.py +0 -26
  102. mcp_agent/resources/examples/workflows/orchestrator.py +0 -74
  103. mcp_agent/resources/examples/workflows/parallel.py +0 -79
  104. mcp_agent/resources/examples/workflows/router.py +0 -54
  105. mcp_agent/resources/examples/workflows/sse.py +0 -23
  106. mcp_agent/telemetry/__init__.py +0 -0
  107. mcp_agent/telemetry/usage_tracking.py +0 -19
  108. mcp_agent/workflows/__init__.py +0 -0
  109. mcp_agent/workflows/embedding/__init__.py +0 -0
  110. mcp_agent/workflows/embedding/embedding_base.py +0 -58
  111. mcp_agent/workflows/embedding/embedding_cohere.py +0 -49
  112. mcp_agent/workflows/embedding/embedding_openai.py +0 -37
  113. mcp_agent/workflows/evaluator_optimizer/__init__.py +0 -0
  114. mcp_agent/workflows/evaluator_optimizer/evaluator_optimizer.py +0 -447
  115. mcp_agent/workflows/intent_classifier/__init__.py +0 -0
  116. mcp_agent/workflows/intent_classifier/intent_classifier_base.py +0 -117
  117. mcp_agent/workflows/intent_classifier/intent_classifier_embedding.py +0 -130
  118. mcp_agent/workflows/intent_classifier/intent_classifier_embedding_cohere.py +0 -41
  119. mcp_agent/workflows/intent_classifier/intent_classifier_embedding_openai.py +0 -41
  120. mcp_agent/workflows/intent_classifier/intent_classifier_llm.py +0 -150
  121. mcp_agent/workflows/intent_classifier/intent_classifier_llm_anthropic.py +0 -60
  122. mcp_agent/workflows/intent_classifier/intent_classifier_llm_openai.py +0 -58
  123. mcp_agent/workflows/llm/__init__.py +0 -0
  124. mcp_agent/workflows/llm/augmented_llm_playback.py +0 -111
  125. mcp_agent/workflows/llm/providers/__init__.py +0 -8
  126. mcp_agent/workflows/orchestrator/__init__.py +0 -0
  127. mcp_agent/workflows/orchestrator/orchestrator.py +0 -535
  128. mcp_agent/workflows/parallel/__init__.py +0 -0
  129. mcp_agent/workflows/parallel/fan_in.py +0 -320
  130. mcp_agent/workflows/parallel/fan_out.py +0 -181
  131. mcp_agent/workflows/parallel/parallel_llm.py +0 -149
  132. mcp_agent/workflows/router/__init__.py +0 -0
  133. mcp_agent/workflows/router/router_base.py +0 -338
  134. mcp_agent/workflows/router/router_embedding.py +0 -226
  135. mcp_agent/workflows/router/router_embedding_cohere.py +0 -59
  136. mcp_agent/workflows/router/router_embedding_openai.py +0 -59
  137. mcp_agent/workflows/router/router_llm.py +0 -304
  138. mcp_agent/workflows/swarm/__init__.py +0 -0
  139. mcp_agent/workflows/swarm/swarm.py +0 -292
  140. mcp_agent/workflows/swarm/swarm_anthropic.py +0 -42
  141. mcp_agent/workflows/swarm/swarm_openai.py +0 -41
  142. {fast_agent_mcp-0.1.13.dist-info → fast_agent_mcp-0.2.0.dist-info}/WHEEL +0 -0
  143. {fast_agent_mcp-0.1.13.dist-info → fast_agent_mcp-0.2.0.dist-info}/entry_points.txt +0 -0
  144. {fast_agent_mcp-0.1.13.dist-info → fast_agent_mcp-0.2.0.dist-info}/licenses/LICENSE +0 -0
  145. /mcp_agent/{workflows/orchestrator → agents/workflow}/orchestrator_prompts.py +0 -0
  146. /mcp_agent/{workflows/llm → llm}/memory.py +0 -0
  147. /mcp_agent/{workflows/llm → llm}/prompt_utils.py +0 -0
@@ -1,320 +0,0 @@
1
- import contextlib
2
- from typing import TYPE_CHECKING, Callable, Dict, List, Optional, Type
3
-
4
- from mcp_agent.agents.agent import Agent
5
- from mcp_agent.context_dependent import ContextDependent
6
- from mcp_agent.workflows.llm.augmented_llm import (
7
- AugmentedLLM,
8
- MessageParamT,
9
- MessageT,
10
- ModelT,
11
- RequestParams,
12
- )
13
-
14
- if TYPE_CHECKING:
15
- from mcp_agent.context import Context
16
-
17
- FanInInput = (
18
- # Dict of agent/source name to list of messages generated by that agent
19
- Dict[str, List[MessageT] | List[MessageParamT]]
20
- # Dict of agent/source name to string generated by that agent
21
- | Dict[str, str]
22
- # List of lists of messages generated by each agent
23
- | List[List[MessageT] | List[MessageParamT]]
24
- # List of strings generated by each agent
25
- | List[str]
26
- )
27
-
28
-
29
- class FanIn(ContextDependent):
30
- """
31
- Aggregate results from multiple parallel tasks into a single result.
32
-
33
- This is a building block of the Parallel workflow, which can be used to fan out
34
- work to multiple agents or other parallel tasks, and then aggregate the results.
35
-
36
- For example, you can use FanIn to combine the results of multiple agents into a single response,
37
- such as a Summarization Fan-In agent that combines the outputs of multiple language models.
38
- """
39
-
40
- def __init__(
41
- self,
42
- aggregator_agent: Agent | AugmentedLLM[MessageParamT, MessageT],
43
- llm_factory: Callable[[Agent], AugmentedLLM[MessageParamT, MessageT]] = None,
44
- context: Optional["Context"] = None,
45
- **kwargs,
46
- ) -> None:
47
- """
48
- Initialize the FanIn with an Agent responsible for processing multiple responses into a single aggregated one.
49
- """
50
-
51
- super().__init__(context=context, **kwargs)
52
-
53
- self.executor = self.context.executor
54
- self.llm_factory = llm_factory
55
- self.aggregator_agent = aggregator_agent
56
-
57
- if not isinstance(self.aggregator_agent, AugmentedLLM):
58
- if not self.llm_factory:
59
- raise ValueError("llm_factory is required when using an Agent")
60
-
61
- async def generate(
62
- self,
63
- messages: FanInInput,
64
- request_params: RequestParams | None = None,
65
- ) -> List[MessageT]:
66
- """
67
- Request fan-in agent generation from a list of messages from multiple sources/agents.
68
- Internally aggregates the messages and then calls the aggregator agent to generate a response.
69
- """
70
- message: str | MessageParamT | List[MessageParamT] = await self.aggregate_messages(messages)
71
-
72
- async with contextlib.AsyncExitStack() as stack:
73
- if isinstance(self.aggregator_agent, AugmentedLLM):
74
- llm = self.aggregator_agent
75
- else:
76
- # Enter agent context
77
- ctx_agent = await stack.enter_async_context(self.aggregator_agent)
78
- llm = await ctx_agent.attach_llm(self.llm_factory)
79
-
80
- return await llm.generate(
81
- message=message,
82
- request_params=request_params,
83
- )
84
-
85
- async def generate_str(
86
- self,
87
- messages: FanInInput,
88
- request_params: RequestParams | None = None,
89
- ) -> str:
90
- """
91
- Request fan-in agent generation from a list of messages from multiple sources/agents.
92
- Internally aggregates the messages and then calls the aggregator agent to generate a
93
- response, which is returned as a string.
94
- """
95
-
96
- message: str | MessageParamT | List[MessageParamT] = await self.aggregate_messages(messages)
97
-
98
- async with contextlib.AsyncExitStack() as stack:
99
- if isinstance(self.aggregator_agent, AugmentedLLM):
100
- llm = self.aggregator_agent
101
- else:
102
- # Enter agent context
103
- ctx_agent = await stack.enter_async_context(self.aggregator_agent)
104
- llm = await ctx_agent.attach_llm(self.llm_factory)
105
-
106
- return await llm.generate_str(message=message, request_params=request_params)
107
-
108
- async def generate_structured(
109
- self,
110
- messages: FanInInput,
111
- response_model: Type[ModelT],
112
- request_params: RequestParams | None = None,
113
- ) -> ModelT:
114
- """
115
- Request a structured fan-in agent generation from a list of messages
116
- from multiple sources/agents. Internally aggregates the messages and then calls
117
- the aggregator agent to generate a response, which is returned as a Pydantic model.
118
- """
119
-
120
- message: str | MessageParamT | List[MessageParamT] = await self.aggregate_messages(messages)
121
-
122
- async with contextlib.AsyncExitStack() as stack:
123
- if isinstance(self.aggregator_agent, AugmentedLLM):
124
- llm = self.aggregator_agent
125
- else:
126
- # Enter agent context
127
- ctx_agent = await stack.enter_async_context(self.aggregator_agent)
128
- llm = await ctx_agent.attach_llm(self.llm_factory)
129
-
130
- return await llm.generate_structured(
131
- message=message,
132
- response_model=response_model,
133
- request_params=request_params,
134
- )
135
-
136
- async def aggregate_messages(self, messages: FanInInput) -> str | MessageParamT | List[MessageParamT]:
137
- """
138
- Aggregate messages from multiple sources/agents into a single message to
139
- use with the aggregator agent generation.
140
-
141
- The input can be a dictionary of agent/source name to list of messages
142
- generated by that agent, or just the unattributed lists of messages to aggregate.
143
-
144
- Args:
145
- messages: Can be one of:
146
- - Dict[str, List[MessageT] | List[MessageParamT]]: Dict of agent names to messages
147
- - Dict[str, str]: Dict of agent names to message strings
148
- - List[List[MessageT] | List[MessageParamT]]: List of message lists from agents
149
- - List[str]: List of message strings from agents
150
-
151
- Returns:
152
- Aggregated message as string, MessageParamT or List[MessageParamT]
153
-
154
- Raises:
155
- ValueError: If input is empty or contains empty/invalid elements
156
- """
157
- # Handle dictionary inputs
158
- if isinstance(messages, dict):
159
- # Check for empty dict
160
- if not messages:
161
- raise ValueError("Input dictionary cannot be empty")
162
-
163
- first_value = next(iter(messages.values()))
164
-
165
- # Dict[str, List[MessageT] | List[MessageParamT]]
166
- if isinstance(first_value, list):
167
- if any(not isinstance(v, list) for v in messages.values()):
168
- raise ValueError("All dictionary values must be lists of messages")
169
- # Process list of messages for each agent
170
- return await self.aggregate_agent_messages(messages)
171
-
172
- # Dict[str, str]
173
- elif isinstance(first_value, str):
174
- if any(not isinstance(v, str) for v in messages.values()):
175
- raise ValueError("All dictionary values must be strings")
176
- # Process string outputs from each agent
177
- return await self.aggregate_agent_message_strings(messages)
178
-
179
- else:
180
- raise ValueError("Dictionary values must be either lists of messages or strings")
181
-
182
- # Handle list inputs
183
- elif isinstance(messages, list):
184
- # Check for empty list
185
- if not messages:
186
- raise ValueError("Input list cannot be empty")
187
-
188
- first_item = messages[0]
189
-
190
- # List[List[MessageT] | List[MessageParamT]]
191
- if isinstance(first_item, list):
192
- if any(not isinstance(item, list) for item in messages):
193
- raise ValueError("All list items must be lists of messages")
194
- # Process list of message lists
195
- return await self.aggregate_message_lists(messages)
196
-
197
- # List[str]
198
- elif isinstance(first_item, str):
199
- if any(not isinstance(item, str) for item in messages):
200
- raise ValueError("All list items must be strings")
201
- # Process list of strings
202
- return await self.aggregate_message_strings(messages)
203
-
204
- else:
205
- raise ValueError("List items must be either lists of messages or strings")
206
-
207
- else:
208
- raise ValueError("Input must be either a dictionary of agent messages or a list of messages")
209
-
210
- # Helper methods for processing different types of inputs
211
- async def aggregate_agent_messages(self, messages: Dict[str, List[MessageT] | List[MessageParamT]]) -> str | MessageParamT | List[MessageParamT]:
212
- """
213
- Aggregate message lists with agent names.
214
-
215
- Args:
216
- messages: Dictionary mapping agent names to their message lists
217
-
218
- Returns:
219
- str | List[MessageParamT]: Messages formatted with agent attribution
220
-
221
- """
222
-
223
- # In the default implementation, we'll just convert the messages to a
224
- # single string with agent attribution
225
- aggregated_messages = []
226
-
227
- if not messages:
228
- return ""
229
-
230
- # Format each agent's messages with attribution
231
- for agent_name, agent_messages in messages.items():
232
- agent_message_strings = []
233
- for msg in agent_messages or []:
234
- if isinstance(msg, str):
235
- agent_message_strings.append(f"Agent {agent_name}: {msg}")
236
- else:
237
- # Assume it's a Message/MessageParamT and add attribution
238
- # TODO -- this should really unpack the text from the message
239
- agent_message_strings.append(f"Agent {agent_name}: {str(msg.content[0])}")
240
-
241
- aggregated_messages.append("\n".join(agent_message_strings))
242
-
243
- # Combine all messages with clear separation
244
- final_message = "\n\n".join(aggregated_messages)
245
- final_message = f"Aggregated responses from multiple Agents:\n\n{final_message}"
246
- return final_message
247
-
248
- async def aggregate_agent_message_strings(self, messages: Dict[str, str]) -> str:
249
- """
250
- Aggregate string outputs with agent names.
251
-
252
- Args:
253
- messages: Dictionary mapping agent names to their string outputs
254
-
255
- Returns:
256
- str: Combined string with agent attributions
257
- """
258
- if not messages:
259
- return ""
260
-
261
- # Format each agent's message with agent attribution
262
- aggregated_messages = [f"Agent {agent_name}: {message}" for agent_name, message in messages.items()]
263
-
264
- # Combine all messages with clear separation
265
- final_message = "\n\n".join(aggregated_messages)
266
- final_message = f"Aggregated responses from multiple Agents:\n\n{final_message}"
267
- return final_message
268
-
269
- async def aggregate_message_lists(self, messages: List[List[MessageT] | List[MessageParamT]]) -> str | MessageParamT | List[MessageParamT]:
270
- """
271
- Aggregate message lists without agent names.
272
-
273
- Args:
274
- messages: List of message lists from different agents
275
-
276
- Returns:
277
- List[MessageParamT]: List of formatted messages
278
- """
279
- aggregated_messages = []
280
-
281
- if not messages:
282
- return ""
283
-
284
- # Format each source's messages
285
- for i, source_messages in enumerate(messages, 1):
286
- source_message_strings = []
287
- for msg in source_messages or []:
288
- if isinstance(msg, str):
289
- source_message_strings.append(f"Source {i}: {msg}")
290
- else:
291
- # Assume it's a MessageParamT or MessageT and add source attribution
292
- source_message_strings.append(f"Source {i}: {str(msg)}")
293
-
294
- aggregated_messages.append("\n".join(source_messages))
295
-
296
- # Combine all messages with clear separation
297
- final_message = "\n\n".join(aggregated_messages)
298
- final_message = f"Aggregated responses from multiple sources:\n\n{final_message}"
299
- return final_message
300
-
301
- async def aggregate_message_strings(self, messages: List[str]) -> str:
302
- """
303
- Aggregate string outputs without agent names.
304
-
305
- Args:
306
- messages: List of string outputs from different agents
307
-
308
- Returns:
309
- str: Combined string with source attributions
310
- """
311
- if not messages:
312
- return ""
313
-
314
- # Format each source's message with attribution
315
- aggregated_messages = [f"Source {i}: {message}" for i, message in enumerate(messages, 1)]
316
-
317
- # Combine all messages with clear separation
318
- final_message = "\n\n".join(aggregated_messages)
319
- final_message = f"Aggregated responses from multiple sources:\n\n{final_message}"
320
- return final_message
@@ -1,181 +0,0 @@
1
- import contextlib
2
- import functools
3
- from typing import TYPE_CHECKING, Any, Callable, Coroutine, Dict, List, Optional, Type
4
-
5
- from mcp_agent.agents.agent import Agent
6
- from mcp_agent.context_dependent import ContextDependent
7
- from mcp_agent.logging.logger import get_logger
8
- from mcp_agent.workflows.llm.augmented_llm import (
9
- AugmentedLLM,
10
- MessageParamT,
11
- MessageT,
12
- ModelT,
13
- RequestParams,
14
- )
15
-
16
- if TYPE_CHECKING:
17
- from mcp_agent.context import Context
18
-
19
- logger = get_logger(__name__)
20
-
21
-
22
- class FanOut(ContextDependent):
23
- """
24
- Distribute work to multiple parallel tasks.
25
-
26
- This is a building block of the Parallel workflow, which can be used to fan out
27
- work to multiple agents or other parallel tasks, and then aggregate the results.
28
- """
29
-
30
- def __init__(
31
- self,
32
- agents: List[Agent | AugmentedLLM[MessageParamT, MessageT]] | None = None,
33
- functions: List[Callable[[MessageParamT], List[MessageT]]] | None = None,
34
- llm_factory: Callable[[Agent], AugmentedLLM[MessageParamT, MessageT]] = None,
35
- context: Optional["Context"] = None,
36
- **kwargs,
37
- ) -> None:
38
- """
39
- Initialize the FanOut with a list of agents, functions, or LLMs.
40
- If agents are provided, they will be wrapped in an AugmentedLLM using llm_factory if not already done so.
41
- If functions are provided, they will be invoked in parallel directly.
42
- """
43
- super().__init__(context=context, **kwargs)
44
- self.executor = self.context.executor
45
- self.llm_factory = llm_factory
46
- self.agents = agents or []
47
- self.functions: List[Callable[[MessageParamT], MessageT]] = functions or []
48
-
49
- if not self.agents and not self.functions:
50
- raise ValueError("At least one agent or function must be provided for fan-out to work")
51
-
52
- if not self.llm_factory:
53
- for agent in self.agents:
54
- if not isinstance(agent, AugmentedLLM):
55
- raise ValueError("llm_factory is required when using an Agent")
56
-
57
- async def generate(
58
- self,
59
- message: str | MessageParamT | List[MessageParamT],
60
- request_params: RequestParams | None = None,
61
- ) -> Dict[str, List[MessageT]]:
62
- """
63
- Request fan-out agent/function generations, and return the results as a dictionary.
64
- The keys are the names of the agents or functions that generated the results.
65
- """
66
- tasks: List[Callable[..., List[MessageT]] | Coroutine[Any, Any, List[MessageT]]] = []
67
- task_names: List[str] = []
68
- task_results = []
69
-
70
- async with contextlib.AsyncExitStack() as stack:
71
- for agent in self.agents:
72
- if isinstance(agent, AugmentedLLM):
73
- llm = agent
74
- else:
75
- # Enter agent context
76
- ctx_agent = await stack.enter_async_context(agent)
77
- llm = await ctx_agent.attach_llm(self.llm_factory)
78
-
79
- tasks.append(
80
- llm.generate(
81
- message=message,
82
- request_params=request_params,
83
- )
84
- )
85
- task_names.append(agent.name)
86
-
87
- # Create bound methods for regular functions
88
- for function in self.functions:
89
- tasks.append(functools.partial(function, message))
90
- task_names.append(function.__name__ or id(function))
91
-
92
- # Wait for all tasks to complete
93
- logger.debug("Running fan-out tasks:", data=task_names)
94
- task_results = await self.executor.execute(*tasks)
95
-
96
- logger.debug("Fan-out tasks completed:", data=dict(zip(task_names, task_results)))
97
- return dict(zip(task_names, task_results))
98
-
99
- async def generate_str(
100
- self,
101
- message: str | MessageParamT | List[MessageParamT],
102
- request_params: RequestParams | None = None,
103
- ) -> Dict[str, str]:
104
- """
105
- Request fan-out agent/function generations and return the string results as a dictionary.
106
- The keys are the names of the agents or functions that generated the results.
107
- """
108
-
109
- def fn_result_to_string(fn, message):
110
- return str(fn(message))
111
-
112
- tasks: List[Callable[..., str] | Coroutine[Any, Any, str]] = []
113
- task_names: List[str] = []
114
- task_results = []
115
-
116
- async with contextlib.AsyncExitStack() as stack:
117
- for agent in self.agents:
118
- if isinstance(agent, AugmentedLLM):
119
- llm = agent
120
- else:
121
- # Enter agent context
122
- ctx_agent = await stack.enter_async_context(agent)
123
- llm = await ctx_agent.attach_llm(self.llm_factory)
124
-
125
- tasks.append(
126
- llm.generate_str(
127
- message=message,
128
- request_params=request_params,
129
- )
130
- )
131
- task_names.append(agent.name)
132
-
133
- # Create bound methods for regular functions
134
- for function in self.functions:
135
- tasks.append(functools.partial(fn_result_to_string, function, message))
136
- task_names.append(function.__name__ or id(function))
137
-
138
- task_results = await self.executor.execute(*tasks)
139
-
140
- return dict(zip(task_names, task_results))
141
-
142
- async def generate_structured(
143
- self,
144
- message: str | MessageParamT | List[MessageParamT],
145
- response_model: Type[ModelT],
146
- request_params: RequestParams | None = None,
147
- ) -> Dict[str, ModelT]:
148
- """
149
- Request a structured fan-out agent/function generation and return the result as a Pydantic model.
150
- The keys are the names of the agents or functions that generated the results.
151
- """
152
- tasks = []
153
- task_names = []
154
- task_results = []
155
-
156
- async with contextlib.AsyncExitStack() as stack:
157
- for agent in self.agents:
158
- if isinstance(agent, AugmentedLLM):
159
- llm = agent
160
- else:
161
- # Enter agent context
162
- ctx_agent = await stack.enter_async_context(agent)
163
- llm = await ctx_agent.attach_llm(self.llm_factory)
164
-
165
- tasks.append(
166
- llm.generate_structured(
167
- message=message,
168
- response_model=response_model,
169
- request_params=request_params,
170
- )
171
- )
172
- task_names.append(agent.name)
173
-
174
- # Create bound methods for regular functions
175
- for function in self.functions:
176
- tasks.append(functools.partial(function, message))
177
- task_names.append(function.__name__ or id(function))
178
-
179
- task_results = await self.executor.execute(*tasks)
180
-
181
- return dict(zip(task_names, task_results))
@@ -1,149 +0,0 @@
1
- import asyncio
2
- from typing import TYPE_CHECKING, Any, Callable, List, Optional, Type, Union
3
-
4
- from mcp_agent.agents.agent import Agent
5
- from mcp_agent.workflows.llm.augmented_llm import (
6
- AugmentedLLM,
7
- MessageParamT,
8
- MessageT,
9
- ModelT,
10
- RequestParams,
11
- )
12
-
13
- if TYPE_CHECKING:
14
- from mcp_agent.context import Context
15
-
16
-
17
- class ParallelLLM(AugmentedLLM[MessageParamT, MessageT]):
18
- """
19
- LLMs can sometimes work simultaneously on a task (fan-out)
20
- and have their outputs aggregated programmatically (fan-in).
21
- This workflow performs both the fan-out and fan-in operations using LLMs.
22
- From the user's perspective, an input is specified and the output is returned.
23
- """
24
-
25
- def __init__(
26
- self,
27
- fan_in_agent: Agent | AugmentedLLM,
28
- fan_out_agents: List[Agent | AugmentedLLM],
29
- llm_factory: Callable[[Agent], AugmentedLLM] = None,
30
- context: Optional["Context"] = None,
31
- include_request: bool = True,
32
- **kwargs,
33
- ) -> None:
34
- super().__init__(context=context, **kwargs)
35
- self.fan_in_agent = fan_in_agent
36
- self.fan_out_agents = fan_out_agents
37
- self.llm_factory = llm_factory
38
- self.include_request = include_request
39
- self.history = None # History tracking is complex in this workflow
40
-
41
- async def ensure_llm(self, agent: Union[Agent, AugmentedLLM]) -> AugmentedLLM:
42
- """Ensure an agent has an LLM attached, using existing or creating new."""
43
- if isinstance(agent, AugmentedLLM):
44
- return agent
45
-
46
- if not hasattr(agent, "_llm") or agent._llm is None:
47
- return await agent.attach_llm(self.llm_factory)
48
-
49
- return agent._llm
50
-
51
- async def generate(
52
- self,
53
- message: str | MessageParamT | List[MessageParamT],
54
- request_params: RequestParams | None = None,
55
- ) -> List[MessageT] | Any:
56
- """Generate responses using parallel fan-out and fan-in."""
57
- # Ensure all agents have LLMs
58
- fan_out_llms = []
59
- for agent in self.fan_out_agents:
60
- llm = await self.ensure_llm(agent)
61
- fan_out_llms.append(llm)
62
-
63
- fan_in_llm = await self.ensure_llm(self.fan_in_agent)
64
-
65
- # Run fan-out operations in parallel
66
- responses = await asyncio.gather(*[llm.generate(message, request_params) for llm in fan_out_llms])
67
-
68
- # Get message string for inclusion in formatted output
69
- message_str = str(message) if isinstance(message, (str, MessageParamT)) else None
70
-
71
- # Run fan-in to aggregate results
72
- result = await fan_in_llm.generate(
73
- self._format_responses(responses, message_str),
74
- request_params=request_params,
75
- )
76
-
77
- return result
78
-
79
- async def generate_str(
80
- self,
81
- message: str | MessageParamT | List[MessageParamT],
82
- request_params: RequestParams | None = None,
83
- ) -> str:
84
- """Generate string responses using parallel fan-out and fan-in."""
85
- # Ensure all agents have LLMs
86
- fan_out_llms = []
87
- for agent in self.fan_out_agents:
88
- llm = await self.ensure_llm(agent)
89
- fan_out_llms.append(llm)
90
-
91
- fan_in_llm = await self.ensure_llm(self.fan_in_agent)
92
-
93
- # Run fan-out operations in parallel
94
- responses = await asyncio.gather(*[llm.generate_str(message, request_params) for llm in fan_out_llms])
95
-
96
- # Get message string for inclusion in formatted output
97
- message_str = str(message) if isinstance(message, (str, MessageParamT)) else None
98
-
99
- # Run fan-in to aggregate results
100
- result = await fan_in_llm.generate_str(
101
- self._format_responses(responses, message_str),
102
- request_params=request_params,
103
- )
104
-
105
- return result
106
-
107
- async def generate_structured(
108
- self,
109
- message: str | MessageParamT | List[MessageParamT],
110
- response_model: Type[ModelT],
111
- request_params: RequestParams | None = None,
112
- ) -> ModelT:
113
- """Generate structured responses using parallel fan-out and fan-in."""
114
- # Ensure all agents have LLMs
115
- fan_out_llms = []
116
- for agent in self.fan_out_agents:
117
- llm = await self.ensure_llm(agent)
118
- fan_out_llms.append(llm)
119
-
120
- fan_in_llm = await self.ensure_llm(self.fan_in_agent)
121
-
122
- # Run fan-out operations in parallel
123
- responses = await asyncio.gather(*[llm.generate_structured(message, response_model, request_params) for llm in fan_out_llms])
124
-
125
- # Get message string for inclusion in formatted output
126
- message_str = str(message) if isinstance(message, (str, MessageParamT)) else None
127
-
128
- # Run fan-in to aggregate results
129
- result = await fan_in_llm.generate_structured(
130
- self._format_responses(responses, message_str),
131
- response_model=response_model,
132
- request_params=request_params,
133
- )
134
-
135
- return result
136
-
137
- def _format_responses(self, responses: List[Any], message: str = None) -> str:
138
- """Format a list of responses for the fan-in agent."""
139
- formatted = []
140
-
141
- # Include the original message if specified
142
- if self.include_request and message:
143
- formatted.append("The following request was sent to the agents:")
144
- formatted.append(f"<fastagent:request>\n{message}\n</fastagent:request>")
145
-
146
- for i, response in enumerate(responses):
147
- agent_name = self.fan_out_agents[i].name
148
- formatted.append(f'<fastagent:response agent="{agent_name}">\n{response}\n</fastagent:response>')
149
- return "\n\n".join(formatted)
File without changes