
    Ki                         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y)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j0                  dz  deej2                     dz  dej4                  dz  dej6                  fdZy)!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(...)
    returnNc                     || _         y )N)_session)selfsessions     t/home/jay/workspace/scripts/.codegraph-venv/lib/python3.12/site-packages/mcp/server/experimental/session_features.py__init__z*ExperimentalServerSessionFeatures.__init__)   s	        task_idc           	         K   | j                   j                  t        j                  t        j                  t        j
                  |                  t        j                         d{   S 7 w)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,   sW      ]]// 4 4E<V<V^e<f gh
 
 	
 
s   A)A2+A0,A2result_typec           	         K   | j                   j                  t        j                  t        j                  t        j
                  |                  |       d{   S 7 w)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;   sQ      ]]// ; ;5CdCdlsCt uv
 
 	
 
s   AA$A"A$c                \   K   t        | j                  |      2 3 d{   }| 7 
6 yw)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   s0      0wG 	 	&L	Gs   ,*(*,*,i`  ttlmessagerequestedSchemar/   c                T  K   | j                   j                  r | j                   j                  j                  nd}t        |       | j                   j	                  t        j                  t        j                  t        j                  ||t        j                  |                        t
        j                         d{   }|j                  j                  }| j                  |      2 3 d{   }
7 57 6 | j                  |t
        j                         d{  7  S w)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   s     8 CG--B]B]dmm11>>cg*;7"mm88## 88 '(7"//C8 ""
 
  $$++~~g. 	 	!!
	. ))'53E3EFFFFsB   CD(C:+D(0C>4C<5C>8D(<C>>$D("D%#D()	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       
           K   | j                   j                  r | j                   j                  j                  nd}t        |       t	        ||
|       t        |       | j                   j                  t        j                  t        j                  t        j                  ||||||||	|
|t        j                  |                        t        j                         d{   }|j                  j                  }| j                  |      2 3 d{   }
7 57 6 | j!                  |t        j"                         d{  7  S w)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   s#    R CG--B]B]dmm11>>cg'4UK@)(3"mm88** ;;!)",%2'6$/&4!)):##."//C8" ""%
 
*  $$++~~g. 	 	!1
.	. ))'53L3LMMMMsB   C"E$D%+EDDDED$EEE)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      s    
c 
e.A.A 


 ']
 
	
(s }U=P=P/Q . 1G1G 441G
 1G 
		1Gp $(7;$(+/*.;?)-/3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 !11D8HN EJJ$&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      sJ    * . .  \ F0
)5<<
0AN ANr   