xj,UdZddlZddlZddlZddlmZddlmZmZddl m Z ddl m Z ddl mZmZmZddlZddlZddlZddlmZdd lmZdd lmZej6d k7rddlZiZeed fe d <ejBZ"e Gdd Z#Gdde Z$dedede%fdZ&dedefdZ'defdZ(defdZ)dedede%fdZ*dedededdegefde+eegdfdzff dZ,ed!dejZe.dzdejZe.dzfd Z/y)"a-Stdio server transport for MCP. Example: ```python async def run_server(): async with stdio_server() as (read_stream, write_stream): server = await create_my_server() await server.run(read_stream, write_stream, init_options) anyio.run(run_server) ``` N)Callable)asynccontextmanagersuppress) dataclass) TextIOWrapper)BinaryIOLiteralTextIO)rebind_std_handle_to_fd)create_context_streams)SessionMessagewin32 _StreamClaim_claimsc,eZdZUeed<dZedzed<y)rfdN private_fd)__name__ __module__ __qualname__int__annotations__rd/mnt/ssd/data/Dropbox/adrian/sandbox/mcp-query/venv/lib/python3.12/site-packages/mcp/server/stdio.pyrr.s G!Jd !rceZdZdZddZy)_UnownedTextWrapperaText layer whose close never closes the underlying buffer. The buffer is not the transport's to close: in the in-place paths it is the sys stream's own buffer, and closing it at garbage collection destroyed sys.stdout for the rest of the process (issue #1933). Ncltt5|jdddy#1swYyxYwN)r ValueErrordetach)selfs rclosez_UnownedTextWrapper.close<s( j !  KKM   s*3returnN)rrr__doc__r#rrrrr4s rrstreamrr%cr |jj|k(S#tttf$rYywxYw)NF)bufferfilenoAttributeErrorOSErrorr )r'rs r_is_backed_by_fdr-As8}}##%++ GZ 0s 66ctjdk(r@tj|}|dkr$tj|t d|d|St j |t jdS)zFDuplicate fd onto a descriptor that cannot land in the standard range.rzduplicate of fd z landed in the standard range)sysplatformosdupr#r,fcntlF_DUPFD_CLOEXEC)r duplicates r_dup_above_stdr8Hsc ||wFF2J > HHY ,RD0MNO O ;;r500! 44rcftjtjtjSr)r3opendevnullO_RDONLYrrr_open_stdin_diversionr=Ss 772::r{{ ++rc tjdS#t$r5tjtjtj cYSwxYw)Nr/)r3r4r,r:r;O_WRONLYrrr_open_stdout_diversionr@Ws=0vvay 0wwrzz2;;//0s;AArc tj||tjdk(r$t t5t |dddyy#t$rYywxYw#1swYyxYw)zOPoint fd back at the wire; the Windows handle rebind never affects the outcome.FrNT)r3dup2r,r1r2rr )rrs r _restore_fdrC^s]  B ||w g  ( #B ' (   ( sA A AAA(mode)rbwbopen_diversionct|s|jdfStt5tvrt dt<ddddfd } t }|_ |} tj|tt5tj|dddtjdk(r#tt5t!dddtj"||d|fS#1swYxYw#t$r|j|fcYSwxYw#t$r|j|fcYSwxYw#t$rWtt5tj|dddn #1swYnxYwt||j|fcYSwxYw#1swYxYw#1swYxYw)aMClaim a standard stream: divert fd and serve the wire from a private duplicate. Best-effort: when descriptors cannot be duplicated or diverted, serves the sys stream's buffer in place, exactly as v1 did, with the claim held. Raises: RuntimeError: fd is already claimed by another transport in this process. Nz>another stdio_server() in this process has already claimed fd cjtjrt5t=dddyy#1swYyxYwr)rrC _claims_lockr)claimrsrreleasez_claim_fd..release}sH    #{2u7G7G'H BK  (I  s <ArF)closefdr$)r-r)rrJr RuntimeErrorr8r,rr3rBrr#rCr1r2r fdopen)rr'rDrGrLr diversion_fdrKs` @r _claim_fdrQjs FB '}}d""  E  =!_`b_cde e   &#B' "E&%'  &  b! '   ||w g  ( #B ' ( 99Zu 5w >>M &}}g%%& &}}g%%& &g  # HH\ " # # #B #}}g%%& ( (sq D  D1D59EF5 G DD21D25EEF2*F  F2 F !F21F25F?G stdinstdoutc Kd}d} sDtdtjdt\}}t j t |ddsCtdtjdt\}}t j t |d tttzd\ }ttd\} fd } fd } t j4d{} | j|| j| ||fdddd{||||yy7Q7#1d{7swY*xYw#||||wwxYww) aTServe MCP over the process's stdin and stdout. While serving, fd 0 points at the null device and fd 1 at stderr, so handlers and children read EOF and their stray output misses the wire; both descriptors are restored on exit. Explicit streams skip the claim, and a second concurrent stdio_server() raises RuntimeError. NrrEzutf-8replace)encodingerrorsrF)rVcK 4d{23d{} tjj|d}t |}j |d{P7V7M#t$r$}j |d{7Yd}~|d}~wwxYw796dddd{7y#1d{7swYyxYw#t j$r*t jjd{7YywxYww)NF)by_name) typesjsonrpc_message_adapter validate_json Exceptionsendr anyioClosedResourceErrorlowlevel checkpoint)linemessageexcsession_messageread_stream_writerrRs r stdin_readerz"stdio_server..stdin_readers 2- G G&+GGd%&+&C&C&Q&QRV`e&Q&fG +9*A055oFFF GG )%"4"9"9#">>>$% G', G G G G G,, 2nn//111 2sDCA"CB+BA$BB+!A&B+BB+"C$B& B/BB B B+BB+B+ C$B'%C*D+B=1B4 2B=9C<D=C4C=4C75C=:D<C==DcK 4d{23d{}|jjdd}j|dzd{jd{[7a7X7!7 6dddd{7y#1d{7swYyxYw#tj $r*tj jd{7YywxYww)NT)by_alias exclude_unset )remodel_dump_jsonwriteflushr`rarbrc)rgjsonrSwrite_stream_readers r stdout_writerz#stdio_server..stdout_writers 2.--1D--o.66FFPTdhFi$ll4$;777$lln,, --7,2E----- ,, 2nn//111 2sCBA-BBA5A/A55BA1B'A3(B-B/A51B3B5B6 BBBCBB BBCB4CCCCCC)rQr1rRr=r` wrap_filerrSr@r r r^create_task_group start_soon) rRrS restore_stdinrestore_stdout stdin_buffer stdout_buffer read_stream write_streamrirstgrhrrs `` @@r stdio_serverr~s]04M04N,*3Asyy$H]*^ 'L-OO$7 w_h$ijE,5aTKa,b )M>__%8QX%YZF*@R[A[*\]^*_'K,B>,RST,U) ) 2 2**, , , MM, ' MM- (|+ + , ,  %    $ O % , , , , ,  %    $ O %seE/C E,D>-E0*E E%E&E*E/>EEEE EEE,,E/)NN)0r&r3r1 threadingcollections.abcr contextlibrr dataclassesriortypingrr r r`anyio.lowlevel mcp_typesr[mcp.os.win32.utilitiesr mcp.shared._context_streamsr mcp.shared.messager r2r5rdictrrLockrJrrboolr-r8r=r@rCtuplerQ AsyncFilestrr~rrrrs  $4!,, :>-<<7&(c>! "'y~~  "" " - V5s5s5,s,00 C S T 4? 4?4?#*:#64?HPQSUXQXHY4? 8Xb$h'$. ./4?n7eooc2T97%//Z]J^aeJe77r