
    )`i                         d Z ddlmZ ddlmZmZmZ ddlmZ ddl	m
Z
mZ ddlmZmZ ddlmZ erddlmZ  ed	ej        
          Z G d d          ZdS )u8  
Experimental server session features for server→client task operations.

This module provides the server-side equivalent of ExperimentalClientFeatures,
allowing the server to send task-augmented requests to the client and poll for results.

WARNING: These APIs are experimental and may change without notice.
    )AsyncIterator)TYPE_CHECKINGAnyTypeVarN)validate_sampling_tools!validate_tool_use_result_messages)"require_task_augmented_elicitationrequire_task_augmented_sampling)poll_until_terminal)ServerSessionResultT)boundc                      e Zd ZdZddZdedej        fdZded	e	e
         de
fd
Zdedeej                 fdZdddedej        dedej        fdZdddddddddd	deej                 dedededz  dej        dz  dedz  dee         dz  deeef         dz  dej        dz  deej                 dz  dej        dz  dej        fdZdS )!ExperimentalServerSessionFeaturesu  
    Experimental server session features for server→client task operations.

    This provides the server-side equivalent of ExperimentalClientFeatures,
    allowing the server to send task-augmented requests to the client and
    poll for results.

    WARNING: These APIs are experimental and may change without notice.

    Access via session.experimental:
        result = await session.experimental.elicit_as_task(...)
    sessionr   returnNc                     || _         d S )N)_session)selfr   s     |/home/jaya/work/projects/VOICE-AGENT/VIET/agent-env/lib/python3.11/site-packages/mcp/server/experimental/session_features.py__init__z*ExperimentalServerSessionFeatures.__init__)   s        task_idc           	         K   | j                             t          j        t          j        t          j        |                              t          j                   d{V S )z
        Send tasks/get to the client to get task status.

        Args:
            task_id: The task identifier

        Returns:
            GetTaskResult containing the task status
        taskIdparamsN)r   send_requesttypesServerRequestGetTaskRequestGetTaskRequestParamsGetTaskResult)r   r   s     r   get_taskz*ExperimentalServerSessionFeatures.get_task,   sq       ]// 4E<V^e<f<f<f g g ghh
 
 
 
 
 
 
 
 	
r   result_typec           	         K   | j                             t          j        t          j        t          j        |                              |           d{V S )a  
        Send tasks/result to the client to retrieve the final result.

        Args:
            task_id: The task identifier
            result_type: The expected result type

        Returns:
            The task result, validated against result_type
        r   r   N)r   r   r    r!   GetTaskPayloadRequestGetTaskPayloadRequestParams)r   r   r&   s      r   get_task_resultz1ExperimentalServerSessionFeatures.get_task_result;   sn       ]// ;5CdlsCtCtCt u u uvv
 
 
 
 
 
 
 
 	
r   c                P   K   t          | j        |          2 3 d{V }|W V  6 dS )a  
        Poll a client task until it reaches terminal status.

        Yields GetTaskResult for each poll, allowing the caller to react to
        status changes. Exits when task reaches a terminal status.

        Respects the pollInterval hint from the client.

        Args:
            task_id: The task identifier

        Yields:
            GetTaskResult for each poll
        N)r   r%   )r   r   statuss      r   	poll_taskz+ExperimentalServerSessionFeatures.poll_taskO   sU       0wGG 	 	 	 	 	 	 	&LLLLL HGGs   %i`  ttlmessagerequestedSchemar/   c                  K   | j         j        r| j         j        j        nd}t          |           | j                             t          j        t          j        t          j        ||t          j	        |                                        t
          j
                   d{V }|j        j        }|                     |          2 3 d{V }6 |                     |t
          j                   d{V S )a  
        Send a task-augmented elicitation to the client and poll until complete.

        The client will create a local task, process the elicitation asynchronously,
        and return the result when ready. This method handles the full flow:
        1. Send elicitation with task field
        2. Receive CreateTaskResult from client
        3. Poll client's task until terminal
        4. Retrieve and return the final ElicitResult

        Args:
            message: The message to present to the user
            requestedSchema: Schema defining the expected response
            ttl: Task time-to-live in milliseconds

        Returns:
            The client's elicitation response

        Raises:
            McpError: If client doesn't support task-augmented elicitation
        Nr.   )r0   r1   taskr   )r   client_paramscapabilitiesr	   r   r    r!   ElicitRequestElicitRequestFormParamsTaskMetadataCreateTaskResultr3   r   r-   r*   ElicitResult)r   r0   r1   r/   client_capscreate_resultr   _s           r   elicit_as_taskz0ExperimentalServerSessionFeatures.elicit_as_taska   s9     8 CG-B]gdm1>>cg*;777"m88# 8 '(7"/C888      "
 
 
 
 
 
 
 
  $+~~g.. 	 	 	 	 	 	 	! / ))'53EFFFFFFFFFs   
C)	r/   system_promptinclude_contexttemperaturestop_sequencesmetadatamodel_preferencestoolstool_choicemessages
max_tokensr?   r@   rA   rB   rC   rD   rE   rF   c       
         B  K   | j         j        r| j         j        j        nd}t          |           t	          ||
|           t          |           | j                             t          j        t          j	        t          j
        ||||||||	|
|t          j        |                                        t          j                   d{V }|j        j        }|                     |          2 3 d{V }6 |                     |t          j                   d{V S )a5  
        Send a task-augmented sampling request and poll until complete.

        The client will create a local task, process the sampling request
        asynchronously, and return the result when ready.

        Args:
            messages: The conversation messages for sampling
            max_tokens: Maximum tokens in the response
            ttl: Task time-to-live in milliseconds
            system_prompt: Optional system prompt
            include_context: Context inclusion strategy
            temperature: Sampling temperature
            stop_sequences: Stop sequences
            metadata: Additional metadata
            model_preferences: Model selection preferences
            tools: Optional list of tools the LLM can use during sampling
            tool_choice: Optional control over tool usage behavior

        Returns:
            The sampling result from the client

        Raises:
            McpError: If client doesn't support task-augmented sampling or tools
            ValueError: If tool_use or tool_result message structure is invalid
        Nr.   )rG   	maxTokenssystemPromptincludeContextrA   stopSequencesrC   modelPreferencesrE   
toolChoicer3   r   )r   r4   r5   r
   r   r   r   r    r!   CreateMessageRequestCreateMessageRequestParamsr8   r9   r3   r   r-   r*   CreateMessageResult)r   rG   rH   r/   r?   r@   rA   rB   rC   rD   rE   rF   r;   r<   r   r=   s                   r   create_message_as_taskz8ExperimentalServerSessionFeatures.create_message_as_task   sr     R CG-B]gdm1>>cg'444UK@@@)(333"m88* ;!)",%2'6$/&4!)):##."/C888     " "%
 
 
 
 
 
 
 
*  $+~~g.. 	 	 	 	 	 	 	! / ))'53LMMMMMMMMMs   2C:)r   r   r   N)__name__
__module____qualname____doc__r   strr    r$   r%   typer   r*   r   r-   ElicitRequestedSchemaintr:   r>   listSamplingMessageIncludeContextfloatdictr   ModelPreferencesTool
ToolChoicerR   rS    r   r   r   r      s5               
c 
e.A 
 
 
 


 ']
 
	
 
 
 
(s }U=P/Q    . 1G 1G 1G1G 41G
 1G 
	1G 1G 1G 1Gp $(7;$(+/*.;?)-/3HN HN HNu,-HN 	HN
 HN TzHN -4HN T\HN S	D(HN sCx.4'HN !1D8HN EJ$&HN %,HN 
	"HN HN HN HN HN HNr   r   )rW   collections.abcr   typingr   r   r   	mcp.typesr    mcp.server.validationr   r   *mcp.shared.experimental.tasks.capabilitiesr	   r
   %mcp.shared.experimental.tasks.pollingr   mcp.server.sessionr   Resultr   r   rd   r   r   <module>rm      s    * ) ) ) ) ) . . . . . . . . . .       \ \ \ \ \ \ \ \        F E E E E E 1000000
')5<
0
0
0AN AN AN AN AN AN AN AN AN ANr   