Ë
    ïñþ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y)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            	       óX  — 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y)Ú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    úg/root/aria/mcps/aria-brain/venv/lib/python3.12/site-packages/mcp/server/experimental/request_context.pyÚis_taskzExperimental.is_task1   s   € ð ×!Ñ!¨Ð-Ð-ó    c                 óL   — | j                   €y| 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*   € ð ×$Ñ$Ð,ØØ×(Ñ(×.Ñ.°dÐ:Ð:r&   T©Úraise_errorÚtool_task_moder+   c                óÖ   — |xs 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=   sj   € ð4 Ò/¤ˆà"&ˆà”=Ò ¨¯ªÜÜ%ØFô‰Eð ”^Ò#¨¯ªÜÜ%ØNôˆEð
 Ð¡Ü˜5“/Ð!àˆr&   Útoolc                óp   — |j                   r|j                   j                  nd}| j                  ||¬«      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   s1   € ð$ .2¯^ª^ˆt�~‰~×)Ò)ÀˆØ×&Ñ& t¸Ð&ÓEÐEr&   c                 óD   — |xs t         }|t        k(  r| j                  syy)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Ò)CØØr&   )Útask_idÚmodel_immediate_responseÚworkr:   r;   c             ƒ   ó
  ‡‡	K  — | j                   €t        d«      ‚| j                  €t        d«      ‚| j                  €t        d«      ‚| j                   }|j                  }|j
                  j                  | j                  |«      ƒ d{  –—† }t        ||j
                  | j                  |j                  |j                  ¬«      Š	d	ˆ	ˆfd„}|j                  |«       d}|�t        |i}t        d
d|i|rd|i¤ŽS i ¤ŽS 7 Œu­w)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Úhandlerc               “   ód  •K  — 	  ‰‰«      ƒ d {  –—† } t        ‰j                  j                  «      s‰j                  | «      ƒ d {  –—†  y y 7 Œ>7 Œ# t        $ rQ}t        ‰j                  j                  «      s(‰j                  t        |«      «      ƒ d {  –—†7   Y d }~y Y d }~y d }~ww xY w­w)N)r   r>   ÚstatusÚcompleteÚ	ExceptionÚfailÚstr)ÚresultÚeÚtask_ctxr<   s     €€r$   Úexecutez&Experimental.run_task.<locals>.executeß   sˆ   øè ø€ ð0Ù# H›~×-�Ü" 8§=¡=×#7Ñ#7Ô8Ø"×+Ñ+¨FÓ3×3Ñ3ð 9ð .øà3ùÜò 0Ü" 8§=¡=×#7Ñ#7Ô8Ø"Ÿ-™-¬¨A«Ó/×/Ö/ô 9ûð0üs\   ƒB0…A �A‘7A ÁAÁ	A ÁB0ÁA ÁA Á	B-Á<B(ÂBÂB(Â
B0Â(B-Â-B0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’   s	  ùè ø€ ðj ×ÑÐ%ÜÐiÓjÐjØ�=‰=Ð ÜÐ7Ó8Ð8Ø×ÑÐ%ÜðAóð ð
 ×$Ñ$ˆà×'Ñ'ˆ
à—]‘]×.Ñ.¨t×/AÑ/AÀ7ÓK×Kˆä$ØØ—-‘-Ø—M‘MØ—-‘-Ø—O‘Oô
ˆö	0ð 	×Ñ˜gÔ&à&*ˆØ#Ð/Ü0Ð2JÐKˆDäÑM TÐMÁ¨g°t¨_ÑMÐMÈ"ÑMÐMð3 Lús   „BDÂDÂA6D)Ú__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   !   sa  … ñð *.€M�< $Ñ&Ó-Ù6;ÀDÈuÔ6UÐÐ,¨tÑ3ÓUÙ%*°4¸eÔ%D€Hˆm˜dÑ"ÓDÙ(-°dÀÔ(G€M�; Ñ%ÓGàð.˜ò .ó ð.ð ð; tò ;ó ð;ð !ò	,à)¨DÑ0ð,ð ð	,ð
 
�TÑ	ó,ðd !ò	FàðFð ð	Fð
 
�TÑ	óFð*Ð+<¸tÑ+Cð Èó ð, #Ø/3ò\NàÐ)Ð*¨I°fÑ,=Ð=Ñ>ð\Nð �t‘ð	\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      sR   ðñ÷ 0ß (Ý å BÝ <Ý ,Ý *ß [÷÷ ÷ ð ÷LNð LNó ñLNr&   