
    kKj:                       U d Z ddlmZ ddlZddlZddlZddlmZ ddlm	Z	 ddl
mZmZmZmZmZmZmZ ddl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mZ ddl m!Z! ddl"m#Z# ddl$m%Z%m&Z&m'Z'm(Z(m)Z)m*Z*m+Z+ 	 ddl,m-Z. ddl/m0Z1 e.e1fZ2de3d<   d!dZ5d!dZ6d"dZ7d!dZ8 ede      Z9 e#e:      Z;e	 G d dee9                Z< G d d      Z=ddd	 	 	 	 	 	 	 d#dZ>e	 G d d              Z?y# e4$ r dZ2Y gw xY w)$z?Function introspection and schema generation for FastMCP tools.    )annotationsN)Callable)	dataclass)	AnnotatedAnyGenericUnionget_args
get_originget_type_hints)	BaseModelPydanticSchemaGenerationError)TypeVar)
ToolResultresolve_serialize_by_alias)ParsedDocstringparse_docstring)compress_schema)
get_logger)AudioFileImagecreate_function_without_paramsget_cached_typeadapteris_class_member_of_typereplace_type)	PrefabApp)	Componentztuple[type, ...]_PREFAB_TYPES c                    | t         u ryt        |       }|t        u s|t        j                  u s|t
        u rt        d t        |       D              S y)zKCheck if *tp* is or contains bytes, recursing through unions and Annotated.Tc              3  2   K   | ]  }t        |        y wN)_contains_bytes_type.0as     o/Users/ahmed/devFolder/Ultron/claude-voice/.venv/lib/python3.12/site-packages/fastmcp/tools/function_parsing.py	<genexpr>z'_contains_bytes_type.<locals>.<genexpr>-   s     ALq'*L   F)bytesr   r	   types	UnionTyper   anyr
   tporigins     r(   r$   r$   '   sF    	U{^F&EOO3v7JAHRLAAA    c                    t        | t              rt        | t              ryt	        |       }|t
        u s|t        j                  u s|t        u rt        d t        |       D              S y)zSCheck if *tp* is or contains a prefab type, recursing through unions and Annotated.Tc              3  2   K   | ]  }t        |        y wr#   )_contains_prefab_typer%   s     r(   r)   z(_contains_prefab_type.<locals>.<genexpr>7   s     B\(+\r*   F)
isinstancetype
issubclassr   r   r	   r,   r-   r   r.   r
   r/   s     r(   r5   r5   1   sR    "d
2} =^F&EOO3v7JBXb\BBBr2   c                    t        |       t        u rt        t        |       d         S t	        | t
              rt        | t              r| S y)zFUnwrap ``Annotated`` and return the underlying Pydantic model, if any.r   N)r   r   _unwrap_modelr
   r6   r7   r8   r   )r0   s    r(   r:   r:   ;   s<    "~"Xb\!_--"d
2y 9	r2   c                    t        |       }|t        u rt        t        |       d         S |t        u s|t
        j                  u r+t        |       D ]  }t        |      }|t        |      c S  yt        |       S )u  Resolve ``by_alias`` for the output schema of return type *tp*.

    Unwraps ``Annotated`` and ``Optional``/``Union`` wrappers to find the
    underlying Pydantic model so the generated schema honors the model's
    ``serialize_by_alias`` config — keeping it consistent with how the runtime
    result is serialized. Containers (``list[Model]`` etc.) are not unwrapped:
    their schema keeps the default, matching the runtime path which only
    special-cases a directly-returned model.

    Known limitation: a single schema is generated with one ``by_alias`` value,
    while the runtime resolves the alias mode per returned value. They cannot
    diverge for a plain single-model return, but a union return can produce more
    than one runtime alias mode that no single schema can describe:

    - distinct models with *conflicting* ``serialize_by_alias`` (e.g. ``A | B``
      where ``A`` opts out but ``B`` opts in), and
    - a model arm alongside a container arm (e.g. ``Model | list[Model]``):
      a directly-returned model honors its config, but a returned ``list`` is
      serialized with the default alias mode, so the two variants disagree.

    Pydantic's schema generator does not consult per-model ``serialize_by_alias``
    and the runtime does not recurse into containers, so honoring every variant
    would require per-arm schema assembly. This is an accepted edge; single-model
    returns and unions whose arms all resolve to the same mode are consistent.
    r   T)	r   r   _resolve_output_by_aliasr
   r	   r,   r-   r:   r   )r0   r1   argmodels       r(   r<   r<   D   ss    4 ^F'Q88&EOO3B<C!#&E 1%88   %b))r2   T)defaultc                      e Zd ZU ded<   y)_WrappedResultr?   resultN)__name__
__module____qualname____annotations__r    r2   r(   rB   rB   o   s    Ir2   rB   c                      e Zd Zy)_UnserializableTypeN)rD   rE   rF   r    r2   r(   rI   rI   t   s    r2   rI   _root_schema
_seen_refsc                  |xs | }|xs
 t               }| j                  d      dk(  ryd| v ry| j                  d      }t        |t              r|j	                  d      sy||v ry|j                  d      }|j                  d      }|}|D ]?  }	|	j                  d	d      j                  d
d      }
t        |t              r|
|vr y||
   }A |}t        |t              syt        ||||hz        S )z1Check if a JSON schema represents an object type.r7   objectT
propertiesz$refz#/F/z~1z~0~rJ   )
setgetr6   str
startswithremoveprefixsplitreplacedict_is_object_schema)schemarK   rL   root_schema	seen_refsrefpointersegmentstargetsegment	unescapedtarget_schemas               r(   rZ   rZ   x   s    (&K#ceI zz&X% v **V
Cc3s~~d';
i t$G}}S!HFOOD#.66tSA	&$'9F+B	"	  MmT* u$ r2   c                  z    e Zd ZU ded<   ded<   ded<   ded<   d	ed
<   dZded<   e	 	 	 d	 	 	 	 	 	 	 	 	 dd       Zy)ParsedFunctionCallable[..., Any]fnrT   namez
str | Nonedescriptiondict[str, Any]input_schemadict[str, Any] | Noneoutput_schemaNr   return_typec                	   |rt        j                  |      }|j                  j                         D ]f  }|j                  t         j
                  j                  k(  rt        d      |j                  t         j
                  j                  k(  s]t        d       |ri|D ]d  }||j                  vrt        d| d      |j                  |   }|j                  t         j
                  j                  k(  sWt        d| d       t        |dd       xs |j                  j                  }t        |      }	t        j                  |      s&t!        |t"        j$                        s|j&                  }t!        |t(              r|j*                  }t        |      }
t-        |	j.                  xs |
j.                  |
j                        }dd	lm}m}  ||      } ||      }|rt7        |t9        |            }t;        |      }|j=                         }|rt9        |      nd }t?        ||d
      }|j                  rI|jA                  di       }|j                  jC                         D ]  \  }}||v sd||   vs|||   d<    d }t        j                  |      }|jD                  }t!        |tF              r 	 tI        |d
      }|jA                  d|      }|}|t         jP                  d tR        dfvrmtU        |      rtV        }tX        rt[        |      rtV        }t]        |t^              rtV        }ta        |tb        je                  tf        th        tj        t^        tl        jn                  jp                  tl        jn                  jr                  tl        jn                  jt                  tl        jn                  jv                  tl        jn                  jx                  g	tX        tV                    }	 t{        |      }t;        |      }|j=                  d|      }|r8t}        |      s-t~        |   }t;        |      }|j=                  d|      }d
|d<   n|}t?        |d
      } | |||j.                  ||xs d |      S # tJ        $ r!}tL        jO                  d|       Y d }~d }~ww xY w# t        $ r/}dtG        |      vrtL        jO                  d|       Y d }~|d }~ww xY w)Nz/Functions with *args are not supported as toolsz2Functions with **kwargs are not supported as toolszParameter 'z-' in exclude_args does not exist in function.z,' in exclude_args must have a default value.rD   )rj   
parametersr   )transform_context_annotationswithout_injected_parametersT)prune_paramsprune_titlesrO   rj   )include_extrasreturnz5Failed to resolve type hint for return annotation: %s.serialization)modeby_aliaszx-fastmcp-wrap-result)ru   rI   z#Unable to generate schema for type )rh   ri   rj   rl   rn   ro   )Ainspect	signaturerq   valueskind	ParameterVAR_POSITIONAL
ValueErrorVAR_KEYWORDr@   emptygetattr	__class__rD   r   	isroutiner6   	functoolspartial__call__staticmethod__func__r   rj   fastmcp.server.dependenciesrr   rs   r   listr   json_schemar   rS   itemsreturn_annotationrT   r   	Exceptionloggerdebug_emptyr   r$   rI   r   r5   r   r   r   rY   fromkeysr   r   r   mcpr,   TextContentImageContentAudioContentResourceLinkEmbeddedResourcer<   rZ   rB   r   ) clsrh   exclude_argsvalidatewrap_non_object_output_schemasigparamarg_namefn_nameouter_docstringinner_docstringparsed_docstringrr   rs   
wrapper_fninput_type_adapterrl   rt   rO   
param_name
param_descrn   output_type
type_hintseoriginal_output_typeclean_output_typerz   type_adapterbase_schemawrapped_typewrapped_adapters                                    r(   from_functionzParsedFunction.from_function   s    ##B'C..0::!2!2!A!AA$%VWW::!2!2!>!>>$L 	 1  ,Hs~~5()(3`a   NN84E}}(9(9(?(??()(3_`  !- "j$/H2<<3H3H)"-   $ZI<M<M-NBb,'B *"-*'33R7R7R&11
	

 +2. 14
 7
DDVWJ3J?)557 .:tL)t&|$
 &&%)),;J*:*E*E*K*K*M&
J*,%Z
-CC<FJz*=9 +N #++ k3'Y ,BtD
(nnX{C  +w~~tS#>>#K01 !6{!C1 '{J?1 !-"		--		..		..		..		22 ' (!&X 44EF56GH*66(8 7  19J;9W#12C#DL&<\&JO$3$?$?,x %@ %M >BM"9:$/M /D Q (44%'/4,
 	
_  YTVWXXYV 1 X(A6LL#F{o!VWXs1   8Q= /A2R* =	R'R""R'*	S"3%SS")NTT)
rh   rg   r   zlist[str] | Noner   boolr   r   rw   rf   )rD   rE   rF   rG   ro   classmethodr   r    r2   r(   rf   rf      s}    
I  ((K *..2~
~
 '~
 	~

 (,~
 
~
 ~
r2   rf   )r0   r   rw   r   )r0   r   rw   ztype[BaseModel] | None)r[   rk   rK   rm   rL   zset[str] | Nonerw   r   )@__doc__
__future__r   r   r{   r,   collections.abcr   dataclassesr   typingr   r   r   r	   r
   r   r   	mcp.typesr   pydanticr   r   typing_extensionsr   
TypeVarExtfastmcp.tools.baser   r   #fastmcp.utilities.docstring_parsingr   r   fastmcp.utilities.json_schemar   fastmcp.utilities.loggingr   fastmcp.utilities.typesr   r   r   r   r   r   r   prefab_ui.appr   
_PrefabAppprefab_ui.components.baser   _PrefabComponentr   rG   ImportErrorr$   r5   r:   r<   r?   rD   r   rB   rI   rZ   rf   r    r2   r(   <module>r      s    E "    $ ! W W W  = 3 E P 9 0  5G'13C&DM#D
#*L sC 	H	 WQZ  	 	 +/"&	-- (-  	-
 
-` G
 G
 G
M  Ms   5C( (C21C2