
    i-                       d Z ddlmZ ddlZddlZddlmZmZmZm	Z	 ddl
ZddlZddlmZ erddlmZ ddlmZ ddlmZ dd	lmZ dd
lmZ  ee      Zeej6                  j8                  ej6                  j:                  z     Z G d d      Zy)z*Prompt-related methods for FastMCP Client.    )annotationsN)TYPE_CHECKINGAnyLiteraloverload)	RootModel)Client)
PromptTask)client_span)inject_trace_context)
get_loggerc                  6   e Zd ZdZdd	 	 	 	 	 ddZddZ	 	 d	 	 	 	 	 	 	 	 	 ddZe	 ddddd	 	 	 	 	 	 	 	 	 	 	 	 	 dd	       Ze	 ddddd
d	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 dd       Z	 dddddd
d	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 ddZ	 	 	 	 d	 	 	 	 	 	 	 	 	 	 	 	 	 ddZ	y)ClientPromptsMixinz2Mixin providing prompt-related methods for Client.Ncursorc                  K   t         j                  d| j                   d       | j                  | j                  j                  |             d{   }|S 7 w)a  Send a prompts/list request and return the complete MCP protocol result.

        Args:
            cursor: Optional pagination cursor from a previous request's nextCursor.

        Returns:
            mcp.types.ListPromptsResult: The complete response object from the protocol,
                containing the list of prompts and any additional metadata.

        Raises:
            RuntimeError: If called while the client is not connected.
            McpError: If the request results in a TimeoutError | JSONRPCError
        [z] called list_promptsr   N)loggerdebugname_await_with_session_monitoringsessionlist_prompts)selfr   results      m/Users/bowang/.openclaw/workspace/ChatDev/.venv/lib/python3.12/site-packages/fastmcp/client/mixins/prompts.pylist_prompts_mcpz#ClientPromptsMixin.list_prompts_mcp"   sX       	q#89:::LL%%V%4
 
 
s   AAAAc                  K   g }d}t               }	 | j                  |       d{   }|j                  |j                         |j                  s	 |S |j                  |v r3t
        j                  d| j                   d|j                  d       	 |S |j                  |j                         |j                  }7 w)a6  Retrieve all prompts available on the server.

        This method automatically fetches all pages if the server paginates results,
        returning the complete list. For manual pagination control (e.g., to handle
        large result sets incrementally), use list_prompts_mcp() with the cursor parameter.

        Returns:
            list[mcp.types.Prompt]: A list of all Prompt objects.

        Raises:
            RuntimeError: If called while the client is not connected.
            McpError: If the request results in a TimeoutError | JSONRPCError
        Nr   r   z.] Server returned duplicate pagination cursor z& for list_prompts; stopping pagination)	setr   extendprompts
nextCursorr   warningr   add)r   all_promptsr   seen_cursorsr   s        r   r   zClientPromptsMixin.list_prompts9   s      /1!!$000??Fv~~.$$    L0		{ #)),,RT   V../&&F ?s   %CB?BCc                  K   t        d| d|| j                  j                               5  t        j	                  d| j
                   d|        d}|rWi }|j                         D ]B  \  }}t        |t              r|||<   t        j                  |      j                  d      ||<   D t        |      }|r|j                  d      }t        j                  j!                  t        j                  j#                  |||rt        j                  j$                  di |nd|	      
      }	| j'                  | j(                  j+                  |	t        j                  j,                               d{   }
n4| j'                  | j(                  j/                  ||             d{   }
|
cddd       S 7 D7 # 1 sw Y   yxY ww)a  Send a prompts/get request and return the complete MCP protocol result.

        Args:
            name (str): The name of the prompt to retrieve.
            arguments (dict[str, Any] | None, optional): Arguments to pass to the prompt. Defaults to None.
            meta (dict[str, Any] | None, optional): Request metadata (e.g., for SEP-1686 tasks). Defaults to None.

        Returns:
            mcp.types.GetPromptResult: The complete response object from the protocol,
                containing the prompt messages and any additional metadata.

        Raises:
            RuntimeError: If called while the client is not connected.
            McpError: If the request results in a TimeoutError | JSONRPCError
        zprompts/get zprompts/get)
session_idr   z] called get_prompt: Nutf-8zmodelcontextprotocol.io/taskr   	argumentstask_metaparamsrequestresult_type)r   r+    )r   	transportget_session_idr   r   r   items
isinstancestrpydantic_coreto_jsondecoder   getmcptypesGetPromptRequestGetPromptRequestParamsTaskMetadatar   r   send_requestGetPromptResult
get_prompt)r   r   r+   metaserialized_argumentskeyvaluepropagated_meta	task_dictr1   r   s              r   get_prompt_mcpz!ClientPromptsMixin.get_prompt_mcp\   s    * 4&!~~446	
 LL1TYYK'<TFCD ;? ')$"+//"3JC!%-49,S1 5B4I4I%4P4W4W#5,S1 #4 348O +//0NO	))4499;;!"6DMSYY33@i@SW-	 <  5   $BBLL-- '$'II$=$= .     $BBLL++AU+V   [
 
FU
 
sA   +G
EF>5F:64F>*F<+F>0
G
:F><F>>GG
F)versionrE   r,   c                  K   y wNr3   )r   r   r+   rL   rE   r,   s         r   rD   zClientPromptsMixin.get_prompt   s      %(   `  )rL   rE   task_idttlc                  K   y wrN   r3   )r   r   r+   rL   rE   r,   rQ   rR   s           r   rD   zClientPromptsMixin.get_prompt   s      rO   )rL   rE   r,   rQ   rR   c                  K   |rt        |      ni }|i |j                  di       d|i|d<   |r"| j                  |||||xs d       d{   S | j                  |||xs d       d{   }	|	S 7 &7 w)a  Retrieve a rendered prompt message list from the server.

        Args:
            name (str): The name of the prompt to retrieve.
            arguments (dict[str, Any] | None, optional): Arguments to pass to the prompt. Defaults to None.
            version (str | None, optional): Specific prompt version to get. If None, gets highest version.
            meta (dict[str, Any] | None): Optional request-level metadata.
            task (bool): If True, execute as background task (SEP-1686). Defaults to False.
            task_id (str | None): Optional client-provided task ID (auto-generated if not provided).
            ttl (int): Time to keep results available in milliseconds (default 60s).

        Returns:
            mcp.types.GetPromptResult | PromptTask: The complete response object if task=False,
                or a PromptTask object if task=True.

        Raises:
            RuntimeError: If called while the client is not connected.
            McpError: If the request results in a TimeoutError | JSONRPCError
        NfastmcprL   )rE   )r   r+   rE   )dictr<   _get_prompt_as_taskrK   )
r   r   r+   rL   rE   r,   rQ   rR   request_metar   s
             r   rD   zClientPromptsMixin.get_prompt   s     > &*tDzr'""9b1'7'L#
 11i#L4HD 2    **1E + 
 
 
s$   AA9A5A9.A7/A97A9c           	       K   t        |      }d}|rWi }|j                         D ]B  \  }}	t        |	t              r|	||<   t	        j
                  |	      j                  d      ||<   D t        j                  j                  t        j                  j                  ||t        j                  j                  |      |            }
| j                  | j                  j                  |
t                     d{   }|j                   }t        |t        j                  j"                        rd|j$                  j&                  }| j(                  j+                  |       t-        | ||d      }t/        j0                  |      | j2                  |<   |S |xs t        t5        j6                               }t-        | |||      S 7 ȭw)a?  Get a prompt for background execution (SEP-1686).

        Returns a PromptTask object that handles both background and immediate execution.

        Args:
            name: Prompt name to get
            arguments: Prompt arguments
            task_id: Optional client-provided task ID (ignored, for backward compatibility)
            ttl: Time to keep results available in milliseconds (default 60s)
            meta: Optional request metadata (e.g., version info)

        Returns:
            PromptTask: Future-like object for accessing task status and results
        Nr)   )rR   r*   r.   r0   )prompt_nameimmediate_result)r   r6   r7   r8   r9   r:   r;   r=   r>   r?   r@   rA   r   r   rB   PromptTaskResponseUnionrootCreateTaskResultr,   taskId_submitted_task_idsr$   r
   weakrefref_task_registryuuiduuid4)r   r   r+   rQ   rR   rE   rI   rF   rG   rH   r1   wrapped_result
raw_resultserver_task_idtask_objsynthetic_task_ids                   r   rW   z&ClientPromptsMixin._get_prompt_as_task   s    0 /t4 7;#% 'oo/
UeS)05(-0=0E0Ee0L0S0S1(-	 0 )),,9933.YY+++4%	 4  - 
  $BBLL%%3 &  
 
 $((
j#))"<"<='__33N$$((8!n$H 3:++h2GD/O !( <3tzz|+<'TJ +
s   C9G;G<C	G)r   r	   r   
str | Nonereturnzmcp.types.ListPromptsResult)r   r	   rl   zlist[mcp.types.Prompt])NN)
r   r	   r   r8   r+   dict[str, Any] | NonerE   rm   rl   mcp.types.GetPromptResultrN   )r   r	   r   r8   r+   rm   rL   rk   rE   rm   r,   zLiteral[False]rl   rn   )r   r	   r   r8   r+   rm   rL   rk   rE   rm   r,   zLiteral[True]rQ   rk   rR   intrl   r
   )r   r	   r   r8   r+   rm   rL   rk   rE   rm   r,   boolrQ   rk   rR   ro   rl   z&mcp.types.GetPromptResult | PromptTask)NNrP   N)r   r	   r   r8   r+   rm   rQ   rk   rR   ro   rE   rm   rl   r
   )
__name__
__module____qualname____doc__r   r   rK   r   rD   rW   r3       r   r   r      s0   <
 /3!+	$. L ,0&*	BBB )B $	B
 
#BH  ,0(
 #&*$((( )(
 ( $( ( 
#( (  ,0

 #&*"


 )

 
 $
 
 
 
 

 
 ,0.
 #&*"... ).
 . $. . . . 
0.f ,0"&*GGG )G 	G
 G $G 
Gru   r   ) rt   
__future__r   rd   ra   typingr   r   r   r   	mcp.typesr=   r9   pydanticr   fastmcp.client.clientr	   fastmcp.client.tasksr
   fastmcp.client.telemetryr   fastmcp.telemetryr   fastmcp.utilities.loggingr   rq   r   r>   r^   rC   r\   r   r3   ru   r   <module>r      sq    0 "   8 8   , + 0 2 0	H	 $II!:!:: 
R Rru   