
    ^sjI                         d Z ddl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
 e	 	 ddej                  e   dz  dej                  e   dz  fd       Zy)	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)MemoryObjectReceiveStreamMemoryObjectSendStreamstdinstdoutc                   K    s#t        j                  t        j                         s#t        j                  t        j                        t        j
                  d      \  }t        j
                  d      \  } fd}fd}t        j                         4 d{   }|j                  |       |j                  |       ||f ddd      d{    y7 >7 # 1 d{  7  sw Y   yxY ww)z
    Server transport for stdio: this communicates with an MCP client by reading
    from the current process' stdin and writing to stdout.
    r   c                    K   	 4 d {    2 3 d {   } 	 t         j                  j                  |       }j	                  |       d {    C7 I7 @# t        $ r$}j	                  |       d {  7   Y d }~od }~ww xY w7 96 d d d       d {  7   y # 1 d {  7  sw Y   y xY w# t
        j                  $ r* t
        j                  j                          d {  7   Y y w xY ww)N)	typesJSONRPCMessagemodel_validate_json	ExceptionsendanyioClosedResourceErrorlowlevel
checkpoint)linemessageexcread_stream_writerr   s      C/root/.hermes/venv/lib/python3.12/site-packages/mcp/server/stdio.pystdin_readerz"stdio_server.<locals>.stdin_reader8   s     	.) ; ;"' ; ;$!"'"6"6"J"J4"P
 -11':::;; % !055c::: ! ; #(; ; ; ; ; (( 	...++---	.s   C3B3 AB3 BBABBABB	BB3 B	B"B6A9
7B<BBBBB3 BB3 C3B0$B'%B0,B3 /C30B3 34C0'C*(C0-C3/C00C3c                    K   	 4 d {    2 3 d {   } | j                  dd      }j                  |dz          d {    j                          d {    Q7 W7 N7 !7 6 d d d       d {  7   y # 1 d {  7  sw Y   y xY w# t        j                  $ r* t        j
                  j                          d {  7   Y y w xY ww)NT)by_aliasexclude_none
)model_dump_jsonwriteflushr   r   r   r   )r   jsonr   write_stream_readers     r   stdout_writerz#stdio_server.<locals>.stdout_writerF   s     	.* ) )%8 ) )'"22Dt2TD ,,td{333 ,,.((	))3( &9) ) ) ) )
 (( 	...++---	.s   CB A#B A>A+A%A++A>A'A>A)A>#B %A+'A>)A>+A>,B 7A:8B =C>BBBB CB 4CC
CCCCN)r   	wrap_filesysr   r   create_memory_object_streamcreate_task_group
start_soon)	r   r   read_streamwrite_streamr   r"   tgr   r!   s	   ``     @@r   stdio_serverr+      s      		*, ',&G&G&J#(-(I(I!(L%L%.. &&( ( (B
l#
m$<''( ( ( ( ( ( (sH   BC=%C$&C=)*C(C=C&C=&C=(C:.C1/C:6C=)NN)__doc__r$   
contextlibr   r   anyio.lowlevelanyio.streams.memoryr   r   	mcp.typesr
   	AsyncFilestrr+        r   <module>r5      sd   (  *   R  )-*.3(??3$&3(OOC 4'3( 3(r4   