
    )`i                         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z  dej        e         dz  f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                4   K    s2t          j        t          t          j        j        d                     s2t          j        t          t          j        j        d                    t          j        d          \  }t          j        d          \  } fd}fd}t          j                    4 d{V }|	                    |           |	                    |           ||fW V  ddd          d{V  dS # 1 d{V swxY 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                    K   	 4 d {V  2 3 d {V } 	 t           j                            |           }n2# t          $ r%}                    |           d {V  Y d }~Rd }~ww xY wt          |          }                    |           d {V  6 	 d d d           d {V  d S # 1 d {V swxY w Y   d S # t          j        $ r( t          j        	                                 d {V  Y d S w xY w)N)
typesJSONRPCMessagemodel_validate_json	Exceptionsendr   anyioClosedResourceErrorlowlevel
checkpoint)linemessageexcsession_messageread_stream_writerr   s       d/home/jaya/work/projects/VOICE-AGENT/VIET/agent-env/lib/python3.11/site-packages/mcp/server/stdio.pystdin_readerz"stdio_server.<locals>.stdin_reader<   s      	.) 	C 	C 	C 	C 	C 	C 	C 	C"' C C C C C C C$!"'"6"J"J4"P"P$ ! ! !055c::::::::: ! '5W&=&=O,11/BBBBBBBBBB #(%	C 	C 	C 	C 	C 	C 	C 	C 	C 	C 	C 	C 	C 	C 	C 	C 	C 	C 	C 	C 	C 	C 	C 	C 	C 	C 	C 	C 	C 	C ( 	. 	. 	..++------------	.sd   B= B*BB*8B*
A'A"B*"A''/B*B= *
B44B= 7B48B= =3C43C4c                    K   	 4 d {V  2 3 d {V } | j                             dd          }                    |dz              d {V                                   d {V  \6 	 d d d           d {V  d S # 1 d {V swxY w Y   d S # t          j        $ r( t          j                                         d {V  Y d S w xY w)NT)by_aliasexclude_none
)r   model_dump_jsonwriteflushr   r   r   r   )r   jsonr	   write_stream_readers     r   stdout_writerz#stdio_server.<locals>.stdout_writerK   s     	.* ) ) ) ) ) ) ) )-@ ) ) ) ) ) ) )/*2BBD_cBddD ,,td{333333333 ,,..(((((((( .A-@) ) ) ) ) ) ) ) ) ) ) ) ) ) ) ) ) ) ) ) ) ) ) ) ) ) ) ) ) )
 ( 	. 	. 	..++------------	.s?   B BA,AB.B 
BB BB 3C
CN)
r   	wrap_filer   sysr   bufferr	   create_memory_object_streamcreate_task_group
start_soon)	r   r	   read_streamwrite_streamr   r&   tgr   r%   s	   ``     @@r   stdio_serverr0   !   s       Sci.> Q Q QRR Usz/@7!S!S!STT ',&G&J&J#(-(I!(L(L%L%. . . . . .. . . . . . &(( ( ( ( ( ( ( (B
l###
m$$$<'''''( ( ( ( ( ( ( ( ( ( ( ( ( ( ( ( ( ( ( ( ( ( ( ( ( ( ( ( ( (s   2D
DD)NN)__doc__r(   
contextlibr   ior   r   anyio.lowlevelanyio.streams.memoryr   r   	mcp.typesr   mcp.shared.messager   	AsyncFilestrr0        r   <module>r<      s    ( 


 * * * * * *            R R R R R R R R       - - - - - - )-*.6( 6(?3$&6(OC 4'6( 6( 6( 6( 6( 6(r;   