
    Chp                    :   % S r SSKJr  SSKrSSKrSSKrSSKrSSKJ	r	J
r
JrJr  SSKJrJrJr  SSKJrJrJrJr  SSKrSSKrSSKJrJr  SSKJr  SS	KJr  SSKJ r   SS
K!J"r"  SSK#J$r$  SSK%J&r&  SSK'J(r(  SSK)J*r*  SSK+J,r,  SSK-J.r.J/r/  SSK0J1r1  \Rd                  " \35      r4\" S\S9r5\" S\S9r6\7\8\4   r9S\:S'   \\ Rv                     r<S\:S'   \=\<\94   r>S\:S'   \R~                  " S5      r@S\:S'    " S S5      rA\S!S j5       rB " S S\\5\64   5      rCS"S  jrDg)#ay  
MCP Server Module

This module provides a framework for creating an MCP (Model Context Protocol) server.
It allows you to easily define and handle various types of requests and notifications
in an asynchronous manner.

Usage:
1. Create a Server instance:
   server = Server("your_server_name")

2. Define request handlers using decorators:
   @server.list_prompts()
   async def handle_list_prompts(request: types.ListPromptsRequest) -> types.ListPromptsResult:
       # Implementation

   @server.get_prompt()
   async def handle_get_prompt(
       name: str, arguments: dict[str, str] | None
   ) -> types.GetPromptResult:
       # Implementation

   @server.list_tools()
   async def handle_list_tools(request: types.ListToolsRequest) -> types.ListToolsResult:
       # Implementation

   @server.call_tool()
   async def handle_call_tool(
       name: str, arguments: dict | None
   ) -> list[types.TextContent | types.ImageContent | types.EmbeddedResource]:
       # Implementation

   @server.list_resource_templates()
   async def handle_list_resource_templates() -> list[types.ResourceTemplate]:
       # Implementation

3. Define notification handlers if needed:
   @server.progress_notification()
   async def handle_progress(
       progress_token: str | int, progress: float, total: float | None,
       message: str | None
   ) -> None:
       # Implementation

4. Run the server:
   async def main():
       async with mcp.server.stdio.stdio_server() as (read_stream, write_stream):
           await server.run(
               read_stream,
               write_stream,
               InitializationOptions(
                   server_name="your_server_name",
                   server_version="your_version",
                   capabilities=server.get_capabilities(
                       notification_options=NotificationOptions(),
                       experimental_capabilities={},
                   ),
               ),
           )

   asyncio.run(main())

The Server class provides methods to register handlers for various MCP requests and
notifications. It automatically manages the request context and handles incoming
messages from the client.
    )annotationsN)AsyncIterator	AwaitableCallableIterable)AbstractAsyncContextManagerAsyncExitStackasynccontextmanager)AnyGeneric	TypeAliascast)MemoryObjectReceiveStreamMemoryObjectSendStream)AnyUrl)TypeVar)create_call_wrapper)ReadResourceContents)InitializationOptions)ServerSession)RequestContext)McpError)ServerMessageMetadataSessionMessage)RequestResponderLifespanResultT)defaultRequestTr   StructuredContentUnstructuredContentCombinationContentrequest_ctxz?contextvars.ContextVar[RequestContext[ServerSession, Any, Any]]c                  2    \ rS rSr   S     SS jjrSrg)NotificationOptionsl   c                (    Xl         X l        X0l        g Nprompts_changedresources_changedtools_changed)selfr)   r*   r+   s       T/home/james-whalen/.local/lib/python3.13/site-packages/mcp/server/lowlevel/server.py__init__NotificationOptions.__init__m   s      /!2*    r(   N)FFF)r)   boolr*   r1   r+   r1   )__name__
__module____qualname____firstlineno__r.   __static_attributes__ r0   r-   r$   r$   l   s3     !&"'#	++  + 	+ +r0   r$   c                  #    0 7v   g7f)zDefault lifespan context manager that does nothing.

Args:
    server: The server instance this lifespan is managing

Returns:
    An empty context object
Nr7   )_s    r-   lifespanr:   x   s      Hs   	c                  ^   \ rS rSrSSSS\4           SS jjr  S     SS jjr      SS jr\  S S j5       r	S r
S rS	 rS
 rS rS rS rS rS rS!S jrS"S jrSS.S#S jjrS rS r  S$         S%S jjr S&       S'S jjr          S(S jrS)S jrSrg)*Server   Nc                    Xl         X l        X0l        X@l        XPl        X`l        [        R                  [        0U l	        0 U l
        0 U l        [        R                  SU5        g )NzInitializing server %r)nameversioninstructionswebsite_urliconsr:   typesPingRequest_ping_handlerrequest_handlersnotification_handlers_tool_cacheloggerdebug)r,   r?   r@   rA   rB   rC   r:   s          r-   r.   Server.__init__   s]     	(&
 }[
 RT"24-t4r0   c           	        SS jn[        U R                  U R                  (       a  U R                  OU" S5      U R                  U=(       d
    [	        5       U=(       d    0 5      U R
                  U R                  U R                  S9$ )z8Create initialization options from this server instance.c                @     SSK Jn  U" U 5      $ ! [         a     gf = f)Nr   )r@   unknown)importlib.metadatar@   	Exception)packager@   s     r-   pkg_version9Server.create_initialization_options.<locals>.pkg_version   s*    6w'' s    
mcp)server_nameserver_versioncapabilitiesrA   rB   rC   )rR   strreturnrY   )r   r?   r@   get_capabilitiesr$   rA   rB   rC   )r,   notification_optionsexperimental_capabilitiesrS   s       r-   create_initialization_options$Server.create_initialization_options   sn    	 %		+/<<4<<[=O..$=(;(=)/R **((**

 
	
r0   c           	     ~   SnSnSnSnSn[         R                  U R                  ;   a  [         R                  " UR                  S9n[         R
                  U R                  ;   a  [         R                  " SUR                  S9n[         R                  U R                  ;   a  [         R                  " UR                  S9n[         R                  U R                  ;   a  [         R                  " 5       n[         R                  U R                  ;   a  [         R                  " 5       n[         R                  " UUUUUUS9$ )z9Convert existing handlers to a ServerCapabilities object.N)listChangedF)	subscribera   )prompts	resourcestoolsloggingexperimentalcompletions)rD   ListPromptsRequestrG   PromptsCapabilityr)   ListResourcesRequestResourcesCapabilityr*   ListToolsRequestToolsCapabilityr+   SetLevelRequestLoggingCapabilityCompleteRequestCompletionsCapabilityServerCapabilities)r,   r\   r]   prompts_capabilityresources_capabilitytools_capabilitylogging_capabilitycompletions_capabilitys           r-   r[   Server.get_capabilities   s     "#!!% ##t'<'<<!&!8!8EYEiEi!j %%)>)>>#(#<#<-A-S-S$ 
 !!T%:%::$44AUAcAcd   D$9$99!&!8!8!:   D$9$99%*%@%@%B"''&*"&2.
 	
r0   c                *    [         R                  5       $ )zFIf called outside of a request context, this will raise a LookupError.)r"   get)r,   s    r-   request_contextServer.request_context   s    
   r0   c                   ^    SU 4S jjnU$ )Nc                   >^ [         R                  S5        [        U [        R                  5      mSU4S jjnUTR
                  [        R                  '   U $ )Nz)Registering handler for PromptListRequestc                   >#    T" U 5      I S h  vN n[        U[        R                  5      (       a  [        R                  " U5      $ [        R                  " [        R                  " US95      $  Na7f)N)rc   )
isinstancerD   ListPromptsResultServerResultreqresultwrappers     r-   handler7Server.list_prompts.<locals>.decorator.<locals>.handler   sY     &s|+fe&=&=>> --f55 !--e.E.Ef.UVV ,   A3A1A"A3)r   ztypes.ListPromptsRequest)rJ   rK   r   rD   ri   rG   funcr   r   r,   s     @r-   	decorator&Server.list_prompts.<locals>.decorator   sH     LLDE)$0H0HIGW ?FD!!%":":;Kr0   )r   zvCallable[[], Awaitable[list[types.Prompt]]] | Callable[[types.ListPromptsRequest], Awaitable[types.ListPromptsResult]]r7   r,   r   s   ` r-   list_promptsServer.list_prompts   s    	W	( r0   c                   ^    SU 4S jjnU$ )Nc                ~   >^  [         R                  S5        SU 4S jjnUTR                  [        R                  '   T $ )Nz(Registering handler for GetPromptRequestc                   >#    T" U R                   R                  U R                   R                  5      I S h  vN n[        R                  " U5      $  N7fr'   )paramsr?   	argumentsrD   r   )r   
prompt_getr   s     r-   r   5Server.get_prompt.<locals>.decorator.<locals>.handler  s>     #'

9M9M#NN
))*55 Os   5AAA)r   ztypes.GetPromptRequest)rJ   rK   rG   rD   GetPromptRequestr   r   r,   s   ` r-   r   $Server.get_prompt.<locals>.decorator  s5     LLCD6 =DD!!%"8"89Kr0   )r   zHCallable[[str, dict[str, str] | None], Awaitable[types.GetPromptResult]]r7   r   s   ` r-   
get_promptServer.get_prompt  s    
	Z
	 r0   c                   ^    SU 4S jjnU$ )Nc                   >^ [         R                  S5        [        U [        R                  5      mSU4S jjnUTR
                  [        R                  '   U $ )Nz,Registering handler for ListResourcesRequestc                   >#    T" U 5      I S h  vN n[        U[        R                  5      (       a  [        R                  " U5      $ [        R                  " [        R                  " US95      $  Na7f)N)rd   )r   rD   ListResourcesResultr   r   s     r-   r   9Server.list_resources.<locals>.decorator.<locals>.handler  sZ     &s|+fe&?&?@@ --f55 !--e.G.GRX.YZZ ,r   )r   ztypes.ListResourcesRequest)rJ   rK   r   rD   rk   rG   r   s     @r-   r   (Server.list_resources.<locals>.decorator  sI     LLGH)$0J0JKG[ AHD!!%"<"<=Kr0   )r   z|Callable[[], Awaitable[list[types.Resource]]] | Callable[[types.ListResourcesRequest], Awaitable[types.ListResourcesResult]]r7   r   s   ` r-   list_resourcesServer.list_resources  s    	[	( r0   c                   ^  SU 4S jjnU$ )Nc                ~   >^  [         R                  S5        SU 4S jjnUTR                  [        R                  '   T $ )Nz4Registering handler for ListResourceTemplatesRequestc                ~   >#    T" 5       I S h  vN n[         R                  " [         R                  " US95      $  N,7f)N)resourceTemplates)rD   r   ListResourceTemplatesResult)r9   	templatesr   s     r-   r   BServer.list_resource_templates.<locals>.decorator.<locals>.handler/  s1     "&&L	))%*K*K^g*hii )s   =;-=)r9   r   )rJ   rK   rG   rD   ListResourceTemplatesRequestr   s   ` r-   r   1Server.list_resource_templates.<locals>.decorator,  s5    LLOPj IPD!!%"D"DEKr0   )r   z5Callable[[], Awaitable[list[types.ResourceTemplate]]]r7   r   s   ` r-   list_resource_templatesServer.list_resource_templates+      	 r0   c                   ^    SU 4S jjnU$ )Nc                ~   >^  [         R                  S5        SU 4S jjnUTR                  [        R                  '   T $ )Nz+Registering handler for ReadResourceRequestc                T  >^ #    T" T R                   R                  5      I S h  vN nSU 4S jjnU===[        S` =b  u   O  =[        S` =b  u   O    O'  n  [        R
                  " S[        SS9  U" US 5      nO}  =[        S` =bW  u   nU Vs/ s H  ob" UR                  UR                  5      PM!     nn[        R                  " [        R                  " US95      $      [        S[        U5       35      e[        R                  " [        R                  " U/S95      $  Ns  snf 7f)	Nc                Z  > U ==[         S` =b7  u   n   [        R                  " TR                  R                  U U=(       d    SS9$     =[
        S` =bW  u   n SS Kn[        R                  " TR                  R                  UR                  U 5      R                  5       U=(       d    SS9$     g )Nr7   z
text/plain)uritextmimeTyper   zapplication/octet-stream)r   blobr   )
rY   rD   TextResourceContentsr   r   bytesbase64BlobResourceContents	b64encodedecode)data	mime_typer   r   s      r-   create_contentPServer.read_resource.<locals>.decorator.<locals>.handler.<locals>.create_contentA  s    *SU]#(#=#=$'JJNN%))2)Bl$  + -UW_)#(#=#=$'JJNN%+%5%5d%;%B%B%D)2)P6P$  -r0   r7   zdReturning str or bytes from read_resource is deprecated. Use Iterable[ReadResourceContents] instead.   )
stacklevel)contentsz+Unexpected return type from read_resource: )r   zstr | bytesr   
str | None)r   r   rY   r   warningswarnDeprecationWarningr   contentr   rD   r   ReadResourceResult
ValueErrortype)	r   r   r   r   r   r   content_itemcontents_listr   s	   `       r-   r   8Server.read_resource.<locals>.decorator.<locals>.handler>  s!    #CJJNN33" 00 J.'(	 #1t"< 1 0/mu)mu]iN<+?+?AWAWXmu & )  %11!44)6  	 0 (+VW[\bWcVd)eff)),,") Q 4:)s#    D(D!A)D(&D#4A.D(#D()r   ztypes.ReadResourceRequest)rJ   rK   rG   rD   ReadResourceRequestr   s   ` r-   r   'Server.read_resource.<locals>.decorator9  s7     LLFG-^ @GD!!%";";<Kr0   )r   zKCallable[[AnyUrl], Awaitable[str | bytes | Iterable[ReadResourceContents]]]r7   r   s   ` r-   read_resourceServer.read_resource8  s    5	]5	n r0   c                   ^  SU 4S jjnU$ )Nc                ~   >^  [         R                  S5        SU 4S jjnUTR                  [        R                  '   T $ )Nz'Registering handler for SetLevelRequestc                   >#    T" U R                   R                  5      I S h  vN   [        R                  " [        R                  " 5       5      $  N-7fr'   )r   levelrD   r   EmptyResultr   r   s    r-   r   <Server.set_logging_level.<locals>.decorator.<locals>.handlerv  s=     3::++,,,))%*;*;*=>> -    AA.A)r   ztypes.SetLevelRequest)rJ   rK   rG   rD   ro   r   s   ` r-   r   +Server.set_logging_level.<locals>.decorators  s3    LLBC? <CD!!%"7"78Kr0   )r   z/Callable[[types.LoggingLevel], Awaitable[None]]r7   r   s   ` r-   set_logging_levelServer.set_logging_levelr  r   r0   c                   ^  SU 4S jjnU$ )Nc                ~   >^  [         R                  S5        SU 4S jjnUTR                  [        R                  '   T $ )Nz(Registering handler for SubscribeRequestc                   >#    T" U R                   R                  5      I S h  vN   [        R                  " [        R                  " 5       5      $  N-7fr'   r   r   rD   r   r   r   s    r-   r   =Server.subscribe_resource.<locals>.decorator.<locals>.handler  ;     3::>>***))%*;*;*=>> +r   )r   ztypes.SubscribeRequest)rJ   rK   rG   rD   SubscribeRequestr   s   ` r-   r   ,Server.subscribe_resource.<locals>.decorator  s3    LLCD? =DD!!%"8"89Kr0   r   z#Callable[[AnyUrl], Awaitable[None]]r7   r   s   ` r-   subscribe_resourceServer.subscribe_resource  r   r0   c                   ^  SU 4S jjnU$ )Nc                ~   >^  [         R                  S5        SU 4S jjnUTR                  [        R                  '   T $ )Nz*Registering handler for UnsubscribeRequestc                   >#    T" U R                   R                  5      I S h  vN   [        R                  " [        R                  " 5       5      $  N-7fr'   r   r   s    r-   r   ?Server.unsubscribe_resource.<locals>.decorator.<locals>.handler  r   r   )r   ztypes.UnsubscribeRequest)rJ   rK   rG   rD   UnsubscribeRequestr   s   ` r-   r   .Server.unsubscribe_resource.<locals>.decorator  s3    LLEF? ?FD!!%":":;Kr0   r   r7   r   s   ` r-   unsubscribe_resourceServer.unsubscribe_resource  r   r0   c                   ^    SU 4S jjnU$ )Nc                   >^ [         R                  S5        [        U [        R                  5      mSUU4S jjnUTR
                  [        R                  '   U $ )Nz(Registering handler for ListToolsRequestc                  >#    T" U 5      I S h  vN n[        U[        R                  5      (       aB  UR                   H  nUTR                  UR
                  '   M     [        R                  " U5      $ TR                  R                  5         U H  nUTR                  UR
                  '   M     [        R                  " [        R                  " US95      $  N7f)N)re   )r   rD   ListToolsResultre   rI   r?   r   clear)r   r   toolr,   r   s      r-   r   5Server.list_tools.<locals>.decorator.<locals>.handler  s     &s|+ fe&;&;<< &6:((3 !- --f55 $$**, &6:((3 !' --e.C.C&.QRR ,s   CCC
C)r   ztypes.ListToolsRequest)rJ   rK   r   rD   rm   rG   r   s     @r-   r   $Server.list_tools.<locals>.decorator  sN     LLCD)$0F0FGGS S" =DD!!%"8"89Kr0   )r   zpCallable[[], Awaitable[list[types.Tool]]] | Callable[[types.ListToolsRequest], Awaitable[types.ListToolsResult]]r7   r   s   ` r-   
list_toolsServer.list_tools  s    	S	8 r0   c           	     |    [         R                  " [         R                  " [         R                  " SUS9/SS95      $ )z3Create a ServerResult with an error CallToolResult.r   r   r   T)r   isError)rD   r   CallToolResultTextContent)r,   error_messages     r-   _make_error_resultServer._make_error_result  s9    !!  **]KL
 	
r0   c                V  #    XR                   ;  a_  [        R                  U R                  ;   aA  [        R                  SU5        U R                  [        R                     " S5      I Sh  vN   U R                   R                  U5      nUc  [        R                  SU5        U$  N:7f)zlGet tool definition from cache, refreshing if necessary.

Returns the Tool object if found, None otherwise.
z(Tool cache miss for %s, refreshing cacheNz5Tool '%s' not listed, no validation will be performed)rI   rD   rm   rG   rJ   rK   r{   warning)r,   	tool_namer   s      r-   _get_cached_tool_definition"Server._get_cached_tool_definition  s     
 ,,,%%)>)>>GS++E,B,BCDIII##I.<NNRT]^ Js   A*B),B'-;B)T)validate_inputc                   ^ ^   SU U4S jjnU$ )a7  Register a tool call handler.

Args:
    validate_input: If True, validates input against inputSchema. Default is True.

The handler validates input against inputSchema (if validate_input=True), calls the tool function,
and builds a CallToolResult with the results:
- Unstructured content (iterable of ContentBlock): returned in content
- Structured content (dict): returned in structuredContent, serialized JSON text returned in content
- Both: returned in content and structuredContent

If outputSchema is defined, validates structuredContent or errors if missing.
c                   >^  [         R                  S5        SU UU4S jjnUTR                  [        R                  '   T $ )Nz'Registering handler for CallToolRequestc                $  >#     U R                   R                  nU R                   R                  =(       d    0 nT	R                  U5      I S h  vN nT
(       a&  U(       a   [        R
                  " X#R                  S9  T" X5      I S h  vN n[        U[        5      (       a"  [        U5      S:X  a  [        [        U5      u  pgO[        U[        5      (       a:  [        [         U5      n["        R$                  " S[&        R(                  " USS9S9/nOK[+        US5      (       a  [        [,        U5      nS nO'T	R                  S[/        U5      R0                   35      $ U(       a@  UR2                  b3  Uc  T	R                  S	5      $  [        R
                  " XsR2                  S9  ["        R4                  " ["        R6                  " [9        U5      USS95      $  GN! [        R                   a(  nT	R                  SUR                   35      s S nA$ S nAff = f GN! [        R                   a(  nT	R                  S
UR                   35      s S nA$ S nAff = f! [:         a$  nT	R                  [=        U5      5      s S nA$ S nAff = f7f)N)instanceschemazInput validation error: r   r   )indentr   __iter__z"Unexpected return type from tool: zOOutput validation error: outputSchema defined but no structured output returnedzOutput validation error: F)r   structuredContentr   )r   r?   r   r	  
jsonschemavalidateinputSchemaValidationErrorr  messager   tuplelenr   r!   dictr   rD   r  jsondumpshasattrr    r   r2   outputSchemar   r  listrQ   rY   )r   r  r   r   eresultsunstructured_contentmaybe_structured_contentr   r,   r  s           r-   r   4Server.call_tool.<locals>.decorator.<locals>.handler  sG    5; #

I #

 4 4 :I!%!A!A)!LLD &$c&//K[K[\
 %)$>>G
 "'511c'la6GIMN`biIjF,.F#GT22378I73S0050A0AvTXT^T^_fopTq0r/s, *55/34G/Q,370#669[\`ah\i\r\r[s7tuu  1 1 =3;#'#:#: q$ h * 3 3=U^o^o p
 !--,,$()=$>.F$) U M  *99 c#'#:#:=UVWV_V_U`;a#bbc ?8 $.#=#= h'+'>'>AZ[\[d[dZe?f'g gh ! ;223q6::;s   JA	I GI "G  I HCI  J!'I J
H  (2I JI H2HHI JHI  I4III JII 
J)JJJJJ)r   ztypes.CallToolRequest)rJ   rK   rG   rD   CallToolRequest)r   r   r,   r  s   ` r-   r   #Server.call_tool.<locals>.decorator  s;     LLBC6; 6;p <CD!!%"7"78Kr0   )r   zVCallable[..., Awaitable[UnstructuredContent | StructuredContent | CombinationContent]]r7   )r,   r  r   s   `` r-   	call_toolServer.call_tool  s#    A	A	 A	F r0   c                   ^    SU 4S jjnU$ )Nc                ~   >^  [         R                  S5        SU 4S jjnUTR                  [        R                  '   T $ )Nz,Registering handler for ProgressNotificationc                   >#    T" U R                   R                  U R                   R                  U R                   R                  U R                   R                  5      I S h  vN   g  N7fr'   )r   progressTokenprogresstotalr  r   s    r-   r   @Server.progress_notification.<locals>.decorator.<locals>.handler+  sL     JJ,,JJ''JJ$$JJ&&	  s   AA*"A(#A*)r   ztypes.ProgressNotification)rJ   rK   rH   rD   ProgressNotificationr   s   ` r-   r   /Server.progress_notification.<locals>.decorator&  s6     LLGH FMD&&u'A'ABKr0   )r   zGCallable[[str | int, float, float | None, str | None], Awaitable[None]]r7   r   s   ` r-   progress_notificationServer.progress_notification%  s    	Y	  r0   c                   ^    SU 4S jjnU$ )z7Provides completions for prompts and resource templatesc                ~   >^  [         R                  S5        SU 4S jjnUTR                  [        R                  '   T $ )Nz'Registering handler for CompleteRequestc           
     :  >#    T" U R                   R                  U R                   R                  U R                   R                  5      I S h  vN n[        R
                  " [        R                  " Ub  US95      $ [        R                  " / S S S9S95      $  NK7f)N)valuesr/  hasMore)
completion)r   refargumentcontextrD   r   CompleteResult
Completion)r   r:  r   s     r-   r   5Server.completion.<locals>.decorator.<locals>.handlerG  s     #'



8K8KSZZM_M_#``
))((%1 $.  #--RtTR  as   A
BBAB)r   ztypes.CompleteRequest)rJ   rK   rG   rD   rq   r   s   ` r-   r   $Server.completion.<locals>.decorator;  s5     LLBC <CD!!%"7"78Kr0   )r   zCallable[[types.PromptReference | types.ResourceTemplateReference, types.CompletionArgument, types.CompletionContext | None], Awaitable[types.Completion | None]]r7   r   s   ` r-   r:  Server.completion8  s    		2 r0   c           
     `  #    [        5        IS h  vN nUR                  U R                  U 5      5      I S h  vN nUR                  [        UUUUS95      I S h  vN n[        R
                  " 5        IS h  vN n	UR                    S h  vN n
[        R                  SU
5        U	R                  U R                  U
UUU5        M@   N N Nr NV NC
 S S S 5      IS h  vN    O! , IS h  vN  (       d  f       O= fS S S 5      IS h  vN    g ! , IS h  vN  (       d  f       g = f7f)N)	statelesszReceived message: %s)r	   enter_async_contextr:   r   anyiocreate_task_groupincoming_messagesrJ   rK   
start_soon_handle_message)r,   read_streamwrite_streaminitialization_optionsraise_exceptionsrD  stacklifespan_contextsessiontgr  s              r-   run
Server.runV  s       "##u%*%>%>t}}T?R%SS!55 *'	 G ..00B%,%>%> 	'LL!7AMM,,(( $S 1	%> 100000 $#####s   D.CD.$DC"DCD:C;D>C(CC
C8C(D.DDDCC(D!C$"D(C?	.C1/C?	;DD.DD.D+DD+'D.c           	       #    [         R                  " SS9 nU==[        S` =bA  u  [        R                  S` =b+  u  pg  U   U R                  XX#U5      I S h  vN   S S S 5        O4    [        R                  S` =b  u  nU R                  U5      I S h  vN   O  U H8  n	[        R                  SU	R                  R                  U	R                  5        M:     S S S 5        g  N! , (       d  f       MX  = f Na! , (       d  f       g = f7f)NT)recordrequest)rootzWarning: %s: %s)r   catch_warningsr   rD   ClientRequest_handle_requestClientNotification_handle_notificationrJ   infocategoryr2   r  )
r,   r  rQ  rP  rN  wr   	respondernotifyr  s
             r-   rJ  Server._handle_message}  s      $$D1QY%Le.A.A.KY""227\lmmm # Z --:33F;;; ; -w/?/?/H/H'//Z  21
 n # < 21sS   D*C7C#C!C#6C7C5AC7	D!C##
C2	-	C77
DDc           
       #    [         R                  S[        U5      R                  5        U R                  R                  [        U5      5      =n(       a  [         R                  S[        U5      R                  5        S n S nUR                  b5  [        UR                  [        5      (       a  UR                  R                  n[        R                  [        UR                  UR                  UUUS95      nU" U5      I S h  vN n	Ub  [        R)                  U5        UR3                  U	5      I S h  vN   O:UR3                  [,        R.                  " [,        R4                  SS95      I S h  vN   [         R                  S	5        g  N! [          a  n
U
R"                  n	 S n
A
NS n
A
f[$        R&                  " 5        a<    [         R                  SUR                  5         Ub  [        R)                  U5        g g [*         a3  n
U(       a  U
e[,        R.                  " S[1        U
5      S S9n	 S n
A
GN-S n
A
ff = f! Ub  [        R)                  U5        f f = f GN' N7f)
NzProcessing request of type %szDispatching request of type %srW  z4Request %s cancelled - duplicate response suppressedr   )coder  r   zMethod not found)rf  r  zResponse sent)rJ   r_  r   r2   rG   r{   rK   message_metadatar   r   r|   r"   setr   
request_idrequest_metar   errorrF  get_cancelled_exc_classresetrQ   rD   	ErrorDatarY   respondMETHOD_NOT_FOUND)r,   r  r   rQ  rP  rN  r   tokenrequest_dataresponseerrs              r-   r\  Server._handle_request  s     	3T#Y5G5GH++//S	::7:LL949;M;MNE!-#++7JwG_G_av<w<w#*#;#;#K#KL $"**,,( , ")- $%%e,//(+++////.   	_%7 . %99002 J&&  $%%e, %  P#I ??3s8$OP $%%e, % ,s   B I#BF FF ,I#7I8:I#2I!3I#F 
H=F*%I  *<H=&I  'I#	H=
(H82I  8H==I   II#!I#c                &  #    U R                   R                  [        U5      5      =n(       a;  [        R	                  S[        U5      R
                  5         U" U5      I S h  vN   g g  N! [         a    [        R                  S5         g f = f7f)Nz#Dispatching notification of type %sz*Uncaught exception in notification handler)rH   r{   r   rJ   rK   r2   rQ   	exception)r,   rc  r   s      r-   r^  Server._handle_notification  s{     0044T&\BB7BLL>V@U@UVOfo%%	 C & O  !MNOs<   ABA, #A*$A, (B*A, ,BBBB)	rI   rC   rA   r:   r?   rH   rG   r@   rB   )r?   rY   r@   r   rA   r   rB   r   rC   zlist[types.Icon] | Noner:   z[Callable[[Server[LifespanResultT, RequestT]], AbstractAsyncContextManager[LifespanResultT]])NN)r\   zNotificationOptions | Noner]   z dict[str, dict[str, Any]] | NonerZ   r   )r\   r$   r]   zdict[str, dict[str, Any]]rZ   ztypes.ServerCapabilities)rZ   z8RequestContext[ServerSession, LifespanResultT, RequestT])r  rY   rZ   types.ServerResult)r  rY   rZ   ztypes.Tool | None)r  r1   )FF)
rK  z5MemoryObjectReceiveStream[SessionMessage | Exception]rL  z&MemoryObjectSendStream[SessionMessage]rM  r   rN  r1   rD  r1   )F)r  z`RequestResponder[types.ClientRequest, types.ServerResult] | types.ClientNotification | ExceptionrQ  r   rP  r   rN  r1   )
r  z9RequestResponder[types.ClientRequest, types.ServerResult]r   r   rQ  r   rP  r   rN  r1   )rc  r   )r2   r3   r4   r5   r:   r.   r^   r[   propertyr|   r   r   r   r   r   r   r   r   r   r  r	  r(  r3  r:  rS  rJ  r\  r^  r6   r7   r0   r-   r<   r<      s    ##'"&)- 55 5 !	5
  5 '5
56 <@FJ
8
 $D
 
	
:)
1)
 $=)
 
"	)
V !	A! !..8t>
  37 Rh&N "'
  %J% =% !6	% % %X "'[q[ [ *	[
 [&9&J9& 9& 	9&
 *9& 9&vOr0   r<   c                \   #    [         R                  " [         R                  " 5       5      $ 7fr'   )rD   r   r   rW  s    r-   rF   rF     s     e//122s   *,)r9   z!Server[LifespanResultT, RequestT]rZ   zAsyncIterator[dict[str, Any]])rX  ztypes.PingRequestrZ   ry  )E__doc__
__future__r   _annotationscontextvarsr  rf   r   collections.abcr   r   r   r   
contextlibr   r	   r
   typingr   r   r   r   rF  r  anyio.streams.memoryr   r   pydanticr   typing_extensionsr   	mcp.typesrD   #mcp.server.lowlevel.func_inspectionr    mcp.server.lowlevel.helper_typesr   mcp.server.modelsr   mcp.server.sessionr   mcp.shared.contextr   mcp.shared.exceptionsr   mcp.shared.messager   r   mcp.shared.sessionr   	getLoggerr2   rJ   r   r   r  rY   r   __annotations__ContentBlockr    r  r!   
ContextVarr"   r$   r:   r<   rF   r7   r0   r-   <module>r     s  AF 3     H H W W 0 0   R  %  C A 3 , - * D /			8	$+S9:s+  $CH~ 9 -!)%*<*<!= Y = %&9;L&L M I M P[OeOefsOtL t	+ 	+ 	 	M	OW_h./ M	O`3r0   