
    k7id                    6   S SK Jr  S SKrS SKrS SKJrJr  S SKJr  S SK	J
r
JrJr  S SKJr  S SKrS SKJr  S SKJr  S S	KJrJr  S S
KJr  S SKJrJrJrJr  S SKJr  S SKrS SK J!r!J"r"  S SK#J$r$  S SK%J&r&  S SK'J(r(  S SK)J*r*  S SK+J,r,J-r-J.r.  S SK/J0r0  S SK1J2r2J3r3  S SK4J5r5  S SK6J7r7  S SK8J9r9J:r:  S SK;J<r<  S SK=J>r>  S SK?J@r@  S SKAJBrB  S SKCJDrDJErE  S SKFJGrG  S SKHJIrI  S SKJJKrK  S S KLJMrM  \
(       a  S SKNJ>r>  \M" \O5      rP\/ \!4   \/ \\!   4   -  rQ " S! S"5      rR " S# S$\G\R5      rS " S% S&\<\R5      rT " S' S(\7\R5      rU " S) S*\D\K5      rV " S+ S,\9\K5      rW " S- S.\:\K5      rX " S/ S0\2\K5      rY " S1 S2\B5      rZ    S8S3 jr[ " S4 S5\!\*   5      r\ " S6 S7\\\*   5      r]g)9    )annotationsN)	AwaitableCallable)Path)TYPE_CHECKINGAnycast)quote)ServerSession)ClientSession)LifespanContextTRequestContext)McpError)METHOD_NOT_FOUNDBlobResourceContentsGetPromptResultTextResourceContents)AnyUrl)ClientFastMCP1Server)ElicitResult)
LogMessage)	RootsList)ClientTransportT)NotFoundErrorResourceError	ToolError)	MCPConfig)PromptPromptMessage)PromptArgument)PromptManager)ResourceResourceTemplate)ResourceManager)Context)get_context)FastMCP)Tool
ToolResult)ToolManager)apply_transformations_to_tools)MirroredComponent)
get_loggerc                  .    \ rS rSr% SrS\S'   SS jrSrg)	ProxyManagerMixin8   zHA mixin for proxy managers to provide a unified client retrieval method.ClientFactoryTclient_factoryc                |   #    U R                  5       n[        R                  " U5      (       a
  UI Sh  vN nU$  N7f)z<Gets a client instance by calling the sync or async factory.N)r3   inspectisawaitable)selfclients     N/home/james-whalen/.local/lib/python3.13/site-packages/fastmcp/server/proxy.py_get_clientProxyManagerMixin._get_client=   s5     $$&v&&!\F "s   1<:< N)returnr   )__name__
__module____qualname____firstlineno____doc____annotations__r:   __static_attributes__r<       r9   r0   r0   8   s    R""rE   r0   c                  Z   ^  \ rS rSrSrSU 4S jjrS	U 4S jjrS
S jrSU 4S jjrSr	U =r
$ )ProxyToolManagerE   zaA ToolManager that sources its tools from a remote client in addition to local and mounted tools.c                2   > [         TU ]  " S0 UD6  Xl        g Nr<   super__init__r3   r7   r3   kwargs	__class__s      r9   rM   ProxyToolManager.__init__H       "6",rE   c                *  >#    [         TU ]  5       I Sh  vN n U R                  5       I Sh  vN nU ISh  vN   UR                  5       I Sh  vN nU H6  nUR                  U;  d  M  [
        R                  X$5      XR                  '   M8     SSS5      ISh  vN   [        UU R                  S9nU$  N N N Nl N"! , ISh  vN  (       d  f       N7= f! [         a*  nUR                  R                  [        :X  a   SnANgUeSnAff = f7f)zMGets the unfiltered tool inventory including local, mounted, and proxy tools.N)toolstransformations)rL   	get_toolsr:   
list_toolsname	ProxyToolfrom_mcp_toolr   errorcoder   r,   rU   )r7   	all_toolsr8   client_toolstooletransformed_toolsrP   s          r9   rV   ProxyToolManager.get_toolsL   s       '+--		++--Fv%+%6%6%88(Dyy	1/8/F/Fv/T	)), ) v ; 00

 ! + . .8 vvv
  	ww||//		s   DB8DC B:
C B<C CB>C-%CC C C "D:C <C >C C CC	CC DC 
D&DD	DDDc                p   #    U R                  5       I Sh  vN n[        UR                  5       5      $  N7f)zJGets the filtered list of tools including local, mounted, and proxy tools.N)rV   listvalues)r7   
tools_dicts     r9   rW   ProxyToolManager.list_toolsf   s-     >>++
J%%'(( ,   646c                t  >#     [         TU ]  X5      I Sh  vN $  N! [         a    U R                  5       I Sh  vN  nU ISh  vN    UR                  X5      I Sh  vN  n[	        UR
                  UR                  S9sSSS5      ISh  vN    s $ ! , ISh  vN  (       d  f        g= ff = f7f)zBCalls a tool, trying local/mounted first, then proxy if not found.Ncontentstructured_content)rL   	call_toolr   r:   r*   rk   rl   )r7   key	argumentsr8   resultrP   s        r9   rm   ProxyToolManager.call_toolk   s     	*3:::: 	++---Fv%//???!"NN'-'@'@ vvvvv	s   B8  B8 B5>B5A
B5B!A$""BB5BB5B8B1	B" B1	,B5/B81B55B8r3   r3   r2   rO   r   )r=   zdict[str, Tool])r=   z
list[Tool])rn   strro   dict[str, Any]r=   r*   )r>   r?   r@   rA   rB   rM   rV   rW   rm   rD   __classcell__rP   s   @r9   rG   rG   E   s    k-!4)
 rE   rG   c                  t   ^  \ rS rSrSrS
U 4S jjrSU 4S jjrSU 4S jjrSS jrSS jr	SU 4S jjr
S	rU =r$ )ProxyResourceManager{   zmA ResourceManager that sources its resources from a remote client in addition to local and mounted resources.c                2   > [         TU ]  " S0 UD6  Xl        g rJ   rK   rN   s      r9   rM   ProxyResourceManager.__init__~   rR   rE   c                ,  >#    [         TU ]  5       I Sh  vN n U R                  5       I Sh  vN nU ISh  vN   UR                  5       I Sh  vN nU HI  n[	        UR
                  5      U;  d  M  [        R                  X$5      U[	        UR
                  5      '   MK     SSS5      ISh  vN   U$  N N N Nk N! , ISh  vN  (       d  f       U$ = f! [         a+  nUR                  R                  [        :X  a   SnAU$ UeSnAff = f7f)zUGets the unfiltered resource inventory including local, mounted, and proxy resources.N)rL   get_resourcesr:   list_resourcesrt   uriProxyResourcefrom_mcp_resourcer   r[   r\   r   )r7   all_resourcesr8   client_resourcesresourcer`   rP   s         r9   r~   "ProxyResourceManager.get_resources   s      $g355	++--Fv)/)>)>)@#@  0H8<<(=);;FM &c(,,&78 !1 v % 6 .#@ vv   	ww||//  		s   DB7DC B9
C B;C CB= C6/C%C 0B?1C 5D9C ;C =C?C CC
CC DC 
D&DD
DDDc                  >#    [         TU ]  5       I Sh  vN n U R                  5       I Sh  vN nU ISh  vN   UR                  5       I Sh  vN nU H6  nUR                  U;  d  M  [
        R                  X$5      XR                  '   M8     SSS5      ISh  vN   U$  N Nw Nn NX N! , ISh  vN  (       d  f       U$ = f! [         a+  nUR                  R                  [        :X  a   SnAU$ UeSnAff = f7f)zUGets the unfiltered template inventory including local, mounted, and proxy templates.N)rL   get_resource_templatesr:   list_resource_templatesuriTemplateProxyTemplatefrom_mcp_templater   r[   r\   r   )r7   all_templatesr8   client_templatestemplater`   rP   s         r9   r   +ProxyResourceManager.get_resource_templates   s      $g<>>	++--Fv)/)G)G)I#I  0H++=@);;FM &&:&:; !1 v % ? .#I vv   	ww||//  		   DB$DC	 B&
C	 B(C	 B.B*B.-%B.C	 B,C	 "D&C	 (C	 *B.,C	 .C4B75CC	 DC	 	
C>C91D7C99C>>Dc                p   #    U R                  5       I Sh  vN n[        UR                  5       5      $  N7f)zRGets the filtered list of resources including local, mounted, and proxy resources.N)r~   rd   re   )r7   resources_dicts     r9   r   #ProxyResourceManager.list_resources   s/     #1133N))+,, 4rh   c                p   #    U R                  5       I Sh  vN n[        UR                  5       5      $  N7f)zRGets the filtered list of templates including local, mounted, and proxy templates.N)r   rd   re   )r7   templates_dicts     r9   r   ,ProxyResourceManager.list_resource_templates   s/     #::<<N))+,, =rh   c           	     2  >#     [         TU ]  U5      I Sh  vN $  N! [         a    U R                  5       I Sh  vN  nU ISh  vN    UR                  U5      I Sh  vN  n[	        US   [
        5      (       a#  US   R                  sSSS5      ISh  vN    s $ [	        US   [        5      (       a#  US   R                  sSSS5      ISh  vN    s $ [        S[        US   5       35      Se! , ISh  vN  (       d  f        g= ff = f7f)zFReads a resource, trying local/mounted first, then proxy if not found.Nr   Unsupported content type: )rL   read_resourcer   r:   
isinstancer   textr   blobr   type)r7   r   r8   rp   rP   s       r9   r   "ProxyResourceManager.read_resource   s     	 .s3333 	 ++---Fv%33C888fQi)=>>!!9>> vv  q	+?@@!!9>> vv (4T&)_4EF  vv	 s   D  D D>DA
DC8!A$"+C8DBD D"&C8DCDDC88D	>D?D	DDDDrr   rs   )r=   zdict[str, Resource])r=   zdict[str, ResourceTemplate])r=   zlist[Resource])r=   zlist[ResourceTemplate])r   zAnyUrl | strr=   str | bytes)r>   r?   r@   rA   rB   rM   r~   r   r   r   r   rD   rv   rw   s   @r9   ry   ry   {   s)    w-..-
-
   rE   ry   c                  j   ^  \ rS rSrSrSU 4S jjrS	U 4S jjrS
S jr S     SU 4S jjjrSr	U =r
$ )ProxyPromptManager   zgA PromptManager that sources its prompts from a remote client in addition to local and mounted prompts.c                2   > [         TU ]  " S0 UD6  Xl        g rJ   rK   rN   s      r9   rM   ProxyPromptManager.__init__   rR   rE   c                  >#    [         TU ]  5       I Sh  vN n U R                  5       I Sh  vN nU ISh  vN   UR                  5       I Sh  vN nU H6  nUR                  U;  d  M  [
        R                  X$5      XR                  '   M8     SSS5      ISh  vN   U$  N Nw Nn NX N! , ISh  vN  (       d  f       U$ = f! [         a+  nUR                  R                  [        :X  a   SnAU$ UeSnAff = f7f)zQGets the unfiltered prompt inventory including local, mounted, and proxy prompts.N)rL   get_promptsr:   list_promptsrX   ProxyPromptfrom_mcp_promptr   r[   r\   r   )r7   all_promptsr8   client_promptspromptr`   rP   s         r9   r   ProxyPromptManager.get_prompts   s      "G/11	++--Fv'-':':'<!<,F{{+53>3N3N"4KK0 - v % 2 .!< vv   	ww||//  		r   c                p   #    U R                  5       I Sh  vN n[        UR                  5       5      $  N7f)zNGets the filtered list of prompts including local, mounted, and proxy prompts.N)r   rd   re   )r7   prompts_dicts     r9   r   ProxyPromptManager.list_prompts   s/     !--//L'')** 0rh   c                <  >#     [         TU ]  X5      I Sh  vN $  N! [         aq    U R                  5       I Sh  vN  nU ISh  vN    UR	                  X5      I Sh  vN  nUsSSS5      ISh  vN    s $ ! , ISh  vN  (       d  f        g= ff = f7f)zFRenders a prompt, trying local/mounted first, then proxy if not found.N)rL   render_promptr   r:   
get_prompt)r7   rX   ro   r8   rp   rP   s        r9   r    ProxyPromptManager.render_prompt   sq     	.t???? 	++---Fv%00AAA vvvvv	s   B  B B>BA
BA=!A$"A=(B4A75B;B=B	BB	BBBBrr   rs   )r=   zdict[str, Prompt])r=   zlist[Prompt]N)rX   rt   ro   zdict[str, Any] | Noner=   r   )r>   r?   r@   rA   rB   rM   r   r   r   rD   rv   rw   s   @r9   r   r      s@    q-.+ ,0 ) 
	 rE   r   c                  ^   ^  \ rS rSrSrSU 4S jjr\SS j5       r S	     S
S jjrSr	U =r
$ )rY   i  z@
A Tool that represents and executes a tool on a remote server.
c                2   > [         TU ]  " S0 UD6  Xl        g rJ   rL   rM   _clientr7   r8   rO   rP   s      r9   rM   ProxyTool.__init__      "6"rE   c                    U " UUR                   UR                  UR                  UR                  UR                  UR
                  UR
                  =(       d    0 R                  S0 5      R                  S/ 5      SS9	$ )z@Factory method to create a ProxyTool from a raw MCP tool schema._fastmcptagsT)	r8   rX   description
parametersr   output_schemametar   	_mirrored)rX   r   inputSchemar   outputSchemar   get)clsr8   mcp_tools      r9   rZ   ProxyTool.from_mcp_tool  so      ,,++ ,,"//--%2**:r:>>vrJ

 
	
rE   c                  #    U R                    ISh  vN   U R                   R                  U R                  US9I Sh  vN nSSS5      ISh  vN   WR                  (       aD  [	        [        [        R                  R                  UR                  S   5      R                  5      e[        UR                  UR                  S9$  N N N{! , ISh  vN  (       d  f       N= f7f)z6Executes the tool by making a call through the client.N)rX   ro   r   rj   )r   call_tool_mcprX   isErrorr   r	   mcptypesTextContentrk   r   r*   structuredContent)r7   ro   contextrp   s       r9   runProxyTool.run  s      <<<<<55YY# 6  F  <
 >>D!6!6q8IJOOPPNN%77
 	
    <<<sV   C'CC'(CC	 CC'CA8C'	CC'C$CC$ C'r   r8   r   rO   r   )r8   r   r   zmcp.types.Toolr=   rY   r   )ro   ru   r   Context | Noner=   r*   )r>   r?   r@   rA   rB   rM   classmethodrZ   r   rD   rv   rw   s   @r9   rY   rY     sL     
 
" #'
!
  
 
	
 
rE   rY   c                     ^  \ rS rSr% SrS\S'   SrS\S'   SS.   SU 4S	 jjjr\      SS
 j5       r	SS jr
SrU =r$ )r   i-  zG
A Resource that represents and reads a resource from a remote server.
r   r   Nstr | bytes | None_value)r   c               >   > [         TU ]  " S0 UD6  Xl        X l        g rJ   )rL   rM   r   r   )r7   r8   r   rO   rP   s       r9   rM   ProxyResource.__init__5  s     	"6"rE   c                    U " UUR                   UR                  UR                  UR                  =(       d    SUR                  UR                  =(       d    0 R                  S0 5      R                  S/ 5      SS9$ )zHFactory method to create a ProxyResource from a raw MCP resource schema.
text/plainr   r   T)r8   r   rX   r   	mime_typer   r   r   )r   rX   r   mimeTyper   r   )r   r8   mcp_resources      r9   r   ProxyResource.from_mcp_resource@  sq       ""$00"++;|""##)r..z2>BB62N	
 		
rE   c                  #    U R                   b  U R                   $ U R                   ISh  vN   U R                  R                  U R                  5      I Sh  vN nSSS5      ISh  vN   [	        WS   [
        5      (       a  US   R                  $ [	        US   [        5      (       a  US   R                  $ [        S[        US   5       35      e N N~ Np! , ISh  vN  (       d  f       N= f7f)z1Read the resource content from the remote server.Nr   r   )r   r   r   r   r   r   r   r   r   r   r   )r7   rp   s     r9   readProxyResource.readS  s     ;;";;<<<<<55dhh??F  <fQi!566!9>>!q	#788!9>>!"<T&)_<M NOO  ?  <<<sW   *C6CC6)CCCC6)C*A-C6CC6C3"C%#C3/C6)r   r   )r8   r   r   r   )r8   r   r   zmcp.types.Resourcer=   r   )r=   r   )r>   r?   r@   rA   rB   rC   r   rM   r   r   r   rD   rv   rw   s   @r9   r   r   -  sw     O!%F% &*			 #		 	 

 )
 
	
 
$P PrE   r   c                  n   ^  \ rS rSrSrSU 4S jjr\      SS j5       r S	       S
S jjrSr	U =r
$ )r   ib  zY
A ResourceTemplate that represents and creates resources from a remote server template.
c                2   > [         TU ]  " S0 UD6  Xl        g rJ   r   r   s      r9   rM   ProxyTemplate.__init__g  r   rE   c                    U " UUR                   UR                  UR                  UR                  =(       d    S0 UR                  UR                  =(       d    0 R                  S0 5      R                  S/ 5      SS9	$ )zHFactory method to create a ProxyTemplate from a raw MCP template schema.r   r   r   T)	r8   uri_templaterX   r   r   r   r   r   r   )r   rX   r   r   r   r   )r   r8   mcp_templates      r9   r   ProxyTemplate.from_mcp_templatek  st    
 %11""$00"++;|""##)r..z2>BB62N

 
	
rE   c                >  #    U R                   R                  " S	0 UR                  5        VVs0 s H  u  pEU[        USS9_M     snnD6nU R                   ISh  vN   U R                  R                  U5      I Sh  vN nSSS5      ISh  vN   [        WS   [        5      (       a  US   R                  nOB[        US   [        5      (       a  US   R                  nO[        S[        US   5       35      e[        U R                  UU R                  U R                  US   R                   U R"                  U R"                  =(       d    0 R%                  S0 5      R%                  S/ 5      US9$ s  snnf  GN  N N! , ISh  vN  (       d  f       GN= f7f)
zACreate a resource from the template by calling the remote server. )safeNr   r   r   r   )r8   r   rX   r   r   r   r   r   r<   )r   formatitemsr
   r   r   r   r   r   r   r   r   r   r   rX   r   r   r   r   )	r7   r   paramsr   kvparameterized_urirp   values	            r9   create_resourceProxyTemplate.create_resource|  sI     !--44 
06?q%##?
 <<<<<556GHHF  < fQi!5661INNEq	#7881INNE"<T&)_<M NOO<<!((Qi(())/r&&z26::62F	
 		
 @H  <<<se   ,FE5FE;FF>E>?FFF C-F>F FFF	FFr   r   )r8   r   r   zmcp.types.ResourceTemplater=   r   r   )r   rt   r   ru   r   r   r=   r   )r>   r?   r@   rA   rB   rM   r   r   r   rD   rv   rw   s   @r9   r   r   b  sn     

+E
	
 
( #'	 
 
  
  	 

 
 
  
rE   r   c                  f   ^  \ rS rSr% SrS\S'   S	U 4S jjr\      S
S j5       rSS jr	Sr
U =r$ )r   i  zE
A Prompt that represents and renders a prompt from a remote server.
r   r   c                2   > [         TU ]  " S0 UD6  Xl        g rJ   r   r   s      r9   rM   ProxyPrompt.__init__  r   rE   c                p   UR                   =(       d    /  Vs/ s H5  n[        UR                  UR                  UR                  =(       d    SS9PM7     nnU " UUR                  UR                  UUR
                  UR
                  =(       d    0 R                  S0 5      R                  S/ 5      SS9$ s  snf )zDFactory method to create a ProxyPrompt from a raw MCP prompt schema.F)rX   r   requiredr   r   T)r8   rX   r   ro   r   r   r   )ro   r!   rX   r   r   r   r   )r   r8   
mcp_promptargro   s        r9   r   ProxyPrompt.from_mcp_prompt  s     "++1r1
 2 XXOO.
 2 	 
 "..//'R,,Z<@@L
 	

s   <B3c                  #    U R                    ISh  vN   U R                   R                  U R                  U5      I Sh  vN nSSS5      ISh  vN   UR                  $  NN N" N! , ISh  vN  (       d  f       WR                  $ = f7f)z6Render the prompt by making a call through the client.N)r   r   rX   messages)r7   ro   rp   s      r9   renderProxyPrompt.render  sW     <<<<<22499iHHF  <  H  <<sV   BA"B*A(A$A(BA&B$A(&B(B
.A1/B
;Br   )r8   r   )r8   r   r   zmcp.types.Promptr=   r   )ro   ru   r=   zlist[PromptMessage])r>   r?   r@   rA   rB   rC   rM   r   r   r  rD   rv   rw   s   @r9   r   r     sJ     O 

)9
	
 
, rE   r   c                  D   ^  \ rS rSrSr SSS.   SU 4S jjjjrSrU =r$ )	FastMCPProxyi  z
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.
Nrr   c                 >^ [         TU ]  " S
0 UD6  Tb  Ub  [        S5      eTbF  [        R                  R
                  (       a  [        R                  " S[        SS9  U4S jnX@l	        OUb  X l	        O[        S5      e[        U R                  U R                  R                  S9U l        [        U R                  S	9U l        [        U R                  S	9U l        g)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: [DEPRECATED] A Client instance. Use client_factory instead for explicit
           session management. When provided, a client_factory will be automatically
           created that provides session isolation for backwards compatibility.
    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.
Nz1Cannot specify both 'client' and 'client_factory'zPassing 'client' to FastMCPProxy is deprecated. Use 'client_factory' instead for explicit session management. For automatic session strategy, use FastMCP.as_proxy().   )
stacklevelc                 $   > T R                  5       $ r   )new)r8   s   r9   deprecated_client_factory8FastMCPProxy.__init__.<locals>.deprecated_client_factory  s    zz|#rE   zMust specify 'client_factory')r3   rU   rr   r<   )rL   rM   
ValueErrorfastmcpsettingsdeprecation_warningswarningswarnDeprecationWarningr3   rG   _tool_managerrU   ry   _resource_managerr   _prompt_manager)r7   r8   r3   rO   r  rP   s    `   r9   rM   FastMCPProxy.__init__  s    . 	"6" ."<PQQ44N& 	$ #<'"0<== ... ..>>

 "6.."
  2ATATUrE   )r  r  r  r3   r   )r8   zClient | Noner3   zClientFactoryT | None)r>   r?   r@   rA   rB   rM   rD   rv   rw   s   @r9   r  r    s8     !%:V 15	:V:V .	:V :VrE   r  c                R   #    [        5       nUR                  5       I Sh  vN $  N7f)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     r9   default_proxy_roots_handlerr    s!      -C!!!!s   '%'c                     ^  \ rS rSrSr  S	U 4S jjr\        S
S j5       r\          SS j5       r\SS j5       r	\        SS j5       r
SrU =r$ )ProxyClienti  z
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.
c                H  > SU;  a  U R                  5       US'   SU;  a	  [        US'   SU;  a  [        R                  US'   SU;  a  [        R                  US'   SU;  a  [        R
                  US'   SU;  a  [        R                  US'   [        TU ]   " S0 USU0-  D6  g )	NrX   rootssampling_handlerelicitation_handlerlog_handlerprogress_handler	transportr<   )	generate_namer  r   default_sampling_handlerdefault_elicitation_handlerdefault_log_handlerdefault_progress_handlerrL   rM   )r7   r'  rO   rP   s      r9   rM   ProxyClient.__init__  s     !//1F6N& 9F7OV+)4)M)MF%& .,7,S,SF()&$/$C$CF=!V+)4)M)MF%&=6[)$<<=rE   c                  #    [        5       nUR                  [        U5      UR                  UR                  UR
                  UR                  S9I Sh  vN n[        U[        R                  R                  [        R                  R                  -  5      (       a  [        S5      e[        R                  R                  SSUS9$  Nr7f)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modelrk   )r'   samplerd   systemPromptr0  	maxTokensmodelPreferencesr   r   r   ResourceLinkEmbeddedResourceRuntimeErrorCreateMessageResult)r   r  r   r   r  rk   s         r9   r)  $ProxyClient.default_sampling_handler5  s      m

N --**''$55 # 
 
 gsyy55		8R8RRSS9::yy,," - 
 	

s   AC	CA3C	c                   #    [        5       nUR                  R                  UUR                  UR                  S9I Sh  vN n[        UR                  UR                  S9$  N"7f)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.
)messagerequestedSchemarelated_request_idN)actionrk   )r'   sessionelicitrA  
request_idr   rC  rk   )r   r@  response_typer   r   r  rp   s          r9   r*  'ProxyClient.default_elicitation_handlerO  s[      m{{))"22"~~ * 
 

 6==&..II
s   =A$A" #A$c                   #    [        5       nUR                  R                  S5      nUR                  R                  S5      nUR                  X1R                  UR
                  US9I Sh  vN   g N7f)zg
A handler that forwards the log notification from the remote server to the proxy's connected clients.
msgextra)levellogger_namerK  N)r'   datar   logrL  logger)r   r@  r  rJ  rK  s        r9   r+  ProxyClient.default_log_handlerb  sW     
 mllu%  )ggcGNNRWgXXXs   A)A3+A1,A3c                X   #    [        5       nUR                  XU5      I Sh  vN   g N7f)zl
A handler that forwards the progress notification from the remote server to the proxy's connected clients.
N)r'   report_progress)r   progresstotalr@  r  s        r9   r,  $ProxyClient.default_progress_handlerl  s$      m!!(7;;;s    *(*r<   )r'  z^ClientTransportT | FastMCP | FastMCP1Server | AnyUrl | Path | MCPConfig | dict[str, Any] | str)r  zlist[mcp.types.SamplingMessage]r   z$mcp.types.CreateMessageRequestParamsr   /RequestContext[ClientSession, LifespanContextT]r=   zmcp.types.CreateMessageResult)
r@  rt   rG  r   r   zmcp.types.ElicitRequestParamsr   rW  r=   r   )r@  r   r=   None)rT  floatrU  zfloat | Noner@  z
str | Noner=   rX  )r>   r?   r@   rA   rB   rM   r   r)  r*  r+  r,  rD   rv   rw   s   @r9   r   r     s    
>>4 
1
 5
 A	

 
'
 
2 JJ J .	J
 AJ 
J J$ Y Y 
<
< 
< 	
<
 

< 
<rE   r   c                  J   ^  \ rS rSrSrSU 4S jjrS	S jrS rS
S jrSr	U =r
$ )StatefulProxyClientiz  al  
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.
c                4   > [         TU ]  " U0 UD6  0 U l        g r   )rL   rM   _caches)r7   argsrO   rP   s      r9   rM   StatefulProxyClient.__init__  s    $)&)FHrE   c                   #    g7f)zj
The stateful proxy client will be forced disconnected when the session is exited.
So we do nothing here.
Nr<   )r7   exc_type	exc_value	tracebacks       r9   	__aexit__StatefulProxyClient.__aexit__  s     s   c                   #    U R                   (       aG  U R                   R                  5       u  pUR                  SS9I Sh  vN   U R                   (       a  MF  gg N7f)z5
Clear all cached clients and force disconnect them.
TforceN)r]  popitem_disconnect)r7   _caches      r9   clearStatefulProxyClient.clear  sG      ll||++-HA##$#/// lll/s   A AAAAc                *  ^ ^^ [        5       R                  mT R                  R                  TS5      mTc\  T R	                  5       m[
        R                  T ST 35        TT R                  T'   UU U4S jnTR                  R                  U5        T$ )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                    >#    TR                   R                  T5        [        R                  T  S35        T R	                  SS9I S h  vN   g  N7f)Nz will be disconnectTrg  )r]  poprP  debugrj  )proxy_clientr7   rD  s   r9   _on_session_exit:StatefulProxyClient.new_stateful.<locals>._on_session_exit  sC       )~-@AB"..T.:::s   AA	A
A)	r'   rD  r]  r   r  rP  rr  _exit_stackpush_async_callback)r7   rt  rs  rD  s   ` @@r9   new_stateful StatefulProxyClient.new_stateful  s     -''||''688:LLLL>wi@A$0DLL!;
 334DErE   )r]  )r^  r   rO   r   )r=   rX  )r=   zClient[ClientTransportT])r>   r?   r@   rA   rB   rM   rd  rm  rx  rD   rv   rw   s   @r9   r[  r[  z  s"    I0 rE   r[  )r   rW  r=   r   )^
__future__r   r5   r  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   pydantic.networksr   r  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.tools.toolr)   r*   fastmcp.tools.tool_managerr+   fastmcp.tools.tool_transformr,   fastmcp.utilities.componentsr-   fastmcp.utilities.loggingr.   fastmcp.serverr>   rP  r2   r0   rG   ry   r   rY   r   r   r   r  r  r   r[  r<   rE   r9   <module>r     s   "   /  + +    , ? *  %  8 3 - * 6 F F ( 1 1 8 8 > * 3 ) / 2 ; 0&	H	 "f*%Yv5F1F(GG
 
3{$5 3lP ?,= P f1(9 1h(
' (
V2PH/ 2Pj:
$&7 :
z&&+ &R@V7 @VF"<""b<&)* b<J2+&67 2rE   