
    "<ih                       d dl mZ d dlZd dlmZmZ d dlmZ d dlm	Z	m
Z
mZ d dlmZ d dlZd dlmZ d dlmZ d d	lmZmZ d d
lmZ d dlmZmZmZmZmZ d dlmZ d dlm Z m!Z! 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+m,Z,m-Z- d dl.m/Z/ d dl0m1Z1m2Z2 d dl3m4Z4 d dl5m6Z6 d dl7m8Z8m9Z9 d dl:m;Z; d dl<m=Z= d dl>m?Z? d dl@mAZA d dlBmCZC d dlDmEZEmFZF d dlGmHZH d dlImJZJ d d lKmLZL d d!lMmNZN e	rd dlOm=Z=  eNeP          ZQeg e f         eg ee          f         z  ZR G d" d#          ZS G d$ d%eHeS          ZT G d& d'e;eS          ZU G d( d)e6eS          ZV G d* d+eEeL          ZW G d, d-e8eL          ZX G d. d/e9eL          ZY G d0 d1e1eL          ZZ G d2 d3eA          Z[d=d8Z\ G d9 d:e e)                   Z] G d; d<e]e)                   Z^dS )>    )annotationsN)	AwaitableCallable)Path)TYPE_CHECKINGAnycastquote)ServerSession)ClientSession)LifespanContextTRequestContext)McpError)METHOD_NOT_FOUNDBlobResourceContentsElicitRequestFormParamsGetPromptResultTextResourceContents)AnyUrl)ClientFastMCP1Server)ElicitResult)
LogMessage)	RootsList)ClientTransportT)NotFoundErrorResourceError	ToolError)	MCPConfig)PromptPromptMessage)PromptArgument)PromptManager)ResourceResourceTemplate)ResourceManager)Context)get_context)FastMCP)
TaskConfig)Tool
ToolResult)ToolManager)apply_transformations_to_tools)MirroredComponent)
get_loggerc                  &    e Zd ZU dZded<   ddZdS )	ProxyManagerMixinzHA mixin for proxy managers to provide a unified client retrieval method.ClientFactoryTclient_factoryreturnr   c                j   K   |                                  }t          j        |          r| d{V }|S )z<Gets a client instance by calling the sync or async factory.N)r5   inspectisawaitable)selfclients     /Users/kimhansen/Desktop/03 Workspace/ceo-agents/chl-effectiveness/mcp-servers/whoop/.venv/lib/python3.11/site-packages/fastmcp/server/proxy.py_get_clientzProxyManagerMixin._get_client=   s@      $$&&v&& 	"!\\\\\\F    N)r6   r   )__name__
__module____qualname____doc____annotations__r=    r>   r<   r3   r3   8   s<         RR""""     r>   r3   c                  D     e Zd ZdZd fdZd fd	ZddZd fdZ xZS )ProxyToolManagerzaA ToolManager that sources its tools from a remote client in addition to local and mounted tools.r5   r4   kwargsr   c                H     t                      j        di | || _        d S NrD   super__init__r5   r:   r5   rG   	__class__s      r<   rL   zProxyToolManager.__init__H   .    ""6""",r>   r6   dict[str, Tool]c                
  K   t                                                       d{V }	 |                                  d{V }|4 d{V  |                                 d{V }|D ].}|j        |vr#t
                              ||          ||j        <   /	 ddd          d{V  n# 1 d{V swxY w Y   n/# t          $ r"}|j        j	        t          k    rn|Y d}~nd}~ww xY wt          || j                  }|S )zMGets the unfiltered tool inventory including local, mounted, and proxy tools.N)toolstransformations)rK   	get_toolsr=   
list_toolsname	ProxyToolfrom_mcp_toolr   errorcoder   r/   rS   )r:   	all_toolsr;   client_toolstooletransformed_toolsrN   s          r<   rT   zProxyToolManager.get_toolsL   s       ''++--------		++--------F U U U U U U U U%+%6%6%8%8888888( U UDy	11/8/F/Fvt/T/T	$),UU U U U U U U U U U U U U U U U U U U U U U U U U U U
  	 	 	w|/// 	 ; 0
 
 

 ! <   "B> AB,B> ,
B66B> 9B6:B> >
C*C%%C*
list[Tool]c                |   K   |                                   d{V }t          |                                          S )zJGets the filtered list of tools including local, mounted, and proxy tools.N)rT   listvalues)r:   
tools_dicts     r<   rU   zProxyToolManager.list_toolsf   sA      >>++++++++
J%%''(((r>   keystr	argumentsdict[str, Any]r-   c                  K   	 t                                          ||           d{V S # t          $ r |                                  d{V }|4 d{V  |                    ||           d{V }t	          |j        |j        |j                  cddd          d{V  cY S # 1 d{V swxY w Y   Y dS w xY w)zBCalls a tool, trying local/mounted first, then proxy if not found.Ncontentstructured_contentmeta)rK   	call_toolr   r=   r-   rl   rm   rn   )r:   rf   rh   r;   resultrN   s        r<   ro   zProxyToolManager.call_toolk   s     	**3	::::::::: 		 		 		++--------F        %//Y????????!"N'-'@                                  		s3   '- ,C =B+C +
B5	5C 8B5	9C ?C r5   r4   rG   r   )r6   rP   )r6   ra   )rf   rg   rh   ri   r6   r-   )	r?   r@   rA   rB   rL   rT   rU   ro   __classcell__rN   s   @r<   rF   rF   E   s        kk- - - - - -! ! ! ! ! !4) ) ) )
         r>   rF   c                  X     e Zd ZdZd fdZd fd	Zd fdZddZddZd fdZ	 xZ
S )ProxyResourceManagerzmA ResourceManager that sources its resources from a remote client in addition to local and mounted resources.r5   r4   rG   r   c                H     t                      j        di | || _        d S rI   rJ   rM   s      r<   rL   zProxyResourceManager.__init__   rO   r>   r6   dict[str, Resource]c                  K   t                                                       d{V }	 |                                  d{V }|4 d{V  |                                 d{V }|D ]H}t	          |j                  |vr0t                              ||          |t	          |j                  <   I	 ddd          d{V  n# 1 d{V swxY w Y   n/# t          $ r"}|j	        j
        t          k    rn|Y d}~nd}~ww xY w|S )zUGets the unfiltered resource inventory including local, mounted, and proxy resources.N)rK   get_resourcesr=   list_resourcesrg   uriProxyResourcefrom_mcp_resourcer   rY   rZ   r   )r:   all_resourcesr;   client_resourcesresourcer^   rN   s         r<   ry   z"ProxyResourceManager.get_resources   s      $gg3355555555	++--------F        )/)>)>)@)@#@#@#@#@#@#@  0  H8<((==);;FHMM &c(,&7&78                            	 	 	w|/// 	 s<   "C A&C4C 
CC CC 
D"C??Ddict[str, ResourceTemplate]c                  K   t                                                       d{V }	 |                                  d{V }|4 d{V  |                                 d{V }|D ].}|j        |vr#t
                              ||          ||j        <   /	 ddd          d{V  n# 1 d{V swxY w Y   n/# t          $ r"}|j        j	        t          k    rn|Y d}~nd}~ww xY w|S )zUGets the unfiltered template inventory including local, mounted, and proxy templates.N)rK   get_resource_templatesr=   list_resource_templatesuriTemplateProxyTemplatefrom_mcp_templater   rY   rZ   r   )r:   all_templatesr;   client_templatestemplater^   rN   s         r<   r   z+ProxyResourceManager.get_resource_templates   s      $gg<<>>>>>>>>	++--------F        )/)G)G)I)I#I#I#I#I#I#I  0  H+=@@);;FHMM &h&:;                            	 	 	w|/// 	 r`   list[Resource]c                |   K   |                                   d{V }t          |                                          S )zRGets the filtered list of resources including local, mounted, and proxy resources.N)ry   rc   rd   )r:   resources_dicts     r<   rz   z#ProxyResourceManager.list_resources   sC      #1133333333N))++,,,r>   list[ResourceTemplate]c                |   K   |                                   d{V }t          |                                          S )zRGets the filtered list of templates including local, mounted, and proxy templates.N)r   rc   rd   )r:   templates_dicts     r<   r   z,ProxyResourceManager.list_resource_templates   sC      #::<<<<<<<<N))++,,,r>   r{   AnyUrl | strstr | bytesc           	     T  K   	 t                                          |           d{V S # t          $ r |                                  d{V }|4 d{V  |                    |           d{V }t	          |d         t
                    r!|d         j        cddd          d{V  cY S t	          |d         t                    r!|d         j        cddd          d{V  cY S t          dt          |d                              d# 1 d{V swxY w Y   Y dS w xY w)zFReads a resource, trying local/mounted first, then proxy if not found.Nr   Unsupported content type: )rK   read_resourcer   r=   
isinstancer   textr   blobr   type)r:   r{   r;   rp   rN   s       r<   r   z"ProxyResourceManager.read_resource   s*     	 ..s333333333 	  	  	 ++--------F 	  	  	  	  	  	  	  	 %33C88888888fQi)=>>  !!9>	  	  	  	  	  	  	  	  	  	  	  	  	  	  	  	   q	+?@@  !!9>	  	  	  	  	  	  	  	  	  	  	  	  	  	  	  	  (FT&)__FF  	  	  	  	  	  	  	  	  	  	  	  	  	  	  	  	  	 	 sF   &, ,D'ADD'0'DD',&D
D	D'D	 D'&D'rq   )r6   rw   )r6   r   )r6   r   )r6   r   )r{   r   r6   r   )r?   r@   rA   rB   rL   ry   r   rz   r   r   rr   rs   s   @r<   ru   ru   |   s        ww- - - - - -     .     .- - - -
- - - -
                   r>   ru   c                  H     e Zd ZdZd fdZd fd	ZddZ	 dd fdZ xZS )ProxyPromptManagerzgA PromptManager that sources its prompts from a remote client in addition to local and mounted prompts.r5   r4   rG   r   c                H     t                      j        di | || _        d S rI   rJ   rM   s      r<   rL   zProxyPromptManager.__init__   rO   r>   r6   dict[str, Prompt]c                  K   t                                                       d{V }	 |                                  d{V }|4 d{V  |                                 d{V }|D ].}|j        |vr#t
                              ||          ||j        <   /	 ddd          d{V  n# 1 d{V swxY w Y   n/# t          $ r"}|j        j	        t          k    rn|Y d}~nd}~ww xY w|S )zQGets the unfiltered prompt inventory including local, mounted, and proxy prompts.N)rK   get_promptsr=   list_promptsrV   ProxyPromptfrom_mcp_promptr   rY   rZ   r   )r:   all_promptsr;   client_promptspromptr^   rN   s         r<   r   zProxyPromptManager.get_prompts   s      "GG//11111111	++--------F        '-':':'<'<!<!<!<!<!<!<,  F{+553>3N3N"F4 4FK0                            	 	 	w|/// 	 r`   list[Prompt]c                |   K   |                                   d{V }t          |                                          S )zNGets the filtered list of prompts including local, mounted, and proxy prompts.N)r   rc   rd   )r:   prompts_dicts     r<   r   zProxyPromptManager.list_prompts   sC      !--////////L''))***r>   NrV   rg   rh   dict[str, Any] | Noner   c                H  K   	 t                                          ||           d{V S # t          $ rj |                                  d{V }|4 d{V  |                    ||           d{V }|cddd          d{V  cY S # 1 d{V swxY w Y   Y dS w xY w)zFRenders a prompt, trying local/mounted first, then proxy if not found.N)rK   render_promptr   r=   
get_prompt)r:   rV   rh   r;   rp   rN   s        r<   r   z ProxyPromptManager.render_prompt   ss     	..tY????????? 	 	 	++--------F        %00yAAAAAAAA                                	s3   '- ,B!B7B!
B	B!B	B! B!rq   )r6   r   )r6   r   N)rV   rg   rh   r   r6   r   )	r?   r@   rA   rB   rL   r   r   r   rr   rs   s   @r<   r   r      s        qq- - - - - -     .+ + + + ,0          r>   r   c                  l     e Zd ZU dZ ed          Zded<   d fd
Zedd            Z		 dddZ
 xZS )rW   zH
    A Tool that represents and executes a tool on a remote server.
    	forbiddenmoder+   task_configr;   r   rG   r   c                H     t                      j        di | || _        d S rI   rK   rL   _clientr:   r;   rG   rN   s      r<   rL   zProxyTool.__init__
  +    ""6"""r>   mcp_toolmcp.types.Toolr6   c                     | ||j         |j        |j        |j        |j        |j        |j        |j        |j        pi                     di                               dg           d          S )z@Factory method to create a ProxyTool from a raw MCP tool schema._fastmcptagsT)r;   rV   titledescription
parametersr   output_schemaiconsrn   r   	_mirrored)	rV   r   r   inputSchemar   outputSchemar   rn   get)clsr;   r   s      r<   rX   zProxyTool.from_mcp_tool  sy     s. ,+ ,"/.-%2**:r::>>vrJJ
 
 
 	
r>   Nrh   ri   contextContext | Noner-   c                  K   | j         4 d{V  t                      }d}t          |d          r|j        }t          |d          r|j        rt          |j                  }t          |d          rLt          |j        d          r7|j        j        r+|j        j        }|r|pi }|	                    d          |d<   | j         
                    | j        ||	           d{V }ddd          d{V  n# 1 d{V swxY w Y   |j        r<t          t          t          j        j        |j        d
                   j                  t)          |j        |j        |j                  S )z6Executes the tool by making a call through the client.Nrequest_contextrn   experimentalis_taskT)exclude_nonezmodelcontextprotocol.io/task)rV   rh   rn   r   rk   )r   r)   hasattrr   rn   dictr   r   task_metadata
model_dumpcall_tool_mcprV   isErrorr   r	   mcptypesTextContentrl   r   r-   structuredContent)r:   rh   r   rn   req_ctxr   rp   s          r<   runzProxyTool.run  s0      < 	 	 	 	 	 	 	 	!mmG*.Dw 122 !17F++ . .--D G^44
 4i@@
  ,4

 %,$8$FM$ #zr?L?W?W)- @X @ @;<  <55Y)$ 6        F-	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	2 > 	QD!6q8IJJOPPPN%7
 
 
 	
s   CC99
DDr;   r   rG   r   )r;   r   r   r   r6   rW   r   )rh   ri   r   r   r6   r-   )r?   r@   rA   rB   r+   r   rC   rL   classmethodrX   r   rr   rs   s   @r<   rW   rW     s           )jk:::K::::      
 
 
 [
& #'&
 &
 &
 &
 &
 &
 &
 &
 &
r>   rW   c                       e Zd ZU dZ ed          Zded<   ded<   dZd	ed
<   ddd fdZe	dd            Z
ddZ xZS )r|   zO
    A Resource that represents and reads a resource from a remote server.
    r   r   r+   r   r   r   Nstr | bytes | None_value)r   r;   c               V     t                      j        di | || _        || _        d S rI   )rK   rL   r   r   )r:   r;   r   rG   rN   s       r<   rL   zProxyResource.__init__Q  s4     	""6"""r>   mcp_resourcemcp.types.Resourcer6   c                     | ||j         |j        |j        |j        |j        pd|j        |j        |j        pi                     di                               dg           t          d          d          S )zHFactory method to create a ProxyResource from a raw MCP resource schema.
text/plainr   r   r   r   T)r;   r{   rV   r   r   	mime_typer   rn   r   r   r   )	r{   rV   r   r   mimeTyper   rn   r   r+   )r   r;   r   s      r<   r}   zProxyResource.from_mcp_resource\  s     s "$$0"+;|$"#)r..z2>>BB62NN"444
 
 
 	
r>   r   c                  K   | j         | j         S | j        4 d{V  | j                            | j                   d{V }ddd          d{V  n# 1 d{V swxY w Y   t	          |d         t
                    r|d         j        S t	          |d         t                    r|d         j        S t          dt          |d                              )z1Read the resource content from the remote server.Nr   r   )r   r   r   r{   r   r   r   r   r   r   r   )r:   rp   s     r<   readzProxyResource.readr  s     ;";< 	@ 	@ 	@ 	@ 	@ 	@ 	@ 	@<55dh????????F	@ 	@ 	@ 	@ 	@ 	@ 	@ 	@ 	@ 	@ 	@ 	@ 	@ 	@ 	@ 	@ 	@ 	@ 	@ 	@ 	@ 	@ 	@ 	@ 	@ 	@ 	@fQi!566 	P!9>!q	#788 	P!9>! NT&)__ N NOOOs   &A
A #A )r;   r   r   r   )r;   r   r   r   r6   r|   )r6   r   )r?   r@   rA   rB   r+   r   rC   r   rL   r   r}   r   rr   rs   s   @r<   r|   r|   H  s           )jk:::K::::OOO!%F%%%% &*		 	 	 	 	 	 	 	 
 
 
 [
*P P P P P P P Pr>   r|   c                  l     e Zd ZU dZ ed          Zded<   d fd
Zedd            Z		 dddZ
 xZS )r   za
    A ResourceTemplate that represents and creates resources from a remote server template.
    r   r   r+   r   r;   r   rG   r   c                H     t                      j        di | || _        d S rI   r   r   s      r<   rL   zProxyTemplate.__init__  r   r>   mcp_templatemcp.types.ResourceTemplater6   c                     | ||j         |j        |j        |j        |j        pd|j        i |j        |j        pi                     di                               dg           t          d          d          S )zHFactory method to create a ProxyTemplate from a raw MCP template schema.r   r   r   r   r   T)r;   uri_templaterV   r   r   r   r   r   rn   r   r   r   )	r   rV   r   r   r   r   rn   r   r+   )r   r;   r   s      r<   r   zProxyTemplate.from_mcp_template  s    
 s%1"$$0"+;|$"#)r..z2>>BB62NN"444
 
 
 	
r>   Nr{   rg   paramsri   r   r   r|   c                  K    | j         j        di d |                                D             }| j        4 d{V  | j                            |           d{V }ddd          d{V  n# 1 d{V swxY w Y   t          |d         t                    r|d         j        }nNt          |d         t                    r|d         j	        }n%t          dt          |d                              t          | j        || j        | j        | j        |d         j        | j        | j        | j        pi                     di                               dg           |
  
        S )	zACreate a resource from the template by calling the remote server.c                8    i | ]\  }}|t          |d           S ) )safer
   ).0kvs      r<   
<dictcomp>z1ProxyTemplate.create_resource.<locals>.<dictcomp>  s+    ???1q%###???r>   Nr   r   r   r   )
r;   r{   rV   r   r   r   r   rn   r   r   rD   )r   formatitemsr   r   r   r   r   r   r   r   r   r|   rV   r   r   r   r   rn   r   )r:   r{   r   r   parameterized_urirp   values          r<   create_resourcezProxyTemplate.create_resource  s#      5D-4 
 
?????
 
 < 	I 	I 	I 	I 	I 	I 	I 	I<556GHHHHHHHHF	I 	I 	I 	I 	I 	I 	I 	I 	I 	I 	I 	I 	I 	I 	I 	I 	I 	I 	I 	I 	I 	I 	I 	I 	I 	I 	I fQi!566 	P1INEEq	#788 	P1INEE NT&)__ N NOOO<!*(Qi(*)/r&&z266::62FF
 
 
 	
s   !A11
A;>A;r   )r;   r   r   r   r6   r   r   )r{   rg   r   ri   r   r   r6   r|   )r?   r@   rA   rB   r+   r   rC   rL   r   r   r   rr   rs   s   @r<   r   r     s           )jk:::K::::      
 
 
 [
. #'	"
 "
 "
 "
 "
 "
 "
 "
 "
r>   r   c                  r     e Zd ZU dZ ed          Zded<   ded<   d fd	Zedd            Z	ddZ
 xZS )r   zM
    A Prompt that represents and renders a prompt from a remote server.
    r   r   r+   r   r   r   r;   c                H     t                      j        di | || _        d S rI   r   r   s      r<   rL   zProxyPrompt.__init__  r   r>   
mcp_promptmcp.types.Promptr6   c                    d |j         pg D             } | ||j        |j        |j        ||j        |j        |j        pi                     di                               dg           t          d          d
  
        S )zDFactory method to create a ProxyPrompt from a raw MCP prompt schema.c                T    g | ]%}t          |j        |j        |j        pd           &S )F)rV   r   required)r#   rV   r   r  )r   args     r<   
<listcomp>z/ProxyPrompt.from_mcp_prompt.<locals>.<listcomp>  sM     
 
 
  XO.  
 
 
r>   r   r   r   r   T)
r;   rV   r   r   rh   r   rn   r   r   r   )rh   rV   r   r   r   rn   r   r+   )r   r;   r   rh   s       r<   r   zProxyPrompt.from_mcp_prompt  s    

 
 "+1r
 
 
	 s""."/'R,,Z<<@@LL"444
 
 
 	
r>   rh   ri   list[PromptMessage]c                   K   | j         4 d{V  | j                             | j        |           d{V }ddd          d{V  n# 1 d{V swxY w Y   |j        S )z6Render the prompt by making a call through the client.N)r   r   rV   messages)r:   rh   rp   s      r<   renderzProxyPrompt.render  s     < 	I 	I 	I 	I 	I 	I 	I 	I<2249iHHHHHHHHF	I 	I 	I 	I 	I 	I 	I 	I 	I 	I 	I 	I 	I 	I 	I 	I 	I 	I 	I 	I 	I 	I 	I 	I 	I 	I 	Is   'A		
AA)r;   r   )r;   r   r   r   r6   r   )rh   ri   r6   r  )r?   r@   rA   rB   r+   r   rC   rL   r   r   r  rr   rs   s   @r<   r   r     s           )jk:::K::::OOO      
 
 
 [
2       r>   r   c                  $     e Zd ZdZd fdZ xZS )FastMCPProxyz
    A FastMCP server that acts as a proxy to a remote MCP-compliant server.
    It uses specialized managers that fulfill requests via a client factory.
    r5   r4   c                    t                      j        di | || _        t          | j        | j        j                  | _        t          | j                  | _        t          | j                  | _	        dS )a  
        Initializes the proxy server.

        FastMCPProxy requires explicit session management via client_factory.
        Use FastMCP.as_proxy() for convenience with automatic session strategy.

        Args:
            client_factory: A callable that returns a Client instance when called.
                           This gives you full control over session creation and reuse.
                           Can be either a synchronous or asynchronous function.
            **kwargs: Additional settings for the FastMCP server.
        )r5   rS   )r5   NrD   )
rK   rL   r5   rF   _tool_managerrS   ru   _resource_managerr   _prompt_managerrM   s      r<   rL   zFastMCPProxy.__init__  s    & 	""6""", .. .>
 
 

 "6."
 "
 "
  2ATUUUr>   )r5   r4   )r?   r@   rA   rB   rL   rr   rs   s   @r<   r
  r
    sQ         
 V  V  V  V  V  V  V  V  V  Vr>   r
  r   /RequestContext[ClientSession, LifespanContextT]r6   r   c                V   K   t                      }|                                 d{V S )z
    A handler that forwards the list roots request from the remote server to the proxy's connected clients and relays the response back to the remote server.
    N)r)   
list_roots)r   ctxs     r<   default_proxy_roots_handlerr    s3       --C!!!!!!!!!r>   c                       e Zd ZdZd fdZedd            Zed d            Zed!d            Zed"d            Z	 xZ
S )#ProxyClientz
    A proxy client that forwards advanced interactions between a remote MCP server and the proxy's connected clients.
    Supports forwarding roots, sampling, elicitation, logging, and progress.
    	transportcClientTransportT | FastMCP[Any] | FastMCP1Server | AnyUrl | Path | MCPConfig | dict[str, Any] | strc                .   d|vr|                                  |d<   d|vr
t          |d<   d|vrt          j        |d<   d|vrt          j        |d<   d|vrt          j        |d<   d|vrt          j        |d<    t                      j        di |d|iz   d S )	NrV   rootssampling_handlerelicitation_handlerlog_handlerprogress_handlerr  rD   )	generate_namer  r  default_sampling_handlerdefault_elicitation_handlerdefault_log_handlerdefault_progress_handlerrK   rL   )r:   r  rG   rN   s      r<   rL   zProxyClient.__init__+  s     !//11F6N&  9F7OV++)4)MF%& ..,7,SF()&&$/$CF=!V++)4)MF%&==6[)$<<=====r>   r  list[mcp.types.SamplingMessage]r   $mcp.types.CreateMessageRequestParamsr   r  r6   mcp.types.CreateMessageResultc                h  K   t                      }|                    t          |          |j        |j        |j        |j                   d{V }t          |t          j	        j
        t          j	        j        z            rt          d          t          j	                            dd|          S )z
        A handler that forwards the sampling request from the remote server to the proxy's connected clients and relays the response back to the remote server.
        )system_prompttemperature
max_tokensmodel_preferencesNzContent is not supported	assistantzfastmcp-client)rolemodelrl   )r)   samplerc   systemPromptr(  	maxTokensmodelPreferencesr   r   r   ResourceLinkEmbeddedResourceRuntimeErrorCreateMessageResult)r   r  r   r   r  rl   s         r<   r  z$ProxyClient.default_sampling_handlerE  s       mm

NN -*'$5 # 
 
 
 
 
 
 
 
 gsy5	8RRSS 	;9:::y,,"	 - 
 
 	
r>   messagerg   response_typer   mcp.types.ElicitRequestParamsr   c                   K   t                      }t          |t                    r|j        ndi d}|j                            |||j                   d{V }t          |j        |j	                  S )z
        A handler that forwards the elicitation request from the remote server to the proxy's connected clients and relays the response back to the remote server.
        object)r   
properties)r6  requestedSchemarelated_request_idN)actionrl   )
r)   r   r   r<  sessionelicit
request_idr   r>  rl   )r   r6  r7  r   r   r  requested_schemarp   s           r<   r   z'ProxyClient.default_elicitation_handler`  s       mm &"9::6F""""55 	
 {)),"~ * 
 
 
 
 
 
 
 

 6=&.IIIIr>   r   Nonec                   K   t                      }|j                            d          }|j                            d          }|                    ||j        |j        |           d{V  dS )zw
        A handler that forwards the log notification from the remote server to the proxy's connected clients.
        msgextra)levellogger_namerF  N)r)   datar   logrG  logger)r   r6  r  rE  rF  s        r<   r!  zProxyClient.default_log_handlery  sr      
 mmlu%%  ))ggcGNRWgXXXXXXXXXXXr>   progressfloattotalfloat | None
str | Nonec                `   K   t                      }|                    |||           d{V  dS )z|
        A handler that forwards the progress notification from the remote server to the proxy's connected clients.
        N)r)   report_progress)r   rL  rN  r6  r  s        r<   r"  z$ProxyClient.default_progress_handler  sA       mm!!(E7;;;;;;;;;;;r>   )r  r  )r  r#  r   r$  r   r  r6   r%  )
r6  rg   r7  r   r   r8  r   r  r6   r   )r6  r   r6   rC  )rL  rM  rN  rO  r6  rP  r6   rC  )r?   r@   rA   rB   rL   r   r  r   r!  r"  rr   rs   s   @r<   r  r  %  s         
> > > > > >4 
 
 
 [
4 J J J [J0 Y Y Y [Y 
< 
< 
< [
< 
< 
< 
< 
<r>   r  c                  :     e Zd ZdZd fdZddZd	 ZddZ xZS )StatefulProxyClienta  
    A proxy client that provides a stateful client factory for the proxy server.

    The stateful proxy client bound its copy to the server session.
    And it will be disconnected when the session is exited.

    This is useful to proxy a stateful mcp server such as the Playwright MCP server.
    Note that it is essential to ensure that the proxy server itself is also stateful.
    argsr   rG   c                H     t                      j        |i | i | _        d S r   )rK   rL   _caches)r:   rU  rG   rN   s      r<   rL   zStatefulProxyClient.__init__  s*    $)&)))FHr>   r6   rC  c                
   K   dS )z
        The stateful proxy client will be forced disconnected when the session is exited.
        So we do nothing here.
        NrD   )r:   exc_type	exc_value	tracebacks       r<   	__aexit__zStatefulProxyClient.__aexit__  s
        r>   c                   K   | j         rA| j                                         \  }}|                    d           d{V  | j         ?dS dS )zE
        Clear all cached clients and force disconnect them.
        TforceN)rW  popitem_disconnect)r:   _caches      r<   clearzStatefulProxyClient.clear  sr       l 	0|++--HAu##$#///////// l 	0 	0 	0 	0 	0r>   Client[ClientTransportT]c                (    t                      j         j                            d          ^                                 t
                               d             j        <    fd}j                            |           S )z
        Create a new stateful proxy client instance with the same configuration.

        Use this method as the client factory for stateful proxy server.
        Nz created for c                    K   j                                        t                                d                                d           d {V  d S )Nz will be disconnectTr^  )rW  poprK  debugra  )proxy_clientr:   r?  s   r<   _on_session_exitz:StatefulProxyClient.new_stateful.<locals>._on_session_exit  sg        )))AAABBB"..T.:::::::::::r>   )	r)   r?  rW  r   newrK  ri  _exit_stackpush_async_callback)r:   rk  rj  r?  s   ` @@r<   new_statefulz StatefulProxyClient.new_stateful  s     --'|''6688::LLLL@@w@@AAA$0DL!; ; ; ; ; ; ;
 334DEEEr>   )rU  r   rG   r   )r6   rC  )r6   re  )	r?   r@   rA   rB   rL   r\  rd  ro  rr   rs   s   @r<   rT  rT    s         I I I I I I   0 0 0       r>   rT  )r   r  r6   r   )_
__future__r   r8   collections.abcr   r   pathlibr   typingr   r   r	   urllib.parser   	mcp.typesr   r   mcp.client.sessionr   mcp.shared.contextr   r   mcp.shared.exceptionsr   r   r   r   r   r   pydantic.networksr   fastmcp.client.clientr   r   fastmcp.client.elicitationr   fastmcp.client.loggingr   fastmcp.client.rootsr   fastmcp.client.transportsr   fastmcp.exceptionsr   r   r   fastmcp.mcp_configr    fastmcp.promptsr!   r"   fastmcp.prompts.promptr#   fastmcp.prompts.prompt_managerr$   fastmcp.resourcesr%   r&   "fastmcp.resources.resource_managerr'   fastmcp.server.contextr(   fastmcp.server.dependenciesr)   fastmcp.server.serverr*   fastmcp.server.tasks.configr+   fastmcp.tools.toolr,   r-   fastmcp.tools.tool_managerr.   fastmcp.tools.tool_transformr/   fastmcp.utilities.componentsr0   fastmcp.utilities.loggingr1   fastmcp.serverr?   rK  r4   r3   rF   ru   r   rW   r|   r   r   r
  r  r  rT  rD   r>   r<   <module>r     sl   " " " " " "  / / / / / / / /       + + + + + + + + + +                 , , , , , , ? ? ? ? ? ? ? ? * * * * * *              % $ $ $ $ $ 8 8 8 8 8 8 8 8 3 3 3 3 3 3 - - - - - - * * * * * * 6 6 6 6 6 6 F F F F F F F F F F ( ( ( ( ( ( 1 1 1 1 1 1 1 1 1 1 1 1 1 1 8 8 8 8 8 8 8 8 8 8 8 8 8 8 > > > > > > * * * * * * 3 3 3 3 3 3 ) ) ) ) ) ) 2 2 2 2 2 2 / / / / / / / / 2 2 2 2 2 2      ; : : : : : 0 0 0 0 0 0 '&&&&&&	H		 "f*%Yv5F1F(GG
 
 
 
 
 
 
 
4 4 4 4 4{$5 4 4 4nP  P  P  P  P ?,= P  P  P f1 1 1 1 1(9 1 1 1hB
 B
 B
 B
 B
' B
 B
 B
J6P 6P 6P 6P 6PH/ 6P 6P 6PrA
 A
 A
 A
 A
$&7 A
 A
 A
H* * * * *&+ * * *Z&V &V &V &V &V7 &V &V &VR" " " "i< i< i< i< i<&)* i< i< i<X2 2 2 2 2+&67 2 2 2 2 2r>   