2828 get_selection_input ,
2929 handle_special_commands ,
3030)
31+ from mcp_agent .core .usage_display import collect_agents_from_provider , display_usage_report
3132from mcp_agent .mcp .mcp_aggregator import SEP # Import SEP once at the top
3233from mcp_agent .mcp .prompt_message_multipart import PromptMessageMultipart
3334from mcp_agent .progress_display import progress_display
3435
3536# Type alias for the send function
3637SendFunc = Callable [[Union [str , PromptMessage , PromptMessageMultipart ], str ], Awaitable [str ]]
3738
39+ # Type alias for the agent getter function
40+ AgentGetter = Callable [[str ], Optional [object ]]
41+
3842
3943class PromptProvider (Protocol ):
4044 """Protocol for objects that can provide prompt functionality."""
41-
42- async def list_prompts (self , server_name : Optional [str ] = None , agent_name : Optional [str ] = None ) -> Mapping [str , List [Prompt ]]:
45+
46+ async def list_prompts (
47+ self , server_name : Optional [str ] = None , agent_name : Optional [str ] = None
48+ ) -> Mapping [str , List [Prompt ]]:
4349 """List available prompts."""
4450 ...
45-
46- async def apply_prompt (self , prompt_name : str , arguments : Optional [Dict [str , str ]] = None , agent_name : Optional [str ] = None , ** kwargs ) -> str :
51+
52+ async def apply_prompt (
53+ self ,
54+ prompt_name : str ,
55+ arguments : Optional [Dict [str , str ]] = None ,
56+ agent_name : Optional [str ] = None ,
57+ ** kwargs ,
58+ ) -> str :
4759 """Apply a prompt."""
4860 ...
4961
@@ -160,17 +172,23 @@ async def prompt_loop(
160172 await self ._list_prompts (prompt_provider , agent )
161173 else :
162174 # Use the name-based selection
163- await self ._select_prompt (
164- prompt_provider , agent , prompt_name
165- )
175+ await self ._select_prompt (prompt_provider , agent , prompt_name )
176+ continue
177+ elif "show_usage" in command_result :
178+ # Handle usage display
179+ await self ._show_usage (prompt_provider , agent )
166180 continue
167181
168182 # Skip further processing if:
169183 # 1. The command was handled (command_result is truthy)
170184 # 2. The original input was a dictionary (special command like /prompt)
171185 # 3. The command result itself is a dictionary (special command handling result)
172186 # This fixes the issue where /prompt without arguments gets sent to the LLM
173- if command_result or isinstance (user_input , dict ) or isinstance (command_result , dict ):
187+ if (
188+ command_result
189+ or isinstance (user_input , dict )
190+ or isinstance (command_result , dict )
191+ ):
174192 continue
175193
176194 if user_input .upper () == "STOP" :
@@ -183,7 +201,9 @@ async def prompt_loop(
183201
184202 return result
185203
186- async def _get_all_prompts (self , prompt_provider : PromptProvider , agent_name : Optional [str ] = None ):
204+ async def _get_all_prompts (
205+ self , prompt_provider : PromptProvider , agent_name : Optional [str ] = None
206+ ):
187207 """
188208 Get a list of all available prompts.
189209
@@ -196,8 +216,10 @@ async def _get_all_prompts(self, prompt_provider: PromptProvider, agent_name: Op
196216 """
197217 try :
198218 # Call list_prompts on the provider
199- prompt_servers = await prompt_provider .list_prompts (server_name = None , agent_name = agent_name )
200-
219+ prompt_servers = await prompt_provider .list_prompts (
220+ server_name = None , agent_name = agent_name
221+ )
222+
201223 all_prompts = []
202224
203225 # Process the returned prompt servers
@@ -326,9 +348,11 @@ async def _select_prompt(
326348 try :
327349 # Get all available prompts directly from the prompt provider
328350 rich_print (f"\n [bold]Fetching prompts for agent [cyan]{ agent_name } [/cyan]...[/bold]" )
329-
351+
330352 # Call list_prompts on the provider
331- prompt_servers = await prompt_provider .list_prompts (server_name = None , agent_name = agent_name )
353+ prompt_servers = await prompt_provider .list_prompts (
354+ server_name = None , agent_name = agent_name
355+ )
332356
333357 if not prompt_servers :
334358 rich_print ("[yellow]No prompts available for this agent[/yellow]" )
@@ -557,3 +581,25 @@ async def _select_prompt(
557581
558582 rich_print (f"[red]Error selecting or applying prompt: { e } [/red]" )
559583 rich_print (f"[dim]{ traceback .format_exc ()} [/dim]" )
584+
585+ async def _show_usage (self , prompt_provider : PromptProvider , agent_name : str ) -> None :
586+ """
587+ Show usage statistics for the current agent(s) in a colorful table format.
588+
589+ Args:
590+ prompt_provider: Provider that has access to agents
591+ agent_name: Name of the current agent
592+ """
593+ try :
594+ # Collect all agents from the prompt provider
595+ agents_to_show = collect_agents_from_provider (prompt_provider , agent_name )
596+
597+ if not agents_to_show :
598+ rich_print ("[yellow]No usage data available[/yellow]" )
599+ return
600+
601+ # Use the shared display utility
602+ display_usage_report (agents_to_show , show_if_progress_disabled = True )
603+
604+ except Exception as e :
605+ rich_print (f"[red]Error showing usage: { e } [/red]" )
0 commit comments