o
    h                     @   s   d Z ddlZddlmZ ddlmZ ddlZddlZddlm	Z	m
Z
 ddlmZ ddlmZ e		ddeje dB deje dB fd	d
ZdS )aY  
Stdio Server Transport Module

This module provides functionality for creating an stdio-based transport layer
that can be used to communicate with an MCP client through standard input/output
streams.

Example usage:
```
    async def run_server():
        async with stdio_server() as (read_stream, write_stream):
            # read_stream contains incoming JSONRPCMessages from stdin
            # write_stream allows sending JSONRPCMessages to stdout
            server = await create_my_server()
            await server.run(read_stream, write_stream, init_options)

    anyio.run(run_server)
```
    N)asynccontextmanager)TextIOWrapper)MemoryObjectReceiveStreamMemoryObjectSendStream)SessionMessagestdinstdoutc              	     s   st ttjjddst ttjjddt d\ }t d\} fdd}fdd}t  4 I dH }|	| |	| ||fV  W d  I dH  dS 1 I dH s`w   Y  dS )	z
    Server transport for stdio: this communicates with an MCP client by reading
    from the current process' stdin and writing to stdout.
    zutf-8)encodingr   c                     s   zZ 4 I d H F 2 z53 d H W } zt j| }W n ty3 } z |I d H  W Y d }~qd }~ww t|} |I d H  q6 W d   I d H  W d S 1 I d H sTw   Y  W d S  tjyn   tj	 I d H  Y d S w )N)
typesJSONRPCMessagemodel_validate_json	Exceptionsendr   anyioClosedResourceErrorlowlevel
checkpoint)linemessageexcsession_message)read_stream_writerr    Q/var/www/html/openai_agents/venv/lib/python3.10/site-packages/mcp/server/stdio.pystdin_reader<   s$   2
z"stdio_server.<locals>.stdin_readerc               	      s   zD4 I d H 0 2 z3 d H W } | j jddd} |d I d H    I d H  q6 W d   I d H  W d S 1 I d H s>w   Y  W d S  tjyX   tj I d H  Y d S w )NT)by_aliasexclude_none
)r   model_dump_jsonwriteflushr   r   r   r   )r   json)r   write_stream_readerr   r   stdout_writerK   s   2z#stdio_server.<locals>.stdout_writerN)
r   	wrap_filer   sysr   bufferr   create_memory_object_streamcreate_task_group
start_soon)r   r   read_streamwrite_streamr   r#   tgr   )r   r   r   r"   r   stdio_server!   s   


.r-   )NN)__doc__r%   
contextlibr   ior   r   anyio.lowlevelanyio.streams.memoryr   r   	mcp.typesr
   mcp.shared.messager   	AsyncFilestrr-   r   r   r   r   <module>   s"    