
    k7iI"                    ,   S SK Jr  S SKJr  S SKJrJrJr  S SKJ	r	J
r
  S SKJr  S SKJrJr  S SKJr  S SKJr  S S	KJr  S S
KJr  S SKJr  / SQr\" \5      r\" S\S9r " S S\5      r " S S\\\   5      r\ " S S\\   5      5       r SS jr!SS jr"g)    )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                  @   ^  \ rS rSrSrSU 4S jjrSU 4S jjrSrU =r$ )ElicitationJsonSchema    a%  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.
c                V   > US   S:X  a  U R                  U5      $ [        TU ]	  U5      $ )z-Override to prevent ref generation for enums.typeenum)enum_schemasupergenerate_inner)selfschema	__class__s     T/home/james-whalen/.local/lib/python3.13/site-packages/fastmcp/server/elicitation.pyr   $ElicitationJsonSchema.generate_inner(   s4     &>V# ##F++w%f--    c                  > [         T	U ]  U5      nUR                  S5      nU(       a  UR                  S/ 5      n/ nSnU H  n[        US5      (       a  UR	                  UR
                  5        SnM3  UR                  R                  SS5      R                  5       nUR	                  U5        XR                  :w  d  M  SnM     U(       a  XRS'   U$ )	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"   s
            r#   r   !ElicitationJsonSchema.enum_schema2   s     $V, ::e$jjB/GJ$!6#344%%f&;&;<'+$ $*;;#6#6sC#@#F#F#HL%%l3#||3+/( "  &0{#r%    )r!   zcore_schema.CoreSchemareturnr   )r!   zcore_schema.EnumSchemar<   r   )	__name__
__module____qualname____firstlineno____doc__r   r   __static_attributes____classcell__)r"   s   @r#   r   r       s    .! !r%   r   c                  2    \ rS rSr% SrSrS\S'   S\S'   Srg	)
r   W   z)Result when user accepts the elicitation.acceptzLiteral['accept']actionr   datar;   N)r=   r>   r?   r@   rA   rG   __annotations__rB   r;   r%   r#   r   r   W   s    3 (F(
Gr%   r   c                       \ rS rSr% S\S'   Srg)r   ^   r   r3   r;   N)r=   r>   r?   r@   rI   rB   r;   r%   r#   r   r   ^   s    Hr%   r   c                j    [        U 5      R                  [        S9n[        U5      n[	        U5        U$ )z_Get the schema for an elicitation response.

Args:
    response_type: The type of the response
)schema_generator)r   json_schemar   r    validate_elicitation_json_schema)response_typer!   s     r#   r   r   c   s<     $M2>>. ? F V$F %V,Mr%   c                   1 SknU R                  S5      S:w  a  [        SU R                  S5       S35      eU R                  S0 5      nUR                  5        GH  u  p4UR                  S5      n[        U[        5      (       a4  SU;   a-  U Vs/ s H  ofS:w  d  M
  UPM     nn[        U5      S:X  a  US	   nOUR                  S
S5      (       a  My  SU;   a  M  SU;   a  M  SU;   at  US   nUR                  S5      (       aG  USS nU R                  S0 5      R                  U0 5      n	SU	;   a  M  U	R                  S5      n
X;   a  M  [        SU SU S35      eSU;   d  SU;   aj  UR                  S/ 5      UR                  S/ 5      -   nU H<  nSU;   d  SU;   a  M  UR                  S5      nX;  d  M)  [        SU SU SU S35      e   GMy  XQ;  a  [        SU SU SU S35      eUS:X  a  [        SU S35      eUS:X  d  GM  UR                  S0 5      nUR                  S5      S:X  d  GM  [        SU S35      e   gs  snf ) a5  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null   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-   	TypeErrorr`   
isinstancelistlen
startswith)r!   ALLOWED_TYPESrW   	prop_nameprop_schema	prop_typetref_pathdef_nameref_defref_typeunion_schemasunion_schema
union_typeitems_schemas                  r#   rO   rO   v   s     ?M zz&X%EfjjQWFXEY Z^ ^
 	

 L"-J","2"2"4	OOF+	 i&&"(1A	1&[Q		Ay>Q& )!I__Z// k! [  [ "6*H"":..#AB< **Wb155hCW$";;v.,,YK7OPXz Zm m  k!W%;'OOGR8;??7TV;WWM -l*f.D)--f5
2#4YK?QR\Q] ^22?@df  !.  ),YK|I; O**78\^   ,YK 8[ [ 
 &??7B7L'830 <_ _ Q #5 Bs   	IIN)rP   ztype[T]r<   dict[str, Any])r!   rs   r<   None)#
__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__r=   loggerr   r   r   r   r   rO   r;   r%   r#   <module>r      s    " ! ( (  D % % 9 0 : 
H	C3. 3n)WQZ  GAJ  &fr%   