
    [ǻi                    j   d Z ddlmZ ddlZddlmZ ddlmZmZm	Z	m
Z
mZmZ ddlmZ ddlmZ ddlmZ  e	d	e
      Z G d deee         Z G d de      Z G d de      Zee   ez  ez  Z G d de      Zeez  ez  ZeeeefZddZddZ ddZ!	 d	 	 	 	 	 	 	 	 	 ddZ"	 d	 	 	 	 	 	 	 	 	 	 	 ddZ#y)z&Elicitation utilities for MCP servers.    )annotationsN)Sequence)GenericLiteralTypeVarUnionget_args
get_origin)	BaseModel)ServerSession)	RequestIdElicitSchemaModelT)boundc                  *    e Zd ZU dZdZded<   ded<   y)AcceptedElicitationz)Result when user accepts the elicitation.acceptLiteral['accept']actionr   dataN__name__
__module____qualname____doc__r   __annotations__     T/opt/lhia/marcimex/agent/venv/lib/python3.12/site-packages/mcp/server/elicitation.pyr   r      s    3 (F(
r   r   c                       e Zd ZU dZdZded<   y)DeclinedElicitationz*Result when user declines the elicitation.declinezLiteral['decline']r   Nr   r   r   r   r    r       s    4!*F*r   r    c                       e Zd ZU dZdZded<   y)CancelledElicitationz)Result when user cancels the elicitation.cancelzLiteral['cancel']r   Nr   r   r   r   r#   r#      s    3 (F(r   r#   c                       e Zd ZU dZdZded<   y)AcceptedUrlElicitationz0Result when user accepts a URL mode elicitation.r   r   r   Nr   r   r   r   r&   r&   '   s    : (F(r   r&   c                    | j                   j                         D ]S  \  }}|j                  }||t        j                  u r't        |      r3t        |      r?t        d| dt         d       y)zCValidate that a Pydantic model only contains primitive field types.NzElicitation schema field 'z' must be a primitive type zw, a sequence of strings (list[str], etc.), or Optional of these types. Nested models and complex types are not allowed.)	model_fieldsitems
annotationtypesNoneType_is_primitive_field_is_string_sequence	TypeError_ELICITATION_PRIMITIVE_TYPES)schema
field_name
field_infor*   s       r   _validate_elicitation_schemar4   4   s    "("5"5";";"= 
J**
u~~!= , ,,ZL8S/0 1_` r   c                    t        |       }|r9	 t        |t              r&t        |       }t	        |      dk(  xr |d   t
        u S 	 yy# t        $ r Y yw xY w)zMCheck if annotation is a sequence of strings (list[str], Sequence[str], etc).   r   F)r
   
issubclassr   r	   lenstrr/   r*   originargss      r   r.   r.   G   sd    
#F	&(+
+4yA~8$q'S.8 ,   		s   5A 	AAc                    | t         v ryt        |       }|t        u s|t        j                  u rt        |       }t        d |D              S y)zDCheck if a field is a primitive type allowed in elicitation schemas.Tc              3  n   K   | ]-  }|t         j                  u xs |t        v xs t        |       / y wN)r+   r,   r0   r.   ).0args     r   	<genexpr>z&_is_primitive_field.<locals>.<genexpr>b   s8      
ilC5>>!dS,H%HdL_`cLdd
s   35F)r0   r
   r   r+   	UnionTyper	   allr:   s      r   r-   r-   W   sW     11 
#F&EOO3
# 
pt
 
 	
 r   c                  K   t        |       |j                         }| j                  |||       d{   }|j                  dk(  r3|j                  '|j                  |j                        }t        |      S |j                  dk(  r
t               S |j                  dk(  r
t               S t        d|j                         7 w)a)  Elicit information from the client/user with schema validation (form mode).

    This method can be used to interactively ask for additional information from the
    client within a tool's execution. The client might display the message to the
    user and collect a response according to the provided schema. Or in case a
    client is an agent, it might decide how to handle the elicitation -- either by asking
    the user or automatically generating a response.

    For sensitive data like credentials or OAuth flows, use elicit_url() instead.
    )messagerequestedSchemarelated_request_idNr   )r   r!   r$   Unexpected elicitation action: )
r4   model_json_schemaelicit_formr   contentmodel_validater   r    r#   
ValueError)sessionrF   r1   rH   json_schemaresultvalidated_datas          r   elicit_with_validationrS   i   s     " !(**,K&&#- '  F }} V^^%?..v~~>"77	)	#"$$	(	"#%% :6==/JKK!s   3CCBCc                  K   | j                  ||||       d{   }|j                  dk(  r
t               S |j                  dk(  r
t               S |j                  dk(  r
t	               S t        d|j                         7 gw)a[  Elicit information from the user via out-of-band URL navigation (URL mode).

    This method directs the user to an external URL where sensitive interactions can
    occur without passing data through the MCP client. Use this for:
    - Collecting sensitive credentials (API keys, passwords)
    - OAuth authorization flows with third-party services
    - Payment and subscription flows
    - Any interaction where data should not pass through the LLM context

    The response indicates whether the user consented to navigate to the URL.
    The actual interaction happens out-of-band. When the elicitation completes,
    the server should send an ElicitCompleteNotification to notify the client.

    Args:
        session: The server session
        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
        related_request_id: Optional ID of the request that triggered this elicitation

    Returns:
        UrlElicitationResult indicating accept, decline, or cancel
    )rF   urlelicitation_idrH   Nr   r!   r$   rI   )
elicit_urlr   r&   r    r#   rN   )rO   rF   rU   rV   rH   rQ   s         r   rW   rW      s     < %%%-	 &  F }} %''	)	#"$$	(	"#%% :6==/JKKs   BBA(B)r1   ztype[BaseModel]returnNone)r*   typerX   boolr?   )
rO   r   rF   r9   r1   ztype[ElicitSchemaModelT]rH   RequestId | NonerX   z%ElicitationResult[ElicitSchemaModelT])rO   r   rF   r9   rU   r9   rV   r9   rH   r\   rX   UrlElicitationResult)$r   
__future__r   r+   collections.abcr   typingr   r   r   r   r	   r
   pydanticr   mcp.server.sessionr   	mcp.typesr   r   r   r    r#   ElicitationResultr&   r]   r9   intfloatr[   r0   r4   r.   r-   rS   rW   r   r   r   <module>rg      s<   , "  $ I I  , 1C )W-?%@ +) +)9 ) ((:;>QQThh )Y ) .0CCFZZ  !$S%6 & , ,0	%L%L%L %%L )	%L
 +%LZ ,0-L-L-L 
-L 	-L
 )-L -Lr   