+
    wiY                        R t ^ RIHt ^ RIt^ RIHt ^ RIHt ^ RIH	t	H
t
 ^ RIHt ^ RIHtHt ^ RIHt ^ R	IHtHt ^ R
IHt ^ RIHt ^ RIHtHtHtHtHtHtH t H!t!H"t"H#t#H$t$H%t%H&t&H't'H(t(H)t)H*t*H+t+H,t,H-t-H.t.H/t/H0t0  ! R R4      t1R# )z
ServerTaskContext - Server-integrated task context with elicitation and sampling.

This wraps the pure TaskContext and adds server-specific functionality:
- Elicitation (task.elicit())
- Sampling (task.create_message())
- Status notifications
)AnyN)TaskResultHandler)ServerSession)validate_sampling_tools!validate_tool_use_result_messages)McpError)"require_task_augmented_elicitationrequire_task_augmented_sampling)TaskContext)QueuedMessageTaskMessageQueue)Resolver)	TaskStore)INVALID_REQUESTTASK_STATUS_INPUT_REQUIREDTASK_STATUS_WORKINGClientCapabilitiesCreateMessageResultCreateTaskResultElicitationCapabilityElicitRequestedSchemaElicitResult	ErrorDataIncludeContextModelPreferences	RequestIdResultSamplingCapabilitySamplingMessageServerNotificationTaskTaskMetadataTaskStatusNotificationTaskStatusNotificationParamsTool
ToolChoicec                     a  ] tR t^5t o RtRR/V 3R lR llt]V 3R lR l4       t]V 3R lR	 l4       t]V 3R
 lR l4       t	V 3R lR lt
RR/V 3R lR lltRR/V 3R lR lltRR/V 3R lR lltV 3R lR ltV 3R lR ltV 3R lR ltV 3R lR ltV 3R lR ltR RR!RR"RR#RR$RR%RR&RR'R/V 3R( lR) lltR*R+/V 3R, lR- lltR*R+R RR!RR"RR#RR$RR%RR&RR'R/	V 3R. lR/ lltR0tV tR# )1ServerTaskContexta  
Server-integrated task context with elicitation and sampling.

This wraps a pure TaskContext and adds server-specific functionality:
- elicit() for sending elicitation requests to the client
- create_message() for sampling requests
- Status notifications via the session

Example:
    async def my_task_work(task: ServerTaskContext) -> CallToolResult:
        await task.update_status("Starting...")

        result = await task.elicit(
            message="Continue?",
            requestedSchema={"type": "object", "properties": {"ok": {"type": "boolean"}}}
        )

        if result.content.get("ok"):
            return CallToolResult(content=[TextContent(text="Done!")])
        else:
            return CallToolResult(content=[TextContent(text="Cancelled")])
handlerNc                F   < V ^8  d   QhRS[ RS[RS[RS[RS[R,          /# )   taskstoresessionqueuer(   N)r    r   r   r   r   )format__classdict__s   "X/home/ubuntu/.local/lib/python3.14/site-packages/mcp/server/experimental/task_context.py__annotate__ServerTaskContext.__annotate__M   sD        	
    #T)    c               X    \        WR7      V n        W0n        W@n        WPn        W n        R# )a  
Create a ServerTaskContext.

Args:
    task: The Task object
    store: The task store
    session: The server session
    queue: The message queue for elicitation/sampling
    handler: The result handler for response routing (required for elicit/create_message)
)r+   r,   N)r
   _ctx_session_queue_handler_store)selfr+   r,   r-   r.   r(   s   &$$$$$r1   __init__ServerTaskContext.__init__M   s$    &  T7	r4   c                    < V ^8  d   QhRS[ /# r*   return)str)r/   r0   s   "r1   r2   r3   i   s     ! ! !r4   c                .    V P                   P                  # )zThe task identifier.)r6   task_idr;   s   &r1   rC   ServerTaskContext.task_idh   s     yy   r4   c                    < V ^8  d   QhRS[ /# r?   )r    )r/   r0   s   "r1   r2   r3   n   s      d r4   c                .    V P                   P                  # )zThe current task state.)r6   r+   rD   s   &r1   r+   ServerTaskContext.taskm   s     yy~~r4   c                    < V ^8  d   QhRS[ /# r?   )bool)r/   r0   s   "r1   r2   r3   s   s     & &d &r4   c                .    V P                   P                  # )z(Whether cancellation has been requested.)r6   is_cancelledrD   s   &r1   rL   ServerTaskContext.is_cancelledr   s     yy%%%r4   c                   < V ^8  d   QhRR/# r*   r@   N )r/   r0   s   "r1   r2   r3   w   s     ) )d )r4   c                :    V P                   P                  4        R# )z"Request cancellation of this task.N)r6   request_cancellationrD   s   &r1   rR   &ServerTaskContext.request_cancellationw   s    		&&(r4   notifyTc                *   < V ^8  d   QhRS[ RS[RR/# )r*   messagerT   r@   NrA   rJ   )r/   r0   s   "r1   r2   r3   }   s"     
, 
,3 
,4 
,4 
,r4   c                  "   V P                   P                  V4      G Rj  xL
  V'       d   V P                  4       G Rj  xL
  R# R#  L( L
5i)z
Update the task's status message.

Args:
    message: The new status message
    notify: Whether to send a notification to the client
N)r6   update_status_send_notification)r;   rV   rT   s   &&$r1   rY   ServerTaskContext.update_status}   sA      ii%%g...))+++  	/+&   AA
AAA	AAc                *   < V ^8  d   QhRS[ RS[RR/# )r*   resultrT   r@   N)r   rJ   )r/   r0   s   "r1   r2   r3      s"     
, 
,V 
, 
, 
,r4   c                  "   V P                   P                  V4      G Rj  xL
  V'       d   V P                  4       G Rj  xL
  R# R#  L( L
5i)z
Mark the task as completed with the given result.

Args:
    result: The task result
    notify: Whether to send a notification to the client
N)r6   completerZ   )r;   r^   rT   s   &&$r1   r`   ServerTaskContext.complete   sA      ii  ((())+++  	)+r\   c                *   < V ^8  d   QhRS[ RS[RR/# )r*   errorrT   r@   NrW   )r/   r0   s   "r1   r2   r3      s"     
, 
, 
, 
, 
,r4   c                  "   V P                   P                  V4      G Rj  xL
  V'       d   V P                  4       G Rj  xL
  R# R#  L( L
5i)z
Mark the task as failed with an error message.

Args:
    error: The error message
    notify: Whether to send a notification to the client
N)r6   failrZ   )r;   rc   rT   s   &&$r1   re   ServerTaskContext.fail   s?      iinnU###))+++  	$+r\   c                   < V ^8  d   QhRR/# rO   rP   )r/   r0   s   "r1   r2   r3      s     
 
$ 
r4   c                V  "   V P                   P                  pV P                  P                  \	        \        \        VP                  VP                  VP                  VP                  VP                  VP                  VP                  R7      R7      4      4      G Rj  xL
  R#  L5i)z.Send a task status notification to the client.)taskIdstatusstatusMessage	createdAtlastUpdatedAtttlpollInterval)paramsN)r6   r+   r7   send_notificationr   r"   r#   ri   rj   rk   rl   rm   rn   ro   )r;   r+   s   & r1   rZ   $ServerTaskContext._send_notification   s}     yy~~mm--&7#{{#{{&*&8&8"&..&*&8&8 HH%)%6%6

 	
 	
s   BB) B'!B)c                   < V ^8  d   QhRR/# rO   rP   )r/   r0   s   "r1   r2   r3      s      t r4   c                    V P                   P                  \        \        4       R7      4      '       g   \	        \        \        RR7      4      hR# )z)Check if the client supports elicitation.)elicitationz.Client does not support elicitation capabilitycoderV   N)r7   check_client_capabilityr   r   r   r   r   rD   s   &r1   _check_elicitation_capability/ServerTaskContext._check_elicitation_capability   sB    }}445GTiTk5lmm(L  nr4   c                   < V ^8  d   QhRR/# rO   rP   )r/   r0   s   "r1   r2   r3      s      D r4   c                    V P                   P                  \        \        4       R7      4      '       g   \	        \        \        RR7      4      hR# )z&Check if the client supports sampling.)samplingz+Client does not support sampling capabilityrv   N)r7   rx   r   r   r   r   r   rD   s   &r1   _check_sampling_capability,ServerTaskContext._check_sampling_capability   sB    }}445GQcQe5fgg(I  hr4   c                ,   < V ^8  d   QhRS[ RS[RS[/# )r*   rV   requestedSchemar@   )rA   r   r   )r/   r0   s   "r1   r2   r3      s)     ? ?? /? 
	?r4   c                b  "   V P                  4        V P                  f   \        R4      hV P                  P	                  V P
                  \        R7      G Rj  xL
  V P                  P                  VVV P
                  R7      pVP                  p\        4       pWPP                  P                  V&   \        RVVVR7      pV P                  P                  V P
                  V4      G Rj  xL
   VP                  4       G Rj  xL
 pV P                  P	                  V P
                  \         R7      G Rj  xL
  \"        P$                  ! V4      #  L Lh LQ L   \&        P(                  ! 4        d7    T P                  P	                  T P
                  \         R7      G Rj  xL 
  h i ; i5i)a  
Send an elicitation request via the task message queue.

This method:
1. Checks client capability
2. Updates task status to "input_required"
3. Queues the elicitation request
4. Waits for the response (delivered via tasks/result round-trip)
5. Updates task status back to "working"
6. Returns the result

Args:
    message: The message to present to the user
    requestedSchema: Schema defining the expected response structure

Returns:
    The client's response

Raises:
    McpError: If client doesn't support elicitation capability
NzEhandler is required for elicit(). Pass handler= to ServerTaskContext.rj   )rV   r   related_task_idrequesttyperV   resolveroriginal_request_id)ry   r9   RuntimeErrorr:   update_taskrC   r   r7   _build_elicit_form_requestidr   _pending_requestsr   r8   enqueuewaitr   r   model_validateanyioget_cancelled_exc_class)r;   rV   r   r   
request_idr   queuedresponse_datas   &&&     r1   elicitServerTaskContext.elicit   sf    4 	**,== fgg kk%%dll;U%VVV --::+ LL ; 

 !(


-5Z6>''
3 *	
 kk!!$,,777
	"*--/1M++))$,,?R)SSS..}==3 	W& 	8 2S,,. 	 ++))$,,?R)SSS	sn   AF/EBF/-E.F/3E E2E 9E:E F/F/E E AF,$F'%F,,F/c                2   < V ^8  d   QhRS[ RS[ RS[ RS[/# )r*   rV   urlelicitation_idr@   )rA   r   )r/   r0   s   "r1   r2   r3     s9     C CC C 	C
 
Cr4   c                d  "   V P                  4        V P                  f   \        R4      hV P                  P	                  V P
                  \        R7      G Rj  xL
  V P                  P                  VVVV P
                  R7      pVP                  p\        4       pW`P                  P                  V&   \        RVVVR7      pV P                  P                  V P
                  V4      G Rj  xL
   VP                  4       G Rj  xL
 pV P                  P	                  V P
                  \         R7      G Rj  xL
  \"        P$                  ! V4      #  L Lh LQ L   \&        P(                  ! 4        d7    T P                  P	                  T P
                  \         R7      G Rj  xL 
  h i ; i5i)aL  
Send a URL mode elicitation request via the task message queue.

This directs the user to an external URL for out-of-band interactions
like OAuth flows, credential collection, or payment processing.

This method:
1. Checks client capability
2. Updates task status to "input_required"
3. Queues the elicitation request
4. Waits for the response (delivered via tasks/result round-trip)
5. Updates task status back to "working"
6. Returns the result

Args:
    message: Human-readable explanation of why the interaction is needed
    url: The URL the user should navigate to
    elicitation_id: Unique identifier for tracking this elicitation

Returns:
    The client's response indicating acceptance, decline, or cancellation

Raises:
    McpError: If client doesn't support elicitation capability
    RuntimeError: If handler is not configured
NzIhandler is required for elicit_url(). Pass handler= to ServerTaskContext.r   )rV   r   r   r   r   r   )ry   r9   r   r:   r   rC   r   r7   _build_elicit_url_requestr   r   r   r   r8   r   r   r   r   r   r   r   )	r;   rV   r   r   r   r   r   r   r   s	   &&&&     r1   
elicit_urlServerTaskContext.elicit_url  sh    @ 	**,== jkk kk%%dll;U%VVV --99) LL	 : 
 !(


-5Z6>''
3 *	
 kk!!$,,777	"*--/1M++))$,,?R)SSS..}==5 	W( 	8 2S,,. 	++))$,,?R)SSS	sn   AF0EBF0.E/F04E E2E :E;E F0F0E E AF-%F(&F--F0system_promptinclude_contexttemperaturestop_sequencesmetadatamodel_preferencestoolstool_choicec                  < V ^8  d   QhRS[ S[,          RS[RS[R,          RS[R,          RS[R,          RS[ S[,          R,          RS[S[S[3,          R,          R	S[R,          R
S[ S[	,          R,          RS[
R,          RS[/# )r*   messages
max_tokensr   Nr   r   r   r   r   r   r   r@   listr   intrA   r   floatdictr   r   r$   r%   r   )r/   r0   s   "r1   r2   r3   P  s     \ \'\ 	\
 Tz\ ($.\ T\\ S	D(\ sCx.4'\ ,d2\ DzD \  $&\ 
\r4   c       	          "   V P                  4        V P                  P                  '       d!   V P                  P                  P                  MRp\	        WV
4       \        V4       V P                  f   \        R4      hV P                  P                  V P                  \        R7      G Rj  xL
  V P                  P                  VVVVVVVVV	V
V P                  R7      pVP                  p\        4       pWP                  P                  V&   \!        RVVVR7      pV P"                  P%                  V P                  V4      G Rj  xL
   VP'                  4       G Rj  xL
 pV P                  P                  V P                  \(        R7      G Rj  xL
  \*        P,                  ! V4      #  EL Li LR L!  \.        P0                  ! 4        d7    T P                  P                  T P                  \(        R7      G Rj  xL 
  h i ; i5i)a  
Send a sampling request via the task message queue.

This method:
1. Checks client capability
2. Updates task status to "input_required"
3. Queues the sampling request
4. Waits for the response (delivered via tasks/result round-trip)
5. Updates task status back to "working"
6. Returns the result

Args:
    messages: The conversation messages for sampling
    max_tokens: Maximum tokens in the response
    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 sampling capability or tools
    ValueError: If tool_use or tool_result message structure is invalid
NzMhandler is required for create_message(). Pass handler= to ServerTaskContext.r   )r   r   r   r   r   r   r   r   r   r   r   r   r   )r~   r7   client_paramscapabilitiesr   r   r9   r   r:   r   rC   r   _build_create_message_requestr   r   r   r   r8   r   r   r   r   r   r   r   )r;   r   r   r   r   r   r   r   r   r   r   client_capsr   r   r   r   r   s   &&$$$$$$$$$      r1   create_message ServerTaskContext.create_messageP  s    X 	'')BF--B]B]B]dmm11>>cgK@)(3== noo kk%%dll;U%VVV --==!'+#)/# LL > 
 !(


-5Z6>''
3 *	
 kk!!$,,777
	"*--/1M++))$,,?R)SSS&55mDDC 	W6 	8 2S,,. 	 ++))$,,?R)SSS	sn   B-H/F10BH
F4HF: #F6$2F: F8F: 0H4H6F: 8F: :AH
HH

Hrn   i`  c                2   < V ^8  d   QhRS[ RS[RS[RS[/# )r*   rV   r   rn   r@   )rA   r   r   r   )r/   r0   s   "r1   r2   r3     s9     L LL /L
 L 
Lr4   c          	       "   V P                   P                  '       d!   V P                   P                  P                  MRp\        V4       V P                  f   \        R4      hV P                  P                  V P                  \        R7      G Rj  xL
  V P                   P                  VVV P                  \        VR7      R7      pVP                  p\        4       pWpP                  P                  V&   \        RVVVR7      pV P                   P#                  V P                  V4      G Rj  xL
   VP%                  4       G Rj  xL
 p	\&        P(                  ! V	4      p
V
P*                  P,                  pV P                   P.                  P1                  V4        Rj  xL
  pK   EL L} Lf LDT P                   P.                  P3                  T\4        4      G Rj  xL 
 pT P                  P                  T P                  \6        R7      G Rj  xL 
  T#   \8        P:                  ! 4        d7    T P                  P                  T P                  \6        R7      G Rj  xL 
  h i ; i5i)a  
Send a task-augmented elicitation via the queue, then poll client.

This is for use inside a task-augmented tool call when you want the client
to handle the elicitation as its own task. The elicitation request is queued
and delivered when the client calls tasks/result. After the client responds
with CreateTaskResult, we poll the client's task until complete.

Args:
    message: The message to present to the user
    requestedSchema: Schema defining the expected response structure
    ttl: Task time-to-live in milliseconds for the client's task

Returns:
    The client's elicitation response

Raises:
    McpError: If client doesn't support task-augmented elicitation
    RuntimeError: If handler is not configured
Nz(handler is required for elicit_as_task()r   rn   )rV   r   r   r+   r   r   )r7   r   r   r   r9   r   r:   r   rC   r   r   r!   r   r   r   r   r8   r   r   r   r   r+   ri   experimental	poll_taskget_task_resultr   r   r   r   )r;   rV   r   rn   r   r   r   r   r   r   create_resultclient_task_id_r^   s   &&&$          r1   elicit_as_task ServerTaskContext.elicit_as_task  s     6 CG--B]B]B]dmm11>>cg*;7== IJJ kk%%dll;U%VVV--::+ LL#&	 ; 
 !(


-5Z6>''
3 *	
 kk!!$,,777	"*--/1M,;;MJM*//66N  ==55??O  a; 	W& 	8 2
O  ==55EE  F
 ++))$,,?R)SSSM,,. 	++))$,,?R)SSS	s   BI2F,BI21F/2I27H 
F1AH  F5$F3%F5(H ,I2/I21H 3F55.H #G&$3H HH I2AI/'I*(I//I2c                  < V ^8  d   QhRS[ S[,          RS[RS[RS[R,          RS[R,          RS[R,          RS[ S[,          R,          R	S[S[S[3,          R,          R
S[R,          RS[ S[	,          R,          RS[
R,          RS[/# )r*   r   r   rn   r   Nr   r   r   r   r   r   r   r@   r   )r/   r0   s   "r1   r2   r3     s     h h'h 	h
 h Tzh ($.h T\h S	D(h sCx.4'h ,d2h DzD h  $&h 
hr4   c       
        &  "   V P                   P                  '       d!   V P                   P                  P                  MRp\        V4       \	        WV4       \        V4       V P                  f   \        R4      hV P                  P                  V P                  \        R7      G Rj  xL
  V P                   P                  VVVVVVVV	V
VV P                  \        VR7      R7      pVP                  p\        4       pWP                  P                   V&   \#        RVVVR7      pV P$                  P'                  V P                  V4      G Rj  xL
   VP)                  4       G Rj  xL
 p\*        P,                  ! V4      pVP.                  P0                  pV P                   P2                  P5                  V4        Rj  xL
  pK   EL! L} Lf LDT P                   P2                  P7                  T\8        4      G Rj  xL 
 pT P                  P                  T P                  \:        R7      G Rj  xL 
  T#   \<        P>                  ! 4        d7    T P                  P                  T P                  \:        R7      G Rj  xL 
  h i ; i5i)a|  
Send a task-augmented sampling request via the queue, then poll client.

This is for use inside a task-augmented tool call when you want the client
to handle the sampling as its own task. The request is queued and delivered
when the client calls tasks/result. After the client responds with
CreateTaskResult, we poll the client's task until complete.

Args:
    messages: The conversation messages for sampling
    max_tokens: Maximum tokens in the response
    ttl: Task time-to-live in milliseconds for the client's task
    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
    RuntimeError: If handler is not configured
Nz0handler is required for create_message_as_task()r   r   )r   r   r   r   r   r   r   r   r   r   r   r+   r   r   ) r7   r   r   r	   r   r   r9   r   r:   r   rC   r   r   r!   r   r   r   r   r8   r   r   r   r   r+   ri   r   r   r   r   r   r   r   )r;   r   r   rn   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r^   s   &&$$$$$$$$$$          r1   create_message_as_task(ServerTaskContext.create_message_as_task  s.    X CG--B]B]B]dmm11>>cg'4K@)(3== QRR kk%%dll;U%VVV --==!'+#)/# LL#& > 
 !(


-5Z6>''
3 *	
 kk!!$,,777	"*--/1M,;;MJM*//66N  ==55??O  aM 	W8 	8 2
O  ==55EE#  F
 ++))$,,?R)SSSM,,. 	++))$,,?R)SSS	s   B(J*G+B%JGJH> )G*AH> ?GGGH> JJH> G.H> H3H> 6H97H> =J>AJJ	JJ)r6   r9   r8   r7   r:   )__name__
__module____qualname____firstlineno____doc__r<   propertyrC   r+   rL   rR   rY   r`   re   rZ   ry   r~   r   r   r   r   r   __static_attributes____classdictcell__)r0   s   @r1   r'   r'   5   s    . -1 6 ! !   & &) )
,$ 
, 
,
,t 
, 
,
,t 
, 
,
 
*  ? ?BC CJ\
 %)\ 26\ %)\ ,0\ +/\ 6:\ $(\ *.\ \|L
 L L\h
 h %)h 26h %)h ,0h +/h 6:h $(h *.h h hr4   r'   )2r   typingr   r   +mcp.server.experimental.task_result_handlerr   mcp.server.sessionr   mcp.server.validationr   r   mcp.shared.exceptionsr   *mcp.shared.experimental.tasks.capabilitiesr   r	   %mcp.shared.experimental.tasks.contextr
   +mcp.shared.experimental.tasks.message_queuer   r   &mcp.shared.experimental.tasks.resolverr   #mcp.shared.experimental.tasks.storer   	mcp.typesr   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r    r!   r"   r#   r$   r%   r'   rP   r4   r1   <module>r      s`      I , \ * > W ; 9      6o or4   