§
    öŽ’j:  ã                   ó¼   — d Z ddlmZ ddlmZ ddlmZmZ ddlZddl	m
Z
 ddlmZ ddlmZ dd	lmZ dd
lmZ ddlmZmZ ddlmZ e G d„ d¦  «        ¦   «         ZdS )zÑ
TaskSupport - Configuration for experimental task support.

This module provides the TaskSupport class which encapsulates all the
infrastructure needed for task-augmented requests: store, queue, and handler.
é    )ÚAsyncIterator)Úasynccontextmanager)Ú	dataclassÚfieldN)Ú	TaskGroup)ÚTaskResultHandler)Únew_session_scope)ÚServerSession)ÚInMemoryTaskStore)ÚInMemoryTaskMessageQueueÚTaskMessageQueue)Ú	TaskStorec                   óò   — e Zd ZU dZeed<   eed<    ed¬¦  «        Ze	ed<    edd¬¦  «        Z
edz  ed	<   dd„Zed
efd„¦   «         Zed
ed         fd„¦   «         Zddœdeded
dfd„Zedd„¦   «         ZdS )ÚTaskSupporta   
    Configuration for experimental task support.

    Encapsulates the task store, message queue, result handler, and task group
    for spawning background work.

    When enabled on a server, this automatically:
    - Configures response routing for each session
    - Provides default handlers for task operations
    - Manages a task group for background task execution

    Example:
        # Simple in-memory setup
        server.experimental.enable_tasks()

        # Custom store/queue for distributed systems
        server.experimental.enable_tasks(
            store=RedisTaskStore(redis_url),
            queue=RedisTaskMessageQueue(redis_url),
        )
    ÚstoreÚqueueF)ÚinitÚhandlerN)r   ÚdefaultÚ_task_groupÚreturnc                 óD   — t          | j        | j        ¦  «        | _        dS )z/Create the result handler from store and queue.N)r   r   r   r   ©Úselfs    úRC:\Projects\DSCons\.venv\Lib\site-packages\mcp\server\experimental\task_support.pyÚ__post_init__zTaskSupport.__post_init__4   s   € å(¨¬°T´ZÑ@Ô@ˆŒˆˆó    c                 ó<   — | j         €t          d¦  «        ‚| j         S )z‚Get the task group for spawning background work.

        Raises:
            RuntimeError: If not within a run() context
        Nz7TaskSupport not running. Ensure Server.run() is active.)r   ÚRuntimeErrorr   s    r   Ú
task_groupzTaskSupport.task_group8   s%   € ð ÔÐ#ÝÐXÑYÔYÐYØÔÐr   c                óÊ   K  — t          j        ¦   «         4 ƒd{V —†}|| _        	 dW V — d| _        n# d| _        w xY w	 ddd¦  «        ƒd{V —† dS # 1 ƒd{V —†swxY w Y   dS )a"  
        Run the task support lifecycle.

        This creates a task group for spawning background task work.
        Called automatically by Server.run().

        Usage:
            async with task_support.run():
                # Task group is now available
                ...
        N)ÚanyioÚcreate_task_groupr   )r   Útgs     r   ÚrunzTaskSupport.runC   s  è è € õ Ô*Ñ,Ô,ð 	(ð 	(ð 	(ð 	(ð 	(ð 	(ð 	(°Ø!ˆDÔð(Ø����à#'�Ô Ð ø 4�Ô Ð'Ð'Ð'Ð'Ð'ð	(ð 	(ð 	(ñ 	(ô 	(ð 	(ð 	(ð 	(ð 	(ð 	(ð 	(ð 	(ð 	(ð 	(ð 	(ð 	(ð 	(ð 	(ð 	(ð 	(ð 	(ð 	(ð 	(ð 	(øøøð 	(ð 	(ð 	(ð 	(ð 	(ð 	(s#   œA¥2ªA²	;»AÁ
AÁA)Ú	statelessÚsessionr&   c                óˆ   — |                      | j        ¦  «         |s#|j        }|j        €t	          ¦   «         |_        dS dS dS )a}  
        Configure a session for task support.

        This registers the result handler as a response router so that
        responses to queued requests (elicitation, sampling) are routed
        back to the waiting resolvers.

        It also assigns the session a task session scope. Task IDs generated
        by `run_task()` embed this scope, and the default task handlers only
        operate on tasks created by the requesting session. Stateless sessions
        are not assigned a scope: each request runs on a fresh session, so a
        task created by one request could never be retrieved by a later one if
        tasks were bound to the session that created them.

        Called automatically by Server.run() for each new session.

        Args:
            session: The session to configure
            stateless: Whether the session belongs to a stateless server run
        N)Úadd_response_routerr   Ú_experimentalÚtask_session_scoper	   )r   r'   r&   Úfeaturess       r   Úconfigure_sessionzTaskSupport.configure_sessionW   sZ   € ð* 	×#Ò# D¤LÑ1Ô1Ð1Øð 	BØÔ,ˆHØÔ*Ð2Ý.?Ñ.AÔ.A�Ô+Ð+Ð+ð	Bð 	Bà2Ð2r   c                 óL   —  | t          ¦   «         t          ¦   «         ¬¦  «        S )a  
        Create in-memory task support.

        Suitable for development, testing, and single-process servers.
        For distributed systems, provide custom store and queue implementations.

        Returns:
            TaskSupport configured with in-memory store and queue
        )r   r   )r   r   )Úclss    r   Ú	in_memoryzTaskSupport.in_memoryr   s/   € ð ˆsÝ#Ñ%Ô%Ý*Ñ,Ô,ð
ñ 
ô 
ð 	
r   )r   N)r   r   )Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   Ú__annotations__r   r   r   r   r   r   r   Úpropertyr    r   r   r%   r
   Úboolr-   Úclassmethodr0   © r   r   r   r      sL  € € € € € € ðð ð, ÐÐÑØÐÐÑØ!& ¨EÐ!2Ñ!2Ô!2€GÐÐ2Ð2Ñ2Ø$) E¨u¸dÐ$CÑ$CÔ$C€K�˜TÑ!ÐCÐCÑCðAð Að Að Að ð ˜Ið  ð  ð  ñ „Xð ð ð(˜=¨Ô.ð (ð (ð (ñ Ôð(ð& NSð Bð Bð B¨ð BÀdð BÐW[ð Bð Bð Bð Bð6 ð
ð 
ð 
ñ „[ð
ð 
ð 
r   r   )r4   Úcollections.abcr   Ú
contextlibr   Údataclassesr   r   r"   Ú	anyio.abcr   Ú+mcp.server.experimental.task_result_handlerr   Ú"mcp.server.experimental.task_scoper	   Úmcp.server.sessionr
   Ú2mcp.shared.experimental.tasks.in_memory_task_storer   Ú+mcp.shared.experimental.tasks.message_queuer   r   Ú#mcp.shared.experimental.tasks.storer   r   r9   r   r   ú<module>rD      s"  ððð ð *Ð )Ð )Ð )Ð )Ð )Ø *Ð *Ð *Ð *Ð *Ð *Ø (Ð (Ð (Ð (Ð (Ð (Ð (Ð (à €€€Ø Ð Ð Ð Ð Ð à IÐ IÐ IÐ IÐ IÐ IØ @Ð @Ð @Ð @Ð @Ð @Ø ,Ð ,Ð ,Ð ,Ð ,Ð ,Ø PÐ PÐ PÐ PÐ PÐ PØ bÐ bÐ bÐ bÐ bÐ bÐ bÐ bØ 9Ð 9Ð 9Ð 9Ð 9Ð 9ð ðh
ð h
ð h
ð h
ð h
ñ h
ô h
ñ „ðh
ð h
ð h
r   