§
    öŽ’j°)  ã                   óø   — d Z ddlZddlmZmZ ddlmZmZ ddlm	Z	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 ddlmZ ddlmZmZ ddlmZmZmZmZmZm Z m!Z!m"Z"m#Z#m$Z$ dZ%e G d„ d¦  «        ¦   «         Z&dS )a	  
Experimental request context features.

This module provides the Experimental class which gives access to experimental
features within a request context, such as task-augmented request handling.

WARNING: These APIs are experimental and may change without notice.
é    N)Ú	AwaitableÚCallable)Ú	dataclassÚfield)ÚAnyÚoverload)Ú
deprecated)ÚServerTaskContext)Úscoped_task_id)ÚTaskSupport)ÚServerSession)ÚMcpError)ÚMODEL_IMMEDIATE_RESPONSE_KEYÚis_terminal)
ÚMETHOD_NOT_FOUNDÚTASK_FORBIDDENÚTASK_REQUIREDÚClientCapabilitiesÚCreateTaskResultÚ	ErrorDataÚResultÚTaskExecutionModeÚTaskMetadataÚToolaR  Passing an explicit task_id to run_task is deprecated. A task created with an explicit ID is not associated with the session that created it: any requestor that presents the ID can read its status and result or cancel it, and it never appears in tasks/list. Omit task_id to let the SDK generate an ID associated with the creating session.c                   óL  — e Zd ZU dZdZedz  ed<    edd¬¦  «        Ze	dz  ed<    edd¬¦  «        Z
edz  ed<    edd¬¦  «        Zedz  ed<   ed	efd
„¦   «         Zed	efd„¦   «         Zddœdedz  ded	edz  fd„Zddœdeded	edz  fd„Zdedz  d	efd„Zedddœdeegee         f         dddedz  d	efd„¦   «         Ze e e!¦  «        ddœdeegee         f         dededz  d	efd„¦   «         ¦   «         Zdddœdeegee         f         dedz  dedz  d	efd„ZdS )ÚExperimentala  
    Experimental features context for task-augmented requests.

    Provides helpers for validating task execution compatibility and
    running tasks with automatic lifecycle management.

    WARNING: This API is experimental and may change without notice.
    NÚtask_metadataF)ÚdefaultÚreprÚ_client_capabilitiesÚ_sessionÚ_task_supportÚreturnc                 ó   — | j         duS )z(Check if this request is task-augmented.N)r   ©Úselfs    úUC:\Projects\DSCons\.venv\Lib\site-packages\mcp\server\experimental\request_context.pyÚis_taskzExperimental.is_task=   s   € ð Ô!¨Ð-Ð-ó    c                 ó0   — | j         €dS | j         j        duS )z*Check if the client declared task support.NF)r    Útasksr%   s    r'   Úclient_supports_tasksz"Experimental.client_supports_tasksB   s#   € ð Ô$Ð,Ø�5ØÔ(Ô.°dÐ:Ð:r)   T©Úraise_errorÚtool_task_moder.   c                óä   — |pt           }d}|t          k    r| j        st          t          d¬¦  «        }n(|t           k    r| j        rt          t          d¬¦  «        }|�|rt          |¦  «        ‚|S )aì  
        Validate that the request is compatible with the tool's task execution mode.

        Per MCP spec:
        - "required": Clients MUST invoke as task. Server returns -32601 if not.
        - "forbidden" (or None): Clients MUST NOT invoke as task. Server returns -32601 if they do.
        - "optional": Either is acceptable.

        Args:
            tool_task_mode: The tool's execution.taskSupport value
                ("forbidden", "optional", "required", or None)
            raise_error: If True, raises McpError on validation failure. If False, returns ErrorData.

        Returns:
            None if valid, ErrorData if invalid and raise_error=False

        Raises:
            McpError: If invalid and raise_error=True
        Nz,This tool requires task-augmented invocation)ÚcodeÚmessagez4This tool does not support task-augmented invocation)r   r   r(   r   r   r   )r&   r/   r.   ÚmodeÚerrors        r'   Úvalidate_task_modezExperimental.validate_task_modeI   s�   € ð4 Ð/¥ˆà"&ˆà•=Ò Ð ¨¬Ð ÝÝ%ØFðñ ô ˆEˆEð •^Ò#Ð#¨¬Ð#ÝÝ%ØNðñ ô ˆEð
 Ð ÐÝ˜5‘/”/Ð!àˆr)   Útoolc                óZ   — |j         r|j         j        nd}|                      ||¬¦  «        S )a  
        Validate that the request is compatible with the given tool.

        Convenience wrapper around validate_task_mode that extracts the mode from a Tool.

        Args:
            tool: The Tool definition
            raise_error: If True, raises McpError on validation failure.

        Returns:
            None if valid, ErrorData if invalid and raise_error=False
        Nr-   )Ú	executionÚtaskSupportr5   )r&   r6   r.   r3   s       r'   Úvalidate_for_toolzExperimental.validate_for_toolw   s3   € ð$ .2¬^ÐEˆtŒ~Ô)Ð)ÀˆØ×&Ò& t¸Ð&ÑEÔEÐEr)   c                 ó@   — |pt           }|t          k    r	| j        sdS dS )a‰  
        Check if this client can use a tool with the given task mode.

        Useful for filtering tool lists or providing warnings.
        Returns False if tool requires "required" but client doesn't support tasks.

        Args:
            tool_task_mode: The tool's execution.taskSupport value

        Returns:
            True if the client can use this tool, False otherwise
        FT)r   r   r,   )r&   r/   r3   s      r'   Úcan_use_toolzExperimental.can_use_toolŒ   s+   € ð Ð/¥ˆØ•=Ò Ð ¨Ô)CÐ Ø�5Øˆtr)   )Útask_idÚmodel_immediate_responseÚworkr=   r>   c             ƒ   ó
   K  — d S ©N© ©r&   r?   r=   r>   s       r'   Úrun_taskzExperimental.run_taskž   s   è è € ð ˜3r)   )r>   c             ƒ   ó
   K  — d S rA   rB   rC   s       r'   rD   zExperimental.run_task§   s   è è € ð ˜3r)   c             ƒ   óR  ‡‡K  — |�!t          j        t          t          d¬¦  «         | j        €t          d¦  «        ‚| j        €t          d¦  «        ‚| j        €t          d¦  «        ‚| j        }|j        }|€$| j        j	        }|j
        }|�t          |¦  «        }|j                             | j        |¦  «        ƒ d{V —†}t          ||j        | j        |j        |j        ¬¦  «        Šdˆˆfd	„}	|                     |	¦  «         d}
|�	t$          |i}
t'          dd
|i|
rd|
ini ¤ŽS )aV	  
        Create a task, spawn background work, and return CreateTaskResult immediately.

        This is the recommended way to handle task-augmented tool calls. It:
        1. Creates a task in the store
        2. Spawns the work function in a background task
        3. Returns CreateTaskResult immediately

        The work function receives a ServerTaskContext with:
        - elicit() for sending elicitation requests
        - create_message() for sampling requests
        - update_status() for progress updates
        - complete()/fail() for finishing the task

        When work() returns a Result, the task is auto-completed with that result.
        If work() raises an exception, the task is auto-failed.

        Generated task IDs embed the session's task scope so that the default
        task handlers only serve the task to the session that created it. An
        explicitly provided `task_id` is used verbatim and is not associated
        with the session, so any session can access it through the default
        handlers; passing one is deprecated for that reason.

        Args:
            work: Async function that does the actual work
            task_id: Deprecated. Optional task ID, used verbatim and not
                associated with the creating session. Omit it to let the SDK
                generate one.
            model_immediate_response: Optional string to include in _meta as
                io.modelcontextprotocol/model-immediate-response

        Returns:
            CreateTaskResult to return to the client

        Raises:
            RuntimeError: If task support is not enabled or task_metadata is missing

        Example:
            @server.call_tool()
            async def handle_tool(name: str, args: dict):
                ctx = server.request_context

                async def work(task: ServerTaskContext) -> CallToolResult:
                    result = await task.elicit(
                        message="Are you sure?",
                        requestedSchema={"type": "object", ...}
                    )
                    confirmed = result.content.get("confirm", False)
                    return CallToolResult(content=[TextContent(text="Done" if confirmed else "Cancelled")])

                return await ctx.experimental.run_task(work)

        WARNING: This API is experimental and may change without notice.
        Né   )Ú
stacklevelzHTask support not enabled. Call server.experimental.enable_tasks() first.zSession not available.zgRequest is not task-augmented (no task field in params). The client must send a task-augmented request.)ÚtaskÚstoreÚsessionÚqueueÚhandlerr#   c               “   óZ  •K  — 	  ‰‰¦  «        ƒ d {V —†} t          ‰j        j        ¦  «        s‰                     | ¦  «        ƒ d {V —† d S d S # t          $ rR}t          ‰j        j        ¦  «        s.‰                     t          |¦  «        ¦  «        ƒ d {V —† Y d }~d S Y d }~d S d }~ww xY wrA   )r   rI   ÚstatusÚcompleteÚ	ExceptionÚfailÚstr)ÚresultÚeÚtask_ctxr?   s     €€r'   Úexecutez&Experimental.run_task.<locals>.execute  s  øè è € ð0Ø#˜t H™~œ~Ð-Ð-Ð-Ð-Ð-Ð-�Ý" 8¤=Ô#7Ñ8Ô8ð 4Ø"×+Ò+¨FÑ3Ô3Ð3Ð3Ð3Ð3Ð3Ð3Ð3Ð3Ð3ð4ð 4øåð 0ð 0ð 0Ý" 8¤=Ô#7Ñ8Ô8ð 0Ø"Ÿ-š-­¨A©¬Ñ/Ô/Ð/Ð/Ð/Ð/Ð/Ð/Ð/Ð/Ð/Ð/Ð/Ð/Ð/ð0ð 0ð 0ð 0ð 0ð 0øøøøð0øøøs   …AA Á
B*ÁAB%Â%B*rI   Ú_meta)r#   NrB   )ÚwarningsÚwarnÚEXPLICIT_TASK_ID_DEPRECATIONÚDeprecationWarningr"   ÚRuntimeErrorr!   r   Ú
task_groupÚ_experimentalÚtask_session_scoper   rJ   Úcreate_taskr
   rL   rM   Ú
start_soonr   r   )r&   r?   r=   r>   Úsupportr^   ÚfeaturesÚsession_scoperI   rW   ÚmetarV   s    `         @r'   rD   zExperimental.run_task±   sƒ  øøè è € ðz ÐÝŒMÕ6Õ8JÐWXÐYÑYÔYÐYØÔÐ%ÝÐiÑjÔjÐjØŒ=Ð ÝÐ7Ñ8Ô8Ð8ØÔÐ%ÝðAñô ð ð
 Ô$ˆàÔ'ˆ
àˆ?Ø”}Ô2ˆHØ$Ô7ˆMØÐ(Ý(¨Ñ7Ô7�à”]×.Ò.¨tÔ/AÀ7ÑKÔKÐKÐKÐKÐKÐKÐKˆå$ØØ”-Ø”MØ”-Ø”Oð
ñ 
ô 
ˆð	0ð 	0ð 	0ð 	0ð 	0ð 	0ð 	0ð 	×Ò˜gÑ&Ô&Ð&à&*ˆØ#Ð/Ý0Ð2JÐKˆDåÐMÐM TÐMÀÐ-L¨g°t¨_¨_È"ÐMÐMÐMr)   )"Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r   Ú__annotations__r   r    r   r!   r   r"   r   ÚpropertyÚboolr(   r,   r   r   r5   r   r:   r<   r   r   r
   r   r   rS   r   rD   r	   r[   rB   r)   r'   r   r   -   s   € € € € € € ðð ð *.€M�< $Ñ&Ð-Ð-Ñ-Ø6;°eÀDÈuÐ6UÑ6UÔ6UÐÐ,¨tÑ3ÐUÐUÑUØ%* U°4¸eÐ%DÑ%DÔ%D€Hˆm˜dÑ"ÐDÐDÑDØ(-¨°dÀÐ(GÑ(GÔ(G€M�; Ñ%ÐGÐGÑGàð.˜ð .ð .ð .ñ „Xð.ð ð; tð ;ð ;ð ;ñ „Xð;ð !ð	,ð ,ð ,à)¨DÑ0ð,ð ð	,ð
 
�TÑ	ð,ð ,ð ,ð ,ðd !ð	Fð Fð FàðFð ð	Fð
 
�TÑ	ðFð Fð Fð Fð*Ð+<¸tÑ+Cð Èð ð ð ð ð$ ð
 Ø/3ðð ð àÐ)Ð*¨I°fÔ,=Ð=Ô>ðð ð	ð
 #&¨¡*ðð 
ðð ð ñ „Xðð Ø€ZÐ,Ñ-Ô-ð 04ðð ð àÐ)Ð*¨I°fÔ,=Ð=Ô>ðð ð	ð
 #&¨¡*ðð 
ðð ð ñ .Ô-ñ „Xðð #Ø/3ðlNð lNð lNàÐ)Ð*¨I°fÔ,=Ð=Ô>ðlNð �t‘ð	lNð
 #&¨¡*ðlNð 
ðlNð lNð lNð lNð lNð lNr)   r   )'rj   rY   Úcollections.abcr   r   Údataclassesr   r   Útypingr   r   Útyping_extensionsr	   Ú$mcp.server.experimental.task_contextr
   Ú"mcp.server.experimental.task_scoper   Ú$mcp.server.experimental.task_supportr   Úmcp.server.sessionr   Úmcp.shared.exceptionsr   Ú%mcp.shared.experimental.tasks.helpersr   r   Ú	mcp.typesr   r   r   r   r   r   r   r   r   r   r[   r   rB   r)   r'   ú<module>ry      sº  ððð ð €€€Ø /Ð /Ð /Ð /Ð /Ð /Ð /Ð /Ø (Ð (Ð (Ð (Ð (Ð (Ð (Ð (Ø  Ð  Ð  Ð  Ð  Ð  Ð  Ð  à (Ð (Ð (Ð (Ð (Ð (à BÐ BÐ BÐ BÐ BÐ BØ =Ð =Ð =Ð =Ð =Ð =Ø <Ð <Ð <Ð <Ð <Ð <Ø ,Ð ,Ð ,Ð ,Ð ,Ð ,Ø *Ð *Ð *Ð *Ð *Ð *Ø [Ð [Ð [Ð [Ð [Ð [Ð [Ð [ðð ð ð ð ð ð ð ð ð ð ð ð ð ð ð ð ð ð ð ð ð ð ð ð!ð ð ðoNð oNð oNð oNð oNñ oNô oNñ „ðoNð oNð oNr)   