+
    wiD                       a  R t/0 t R t^ RIt^ RIt^ RIHt ^ RIHt ^ RIH	t	 ^ RI
Ht ^ RIHtHtHt ^ RIt^ RIt^ RIHt ^ RIHtHt ^ RIt^ R	IH
t
 ^ R
IHtHtHtHtHt ^ RIHt ^ RI H!t! ^ RI"H#t# ^ RI$H%t% ^ RI&H't' ^ RI(H)t)  ! R R]4      t* ! R R]4      t+]!]*,          ]+,          t,] ^ k ] ! R R4      4       t- ! R R4      t.R# )a*  
SessionGroup concurrently manages multiple MCP session connections.

Tools, resources, and prompts are aggregated across servers. Servers may
be connected to or disconnected from at any point after initialization.

This abstractions can handle naming collisions using a custom user-provided
hook.
N)Callable)	dataclass)	timedelta)TracebackType)Any	TypeAliasoverload)	BaseModel)Self
deprecated)types)ElicitationFnTListRootsFnT
LoggingFnTMessageHandlerFnTSamplingFnT)
sse_client)StdioServerParameters)streamable_http_client)create_mcp_http_client)McpError)ProgressFnTc                   <   a  ] tR t^#t o RtRt^tRtV 3R ltRt	V t
R# )SseServerParametersz(Parameters for intializing a sse_client.Nc                n   < V ^8  d   Qh/ S[ ;R&   S[S[ S[3,          R,          ;R&   S[;R&   S[;R&   # )   urlNheaderstimeoutsse_read_timeout)strdictr   float)format__classdict__s   "L/home/ubuntu/.local/lib/python3.14/site-packages/mcp/client/session_group.py__annotate__ SseServerParameters.__annotate__#   sE      
H	  #s(^d")    $      ,  )__name__
__module____qualname____firstlineno____doc__r   r   r   __annotate_func____static_attributes____classdictcell__r$   s   @r%   r   r   #   s%     2 &*G G %  r(   r   c                   \   a  ] tR t^3t o RtRt]! ^R7      t]! RR7      tRt	V 3R lt
RtV tR# )StreamableHttpParametersz4Parameters for intializing a streamable_http_client.N)secondsTc                z   < V ^8  d   Qh/ S[ ;R&   S[S[ S[3,          R,          ;R&   S[;R&   S[;R&   S[;R&   # )r   r   Nr   r   r   terminate_on_close)r    r!   r   r   bool)r#   r$   s   "r%   r&   %StreamableHttpParameters.__annotate__3   sR      
H	  #s(^d")  .   ;   #! r(   r)   r*   )r+   r,   r-   r.   r/   r   r   r   r   r8   r0   r1   r2   r3   s   @r%   r5   r5   3   s7     > &*G #2.G #,F";  $!  r(   r5   c                   L   a  ] tR t^Kt o RtRtRtRtRtRt	Rt
RtV 3R ltRtV tR# )ClientSessionParametersz>Parameters for establishing a client session to an MCP server.Nc                   < V ^8  d   Qh/ S[ R,          ;R&   S[R,          ;R&   S[R,          ;R&   S[R,          ;R&   S[R,          ;R&   S[R,          ;R&   S[P                  R,          ;R&   # )	r   Nread_timeout_secondssampling_callbackelicitation_callbacklist_roots_callbacklogging_callbackmessage_handlerclient_info)r   r   r   r   r   r   r   Implementation)r#   r$   s   "r%   r&   $ClientSessionParameters.__annotate__K   s      $d*1	 
 #T)0  )4/6  &,3  !4'.  '-4  %%,3 r(   r)   )r+   r,   r-   r.   r/   r>   r?   r@   rA   rB   rC   rD   r0   r1   r2   r3   s   @r%   r<   r<   K   s7     H-1,026/3*.04O/3K  r(   r<   c                     a  ] tR t^Xt o Rt ! R R]4      t]]]	P                  .]3,          tR,V 3R lR lltV 3R lR ltV 3R	 lR
 lt]V 3R lR l4       t]V 3R lR l4       t]V 3R lR l4       t]V 3R lR l4       t]R,RR/V 3R lR lll4       t]]! R4      RRRRRR/V 3R lR ll4       4       tR-RRRR/V 3R lR llltV 3R lR ltV 3R  lR! ltR.V 3R" lR# lltV 3R$ lR% ltV 3R& lR' ltV 3R( lR) ltV 3R* ltR+tV tR# )/ClientSessionGroupa]  Client for managing connections to multiple MCP servers.

This class is responsible for encapsulating management of server connections.
It aggregates tools, resources, and prompts from all connected servers.

For auxiliary handlers, such as resource subscription, this is delegated to
the client and can be accessed via the session.

Example Usage:
    name_fn = lambda name, server_info: f"{(server_info.name)}_{name}"
    async with ClientSessionGroup(component_name_hook=name_fn) as group:
        for server_param in server_params:
            await group.connect_to_server(server_param)
        ...

c                   Z   a  ] tR t^jt o Rt]! 4       t]! 4       t]! 4       tV 3R lt	Rt
V tR# )"ClientSessionGroup._ComponentNamesz*Used for reverse index to find components.c                n   < V ^8  d   Qh/ S[ S[,          ;R&   S[ S[,          ;R&   S[ S[,          ;R&   # )r   prompts	resourcestools)setr    )r#   r$   s   "r%   r&   /ClientSessionGroup._ComponentNames.__annotate__j   s8      S!  s8#	 
 3x r(   r)   N)r+   r,   r-   r.   r/   rO   rL   rM   rN   r0   r1   r2   r3   s   @r%   _ComponentNamesrJ   j   s$     8E!e	%  r(   rQ   Nc                Z   < V ^8  d   QhRS[ P                  R,          RS[R,          RR/# )r   
exit_stackNcomponent_name_hookreturn)
contextlibAsyncExitStack_ComponentNameHook)r#   r$   s   "r%   r&   ClientSessionGroup.__annotate__   s9     8 8--48 0$68 
	8r(   c                    / V n         / V n        / V n        / V n        / V n        Vf#   \
        P                  ! 4       V n        RV n        MWn        RV n        / V n	        W n
        R# )zInitializes the MCP client.NTF)_tools
_resources_prompts	_sessions_tool_to_sessionrV   rW   _exit_stack_owns_exit_stack_session_exit_stacks_component_name_hook)selfrS   rT   s   &&&r%   __init__ClientSessionGroup.__init__   sc      ")88:D$(D!)$)D!$&!$7!r(   c                    < V ^8  d   QhRS[ /# r   rU   )r
   )r#   r$   s   "r%   r&   rY      s      $ r(   c                z   "   V P                   '       d#   V P                  P                  4       G R j  xL
  V #  L5iN)ra   r`   
__aenter__rd   s   &r%   rk   ClientSessionGroup.__aenter__   s3        ""--/// 0s   0;9;c          	      z   < V ^8  d   QhRS[ S[,          R,          RS[R,          RS[R,          RS[R,          /# )r   	_exc_typeN_exc_val_exc_tbrU   )typeBaseExceptionr   r9   )r#   r$   s   "r%   r&   rY      sK     1 1&-1  $&1 %	1
 
1r(   c                  "   V P                   '       d#   V P                  P                  4       G Rj  xL
  \        P                  ! 4       ;_uu_4       GRj  xL
 pV P
                  P                  4        F  pVP                  VP                  4       K   	  RRR4      GRj  xL
  R#  Lx LT L
  + GRj  xL 
 '       g   i     R# ; i5i)z?Closes session exit stacks and main exit stack upon completion.N)ra   r`   acloseanyiocreate_task_grouprb   values
start_soon)rd   ro   rp   rq   tgrS   s   &&&&  r%   	__aexit__ClientSessionGroup.__aexit__   s         ""))+++ **,,,"77>>@
j//0 A -,, , -,,,s]   0CB+%CB-C=B1C$B/%C-C/C1C	7B:8
C	C		Cc                D   < V ^8  d   QhRS[ S[P                  ,          /# rh   )listmcpClientSession)r#   r$   s   "r%   r&   rY      s     + +$s001 +r(   c                H    \        V P                  P                  4       4      # )z+Returns the list of sessions being managed.)r~   r^   keysrl   s   &r%   sessionsClientSessionGroup.sessions   s     DNN'')**r(   c                J   < V ^8  d   QhRS[ S[S[P                  3,          /# rh   )r!   r    r   Prompt)r#   r$   s   "r%   r&   rY      s"      c5<</0 r(   c                    V P                   # )z8Returns the prompts as a dictionary of names to prompts.)r]   rl   s   &r%   rL   ClientSessionGroup.prompts   s     }}r(   c                J   < V ^8  d   QhRS[ S[S[P                  3,          /# rh   )r!   r    r   Resource)r#   r$   s   "r%   r&   rY      s"      4U^^ 34 r(   c                    V P                   # )z<Returns the resources as a dictionary of names to resources.)r\   rl   s   &r%   rM   ClientSessionGroup.resources   s     r(   c                J   < V ^8  d   QhRS[ S[S[P                  3,          /# rh   )r!   r    r   Tool)r#   r$   s   "r%   r&   rY      s!      tCO, r(   c                    V P                   # )z4Returns the tools as a dictionary of names to tools.)r[   rl   s   &r%   rN   ClientSessionGroup.tools   s     {{r(   metac                   < V ^8  d   QhRS[ RS[S[ S[3,          RS[R,          RS[R,          RS[S[ S[3,          R,          RS[P                  /# )r   name	argumentsr>   Nprogress_callbackr   rU   r    r!   r   r   r   r   CallToolResult)r#   r$   s   "r%   r&   rY      sl     # ## S># ($.	#
 '-# 38nt## 
		#r(   c                  "   R # 5irj   r)   )rd   r   r   r>   r   r   s   &&&&&$r%   	call_toolClientSessionGroup.call_tool   s
       #   z<The 'args' parameter is deprecated. Use 'arguments' instead.r>   r   c                   < V ^8  d   QhRS[ RS[S[ S[3,          RS[R,          RS[R,          RS[S[ S[3,          R,          RS[P                  /# )r   r   argsr>   Nr   r   rU   r   )r#   r$   s   "r%   r&   rY      sl     # ## 38n	#
 ($.# '-# 38nt## 
		#r(   c                  "   R # 5irj   r)   )rd   r   r   r>   r   r   s   &&$$$$r%   r   r      s
       #r   r   c                   < V ^8  d   QhRS[ RS[S[ S[3,          R,          RS[R,          RS[R,          RS[S[ S[3,          R,          RS[S[ S[3,          R,          RS[P                  /# )	r   r   r   Nr>   r   r   r   rU   r   )r#   r$   s   "r%   r&   rY      s     
 

 S>D(
 ($.	

 '-
 38nt#
 38nt#
 
		
r(   c                  "   V P                   V,          pV P                  V,          P                  pTP                  TVf   TMTVVVR7      G Rj  xL
 #  L5i)z-Executes a tool given its name and arguments.N)r>   r   r   )r_   rN   r   r   )	rd   r   r   r>   r   r   r   sessionsession_tool_names	   &&&&&$$  r%   r   r      sc      ''- JJt,11&&I4!5/ ' 
 
 	
 
s   AAAAc                8   < V ^8  d   QhRS[ P                  RR/# )r   r   rU   N)r   r   )r#   r$   s   "r%   r&   rY      s!     #2 #2C4E4E #2$ #2r(   c                  "   WP                   9   pWP                  9   pV'       g8   V'       g0   \        \        P                  ! \        P
                  RR7      4      hV'       d   V P                   P                  V4      pVP                   F"  pWPP                  9   g   K  V P                  V K$  	  VP                   F"  pWPP                  9   g   K  V P                  V K$  	  VP                   F?  pWPP                  9   d   V P                  V WPP                  9   g   K2  V P                  V KA  	  V'       d6   V P                  P                  V4      pVP                  4       G Rj  xL
  R# R#  L5i)z%Disconnects from a single MCP server.z8Provided session is not managed or already disconnected.codemessageN)r^   rb   r   r   	ErrorDataINVALID_PARAMSpoprL   r]   rM   r\   rN   r[   r_   ru   )rd   r   session_known_for_componentssession_known_for_stackcomponent_namesr   session_stack_to_closes   &&     r%   disconnect_from_server)ClientSessionGroup.disconnect_from_server   s/     (/..'@$")-F-F"F+4K--V  ("nn009O (//==(d+ 0 (11??*- 2 (--;;&D)000--d3	 . #%)%>%>%B%B7%K"(//111 #1s5   ,E56E5&7E5"-E5A
E5#E5<.E5*E3+	E5c                h   < V ^8  d   QhRS[ P                  RS[P                  RS[P                  /# )r   server_infor   rU   r   rE   r   r   )r#   r$   s   "r%   r&   rY     s7       //:=:K:K			r(   c                D   "   V P                  W4      G Rj  xL
  V#  L5iz Connects to a single MCP server.N)_aggregate_components)rd   r   r   s   &&&r%   connect_with_session'ClientSessionGroup.connect_with_session  s%      ((>>> 	?s     c                N   < V ^8  d   QhRS[ RS[R,          RS[P                  /# )r   server_paramssession_paramsNrU   )ServerParametersr<   r   r   )r#   r$   s   "r%   r&   rY     s9     E E'E 0$6E 
			Er(   c                   "   T P                  Y;'       g    \        4       4      G Rj  xL
 w  r4V P                  W44      G Rj  xL
 #  L L5ir   )_establish_sessionr<   r   )rd   r   r   r   r   s   &&&  r%   connect_to_server$ClientSessionGroup.connect_to_server  sF      &*%<%<]LwLw^u^w%xx..{DDD  yDs!   'AA	AAAAc                j   < V ^8  d   QhRS[ RS[RS[S[P                  S[P                  3,          /# )r   r   r   rU   )r   r<   tupler   rE   r   r   )r#   r$   s   "r%   r&   rY   $  sD     B B'B 0B 
u##S%6%66	7	Br(   c                  "   \         P                  ! 4       p \        V\        4      '       d4   \        P
                  ! V4      pVP                  V4      G Rj  xL
 w  rVEM\        V\        4      '       dT   \        VP                  VP                  VP                  VP                  R7      pVP                  V4      G Rj  xL
 w  rVM\        VP                  \        P                  ! VP                  P!                  4       VP                  P!                  4       R7      R7      pVP                  V4      G Rj  xL
  \#        VP                  VVP$                  R7      pVP                  V4      G Rj  xL
 w  rVpVP                  \        P&                  ! VVVP(                  VP*                  VP,                  VP.                  VP0                  VP2                  VP4                  R7	      4      G Rj  xL
 p	V	P7                  4       G Rj  xL
 p
W0P8                  V	&   V P:                  P                  V4      G Rj  xL
  V
P<                  V	3#  EL EL EL L Lf LP L!  \>         d    TPA                  4       G Rj  xL 
  h i ; i5i)z,Establish a client session to an MCP server.N)r   r   r   r   )read)r   r   )r   http_clientr8   )r>   r?   r@   rA   rB   rC   rD   )!rV   rW   
isinstancer   r   stdio_cliententer_async_contextr   r   r   r   r   r   r   httpxTimeouttotal_secondsr   r8   r   r>   r?   r@   rA   rB   rC   rD   
initializerb   r`   
serverInfo	Exceptionru   )rd   r   r   session_stackclientr   writehttpx_client_r   results   &&&        r%   r   %ClientSessionGroup._establish_session$  s=     #113:	-)>??))-8$1$E$Ef$MMeM+>??#%)))11)11%2%C%C	 %2$E$Ef$MMe5)11!MM%--;;=*;;IIK  $77EEE/%)) ,'4'G'G
 (5'H'H'P!PQ)==!!)7)L)L&4&F&F)7)L)L(6(J(J%3%D%D$2$B$B . : :
 G #--//F 2?%%g. ""66}EEE$$g--c N N F "Q 0 F  	  &&(((		s   JA I# IA)I# IA8I# <I=:I# 7I8A>I# 6I7I# I0I# ?I! I# JI# I# I# I# I# I# !I# #J	JJ		Jc                R   < V ^8  d   QhRS[ P                  RS[P                  RR/# )r   r   r   rU   Nr   )r#   r$   s   "r%   r&   rY   h  s5     R; R;u7K7K R;VYVgVg R;lp R;r(   c                  "   V P                  4       p/ p/ p/ p/ p VP                  4       G Rj  xL
 P                  pV F>  p	V P                  V	P                  V4      p
WV
&   VP                  P                  V
4       K@  	   VP                  4       G Rj  xL
 P                  pV F>  pV P                  VP                  V4      p
WV
&   VP                  P                  V
4       K@  	   VP                  4       G Rj  xL
 P                  pV FB  pV P                  VP                  V4      p
WV
&   W'V
&   VP                  P                  V
4       KD  	  \        WEV34      '       g   V P                  V VP                  4       V P                   P                  4       ,          pV'       d3   \        \"        P$                  ! \"        P&                  V R2R7      4      hVP                  4       V P(                  P                  4       ,          pV'       d3   \        \"        P$                  ! \"        P&                  V R2R7      4      hVP                  4       V P*                  P                  4       ,          pV'       d3   \        \"        P$                  ! \"        P&                  V R2R7      4      hW0P,                  V&   V P                   P/                  V4       V P(                  P/                  V4       V P*                  P/                  V4       V P0                  P/                  V4       R#  EL  \         d%   p\        P                  ! RT 24        Rp?ELRp?ii ; i EL  \         d%   p\        P                  ! RT 24        Rp?ELRp?ii ; i EL  \         d%   p\        P                  ! RT 24        Rp?ELnRp?ii ; i5i)	z>Aggregates prompts, resources, and tools from a given session.NzCould not fetch prompts: zCould not fetch resources: zCould not fetch tools: z  already exist in group prompts.r   z" already exist in group resources.z already exist in group tools.)rQ   list_promptsrL   _component_namer   addr   loggingwarninglist_resourcesrM   
list_toolsrN   anyrb   r   r]   r   r   r   r\   r[   r^   updater_   )rd   r   r   r   prompts_tempresources_temp
tools_temptool_to_session_temprL   promptr   errrM   resourcerN   toolmatching_promptsmatching_resourcesmatching_toolss   &&&                r%   r   (ClientSessionGroup._aggregate_componentsh  s;     ..0 1346,.
=?	?$1133<<G!++FKKE%+T"''++D1 "	A&5577BBI%++HMM;G'/t$))--d3 &	="--//66E++DII{C#'4 -4T*%%))$/	  L*=>>))'2 (,,.1C1C1EE--/00PQ  ,002T__5I5I5KK--122TU  $*T[[-=-=-??---..LM  #2w\*~.:&$$%9:C 4
  	?OO7u=>>	?
 8
  	AOO9#?@@	A
 0  	=OO5cU;<<	=s   O
L. L+AL. M# M AM# *N =N>AN AO
+A(O
A(O
=B.O
+L. .M9MO
MO
 M# #N.NO
NO
N O#O<O
OO
c                @   < V ^8  d   QhRS[ RS[P                  RS[ /# )r   r   r   rU   )r    r   rE   )r#   r$   s   "r%   r&   rY     s(      C e6J6J s r(   c                L    V P                   '       d   V P                  W4      # V# rj   )rc   )rd   r   r   s   &&&r%   r   "ClientSessionGroup._component_name  s$    $$$,,T??r(   c                  < V ^8  d   Qh/ S[ S[S[P                  3,          ;R&   S[ S[S[P                  3,          ;R&   S[ S[S[P
                  3,          ;R&   S[ S[P                  S[3,          ;R&   S[ S[S[P                  3,          ;R&   S[	P                  ;R&   S[ S[P                  S[	P                  3,          ;R&   S[;R&   S[R	,          ;R
&   # )r   r]   r\   r[   r^   r_   r`   rb   rX   Nrc   )r!   r    r   r   r   r   r   r   rQ   rV   rW   r   rX   )r#   r$   s   "r%   r&   rY   X   s     4 3$%%5 6 S%..())7 8 ejj!!9 > C%%677? @ 3 1 1122A B ***C D s00*2K2KKLLE N "NO P -t33Q r(   )	rc   r`   ra   r]   r\   rb   r^   r_   r[   )NN)NNNrj   ) r+   r,   r-   r.   r/   r	   rQ   r   r    r   rE   rX   re   rk   r{   propertyr   rL   rM   rN   r   r   r   r   r   r   r   r   r   r0   r1   r2   r3   s   @r%   rH   rH   X   su    " )  * %-c53G3G-H#-M$N8 8, 1 1" + +       # '+# # # NO#
 26# 15# '+# # P #
 '+
 '+
 
*#2 #2J E EB BHR; R;h I  r(   rH   c                @    V ^8  d   Qh/ ^ \         9   d
   \        ;R&   # )r   r   )__conditional_annotations__r   )r#   s   "r%   r&   r&      s!      J e d) dKr(   )0r   r/   rV   r   collections.abcr   dataclassesr   datetimer   r   r   typingr   r   r   rv   r   pydanticr	   typing_extensionsr
   r   r   mcp.client.sessionr   r   r   r   r   mcp.client.sser   mcp.client.stdior   mcp.client.streamable_httpr   mcp.shared._httpx_utilsr   mcp.shared.exceptionsr   mcp.shared.sessionr   r   r5   r   r<   rH   r&   )r   s   @r%   <module>r     s      $ !   + +    . 
  g g % 2 = : * *%) % $y $& 46IILdd  d
 	4 	4 	4g gr(   