§
    ­eîi !  ã                   óÐ   — d 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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 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.
é    )Ú	AwaitableÚCallable)Ú	dataclassÚfield)ÚAny)ÚServerTaskContext)ÚTaskSupport)ÚServerSession)ÚMcpError)ÚMODEL_IMMEDIATE_RESPONSE_KEYÚis_terminal)
ÚMETHOD_NOT_FOUNDÚTASK_FORBIDDENÚTASK_REQUIREDÚClientCapabilitiesÚCreateTaskResultÚ	ErrorDataÚResultÚTaskExecutionModeÚTaskMetadataÚToolc            	       ó„  — 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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    úk/home/div/.hermes/hermes-agent/venv/lib/python3.11/site-packages/mcp/server/experimental/request_context.pyÚis_taskzExperimental.is_task1   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_tasks6   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_mode=   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ÚtaskSupportr2   )r#   r3   r+   r0   s       r$   Úvalidate_for_toolzExperimental.validate_for_toolk   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,   r0   s      r$   Úcan_use_toolzExperimental.can_use_tool€   s+   € ð Ð/¥ˆØ•=Ò Ð ¨Ô)CÐ Ø�5Øˆtr&   )Útask_idÚmodel_immediate_responseÚworkr:   r;   c             ƒ   óÀ  ‡‡	K  — | j         €t          d¦  «        ‚| j        €t          d¦  «        ‚| j        €t          d¦  «        ‚| j         }|j        }|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 )as  
        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.

        Args:
            work: Async function that does the actual work
            task_id: Optional task ID (generated if not provided)
            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.
        NzHTask 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)N)r   r>   Ú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*r>   Ú_meta)r    N© )r   ÚRuntimeErrorr   r   Ú
task_groupr?   Úcreate_taskr   rA   rB   Ú
start_soonr   r   )
r#   r<   r:   r;   ÚsupportrP   r>   rL   ÚmetarK   s
    `       @r$   Úrun_taskzExperimental.run_task’   s9  øøè è € ðj ÔÐ%ÝÐiÑjÔjÐjØŒ=Ð ÝÐ7Ñ8Ô8Ð8ØÔÐ%ÝðAñô ð ð
 Ô$ˆàÔ'ˆ
à”]×.Ò.¨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   r2   r   r7   r9   r   r   r   r   rH   r   rU   rN   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ð\Nð \Nð \NàÐ)Ð*¨I°fÔ,=Ð=Ô>ð\Nð �t‘ð	\Nð
 #&¨¡*ð\Nð 
ð\Nð \Nð \Nð \Nð \Nð \Nr&   r   N) rY   Úcollections.abcr   r   Údataclassesr   r   Útypingr   Ú$mcp.server.experimental.task_contextr   Ú$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   rN   r&   r$   ú<module>rf      s}  ððð ð 0Ð /Ð /Ð /Ð /Ð /Ð /Ð /Ø (Ð (Ð (Ð (Ð (Ð (Ð (Ð (Ø Ð Ð Ð Ð Ð à BÐ BÐ BÐ BÐ BÐ BØ <Ð <Ð <Ð <Ð <Ð <Ø ,Ð ,Ð ,Ð ,Ð ,Ð ,Ø *Ð *Ð *Ð *Ð *Ð *Ø [Ð [Ð [Ð [Ð [Ð [Ð [Ð [ðð ð ð ð ð ð ð ð ð ð ð ð ð ð ð ð ð ð ð ð ð ð ð ð ðLNð LNð LNð LNð LNñ LNô LNñ „ðLNð LNð LNr&   