+
    win                     n   R t ^ RIt^ RIt^ RIHtHtHt ^ RIHt ^ RIH	t	 ^ RI
Ht ^ RIHtHt ^ RIHt ^ RIt^ RIt^ RIHt ^ R	IHtHt ^ R
IHtHtHt ^ RIHt ^ RIHtH t  ^ RI!H"t"H#t# ^ RI$H%t%H&t&H't'H(t(H)t)H*t*H+t+H,t, ]PZ                  ! ].4      t/]#]0,          t1]]1,          t2]]#,          t3]. ]4R,          3,          t5Rt6Rt7Rt8Rt9^t:Rt;Rt<Rt=Rt>]?! 4       t@ ! R R]04      tA ! R R]A4      tB]	 ! R R4      4       tC ! R R4      tD]RRR R!/R" R# ll4       tE]]! R$4      R^R'R!] R3R% R& ll4       4       tFR# )(z
StreamableHTTP Client Transport Module

This module implements the StreamableHTTP transport for MCP clients,
providing support for HTTP POST requests with optional SSE streaming responses
and session management.
N)AsyncGenerator	AwaitableCallable)asynccontextmanager)	dataclass)	timedelta)Anyoverload)warn)	TaskGroup)MemoryObjectReceiveStreamMemoryObjectSendStream)EventSourceServerSentEventaconnect_sse)
deprecated)McpHttpClientFactorycreate_mcp_http_client)ClientMessageMetadataSessionMessage)	ErrorDataInitializeResultJSONRPCErrorJSONRPCMessageJSONRPCNotificationJSONRPCRequestJSONRPCResponse	RequestIdzmcp-session-idzmcp-protocol-versionzlast-event-idi  zcontent-typeacceptzapplication/jsonztext/event-streamc                       ] tR t^CtRtRtR# )StreamableHTTPErrorz3Base exception for StreamableHTTP transport errors. N__name__
__module____qualname____firstlineno____doc____static_attributes__r!       N/home/ubuntu/.local/lib/python3.14/site-packages/mcp/client/streamable_http.pyr    r    C   s    =r)   r    c                       ] tR t^GtRtRtR# )ResumptionErrorz*Raised when resumption request is invalid.r!   Nr"   r!   r)   r*   r,   r,   G   s    4r)   r,   c                   8   a  ] tR t^Kt o RtRtRtV 3R ltRtV t	R# )RequestContextz Context for a request operation.Nc                   < V ^8  d   Qh/ S[ P                  ;R&   S[R,          ;R&   S[;R&   S[R,          ;R&   S[;R&   S[S[S[3,          R,          ;R&   S[R,          ;R&   # )	   clientN
session_idsession_messagemetadataread_stream_writerheaderssse_read_timeout)httpxAsyncClientstrr   r   StreamWriterdictfloat)format__classdict__s   "r*   __annotate__RequestContext.__annotate__K   s      	 
 d
  $#  $d**  %$  #s(^d")  dl) r)   r!   )
r#   r$   r%   r&   r'   r6   r7   __annotate_func__r(   __classdictcell__r?   s   @r*   r.   r.   K   s     * &*G%)  r)   r.   c                     a  ] tR t^Xt o Rt]V 3R lR l4       t]]! R4      R-V 3R lR ll4       4       t]]]]3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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R/V 3R lR lltR/V 3R lR lltR0V 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R) ltV 3R* lR+ ltR,tV tR# )1StreamableHTTPTransportz/StreamableHTTP client transport implementation.c                $   < V ^8  d   QhRS[ RR/# )r0   urlreturnNr:   )r>   r?   s   "r*   r@   $StreamableHTTPTransport.__annotate__\   s    --C-D-r)   c                    R # Nr!   )selfrH   s   &&r*   __init__ StreamableHTTPTransport.__init__[   s    *-r)   zyParameters headers, timeout, sse_read_timeout, and auth are deprecated. Configure these on the httpx.AsyncClient instead.Nc                   < V ^8  d   QhRS[ RS[S[ S[ 3,          R,          RS[S[,          RS[S[,          RS[P
                  R,          RR/# )r0   rH   r6   Ntimeoutr7   authrI   )r:   r<   r=   r   r8   Auth)r>   r?   s   "r*   r@   rK   c   sg       c3h$& "	
  )+ jj4 
r)   c                    R # rM   r!   )rN   rH   r6   rR   r7   rS   s   &&&&&&r*   rO   rP   ^   s     r)   c                <   < V ^8  d   QhRS[ RS[RS[RS[RS[RR/# )r0   rH   r6   rR   r7   rS   rI   N)r:   r   )r>   r?   s   "r*   r@   rK   l   sG     &% &%&% &% 	&%
 &% &% 
&%r)   c                d   . pV\         Jd   VP                  R4       V\         Jd   VP                  R4       V\         Jd   VP                  R4       V\         Jd   VP                  R4       V'       d&   \        RRP                  V4       R2\        ^R7       Wn        R	V n        R	V n        R	# )
a  Initialize the StreamableHTTP transport.

Args:
    url: The endpoint URL.
    headers: Optional headers to include in requests.
    timeout: HTTP timeout for regular operations.
    sse_read_timeout: Timeout for SSE read operations.
    auth: Optional HTTPX authentication handler.
r6   rR   r7   rS   zParameters , zV are deprecated and will be ignored. Configure these on the httpx.AsyncClient instead.)
stacklevelN)_UNSETappendr
   joinDeprecationWarningrH   r2   protocol_version)rN   rH   r6   rR   r7   rS   deprecated_paramss   &&&&&& r*   rO   rP   l   s    $ (*& $$Y/& $$Y/6)$$%78v$$V,dii(9:; <D D"	  $r)   c                6   < V ^8  d   QhRS[ S[S[3,          /# )r0   rI   )r<   r:   )r>   r?   s   "r*   r@   rK      s      $sCx. r)   c                    / p\          R\         2V\        &   \         V\        &   V P                  '       d   V P                  V\
        &   V P                  '       d   V P                  V\        &   V# )zBuild MCP-specific request headers.

These headers will be merged with the httpx.AsyncClient's default headers,
with these MCP-specific headers taking precedence.
rX   )JSONSSEACCEPTCONTENT_TYPEr2   MCP_SESSION_IDr^   MCP_PROTOCOL_VERSION)rN   r6   s   & r*   _prepare_headers(StreamableHTTPTransport._prepare_headers   s`     #%!F"SE* $???&*ooGN#   ,0,A,AG()r)   c                &   < V ^8  d   QhRS[ RS[/# r0   messagerI   r   bool)r>   r?   s   "r*   r@   rK      s     ` `. `T `r)   c                x    \        VP                  \        4      ;'       d    VP                  P                  R8H  # )z2Check if the message is an initialization request.
initialize)
isinstancerootr   methodrN   rl   s   &&r*   _is_initialization_request2StreamableHTTPTransport._is_initialization_request   s+    ',,7__GLL<O<OS_<__r)   c                &   < V ^8  d   QhRS[ RS[/# rk   rm   )r>   r?   s   "r*   r@   rK      s     t tN tt tr)   c                x    \        VP                  \        4      ;'       d    VP                  P                  R8H  # )z4Check if the message is an initialized notification.znotifications/initialized)rq   rr   r   rs   rt   s   &&r*   _is_initialized_notification4StreamableHTTPTransport._is_initialized_notification   s,    ',,(;<ssATATXsAssr)   c                8   < V ^8  d   QhRS[ P                  RR/# )r0   responserI   N)r8   Response)r>   r?   s   "r*   r@   rK      s'     C C..C 
Cr)   c                    VP                   P                  \        4      pV'       d+   W n        \        P                  RV P                   24       R# R# )z3Extract and store session ID from response headers.zReceived session ID: N)r6   getrf   r2   loggerinfo)rN   r|   new_session_ids   && r*   '_maybe_extract_session_id_from_response?StreamableHTTPTransport._maybe_extract_session_id_from_response   s@    
 "))--n=,OKK//@AB r)   c                $   < V ^8  d   QhRS[ RR/# )r0   rl   rI   N)r   )r>   r?   s   "r*   r@   rK      s#     E EE 
Er)   c                   \        VP                  \        4      '       d   VP                  P                  '       dj    \        P
                  ! VP                  P                  4      p\        VP                  4      V n        \        P                  RV P                   24       R# R# R#   \         dP   p\        P                  RT 24       \        P                  RTP                  P                   24        Rp?R# Rp?ii ; i)z>Extract protocol version from initialization response message.zNegotiated protocol version: z=Failed to parse initialization response as InitializeResult: zRaw result: N)rq   rr   r   resultr   model_validater:   protocolVersionr^   r   r   	Exceptionwarning)rN   rl   init_resultexcs   &&  r*   ,_maybe_extract_protocol_version_from_messageDStreamableHTTPTransport._maybe_extract_protocol_version_from_message   s    
 gllO449L9L9L	E.==gll>Q>QR(+K,G,G(H%;D<Q<Q;RST :M4  ESTWSXY gll.A.A-BCDD	Es   A&B* *D5AC??Dc                   < V ^8  d   QhRS[ RS[RS[R,          RS[S[.S[R,          3,          R,          RS[RS[/# )r0   sser5   original_request_idNresumption_callbackis_initializationrI   )r   r;   r   r   r:   r   rn   )r>   r?   s   "r*   r@   rK      sa     - -- )- '-	-
 &seYt_&<=D-  - 
-r)   c                  "   VP                   R8X  EdW   VP                  '       g7   VP                  '       d#   V'       d   V! VP                  4      G Rj  xL
  R#  \        P                  ! VP                  4      p\
        P                  RV 24       V'       d   V P                  V4       Ve<   \        VP                  \        \        ,          4      '       d   W6P                  n        \        V4      pVP                  V4      G Rj  xL
  VP                  '       d#   V'       d   V! VP                  4      G Rj  xL
  \        VP                  \        \        ,          4      # \
        P!                  RVP                    24       R#  EL9 L LR  \         d;   p\
        P                  R4       TP                  T4      G Rj  xL 
   Rp?R# Rp?ii ; i5i)z@Handle an SSE event, returning True if the response is complete.rl   NFzSSE message: zError parsing SSE messagezUnknown SSE event: )eventdataidr   model_validate_jsonr   debugr   rq   rr   r   r   r   sendr   	exceptionr   )	rN   r   r5   r   r   r   rl   r3   r   s	   &&&&&&   r*   _handle_sse_event)StreamableHTTPTransport._handle_sse_event   s]     99	!8886661-cff555(<<SXXF}WI67 %EEgN '2z',,P_bnPn7o7o&9LLO"0"9(--o>>> 6661-cff555 "',,,0NOO NN0<=? 6 ? 6    !<=(--c222s   $GGGGFG?F A/F FF $F ,F F(F *&GF F G!)G
GGGGGc                >   < V ^8  d   QhRS[ P                  RS[RR/# )r0   r1   r5   rI   N)r8   r9   r;   )r>   r?   s   "r*   r@   rK      s/     41 41!!41 )41 
	41r)   c                  "   RpRp^ pV\         8  d    V P                  '       g   R# V P                  4       pV'       d	   W6\        &   \	        VRV P
                  VR7      ;_uu_4       GRj  xL
 pVP                  P                  4        \        P                  R4       VP                  4         Rj  xL
  pVP                  '       d   VP                  pVP                  e   VP                  pV P                  W4      G Rj  xL
  K]  R#  L L\ LD_^ pRRR4      GRj  xL 
  MU  + GRj  xL 
 '       g   i     M<; i  \         d,   p	\        P                  RT	 24       T^,          p Rp	?	MRp	?	ii ; iT\         8  d    \        P                  R\          R24       R# Te   TM\        p
\        P!                  RT
 R	24       \"        P$                  ! T
R
,          4      G Rj  xL 
  EK  5i)zDHandle GET stream for server-initiated messages with auto-reconnect.NGETr6   zGET SSE connection establishedzGET stream error: z&GET stream max reconnection attempts (
) exceededz)GET stream disconnected, reconnecting in zms...     @@)MAX_RECONNECTION_ATTEMPTSr2   rh   LAST_EVENT_IDr   rH   r|   raise_for_statusr   r   	aiter_sser   retryr   r   DEFAULT_RECONNECTION_DELAY_MSr   anyiosleep)rN   r1   r5   last_event_idretry_interval_msattemptr6   event_sourcer   r   delay_mss   &&&        r*   handle_get_stream)StreamableHTTPTransport.handle_get_stream   s     %)(,11 //1 -:M*'HH#	  
 " ))::<LL!AB%1%;%;%= N Nc666,/FFM99003		-"44SMMM5 2N N &>  G)     ,  1#781 33EF_E``jkl ->,I(OlHKKCH:UST++h/000s   G9E G9E -E .D/E 2A D,2D6D
7D:AD,D
	D,G9E DD,D,E %D(&E +G9,E	2D53
E	>E	 E G9E E>!E94G99E>>A2G90G31G9c                $   < V ^8  d   QhRS[ RR/# r0   ctxrI   Nr.   )r>   r?   s   "r*   r@   rK   -  s      N t r)   c           	       "   V P                  4       pVP                  '       d;   VP                  P                  '       d   VP                  P                  V\        &   M\	        R4      hRp\        VP                  P                  P                  \        4      '       d+   VP                  P                  P                  P                  p\        VP                  RV P                  VR7      ;_uu_4       GRj  xL
 pVP                  P                  4        \         P#                  R4       VP%                  4         Rj  xL
  pT P'                  TVP(                  TVP                  '       d   VP                  P*                  MR4      G Rj  xL
 pV'       g   Kb  VP                  P-                  4       G Rj  xL
   RRR4      GRj  xL
  R#  L L LG LDL L  + GRj  xL 
 '       g   i     R# ; i5i)z/Handle a resumption request using GET with SSE.z.Resumption request requires a resumption tokenNr   r   z)Resumption GET SSE connection established)rh   r4   resumption_tokenr   r,   rq   r3   rl   rr   r   r   r   r1   rH   r|   r   r   r   r   r   r5   on_resumption_token_updateaclose)rN   r   r6   r   r   r   is_completes   &&     r*   _handle_resumption_request2StreamableHTTPTransport._handle_resumption_request-  s    '')<<<CLL999%(\\%B%BGM"!"RSS #c))1166GG"%"5"5"="="B"B"E"EJJHH	
 
 

 !!224LLDE)335 	 	c$($:$:**'?B|||CLL;;QU	%  ;&//66888%
 
 
	 9 6
 
 
 
s   >G=B8G=9G:G==A G =GG
GAG G
	G G <G
=G G=GG=GG G G G= G:	&G)'
G:	2G:	4	G=c                $   < V ^8  d   QhRS[ RR/# r   r   )r>   r?   s   "r*   r@   rK   N  s     ( (n ( (r)   c           
     "  "   V P                  4       pVP                  P                  pV P                  V4      pVP                  P                  RV P                  VP                  RRRR7      VR7      ;_uu_4       GRj  xL
 pVP                  ^8X  d)   \        P                  R4        RRR4      GRj  xL
  R# VP                  R8X  dl   \        VP                  \        4      '       d9   V P                  VP                  VP                  P                   4      G Rj  xL
   RRR4      GRj  xL
  R# VP#                  4        V'       d   V P%                  V4       \        VP                  \        4      '       d   VP&                  P)                  \*        R	4      P-                  4       pVP/                  \0        4      '       d&   V P3                  WQP                  V4      G Rj  xL
  MZVP/                  \4        4      '       d   V P7                  WQV4      G Rj  xL
  M$V P9                  VVP                  4      G Rj  xL
  RRR4      GRj  xL
  R#  EL EL ELA EL3 L} LI L& L  + GRj  xL 
 '       g   i     R# ; i5i)
z/Handle a POST request with response processing.POSTTjson)by_aliasmodeexclude_none)r   r6   NzReceived 202 Acceptedi   )rh   r3   rl   ru   r1   streamrH   
model_dumpstatus_coder   r   rq   rr   r   _send_session_terminated_errorr5   r   r   r   r6   r   re   lower
startswithrb   _handle_json_responserc   _handle_sse_response_handle_unexpected_content_type)rN   r   r6   rl   r   r|   content_types   &&     r*   _handle_post_request,StreamableHTTPTransport._handle_post_requestN  s    '')%%-- ;;GD::$$HH##TT#R	 % 
 
 

 ##s*45
 
 
 ##s*gllN;;==..   !
 
 
$ %%' <<XF ',,77'//33L"EKKM**40044X?U?UWhiii!,,S1133HCTUUU>>$..  ?
 
 

6 jU?
 
 
 
s   A?JIJ(I2-J8I!9J?A$I2#I$$I2)J4I'5J;I2A9I2I2,I*-I2I2"I,#$I2I.I2JI0J!J$I2'J*I2,I2.I20J2J	8I;9
J	J		Jc                D   < V ^8  d   QhRS[ P                  RS[RS[RR/# )r0   r|   r5   r   rI   N)r8   r}   r;   rn   )r>   r?   s   "r*   r@   rK   x  s7     / /../ )/  	/
 
/r)   c                ~  "    VP                  4       G Rj  xL
 p\        P                  ! V4      pV'       d   V P                  V4       \	        V4      pVP                  V4      G Rj  xL
  R#  LY L  \         d;   p\        P                  R4       TP                  T4      G Rj  xL 
   Rp?R# Rp?ii ; i5i)z%Handle JSON response from the server.NzError parsing JSON response)	areadr   r   r   r   r   r   r   r   )rN   r|   r5   r   contentrl   r3   r   s   &&&&    r*   r   -StreamableHTTPTransport._handle_json_responsex  s     	/$NN,,G$88AG !AA'J,W5O$))/::: - ; 	/:;$))#...	/sa   B=A5 A1!A5 0A5 *A3+A5 /B=1A5 3A5 5B: )B5)B,*B5/B=5B::B=c                D   < V ^8  d   QhRS[ P                  RS[RS[RR/# )r0   r|   r   r   rI   N)r8   r}   r.   rn   )r>   r?   s   "r*   r@   rK     s=     &S &S..&S &S  	&S
 
&Sr)   c                  "   RpRp \        V4      pVP                  4         Rj  xL
  pVP                  '       d   VP                  pVP                  e   VP                  pT P	                  TVP
                  VP                  '       d   VP                  P                  MRVR7      G Rj  xL
 pV'       g   K  VP                  4       G Rj  xL
   R#  L L+ LDM0  \         d#   p	\        P                  RT	 24        Rp	?	MRp	?	ii ; iTe3   \        P                  R4       T P                  Y$T4      G Rj  xL 
  R# R# 5i)z$Handle SSE response from the server.N)r   r   zSSE stream ended: (SSE stream disconnected, reconnecting...)r   r   r   r   r   r5   r4   r   r   r   r   r   r   _handle_reconnection)
rN   r|   r   r   r   r   r   r   r   es
   &&&&      r*   r   ,StreamableHTTPTransport._handle_sse_response  s-     %)(,	3&x0L)335  c666$'FFM 99((+		%$($:$:**TWT`T`T`)P)Pfj&7	 %; %  ;"//+++' ,% 6(  	3LL-aS122	3 $KKBC++C@QRRR %s   E	C! CCCA"C! !C! /C0	C! >C! CC! E	CC! C! C!  E	!D,D	E		D1E	?E 	E	c          
      D   < V ^8  d   QhRS[ RS[RS[R,          RS[RR/# )r0   r   r   r   Nr   rI   )r.   r:   int)r>   r?   s   "r*   r@   rK     sH     =` =`=` =` :	=`
 =` 
=`r)   c           	       "   V\         8  d    \        P                  R\          R24       R# Ve   TM\        p\        P
                  ! VR,          4      G Rj  xL
  V P                  4       pW&\        &   Rp\        VP                  P                  P                  \        4      '       d+   VP                  P                  P                  P                  p \        VP                  RV P                   VR7      ;_uu_4       GRj  xL
 pVP"                  P%                  4        \        P'                  R4       Tp	Tp
VP)                  4         Rj  xL
  pVP                  '       d   VP                  p	VP*                  e   VP*                  p
T P-                  TVP.                  TVP0                  '       d   VP0                  P2                  MR4      G Rj  xL
 pV'       g   K  VP"                  P5                  4       G Rj  xL
   RRR4      GRj  xL
  R#  EL EL L LK L! LD\        P'                  R4       T P7                  YT
^ 4      G Rj  xL 
  RRR4      GRj  xL 
  R#   + GRj  xL 
 '       g   i     R# ; i  \8         dF   p\        P                  R	T 24       T P7                  YY4^,           4      G Rj  xL 
   Rp?R# Rp?ii ; i5i)
zFReconnect with Last-Event-ID to resume stream after server disconnect.zMax reconnection attempts (r   Nr   r   r   zReconnected to SSE streamr   zReconnection failed: )r   r   r   r   r   r   rh   r   rq   r3   rl   rr   r   r   r   r1   rH   r|   r   r   r   r   r   r5   r4   r   r   r   r   )rN   r   r   r   r   r   r6   r   r   reconnect_last_event_idreconnect_retry_msr   r   r   s   &&&&&         r*   r   ,StreamableHTTPTransport._handle_reconnection  sg     //LL67P6QQ[\] ):(E$Khkk(V+,,,'')!. #c))1166GG"%"5"5"="="B"B"E"E$	`#

	  
 %%66878 0='%6"!-!7!7!9  #vvv25&&/yy,-0YY*(,(>(>..+CF<<<??UY	) #K #{*33::<<<7   	-# =5 ":" FG//N`bcddd?     @  	`LL045++C@Q]^S^___	`s  AK HA<K /J H J 
AI0H+H#
H+A#I0:I0H%
	I0(I0H'
I0J H)J K  J #H+%I0'I0)J +,I0II0J (I+)J .K 0J
	6I97
J
	J
	J K 
J K4KKKK KK c                *   < V ^8  d   QhRS[ RS[RR/# )r0   r   r5   rI   N)r:   r;   )r>   r?   s   "r*   r@   rK     s)     = == )= 
	=r)   c                   "   RV 2p\         P                  V4       VP                  \        V4      4      G Rj  xL
  R#  L5i)z+Handle unexpected content type in response.zUnexpected content type: N)r   errorr   
ValueError)rN   r   r5   	error_msgs   &&& r*   r   7StreamableHTTPTransport._handle_unexpected_content_type  s8      0~>	Y %%j&;<<<s   8AAAc                *   < V ^8  d   QhRS[ RS[RR/# )r0   r5   
request_idrI   N)r;   r   )r>   r?   s   "r*   r@   rK     s)     7 7(7 7 
	7r)   c           	        "   \        RV\        RRR7      R7      p\        \        V4      4      pVP	                  V4      G Rj  xL
  R#  L5i)z)Send a session terminated error response.z2.0iX  zSession terminated)coderl   )jsonrpcr   r   N)r   r   r   r   r   )rN   r5   r   jsonrpc_errorr3   s   &&&  r*   r   6StreamableHTTPTransport._send_session_terminated_error  sG      %0DE

 ))FG %%o666s   AAAAc                x   < V ^8  d   QhRS[ P                  RS[RS[RS[S[,          RS[. R3,          RS[RR/# )	r0   r1   write_stream_readerr5   write_streamstart_get_streamNtgrI   )r8   r9   StreamReaderr;   r   r   r   r   )r>   r?   s   "r*   r@   rK     se     5( 5(!!5( *5( )	5(
 -^<5( #2t8,5( 5( 
5(r)   c                  a aa"    T;_uu_4       GRj  xL
  V  Rj  xL
  pVP                   p\        VP                  \        4      '       d   VP                  MRp	\	        T	;'       d    V	P
                  4      o\        P                  RV 24       S P                  V4      '       d   V! 4        \        VS P                  VV	VR7      oVVV 3R lp
\        VP                  \        4      '       d   VP                  V
4       K  V
! 4       G Rj  xL
  K   EL L LEDRRR4      GRj  xL 
  MB  + GRj  xL 
 '       g   i     M); i  \         d    \        P                  R4        Mi ; iTP!                  4       G Rj  xL 
  TP!                  4       G Rj  xL 
  R#   TP!                  4       G Rj  xL 
  TP!                  4       G Rj  xL 
  i ; i5i)z&Handle writing requests to the server.NzSending client message: )r1   r2   r3   r4   r5   c                     <"   S'       d   SP                  S 4      G R j  xL
  R # SP                  S 4      G R j  xL
  R #  L! L5irM   )r   r   )r   is_resumptionrN   s   r*   handle_request_asyncAStreamableHTTPTransport.post_writer.<locals>.handle_request_async1  s:     ("&"A"A#"FFF"&";";C"@@@ G@s   AAAAAAzError in post_writer)rl   rq   r4   r   rn   r   r   r   ry   r.   r2   rr   r   
start_soonr   r   r   )rN   r1   r   r5   r   r   r   r3   rl   r4   r   r   r   s   f&&&&&&    @@r*   post_writer#StreamableHTTPTransport.post_writer  s    +	(***-@ #5 #5/-55G &o&>&>@UVV (00!  %))O)Oh6O6O$PMLL#;G9!EF 88AA(*(%#'??(7!)+=CA "',,??&:;2444I +#5F 5G .A +*****L  	534	5 %++---%%''' %++---%%'''s   G#E DE D6D"D
D"A	D6.?D6.A&D6D 
D6E D" D6"D6$E /D20E 5F, 6E	<D?=
E	E	
E F, E  E52F, 4E55F, 8G#FG#$F'%G#,G  GG GG  G#c                8   < V ^8  d   QhRS[ P                  RR/# )r0   r1   rI   N)r8   r9   )r>   r?   s   "r*   r@   rK   C  s%     A Ae.?.? AD Ar)   c                  "   V P                   '       g   R#  V P                  4       pVP                  V P                  VR7      G Rj  xL
 pVP                  R8X  d   \
        P                  R4       R# VP                  R9  d%   \
        P                  RVP                   24       R# R#  Lc  \         d$   p\
        P                  RT 24        Rp?R# Rp?ii ; i5i)z2Terminate the session by sending a DELETE request.Nr   i  z)Server does not allow session terminationzSession termination failed: )      )	r2   rh   deleterH   r   r   r   r   r   )rN   r1   r6   r|   r   s   &&   r*   terminate_session)StreamableHTTPTransport.terminate_sessionC  s     		A++-G#]]488W]EEH##s*HI%%Z7!=h>R>R=STU 8	 F  	ANN9#?@@	AsL   C0B. B,	*B. 3C53B. (C,B. .C9CCCCc                .   < V ^8  d   QhRS[ R,          /# r0   rI   NrJ   )r>   r?   s   "r*   r@   rK   S  s      d
 r)   c                    V P                   # )zGet the current session ID.)r2   )rN   s   &r*   get_session_id&StreamableHTTPTransport.get_session_idS  s    r)   )r^   r2   rH   )N   ,  N)NNF)F)N    )r#   r$   r%   r&   r'   r	   rO   r   rZ   rh   ru   ry   r   r   r   r   r   r   r   r   r   r   r   r   r  r	  r(   rC   rD   s   @r*   rF   rF   X   s    9- -	< 	 
  &&% &%P "` `t tC CE E"- -^41 41l B( (T/ /*&S &SP=` =`~= =7 75( 5(nA A  r)   rF   http_clientterminate_on_closeTc                    V ^8  d   QhR\         R\        P                  R,          R\        R\        \
        \        \        \        ,          ,          \        \        ,          \        3,          R3,          /# )r0   rH   r  Nr  rI   )r:   r8   r9   rn   r   tupler   r   r   r   GetSessionIdCallback)r>   s   "r*   r@   r@   Y  sw     P( P(	P( ""T)P( 	P(
 	!.9"<=~.	
 	
P(r)   c              h  a	a
aa"   \         P                  \        \        ,          ,          ! ^ 4      w  o
p\         P                  \        ,          ! ^ 4      w  rEVRJpVo	S	f   \	        4       o	\        V 4      o\         P                  ! 4       ;_uu_4       GRj  xL
 o \        P                  RV  24       \        P                  ! 4       ;_uu_4       GRj  xL
 pV'       g   VP                  S	4      G Rj  xL
  R V	V
VV3R llpSP                  SP                  S	VS
VVS4        VVSP                  35x  SP                  '       d"   V'       d   SP!                  S	4      G Rj  xL
  SP"                  P%                  4        RRR4      GRj  xL
  S
P'                  4       G Rj  xL
  VP'                  4       G Rj  xL
  RRR4      GRj  xL
  R#  ELD EL L Lx  SP                  '       d#   T'       d   SP!                  S	4      G Rj  xL 
  SP"                  P%                  4        i ; i L  + GRj  xL 
 '       g   i     L; i L L  S
P'                  4       G Rj  xL 
  TP'                  4       G Rj  xL 
  i ; i L  + GRj  xL 
 '       g   i     R# ; i5i)a  
Client transport for StreamableHTTP.

Args:
    url: The MCP server endpoint URL.
    http_client: Optional pre-configured httpx.AsyncClient. If None, a default
        client with recommended MCP timeouts will be created. To configure headers,
        authentication, or other HTTP settings, create an httpx.AsyncClient and pass it here.
    terminate_on_close: If True, send a DELETE request to terminate the session
        when the context exits.

Yields:
    Tuple containing:
        - read_stream: Stream for reading messages from the server
        - write_stream: Stream for sending messages to the server
        - get_session_id_callback: Function to retrieve the current session ID

Example:
    See examples/snippets/clients/ for usage patterns.
Nz'Connecting to StreamableHTTP endpoint: c                    V ^8  d   QhRR/# r  r!   )r>   s   "r*   r@   ,streamable_http_client.<locals>.__annotate__  s     [ [$ [r)   c                  B   < SP                  SP                  S S4       R # rM   )r   r   )r1   r5   r   	transports   r*   r   0streamable_http_client.<locals>.start_get_stream  s    MM)"="=vGYZr)   )r   create_memory_object_streamr   r   r   rF   create_task_groupr   r   
contextlibAsyncExitStackenter_async_contextr   r   r	  r2   r  cancel_scopecancelr   )rH   r  r  read_streamr   r   client_providedstackr   r1   r5   r   r  s   &$$      @@@@r*   streamable_http_clientr#  X  s    D ',&G&GYbHb&cde&f#(-(I(I.(YZ[(\%L "-OF~')',I&&(((B!	(LLB3%HI!00222e&33F;;;[ [ ))'& $	-#$!00  !+++0B'99&AAAOO**,7 32: %++---%%'''E )(( 3 <. B !+++0B'99&AAAOO**,7 3222: .' %++---%%'''E )(((sD  BJ2GJ2J9IG IH<	9G#:1H<	,G'>H<	H<	,G%-H<	IH:IJ.I/JIJJ2JJ2 I#H<	%H<	'H7H7H H77H<	:I<II
IIIJJJ0I31J	J
JJJ2J/	J
J/	'J/	)	J2z%Use `streamable_http_client` instead.c                ~   V ^8  d   QhR\         R\        \         \         3,          R,          R\        \        ,          R\        \        ,          R\        R\
        R\        P                  R,          R	\        \        \        \        \        ,          ,          \        \        ,          \        3,          R3,          /# )
r0   rH   r6   NrR   r7   r  httpx_client_factoryrS   rI   )r:   r<   r=   r   rn   r   r8   rT   r   r  r   r   r   r   r  )r>   s   "r*   r@   r@     s     $ $	$#s(^d"$ Y$ i'	$
 $ /$ **t
$ 	!.9"<=~.	
 	
$r)   c           
       "   \        V\        4      '       d   VP                  4       MTp\        V\        4      '       d   VP                  4       MTpV! V\        P                  ! WxR 7      VR7      p	T	;_uu_4       GRj  xL
  \        V V	VR7      ;_uu_4       GRj  xL
 p
V
5x  RRR4      GRj  xL
  RRR4      GRj  xL
  R#  LJ L- L  + GRj  xL 
 '       g   i     L1; i L(  + GRj  xL 
 '       g   i     R# ; i5i))read)r6   rR   rS   N)r  r  )rq   r   total_secondsr8   Timeoutr#  )rH   r6   rR   r7   r  r%  rS   timeout_secondssse_read_timeout_secondsr1   streamss   &&&&&&&    r*   streamablehttp_clientr-    s     & 2<GY1O1Og++-U\O,67G,S,S&&(Yi 
 "oMF vv)1
 
 
 M
 
 vv
 
 
 
 vvvs   A?DCDC0 C!C0$C	*C05C6C0:DC.DC0C0C+C
C+$C+&C0.D0D
	6C97
D
	D
		Dr  )Gr'   r  loggingcollections.abcr   r   r   r   dataclassesr   datetimer   typingr   r	   warningsr
   r   r8   	anyio.abcr   anyio.streams.memoryr   r   	httpx_sser   r   r   typing_extensionsr   mcp.shared._httpx_utilsr   r   mcp.shared.messager   r   	mcp.typesr   r   r   r   r   r   r   r   	getLoggerr#   r   r   SessionMessageOrErrorr;   r   r:   r  rf   rg   r   r   r   re   rd   rb   rc   objectrZ   r    r,   r.   rF   r#  r-  r!   r)   r*   <module>r>     s|     ? ? * !        R @ @ ( E	 	 	 
		8	$ '2 %&;<(8C$J/ !-  !%  	  
>) >5) 5 	* 	* 	*} }@ P( -1P(  $	P( P(f 34 &*!#*0#1G"$ 5 $r)   