o
    iI"                     @  s  d dl mZ d dlmZ d dlmZmZmZ d dlm	Z	m
Z
 d dl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 g dZeeZededZG dd deZG dd deee ZeG dd dee Z d ddZ!d!ddZ"dS )"    )annotations)	dataclass)AnyGenericLiteral)CancelledElicitationDeclinedElicitation)	BaseModel)GenerateJsonSchemaJsonSchemaValue)core_schema)TypeVar)compress_schema)
get_logger)get_cached_typeadapter)AcceptedElicitationr   r   ScalarElicitationTypeget_elicitation_schemaT)defaultc                      s0   e Zd ZdZd fddZd fd	d
Z  ZS )ElicitationJsonSchemaa5  Custom JSON schema generator for MCP elicitation that always inlines enums.

    MCP elicitation requires inline enum schemas without $ref/$defs references.
    This generator ensures enums are always generated inline for compatibility.
    Optionally adds enumNames for better UI display when available.
    schemacore_schema.CoreSchemareturnr   c                   s"   |d dkr|  |S t |S )z-Override to prevent ref generation for enums.typeenum)enum_schemasupergenerate_inner)selfr   	__class__ `/var/www/html/karishye-ai-python/venv/lib/python3.10/site-packages/fastmcp/server/elicitation.pyr   (   s   
z$ElicitationJsonSchema.generate_innercore_schema.EnumSchemac           	        s   t  |}|d}|rE|dg }g }d}|D ]%}t|dr)||j d}q|jdd }|| ||j	kr>d}q|rE||d< |S )	zGenerate inline enum schema with optional enumNames for better UI.

        If enum members have a _display_name_ attribute or custom __str__,
        we'll include enumNames for better UI representation.
        clsmembersF_display_name_T_ 	enumNames)
r   r   gethasattrappendr'   namereplacetitlevalue)	r   r   resultenum_clsr&   
enum_nameshas_custom_namesmemberdisplay_namer    r"   r#   r   2   s$   



z!ElicitationJsonSchema.enum_schema)r   r   r   r   )r   r$   r   r   )__name__
__module____qualname____doc__r   r   __classcell__r"   r"   r    r#   r       s    
r   c                   @  s&   e Zd ZU dZdZded< ded< dS )r   z)Result when user accepts the elicitation.acceptzLiteral['accept']actionr   dataN)r8   r9   r:   r;   r>   __annotations__r"   r"   r"   r#   r   W   s   
 r   c                   @  s   e Zd ZU ded< dS )r   r   r1   N)r8   r9   r:   r@   r"   r"   r"   r#   r   ^   s   
 r   response_typetype[T]r   dict[str, Any]c                 C  s$   t | jtd}t|}t| |S )zkGet the schema for an elicitation response.

    Args:
        response_type: The type of the response
    )schema_generator)r   json_schemar   r    validate_elicitation_json_schema)rA   r   r"   r"   r#   r   c   s   r   r   Nonec              
   C  s   h d}|  ddkrtd|  d d|  di }| D ]\}}| d}t|trDd|v rCdd	 |D }t|d
krC|d }n| ddrKq d|v rPq d|v rUq d|v r|d }|dr|dd }|  di  |i }d|v rwq | d}	|	|v rq td| d| dd|v sd|v r| dg | dg  }
|
D ]"}d|v sd|v rq| d}||vrtd| d| d| dqq ||vrtd| d| d| d|dkrtd| d|dkr| d i }| ddkrtd| d!q dS )"aa  Validate that a JSON schema follows MCP elicitation requirements.

    This ensures the schema is compatible with MCP elicitation requirements:
    - Must be an object schema
    - Must only contain primitive field types (string, number, integer, boolean)
    - Must be flat (no nested objects or arrays of objects)
    - Allows const fields (for Literal types) and enum fields (for Enum types)
    - Only primitive types and their nullable variants are allowed

    Args:
        schema: The JSON schema to validate

    Raises:
        TypeError: If the schema doesn't meet MCP elicitation requirements
    >   numberstringbooleanintegerr   objectz7Elicitation schema must be an object schema, got type 'zR'. Elicitation schemas are limited to flat objects with primitive properties only.
propertiesnullc                 S  s   g | ]}|d kr|qS )rN   r"   ).0tr"   r"   r#   
<listcomp>   s    z4validate_elicitation_json_schema.<locals>.<listcomp>   r   nullableFconstr   z$refz#/$defs/   Nz$defszElicitation schema field 'z' contains a reference 'z\' that could not be validated. Only references to enum types or primitive types are allowed.oneOfanyOfz' has union type 'z&' which is not a primitive type. Only z$ are allowed in elicitation schemas.z' has type 'z|' is an object, but nested objects are not allowed. Elicitation schemas must be flat objects with primitive properties only.arrayitemsz' is an array of objects, but arrays of objects are not allowed. Elicitation schemas must be flat objects with primitive properties only.)r+   	TypeErrorrY   
isinstancelistlen
startswith)r   ALLOWED_TYPESrM   	prop_nameprop_schema	prop_typeref_pathdef_nameref_defref_typeunion_schemasunion_schema
union_typeitems_schemar"   r"   r#   rF   v   s   






rF   N)rA   rB   r   rC   )r   rC   r   rG   )#
__future__r   dataclassesr   typingr   r   r   mcp.server.elicitationr   r   pydanticr	   pydantic.json_schemar
   r   pydantic_corer   typing_extensionsr   fastmcp.utilities.json_schemar   fastmcp.utilities.loggingr   fastmcp.utilities.typesr   __all__r8   loggerr   r   r   r   r   rF   r"   r"   r"   r#   <module>   s(    7
