+
    wi8                      a  0 t $ R t^ RIHt ^ RIt^ RIt^ RIt^ RIt^ RI	t	^ RI
HtHtHtHt ^ RIHtHtHt ^ RIHtHtHtHt ^ RIt^ RIt^ RIHtHt ^ RIHt ^ RIH t  ^ RI!H"t" ^ R	I#H$t$ ^ R
I%H&t& ^ RI'H(t( ^ RI)H*t* ^ RI+H,t, ^ RI-H.t. ^ RI/H0t0 ^ RI1H2t2H3t3 ^ RI4H5t5H6t6 ^ RI7H8t8 ^ RI9H:t: ]Pv                  ! ]<4      t=] ! R]R7      t>] ! R]R7      t?]@]A]3,          tBR]CR&   ]]"P                  ,          tER]CR&   ]F]E]B3,          tGR]CR&   ]P                  ! R4      tIR]CR&    ! R R4      tJ]R R  l4       tK ! R! R"]]>]?3,          4      tLR# R$ ltMR# )%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)Experimental)ExperimentalHandlers)create_call_wrapper)ReadResourceContents)InitializationOptions)ServerSession)RequestContext)McpErrorUrlElicitationRequiredError)ServerMessageMetadataSessionMessage)RequestResponder)validate_and_warn_tool_nameLifespanResultT)defaultRequestTr   StructuredContentUnstructuredContentCombinationContentrequest_ctxz?contextvars.ContextVar[RequestContext[ServerSession, Any, Any]]c                  &    ] tR t^ptRR R lltRtR# )NotificationOptionsc               $    V ^8  d   QhRRRRRR/# )   prompts_changedboolresources_changedtools_changed )formats   "N/home/ubuntu/.local/lib/python3.14/site-packages/mcp/server/lowlevel/server.py__annotate__ NotificationOptions.__annotate__q   s(     + ++  + 	+    c                	*    Wn         W n        W0n        R # Nr*   r,   r-   )selfr*   r,   r-   s   &&&&r0   __init__NotificationOptions.__init__q   s      /!2*r3   r6   N)FFF)__name__
__module____qualname____firstlineno__r8   __static_attributes__r.   r3   r0   r'   r'   p   s    + +r3   r'   c                    V ^8  d   QhRRRR/# )r)   _z!Server[LifespanResultT, RequestT]returnzAsyncIterator[dict[str, Any]]r.   )r/   s   "r0   r1   r1   }   s     	 	7 	<Y 	r3   c                  "   / 5x  R# 5i)zDefault lifespan context manager that does nothing.

Args:
    server: The server instance this lifespan is managing

Returns:
    An empty context object
Nr.   )r@   s   &r0   lifespanrC   |   s      Hs   
c                     ] tR t^tRRRR]3R R lltR(R R lltR R lt]R R	 l4       t	]R
 R l4       t
R tR tR tR tR tR tR tR tR tR R ltR R ltRR/R R lltR tR tR)R R  lltR*R! R" lltR# R$ ltR% R& ltR'tR# )+ServerNc               0    V ^8  d   QhRRRRRRRRRRR	R
/# )r)   namestrversion
str | Noneinstructionswebsite_urliconszlist[types.Icon] | NonerC   z[Callable[[Server[LifespanResultT, RequestT]], AbstractAsyncContextManager[LifespanResultT]]r.   )r/   s   "r0   r1   Server.__annotate__   sF     5 55 5 !	5
  5 '5
5r3   c                	    Wn         W n        W0n        W@n        WPn        W`n        \        P                  \        /V n	        / V n
        / V n        R V n        \        P                  RV4       R # )NzInitializing server %r)rG   rI   rK   rL   rM   rC   typesPingRequest_ping_handlerrequest_handlersnotification_handlers_tool_cache_experimental_handlersloggerdebug)r7   rG   rI   rK   rL   rM   rC   s   &&&&&&&r0   r8   Server.__init__   se     	(&
 }[
 RT"24CG#-t4r3   c               $    V ^8  d   QhRRRRRR/# )r)   notification_optionszNotificationOptions | Noneexperimental_capabilitiesz dict[str, dict[str, Any]] | NonerA   r   r.   )r/   s   "r0   r1   rN      s)     
 
8
 $D
 
	
r3   c           	         R R lp\        V P                  V P                  '       d   V P                  MV! R4      T P                  T;'       g    \	        4       T;'       g    / 4      V P
                  V P                  V P                  R7      # )z8Create initialization options from this server instance.c                    V ^8  d   QhRRRR/# )r)   packagerH   rA   r.   )r/   s   "r0   r1   :Server.create_initialization_options.<locals>.__annotate__   s     	 	 	 	r3   c                D     ^ RI Hp V! V 4      #   \         d     R# i ; i)    )rI   unknown)importlib.metadatarI   	Exception)r_   rI   s   & r0   pkg_version9Server.create_initialization_options.<locals>.pkg_version   s*    6w'' s    mcp)server_nameserver_versioncapabilitiesrK   rL   rM   )r   rG   rI   get_capabilitiesr'   rK   rL   rM   )r7   r[   r\   rf   s   &&& r0   create_initialization_options$Server.create_initialization_options   sv    	 %		+/<<<4<<[=O..$==(;(=)//R **((**

 
	
r3   c               $    V ^8  d   QhRRRRRR/# )r)   r[   r'   r\   zdict[str, dict[str, Any]]rA   ztypes.ServerCapabilitiesr.   )r/   s   "r0   r1   rN      s(     , ,1, $=, 
"	,r3   c           	        RpRpRpRpRp\         P                  V P                  9   d"   \         P                  ! VP                  R7      p\         P
                  V P                  9   d#   \         P                  ! RVP                  R7      p\         P                  V P                  9   d"   \         P                  ! VP                  R7      p\         P                  V P                  9   d   \         P                  ! 4       p\         P                  V P                  9   d   \         P                  ! 4       p\         P                  ! VVVVVVR7      pV P                   '       d   V P                   P#                  V4       V# )z9Convert existing handlers to a ServerCapabilities object.N)listChangedF)	subscriberq   )prompts	resourcestoolsloggingexperimentalcompletions)rP   ListPromptsRequestrS   PromptsCapabilityr*   ListResourcesRequestResourcesCapabilityr,   ListToolsRequestToolsCapabilityr-   SetLevelRequestLoggingCapabilityCompleteRequestCompletionsCapabilityServerCapabilitiesrV   update_capabilities)	r7   r[   r\   prompts_capabilityresources_capabilitytools_capabilitylogging_capabilitycompletions_capabilityrk   s	   &&&      r0   rl   Server.get_capabilities   sE    "#!!% ##t'<'<<!&!8!8EYEiEi!j %%)>)>>#(#<#<-A-S-S$ 
 !!T%:%::$44AUAcAcd   D$9$99!&!8!8!:   D$9$99%*%@%@%B"//&*"&2.
 &&&'';;LIr3   c                   V ^8  d   QhRR/# )r)   rA   z8RequestContext[ServerSession, LifespanResultT, RequestT]r.   )r/   s   "r0   r1   rN      s     ! !	A!r3   c                *    \         P                  4       # )zFIf called outside of a request context, this will raise a LookupError.)r%   getr7   s   &r0   request_contextServer.request_context   s    
   r3   c                   V ^8  d   QhRR/# )r)   rA   r   r.   )r/   s   "r0   r1   rN      s     	+ 	+2 	+r3   c                    V P                   f&   \        W P                  V P                  4      V n         V P                   # )zuExperimental APIs for tasks and other features.

WARNING: These APIs are experimental and may change without notice.
)rV   r   rS   rT   r   s   &r0   rw   Server.experimental   s8     &&.*>tEZEZ\`\v\v*wD'***r3   c                	   a  R  V 3R llpV# )c                   V ^8  d   QhRR/# )r)   funczvCallable[[], Awaitable[list[types.Prompt]]] | Callable[[types.ListPromptsRequest], Awaitable[types.ListPromptsResult]]r.   )r/   s   "r0   r1   )Server.list_prompts.<locals>.__annotate__  s     	 	W	r3   c                   <a \         P                  R 4       \        V \        P                  4      oR V3R llpVSP
                  \        P                  &   V # )z)Registering handler for PromptListRequestc                   V ^8  d   QhRR/# )r)   reqztypes.ListPromptsRequestr.   )r/   s   "r0   r1   <Server.list_prompts.<locals>.decorator.<locals>.__annotate__  s     W W#; Wr3   c                   <"   S! V 4      G R j  xL
 p\        V\        P                  4      '       d   \        P                  ! V4      # \        P                  ! \        P                  ! VR7      4      #  Le5i)N)rs   )
isinstancerP   ListPromptsResultServerResultr   resultwrappers   & r0   handler7Server.list_prompts.<locals>.decorator.<locals>.handler  sY     &s|+fe&=&=>> --f55 !--e.E.Ef.UVV ,   A7A5A&A7)rW   rX   r   rP   ry   rS   r   r   r   r7   s   & @r0   	decorator&Server.list_prompts.<locals>.decorator  sN     LLDE)$0H0HIGW W ?FD!!%":":;Kr3   r.   r7   r   s   f r0   list_promptsServer.list_prompts      	 	( r3   c                	   a  R  V 3R llpV# )c                   V ^8  d   QhRR/# )r)   r   zHCallable[[str, dict[str, str] | None], Awaitable[types.GetPromptResult]]r.   )r/   s   "r0   r1   'Server.get_prompt.<locals>.__annotate__  s     
	 
	Z
	r3   c                   <a  \         P                  R 4       R V 3R llpVSP                  \        P                  &   S # )z(Registering handler for GetPromptRequestc                   V ^8  d   QhRR/# )r)   r   ztypes.GetPromptRequestr.   )r/   s   "r0   r1   :Server.get_prompt.<locals>.decorator.<locals>.__annotate__  s     6 6#9 6r3   c                   <"   S! V P                   P                  V P                   P                  4      G R j  xL
 p\        P                  ! V4      #  L5ir5   )paramsrG   	argumentsrP   r   )r   
prompt_getr   s   & r0   r   5Server.get_prompt.<locals>.decorator.<locals>.handler  s>     #'

9M9M#NN
))*55 Os   5AAA)rW   rX   rS   rP   GetPromptRequestr   r   r7   s   f r0   r   $Server.get_prompt.<locals>.decorator  s:     LLCD6 6 =DD!!%"8"89Kr3   r.   r   s   f r0   
get_promptServer.get_prompt  s    
	 
	 r3   c                	   a  R  V 3R llpV# )c                   V ^8  d   QhRR/# )r)   r   z|Callable[[], Awaitable[list[types.Resource]]] | Callable[[types.ListResourcesRequest], Awaitable[types.ListResourcesResult]]r.   )r/   s   "r0   r1   +Server.list_resources.<locals>.__annotate__)  s     	 	[	r3   c                   <a \         P                  R 4       \        V \        P                  4      oR V3R llpVSP
                  \        P                  &   V # )z,Registering handler for ListResourcesRequestc                   V ^8  d   QhRR/# )r)   r   ztypes.ListResourcesRequestr.   )r/   s   "r0   r1   >Server.list_resources.<locals>.decorator.<locals>.__annotate__1  s     [ [#= [r3   c                   <"   S! V 4      G R j  xL
 p\        V\        P                  4      '       d   \        P                  ! V4      # \        P                  ! \        P                  ! VR7      4      #  Le5i)N)rt   )r   rP   ListResourcesResultr   r   s   & r0   r   9Server.list_resources.<locals>.decorator.<locals>.handler1  sZ     &s|+fe&?&?@@ --f55 !--e.G.GRX.YZZ ,r   )rW   rX   r   rP   r{   rS   r   s   & @r0   r   (Server.list_resources.<locals>.decorator)  sO     LLGH)$0J0JKG[ [ AHD!!%"<"<=Kr3   r.   r   s   f r0   list_resourcesServer.list_resources(  r   r3   c                	   a  R  V 3R llpV# )c                   V ^8  d   QhRR/# )r)   r   z5Callable[[], Awaitable[list[types.ResourceTemplate]]]r.   )r/   s   "r0   r1   4Server.list_resource_templates.<locals>.__annotate__@  s     	 	Q 	r3   c                   <a  \         P                  R 4       R V 3R llpVSP                  \        P                  &   S # )z4Registering handler for ListResourceTemplatesRequestc                   V ^8  d   QhRR/# )r)   r@   r
   r.   )r/   s   "r0   r1   GServer.list_resource_templates.<locals>.decorator.<locals>.__annotate__C  s     j j jr3   c                   <"   S! 4       G R j  xL
 p\         P                  ! \         P                  ! VR7      4      #  L/5i)N)resourceTemplates)rP   r   ListResourceTemplatesResult)r@   	templatesr   s   & r0   r   BServer.list_resource_templates.<locals>.decorator.<locals>.handlerC  s1     "&&L	))%*K*K^g*hii )s   A >0A )rW   rX   rS   rP   ListResourceTemplatesRequestr   s   f r0   r   1Server.list_resource_templates.<locals>.decorator@  s;    LLOPj j IPD!!%"D"DEKr3   r.   r   s   f r0   list_resource_templatesServer.list_resource_templates?      	 	 r3   c                	   a  R  V 3R llpV# )c                   V ^8  d   QhRR/# )r)   r   zKCallable[[AnyUrl], Awaitable[str | bytes | Iterable[ReadResourceContents]]]r.   )r/   s   "r0   r1   *Server.read_resource.<locals>.__annotate__M  s     :	 :	]:	r3   c                   <a  \         P                  R 4       R V 3R llpVSP                  \        P                  &   S # )z+Registering handler for ReadResourceRequestc                   V ^8  d   QhRR/# )r)   r   ztypes.ReadResourceRequestr.   )r/   s   "r0   r1   =Server.read_resource.<locals>.decorator.<locals>.__annotate__R  s     2 2#< 2r3   c                  <a "   S! S P                   P                  4      G R j  xL
 pR	R V 3R lllpT;;;\        Rc ;e   w   M ;\        Rc ;e   w   M  M* p \        P
                  ! R\        ^R7       V! VR 4      pM ;\        Rc ;eg   w   pV Uu. uF,  pV! VP                  VP                  \        VRR 4      4      NK.  	  pp\        P                  ! \        P                  ! VR7      4      #    \        R\        V4       24      h\        P                  ! \        P                  ! V.R7      4      #  ELu upi 5i)
Nc               $    V ^8  d   QhRRRRRR/# )r)   datazstr | bytes	mime_typerJ   metazdict[str, Any] | Noner.   )r/   s   "r0   r1   NServer.read_resource.<locals>.decorator.<locals>.handler.<locals>.__annotate__U  s"        Sh r3   c           
       < Ve   RV/M/ pT ;;\         Rc ;e?   w   p  \        P                  ! RRSP                  P                  RT RT;'       g    R/VB #   ;\
        Rc ;e`   w   p \        P                  ! RRSP                  P                  R\        P                  ! V 4      P                  4       RT;'       g    R/VB #   R # )	N_metar.   uritextmimeTypez
text/plainblobzapplication/octet-stream)
rH   rP   TextResourceContentsr   r   bytesBlobResourceContentsbase64	b64encodedecode)r   r   r   meta_kwargsr   s   &&& r0   create_contentPServer.read_resource.<locals>.decorator.<locals>.handler.<locals>.create_contentU  s    EIEU7D/[]K*SU]#(#=#= $$'JJNN$%)$ *3)B)Bl$ #.	$  + -UW_#(#=#= $$'JJNN$%+%5%5d%;%B%B%D$ *3)P)P6P$ #.	$  -r3   r.   zdReturning str or bytes from read_resource is deprecated. Use Iterable[ReadResourceContents] instead.)
stacklevelr   )contentsz+Unexpected return type from read_resource: r5   )r   r   rH   r   warningswarnDeprecationWarningr   contentr   getattrrP   r   ReadResourceResult
ValueErrortype)	r   r   r   r   r   r   content_itemcontents_listr   s	   f       r0   r   8Server.read_resource.<locals>.decorator.<locals>.handlerR  s?    #CJJNN33 & 00 J.'(	 #1t"< 1 0/
 19	) 19 + , 4 4l6L6LgVbdjlpNq 19	 & )  %11!44)6   0 (+VW[\bWcVd)eff)),,") [ 4>)s#    ED<A2E2D?	A4E?E)rW   rX   rS   rP   ReadResourceRequestr   s   f r0   r   'Server.read_resource.<locals>.decoratorM  s<     LLFG2 2h @GD!!%";";<Kr3   r.   r   s   f r0   read_resourceServer.read_resourceL  s    :	 :	x r3   c                	   a  R  V 3R llpV# )c                   V ^8  d   QhRR/# )r)   r   z/Callable[[types.LoggingLevel], Awaitable[None]]r.   )r/   s   "r0   r1   .Server.set_logging_level.<locals>.__annotate__  s     	 	K 	r3   c                   <a  \         P                  R 4       R V 3R llpVSP                  \        P                  &   S # )z'Registering handler for SetLevelRequestc                   V ^8  d   QhRR/# )r)   r   ztypes.SetLevelRequestr.   )r/   s   "r0   r1   AServer.set_logging_level.<locals>.decorator.<locals>.__annotate__  s     ? ?#8 ?r3   c                   <"   S! V P                   P                  4      G R j  xL
  \        P                  ! \        P                  ! 4       4      #  L-5ir5   )r   levelrP   r   EmptyResultr   r   s   &r0   r   <Server.set_logging_level.<locals>.decorator.<locals>.handler  s=     3::++,,,))%*;*;*=>> -    AA.A)rW   rX   rS   rP   r   r   s   f r0   r   +Server.set_logging_level.<locals>.decorator  s8    LLBC? ? <CD!!%"7"78Kr3   r.   r   s   f r0   set_logging_levelServer.set_logging_level  r   r3   c                	   a  R  V 3R llpV# )c                   V ^8  d   QhRR/# r)   r   z#Callable[[AnyUrl], Awaitable[None]]r.   )r/   s   "r0   r1   /Server.subscribe_resource.<locals>.__annotate__       	 	? 	r3   c                   <a  \         P                  R 4       R V 3R llpVSP                  \        P                  &   S # )z(Registering handler for SubscribeRequestc                   V ^8  d   QhRR/# )r)   r   ztypes.SubscribeRequestr.   )r/   s   "r0   r1   BServer.subscribe_resource.<locals>.decorator.<locals>.__annotate__  s     ? ?#9 ?r3   c                   <"   S! V P                   P                  4      G R j  xL
  \        P                  ! \        P                  ! 4       4      #  L-5ir5   r   r   rP   r   r  r  s   &r0   r   =Server.subscribe_resource.<locals>.decorator.<locals>.handler  ;     3::>>***))%*;*;*=>> +r  )rW   rX   rS   rP   SubscribeRequestr   s   f r0   r   ,Server.subscribe_resource.<locals>.decorator  s8    LLCD? ? =DD!!%"8"89Kr3   r.   r   s   f r0   subscribe_resourceServer.subscribe_resource  r   r3   c                	   a  R  V 3R llpV# )c                   V ^8  d   QhRR/# r  r.   )r/   s   "r0   r1   1Server.unsubscribe_resource.<locals>.__annotate__  r  r3   c                   <a  \         P                  R 4       R V 3R llpVSP                  \        P                  &   S # )z*Registering handler for UnsubscribeRequestc                   V ^8  d   QhRR/# )r)   r   ztypes.UnsubscribeRequestr.   )r/   s   "r0   r1   DServer.unsubscribe_resource.<locals>.decorator.<locals>.__annotate__  s     ? ?#; ?r3   c                   <"   S! V P                   P                  4      G R j  xL
  \        P                  ! \        P                  ! 4       4      #  L-5ir5   r  r  s   &r0   r   ?Server.unsubscribe_resource.<locals>.decorator.<locals>.handler  r  r  )rW   rX   rS   rP   UnsubscribeRequestr   s   f r0   r   .Server.unsubscribe_resource.<locals>.decorator  s8    LLEF? ? ?FD!!%":":;Kr3   r.   r   s   f r0   unsubscribe_resourceServer.unsubscribe_resource  r   r3   c                	   a  R  V 3R llpV# )c                   V ^8  d   QhRR/# )r)   r   zpCallable[[], Awaitable[list[types.Tool]]] | Callable[[types.ListToolsRequest], Awaitable[types.ListToolsResult]]r.   )r/   s   "r0   r1   'Server.list_tools.<locals>.__annotate__  s     	 	S	r3   c                   <a \         P                  R 4       \        V \        P                  4      oR VV3R llpVSP
                  \        P                  &   V # )z(Registering handler for ListToolsRequestc                   V ^8  d   QhRR/# )r)   r   ztypes.ListToolsRequestr.   )r/   s   "r0   r1   :Server.list_tools.<locals>.decorator.<locals>.__annotate__  s     S S#9 Sr3   c                  <"   S! V 4      G R j  xL
 p\        V\        P                  4      '       dX   VP                   F1  p\	        VP
                  4       VSP                  VP
                  &   K3  	  \        P                  ! V4      # SP                  P                  4        V F1  p\	        VP
                  4       VSP                  VP
                  &   K3  	  \        P                  ! \        P                  ! VR7      4      #  L5i)N)ru   )	r   rP   ListToolsResultru   r   rG   rU   r   clear)r   r   toolr7   r   s   &  r0   r   5Server.list_tools.<locals>.decorator.<locals>.handler  s     &s|+ fe&;&;<< &3DII>6:((3 !- !--f55 $$**, &3DII>6:((3 !' !--e.C.C&.QRR! ,s   D	DC8D	)rW   rX   r   rP   r}   rS   r   s   & @r0   r   $Server.list_tools.<locals>.decorator  sN     LLCD)$0F0FGGS S& =DD!!%"8"89Kr3   r.   r   s   f r0   
list_toolsServer.list_tools  s    	 	< r3   c                    V ^8  d   QhRRRR/# )r)   error_messagerH   rA   types.ServerResultr.   )r/   s   "r0   r1   rN     s     
 
 
8J 
r3   c           	         \         P                  ! \         P                  ! \         P                  ! RVR7      .RR7      4      # )z3Create a ServerResult with an error CallToolResult.r   r   r   T)r   isError)rP   r   CallToolResultTextContent)r7   r?  s   &&r0   _make_error_resultServer._make_error_result  s9    !!  **]KL
 	
r3   c                    V ^8  d   QhRRRR/# )r)   	tool_namerH   rA   ztypes.Tool | Noner.   )r/   s   "r0   r1   rN     s      3 CT r3   c                d  "   WP                   9  de   \        P                  V P                  9   dF   \        P                  RV4       V P                  \        P                  ,          ! R4      G Rj  xL
  V P                   P                  V4      pVf   \        P                  RV4       V#  L;5i)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)rU   rP   r}   rS   rW   rX   r   warning)r7   rI  r9  s   && r0   _get_cached_tool_definition"Server._get_cached_tool_definition  s     
 ,,,%%)>)>>GS++E,B,BCDIII##I.<NNRT]^ Js   A0B02B.3<B0validate_inputTc                   V ^8  d   QhRR/# )r)   rN  r+   r.   )r/   s   "r0   r1   rN     s     a a4 ar3   c                  a a R V V3R llpV# )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                   V ^8  d   QhRR/# )r)   r   zCallable[..., Awaitable[UnstructuredContent | StructuredContent | CombinationContent | types.CallToolResult | types.CreateTaskResult]]r.   )r/   s   "r0   r1   &Server.call_tool.<locals>.__annotate__  s     P	 P		P	r3   c                   <a  \         P                  R 4       R V VV3R llpVSP                  \        P                  &   S # )z'Registering handler for CallToolRequestc                   V ^8  d   QhRR/# )r)   r   ztypes.CallToolRequestr.   )r/   s   "r0   r1   9Server.call_tool.<locals>.decorator.<locals>.__annotate__	  s     ?; ?;#8 ?;r3   c                J  <"    V P                   P                  pV P                   P                  ;'       g    / pS	P                  V4      G R j  xL
 pS
'       d+   V'       d#    \        P
                  ! W#P                  R7       S! W4      G R j  xL
 p\        V\        P                  4      '       d   \        P                  ! V4      # \        V\        P                  4      '       d   \        P                  ! V4      # \        V\        4      '       d$   \!        V4      ^8X  d   \#        \$        V4      w  rgM\        V\&        4      '       dA   \#        \(        V4      p\        P*                  ! R\,        P.                  ! V^R7      R7      .pML\1        VR4      '       d   \#        \2        V4      pR pM'S	P                  R\5        V4      P6                   24      # V'       dF   VP8                  e8   Vf   S	P                  R4      #  \        P
                  ! WsP8                  R7       \        P                  ! \        P                  ! \;        V4      VR
R7      4      #  EL  \        P                   d)   pS	P                  RTP                   24      u R p?# R p?ii ; i EL  \        P                   d)   pS	P                  R	TP                   24      u R p?# R p?ii ; i  \<         d    h \>         d%   pS	P                  \A        T4      4      u R p?# R p?ii ; i5i)N)instanceschemazInput validation error: r   )indentrB  __iter__z"Unexpected return type from tool: zOOutput validation error: outputSchema defined but no structured output returnedzOutput validation error: F)r   structuredContentrC  )!r   rG   r   rL  
jsonschemavalidateinputSchemaValidationErrorrF  messager   rP   rD  r   CreateTaskResulttuplelenr   r$   dictr"   rE  jsondumpshasattrr#   r   r:   outputSchemalistr   re   rH   )r   rI  r   r9  eresultsunstructured_contentmaybe_structured_contentr   r7   rN  s   &       r0   r   4Server.call_tool.<locals>.decorator.<locals>.handler	  s    >; #

I #

 4 4 : :I!%!A!A)!LLD &$c&//K[K[\
 %)$>>G
 "'5+?+?@@$11'::#GU-C-CDD$11'::#GU33G8IIMN`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$) _ M  *99 c#'#:#:=UVWV_V_U`;a#bbc ?B $.#=#= h'+'>'>AZ[\[d[dZe?f'g gh 3    ;223q6::;s   L#A
K& I K& K& %!I# K& J#9K& L#5K& L#CK& L#K& %"K& L#	!J& *5K& L# K& #J 8JJ K& L#J  K& &K#;KK#K& L#K##K& &L ;L <LL L#L  L#)rW   rX   rS   rP   CallToolRequest)r   r   r7   rN  s   f r0   r   #Server.call_tool.<locals>.decorator  s;     LLBC?; ?;B <CD!!%"7"78Kr3   r.   )r7   rN  r   s   fd r0   	call_toolServer.call_tool  s    P	 P	d r3   c                	   a  R  V 3R llpV# )c                   V ^8  d   QhRR/# )r)   r   zGCallable[[str | int, float, float | None, str | None], Awaitable[None]]r.   )r/   s   "r0   r1   2Server.progress_notification.<locals>.__annotate__P  s     	 	Y	r3   c                   <a  \         P                  R 4       R V 3R llpVSP                  \        P                  &   S # )z,Registering handler for ProgressNotificationc                   V ^8  d   QhRR/# )r)   r   ztypes.ProgressNotificationr.   )r/   s   "r0   r1   EServer.progress_notification.<locals>.decorator.<locals>.__annotate__U  s      #= r3   c                   <"   S! V P                   P                  V P                   P                  V P                   P                  V P                   P                  4      G R j  xL
  R #  L5ir5   )r   progressTokenprogresstotalr`  r  s   &r0   r   @Server.progress_notification.<locals>.decorator.<locals>.handlerU  sL     JJ,,JJ''JJ$$JJ&&	  s   AA+"A)#A+)rW   rX   rT   rP   ProgressNotificationr   s   f r0   r   /Server.progress_notification.<locals>.decoratorP  s;     LLGH  FMD&&u'A'ABKr3   r.   r   s   f r0   progress_notificationServer.progress_notificationO  s    	 	  r3   c                   a  R V 3R llpV# )z7Provides completions for prompts and resource templatesc                   V ^8  d   QhRR/# )r)   r   zCallable[[types.PromptReference | types.ResourceTemplateReference, types.CompletionArgument, types.CompletionContext | None], Awaitable[types.Completion | None]]r.   )r/   s   "r0   r1   'Server.completion.<locals>.__annotate__e  s     	 		r3   c                   <a  \         P                  R 4       R V 3R llpVSP                  \        P                  &   S # )z'Registering handler for CompleteRequestc                   V ^8  d   QhRR/# )r)   r   ztypes.CompleteRequestr.   )r/   s   "r0   r1   :Server.completion.<locals>.decorator.<locals>.__annotate__q  s      #8 r3   c           
     N  <"   S! V P                   P                  V P                   P                  V P                   P                  4      G R j  xL
 p\        P
                  ! \        P                  ! Ve   VR7      4      # \        P                  ! . R R R7      R7      4      #  LU5i)N)valuesr|  hasMore)
completion)r   refargumentcontextrP   r   CompleteResult
Completion)r   r  r   s   & r0   r   5Server.completion.<locals>.decorator.<locals>.handlerq  s     #'



8K8KSZZM_M_#``
))((%1 $.  #--RtTR  as   A
B%B#AB%)rW   rX   rS   rP   r   r   s   f r0   r   $Server.completion.<locals>.decoratore  s:     LLBC  <CD!!%"7"78Kr3   r.   r   s   f r0   r  Server.completionb  s    	 	2 r3   c          
     ,    V ^8  d   QhRRRRRRRRR	R/# )
r)   read_streamz5MemoryObjectReceiveStream[SessionMessage | Exception]write_streamz&MemoryObjectSendStream[SessionMessage]initialization_optionsr   raise_exceptionsr+   	statelessr.   )r/   s   "r0   r1   rN     s<     2- 2-J2- =2- !6	2- 2- 2-r3   c                	  "   \        4       ;_uu_4       GR j  xL
 pVP                  V P                  V 4      4      G R j  xL
 pVP                  \        VVVVR7      4      G R j  xL
 pV P                  '       d   V P                  P
                  MR p	V	e9   V	P                  V4       VP                  V	P                  4       4      G R j  xL
  \        P                  ! 4       ;_uu_4       GR j  xL
 p
 VP                    R j  xL
  p\        P                  RV4       V
P                  V P                  VVVV4       KA   EL- EL L L L\ LHDI T
P                  P!                  4        M  T
P                  P!                  4        i ; iR R R 4      GR j  xL 
  M  + GR j  xL 
 '       g   i     M; iR R R 4      GR j  xL 
  R #   + GR j  xL 
 '       g   i     R # ; i5i)N)r  zReceived message: %s)r   enter_async_contextrC   r   rV   task_supportconfigure_sessionrunanyiocreate_task_groupincoming_messagesrW   rX   
start_soon_handle_messagecancel_scopecancel)r7   r  r  r  r  r  stacklifespan_contextsessionr  tgr`  s   &&&&&&      r0   r  
Server.run  sw      "###u%*%>%>t}}T?R%SS!55 *'	 G HLGbGbGb466CChlL'..w7//0@0@0BCCC..000B-)0)B)B 	 	g%;WE 00##,,- $S D0	)B OO**,BOO**,# 100000# $#####s  G.EG.$GE	%G'E(G>AGE%G3E4G7F"	9E1E
	E
E
9E1G.	GGGGE
E1F"	1FF"	GFG"F;(F+)
F;4F;6G>G.	G
G.G+	G
G+	#G+	%	G.c               (    V ^8  d   QhRRRRRRRR/# )	r)   r`  z`RequestResponder[types.ClientRequest, types.ServerResult] | types.ClientNotification | Exceptionr  r   r  r   r  r+   r.   )r/   s   "r0   r1   rN     s8     [ [q[ [ *	[
 [r3   c                	  "   \         P                  ! R R7      ;_uu_ 4       pT;;\        Rc ;eK   w  \        P                  Rc ;e4   w  rg T;_uu_ 4        V P                  WW#V4      G Rj  xL
  RRR4       M  ;\        P                  Rc ;e   w  p V P                  V4      G Rj  xL
  MM \        Rc ;eA   w   \        P                  RV 24       VP                  RRR	R
7      G Rj  xL
  V'       d   Vh V F8  p	\        P                  RV	P                  P                  V	P                  4       K:  	  RRR4       R#  L  + '       g   i     K[  ; i L Lo  + '       g   i     R# ; i5i)T)recordrequestN)rootr.   z Received exception from stream: errorzInternal Server Errorzmcp.server.exception_handler)r  r   rW   zWarning: %s: %s)r   catch_warningsr   rP   ClientRequest_handle_requestClientNotification_handle_notificationre   rW   r  send_log_messageinfocategoryr:   r`  )
r7   r`  r  r  r  wr   	respondernotifyrK  s
   &&&&&     r0   r  Server._handle_message  s3     $$D11QY%Le.A.A.KY""227\lmmm # Z ;U--::33F;;; ;[LL#CG9!MN!22%4= 3   
 (% ! -w/?/?/H/H'//Z # 21 n # < 211sl    E44E E	-E.E	29E +E,AE .E/	E ;AE <
E4E	E
E E  E1	+	E4c          
     ,    V ^8  d   QhRRRRRRRRR	R
/# )r)   r`  z9RequestResponder[types.ClientRequest, types.ServerResult]r   ztypes.ClientRequestTyper  r   r  r   r  r+   r.   )r/   s   "r0   r1   rN     sC     \& \&J\& %\& 	\&
 *\& \&r3   c                	   "   \         P                  R \        V4      P                  4       V P                  P                  \        V4      4      ;p'       Ed   \         P                  R\        V4      P                  4       Rp RpRp	Rp
VP                  ec   \        VP                  \        4      '       dC   VP                  P                  pVP                  P                  p	VP                  P                  p
VP                  '       d   VP                  P                  MRpV P                  '       d   V P                  P                   MRpRp\#        VR4      '       d&   VP$                  e   \'        VP$                  RR4      p\(        P+                  \-        VP.                  VP0                  VV\3        VVVVR7      VV	V
R7      4      pV! V4      G Rj  xL
 pTe   \(        P?                  T4       M&\B        PD                  ! \B        PH                  R	R
7      p VPK                  V4      G Rj  xL
  \         P                  R4       R#  Lu  \4         d   pTP6                  p Rp?LRp?i\8        P:                  ! 4        dS    TP<                  '       d?   \         P                  RTP.                  4        Te   \(        P?                  T4       R# R# h \@         d8   pT'       d   Th\B        PD                  ! ^ \G        T4      RR7      p Rp?EL9Rp?ii ; i  Te   \(        P?                  T4       i i ; i EL  \8        PL                  \8        PN                  3 d%    \         P                  RTP.                  4        R# i ; i5i)zProcessing request of type %szDispatching request of type %sNr   task)task_metadata_client_capabilities_session_task_support)r  close_sse_streamclose_standalone_sse_streamz4Request %s cancelled - duplicate response suppressed)coder`  r   zMethod not found)r  r`  z*Response for %s dropped - transport closedzResponse sent)(rW   r  r   r:   rS   r   rX   message_metadatar   r   r   r  r  client_paramsrk   rV   r  rg  r   r   r%   setr   
request_idrequest_metar   r   r  r  get_cancelled_exc_class	cancelledresetre   rP   	ErrorDatarH   METHOD_NOT_FOUNDrespondBrokenResourceErrorClosedResourceError)r7   r`  r   r  r  r  r   tokenrequest_dataclose_sse_stream_cbclose_standalone_sse_stream_cbclient_capabilitiesr  r  responseerrs   &&&&&&          r0   r  Server._handle_request  s     	3T#Y5G5GH++//S	::7:LL949;M;MNE;-#&*#15.++7J,,.C= = $+#;#;#K#KL*1*B*B*S*S'5<5M5M5i5i2 MTLaLaLag&;&;&H&Hgk#KOKfKfKft::GGlp $3))cjj.D$+CJJ$EM#"**,,($*71D%,*6	 !-)<4R" ")-* $%%e,++*H

	//(+++ 	_%S . %99002 $$$ KKN**  $%%e, %  P#I ??3s8$OP $%%e, % ,))5+D+DE 	 LLEwGYGYZ	s   BNBI )I 7B#I II A N M 4L?5M 9NI LI.)L  .L
L L=L  >NN	L%L&,LL  LL   L<<N?M ANN
NNc                   V ^8  d   QhRR/# )r)   r  r
   r.   )r/   s   "r0   r1   rN   -  s     O O Or3   c                	0  "   V P                   P                  \        V4      4      ;p'       d=   \        P	                  R \        V4      P
                  4        V! V4      G Rj  xL
  R# R#  L  \         d    \        P                  R4        R# i ; i5i)z#Dispatching notification of type %sNz*Uncaught exception in notification handler)rT   r   r   rW   rX   r:   re   	exception)r7   r  r   s   && r0   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)
rV   rU   rM   rK   rC   rG   rT   rS   rI   rL   )NN)FF)F)r:   r;   r<   r=   rC   r8   rm   rl   propertyr   rw   r   r   r   r   r  r  r"  r.  r<  rF  rL  rq  r  r  r  r  r  r  r>   r.   r3   r0   rE   rE      s     ##'"&)- 54
:,\ ! ! 	+ 	+..=~B
 a$ aF&<2-h[6\&|O Or3   rE   c                    V ^8  d   QhRRRR/# )r)   r  ztypes.PingRequestrA   r@  r.   )r/   s   "r0   r1   r1   7  s     3 3!2 37I 3r3   c                \   "   \         P                  ! \         P                  ! 4       4      # 5ir5   )rP   r   r  r  s   &r0   rR   rR   7  s     e//122s   *,)N__conditional_annotations____doc__
__future__r   _annotationsr   contextvarsre  rv   r   collections.abcr   r   r   r   
contextlibr   r   r	   typingr
   r   r   r   r  r\  anyio.streams.memoryr   r   pydanticr   typing_extensionsr   	mcp.typesrP   'mcp.server.experimental.request_contextr    mcp.server.lowlevel.experimentalr   #mcp.server.lowlevel.func_inspectionr    mcp.server.lowlevel.helper_typesr   mcp.server.modelsr   mcp.server.sessionr   mcp.shared.contextr   mcp.shared.exceptionsr   r   mcp.shared.messager   r   mcp.shared.sessionr   mcp.shared.tool_name_validationr   	getLoggerr:   rW   r   r!   rd  rH   r"   __annotations__ContentBlockr#   rb  r$   
ContextVarr%   r'   rC   rE   rR   )r  s   @r0   <module>r     s*  AF 3      H H W W 0 0   R  %  @ A C A 3 , - G D / G			8	$+S9:s+  $CH~ 9 -!)%*<*<!= Y = %&9;L&L M I M P[OeOefsOtL t	+ 	+ 	 	k
OW_h./ k
O\3r3   