
    i                         d dl Z d dlmZ d dlZd dlmZ d dlmZ d dl	m
Z
 d dlmZ d dlmZmZ d dlmZ  G d d	e      Ze j$                  d
eez  ded   fd       Zy)    N)AsyncIterator)ClientSession)FastMCP)#create_client_server_memory_streams)Unpack)ClientTransportSessionKwargsc                   n    e Zd ZdZd
deez  defdZej                  de
e   dee   fd       ZdefdZy	)FastMCPTransporta\  In-memory transport for FastMCP servers.

    This transport connects directly to a FastMCP server instance in the same
    Python process. It works with both FastMCP 2.x servers and FastMCP 1.0
    servers from the low-level MCP SDK. This is particularly useful for unit
    tests or scenarios where client and server run in the same runtime.
    mcpraise_exceptionsc                      || _         || _        y)z=Initialize a FastMCPTransport from a FastMCP server instance.N)serverr   )selfr   r   s      p/Users/bowang/.openclaw/workspace/ChatDev/.venv/lib/python3.12/site-packages/fastmcp/client/transports/memory.py__init__zFastMCPTransport.__init__   s      0    session_kwargsreturnc           
     &   
K   t               4 d {   \  }}|\  }}|\  
d }t        j                         4 d {   }t         j                        4 d {    |j                   
fd       	 t        d||d|4 d {   }| d d d       d {    |j                  j                          d d d       d {    d d d       d {    ||d d d       d {    y 7 7 7 7 n7 [# 1 d {  7  sw Y   kxY w# t        $ r}	|	}Y d }	~	~d }	~	ww xY w# |j                  j                          w xY w7 # 1 d {  7  sw Y   xY w7 # 1 d {  7  sw Y   xY w7 # 1 d {  7  sw Y   y xY ww)Nr   c                       j                   j                  j                   j                   j                  j                          j                        S )N)r   )r   _mcp_serverruncreate_initialization_optionsr   )r   server_readserver_writes   r   <lambda>z2FastMCPTransport.connect_session.<locals>.<lambda>5   sD    DKK3377#$//MMO)-)>)>	 8 r   )read_streamwrite_stream )
r   anyiocreate_task_group_enter_server_lifespanr   
start_soonr   BaseExceptioncancel_scopecancel)r   r   client_streamsserver_streamsclient_readclient_writeexception_to_raisetgclient_sessioner   r   s   `         @@r   connect_sessionz FastMCPTransport.connect_session    sd     788 =
(6%K(6%K
 8< ''))R&dkk::
-,  $/%1  )    (,,    OO**,+ ;: *)2 "-((O 988 *:          % +)*&+ OO**,+ ;::: *))) 9888sR  FC6F(E< C8E<E%C:
 E%#E;DC<DD 	D"C>
#D'EE%EE%E<E#E<%F0E:1F8E<:E%<D>D DD	DD	D*	D%	 D-%D*	*D--E			EE%E EE E%#E<%E7	+E.,E7	3E<:F<FFF
Fc                 6    d| j                   j                   dS )Nz<FastMCPTransport(server='z')>)r   name)r   s    r   __repr__zFastMCPTransport.__repr__M   s    +DKK,<,<+=SAAr   N)F)__name__
__module____qualname____doc__r   FastMCP1Serverboolr   
contextlibasynccontextmanagerr   r	   r   r   r1   strr4   r!   r   r   r   r      sb    1Gn4 1 1 ##*) &} 5*)	}	%*) $*)XB# Br   r   r   r   c                   K   t        | t              r/| j                         4 d{    d ddd      d{    yd y7  7 # 1 d{  7  sw Y   yxY ww)z`Enters the server's lifespan context for FastMCP servers and does nothing for FastMCP 1 servers.N)
isinstancer   _lifespan_managerr   s    r   r$   r$   Q   sG     
 &'"++-- .-- 	 .----sA   %A!AA!AA!A
A!
A!AAAA!)r;   collections.abcr   r"   r   r   mcp.server.fastmcpr   r9   mcp.shared.memoryr   typing_extensionsr   fastmcp.client.transports.baser   r	   fastmcp.server.serverr   r<   r$   r!   r   r   <module>rG      se     )   8 A $ I )@B @BF n$4  r   