
    k7i5[                     x   S r SSKJr  SSKJr  SSKJrJrJr  \" \	5      r
S\S-  S\S-  4S	 jr SS
\\\4   S\S-  S\\\4   4S jjrS\\\4   S\\\4   4S jr SS\S\S\\\\4   \\\\\4   4   4   4S jjrS\S\\\4   4S jr  SS\\\4   S\\\4   S-  S\S-  S\\\4   S-  4S jjr/ SQrg)z5Schema manipulation utilities for OpenAPI operations.    )Any)
get_logger   )	HTTPRoute
JsonSchemaResponseInfoschemaNreturnc                 <   U (       a  [        U [        5      (       d  U $ U R                  5       n/ SQnU H  nX1;   d  M
  UR                  U5        M     SU;   aP  US   R	                  5        VVs0 s H  u  pEU[        U5      _M     snnUS'   US   (       d  UR                  S5        SU;   a,  [        US   5      US'   US   (       d  UR                  S5        SU;   a4  [        US   [        5      (       a  [        US   5      US'   U$ US   SL a   U$ s  snnf )zO
Clean up a schema dictionary for display by removing internal/complex fields.
)allOfanyOfoneOfnotnullablediscriminatorreadOnly	writeOnly
deprecatedxmlexternalDocs
propertiesitemsadditionalPropertiesT)
isinstancedictcopypopr   clean_schema_for_display)r	   cleanedfields_to_removefieldkvs         h/home/james-whalen/.local/lib/python3.13/site-packages/fastmcp/experimental/utilities/openapi/schemas.pyr   r      s-    FD11 kkmG& "KK "
 w7>|7L7R7R7T!
7TtqA'**7T!
 |$KK%'3GG4DEwKK (g45t<<.F.//G*+ N	 +,4N/!
s   0Dinfodescriptionc                    U R                  5       nUR                  S5      =n(       an  [        U[        5      (       aX  UR	                  S5      (       a  UR                  S5      S   nSU 3US'   OUR	                  S5      (       d  [        SU S35      eOUR                  S	5      =n(       aH  SU;   a  [        U5      US	'   OYUR                  5        VVs0 s H  u  pgU[        U5      _M     snnUS	'   O&UR                  S
5      =n(       a  [        U5      US
'   S H7  n	[        UR                  U	/ 5      5       H  u  p[        U5      X)   U
'   M     M9     UR                  S5      =n(       a#  [        U[        5      (       d  [        U5      US'   U R                  SU5      (       a  UR                  S5      (       d  XS'   U$ s  snnf )aC  
Replace openapi $ref with jsonschema $defs recursively.

Examples:
- {"type": "object", "properties": {"$ref": "#/components/schemas/..."}}
- {"type": "object", "additionalProperties": {"$ref": "#/components/schemas/..."}, "properties": {...}}
- {"$ref": "#/components/schemas/..."}
- {"items": {"$ref": "#/components/schemas/..."}}
- {"anyOf": [{"$ref": "#/components/schemas/..."}]}
- {"allOf": [{"$ref": "#/components/schemas/..."}]}
- {"oneOf": [{"$ref": "#/components/schemas/..."}]}

Args:
    info: dict[str, Any]
    description: str | None

Returns:
    dict[str, Any]
$refz#/components/schemas//#/$defs/z#/z/External or non-local reference not supported: z. FastMCP only supports local schema references starting with '#/'. Please include all schema definitions within the OpenAPI document.r   r   )r   r   r   r   r&   )r   getr   str
startswithsplit
ValueError_replace_ref_with_defsr   	enumeratebool)r%   r&   r	   ref_pathschema_namer   	prop_nameprop_schemaitem_schemasectioniitemr   s                r$   r1   r1   J   s   , YY[F::f%%x%h$$""#:;;&nnS1"5#+K=!9v((.. EhZ PY Z 
 zz,/	/	/Z#9*#EF<  /9.>.>.@$.@*I 1+>>.@$F<  

7+	+	+0=w. GR!89GA!7!=FOA : /  &zz*@AAA.55-C$.F)* xx{++FJJ}4M4M +}M!$s   Gc                 ,   SU ;   d  SU ;   d  SU ;   a  U $ [        U R                  S5      [        5      (       a  SU S   ;   a  U $ SU ;   GaI  U S   n[        U[        5      (       Ga.  US;   a  US:X  aJ  SU ;   aD  U R	                  5       n/ S	Qn0 nU H  nXR;   d  M
  UR                  U5      XE'   M     USS0/US'   U$ US
:X  aJ  SU ;   aD  U R	                  5       n/ S	Qn0 nU H  nXV;   d  M
  UR                  U5      XE'   M     USS0/US'   U$ 0 nU R	                  5       n/ S	QnU H  nXW;   d  M
  UR                  U5      XE'   M     USS0/US'   U$ / S	Qn0 nU R	                  5       nU H  nXW;   d  M
  UR                  U5      XE'   M     USS0/US'   U$ U $ )z
Make an optional parameter schema nullable to allow None values.

For optional parameters, we need to allow null values in addition to the
specified type to handle cases where None is passed for optional parameters.
r   r   r   typenull)arrayobjectr?   r   )defaultr&   titleexampler@   r   )r   r,   listr-   r   r   )r	   original_typearray_schematop_level_fieldsnullable_schemar!   object_schemaoriginal_schemas           r$   !_make_optional_parameter_nullablerK      s    &Gv-F1B &**V$d++&.0H vmS)) 33 !G+60A#);;=L'U$&(O "2 05A5E5Ee5LO2 "2 1=vv>N/OOG,**"h.<63I$*KKMM'U$&(O "2 15B5F5Fu5MO2 "2 1>?O/POG,** ')O&,kkmO'U$!1 35D5H5H5OO2 "2 1@&&AQ/ROG,** $R "$"(++--E/1@1D1DU1K. . -<ff=M+N(&&M    routeconvert_refsc                 |  ^#^$ 0 n/ n0 n[        5       [        5       [        5       [        5       S.n0 nU R                   H*  nXWR                     R                  UR                  5        M,     U R
                  (       Ga  U R
                  R                  (       Ga  [        [        U R
                  R                  5      5      nU(       a#  [        U R
                  R                  U   5      n	OU R
                  R                  U   n	U R
                  R                  (       a/  U	R                  S5      (       d  U R
                  R                  U	S'   SU	;   a  [        U	S   [        5      (       a  0 n
/ nU	S    HP  n[        U[        5      (       d  M  SU;   a  U
R                  US   5        SU;   d  M<  UR!                  US   5        MR     XS'   U(       a?  [        5       nU Vs/ s H$  oU;   a  M
  UR                  U5      (       a  M"  UPM&     snU	S'   U	R#                  SS5        U	R                  S0 5      n[        5       nUR%                  5        H  nUR                  U5        M     [        UR'                  5       5      nUU-  nU R                   GH  nUR                  U;   Ga   UR                   SUR                   3nUR(                  (       a  UR+                  U5        UR                  UR                  S.UU'   U(       a!  [        UR,                  UR                  5      nOPUR,                  R/                  5       nUR                  (       a%  UR                  S5      (       d  UR                  US'   UR                  SS	5      nS
UR                  R1                  5        S3nU(       a  U SU 3US'   OUUS'   UUU'   GM5  UR(                  (       a  UR+                  UR                  5        UR                  UR                  S.XGR                  '   U(       a!  [        UR,                  UR                  5      nOPUR,                  R/                  5       nUR                  (       a%  UR                  S5      (       d  UR                  US'   UX'R                  '   GM     U R
                  (       Ga]  U R
                  R                  (       GaA  SW	;   a@  U(       d9  XS'   U R
                  R(                  (       a  UR+                  S5        SSS.US'   OU(       ad  UR3                  5        H  u  nnUUU'   SUS.UU'   M     U R
                  R(                  (       a!  UR!                  U	R                  S/ 5      5        OU	R                  SS5      R5                  5       nSSKnUR9                  SSU5      nU(       a  US   R;                  5       (       a  SnXU'   U R
                  R(                  (       a  UR+                  U5        SUS.UU'   SUUS.nU R<                  nU(       Ga  U(       a  UR/                  5       nUR3                  5        H+  u  nn[        U[        5      (       d  M  [        U5      UU'   M-     [        5       m$U#U$4S jm#T#" U5        T$(       a  Sn U (       dG  [?        T$5      n![        T$5       H  nUU;   d  M  T#" UU   5        M     [?        T$5      U!:H  n U (       d  MG  UR3                  5        VV"s0 s H  u  nn"UT$;   d  M  UU"_M     sn"nUS'   UU4$ UUS'   UU4$ s  snf s  sn"nf )a}  
Combines parameter and request body schemas into a single schema.
Handles parameter name collisions by adding location suffixes.
Also returns parameter mapping for request director.

Args:
    route: HTTPRoute object

Returns:
    Tuple of (combined schema dictionary, parameter mapping)
    Parameter mapping format: {'flat_arg_name': {'location': 'path', 'openapi_name': 'id'}}
)pathqueryheadercookier&   r   r   requiredN__)locationopenapi_name (z parameter) r(   bodyrB   r   z[^a-zA-Z0-9_]_	body_datar@   )r=   r   rT   c                 v  > [        U [        5      (       a|  SU ;   aV  [        U S   [        5      (       a>  U S   nUR                  S5      (       a#  TR	                  UR                  S5      S   5        U R                  5        H  nT" U5        M     g[        U [        5      (       a  U  H  nT" U5        M     gg)z%Recursively find all $ref references.r(   r+   r)   r*   N)r   r   r-   r.   addr/   valuesrD   )valuerefr#   r;   find_refs_in_value	used_refss       r$   rc   ;_combine_schemas_and_map_params.<locals>.find_refs_in_value  s    eT**:eFmS+I+I#Fm>>*55%MM#))C.*<="\\^*1- ,t,, %*40 !& -rL   F$defs) set
parametersrV   r_   namerequest_bodycontent_schemanextiterr1   r&   r,   r   rD   r   updateextendr   r`   keysrT   appendschema_r   
capitalizer   lowerresubisdigitrequest_schemaslen)%rM   rN   r   rT   parameter_mapparam_names_by_location
body_propsparamcontent_typebody_schemamerged_propsmerged_required
sub_schemaseenxall_non_body_paramslocation_paramsbody_param_namescolliding_paramssuffixed_nameparam_schemaoriginal_desclocation_descr6   r7   
param_nameru   resultschema_defsall_defsri   r	   collected_allinitial_count
def_schemarc   rd   s%                                      @@r$   _combine_schemas_and_map_paramsr      sY     JHM %%	 J!!/33EJJ? " e00???D!3!3!B!BCD 0""11,?K  ,,;;LIK))+//-2P2P).););)G)GK& k!jW1Et&L&LL O)'2
j$//#z1$++J|,DE!Z/'..z*/EF 3 )5%u.+.!DyADHHQKA+J' OOGT* __\26
 %299;""?3 < :??,-*-== !!::))$zzl"U^^,<=M~~. "NN %

,M-( 5emmUEVEVW$}}113$$\-=-=m-L-L272C2CL/(,,]B?M 9 9 ;<KHM1>q.P]+.;]+
 )5J}% ~~

+ "NN %

)M**% 5emmUEVEVW$}}113$$\-=-=m-L-L272C2CL/
 &2Jzz"k "p e00???[  "-v!!**'17$PM&!*4*:*:*<&	;(3
9% !'$-,i(	 += !!**
B ?@ %&9??AJ  0#zBJA!6!6!8!8(
%0z"!!**
+5;Z(XM*%  F ''K"'')H ( 0ffd++%;F%CHTN !1
 I1 z*  %'$'	NM $Y8+.x~> !0 %(	Nm$CM (- -5NN,<#,<(jy( %D*$,<#w =   *F7O=  O+|#s   #	\30\3\3\8\8c                      [        U 5      u  pU$ )a   
Combines parameter and request body schemas into a single schema.
Handles parameter name collisions by adding location suffixes.

This is a backward compatibility wrapper around _combine_schemas_and_map_params.

Args:
    route: HTTPRoute object

Returns:
    Combined schema dictionary
)r   )rM   r	   r\   s      r$   _combine_schemasr     s     06IFMrL   	responsesschema_definitionsopenapi_versionc                    U (       d  g/ SQnSnU H  nXP;   d  M
  X   n  O   Uc3  U R                  5        H  u  pVUR                  S5      (       d  M  Un  O   Ub  UR                  (       d  g/ SQnSnU H#  n	XR                  ;   d  M  UR                  U	   n  O   UcV  UR                  (       aE  [        [	        UR                  5      5      n
UR                  U
   n[
        R                  SU
 35        U(       a  [        U[        5      (       d  g[        U5      nSU;   aH  U(       aA  US   nUR                  S5      (       a&  UR                  S5      S	   nX;   a  [        X   5      nU(       a$  UR                  S
5      (       a  SSKJn  U" X5      nUR                  S5      S:w  a  SSU0S/SS.nUnU(       a  UR                  5       nUR                  5        H+  u  nn[        U[        5      (       d  M  [        U5      UU'   M-     U(       aK  UR                  S
5      (       a5  SSKJn  [        UR!                  5       5       H  nU" UU   U5      UU'   M     UUS'   U$ )a  
Extract output schema from OpenAPI responses for use as MCP tool output schema.

This function finds the first successful response (200, 201, 202, 204) with a
JSON-compatible content type and extracts its schema. If the schema is not an
object type, it wraps it to comply with MCP requirements.

Args:
    responses: Dictionary of ResponseInfo objects keyed by status code
    schema_definitions: Optional schema definitions to include in the output schema
    openapi_version: OpenAPI version string, used to optimize nullable field handling

Returns:
    dict: MCP-compliant output schema with potential wrapping, or None if no suitable schema found
N)2002012022042)zapplication/jsonzapplication/vnd.api+jsonzapplication/hal+jsonzapplication/ld+jsonz	text/jsonz/Using non-JSON content type for output schema: r(   r+   r)   r*   z3.0r   )%convert_openapi_schema_to_json_schemar=   r@   r   T)r=   r   rT   zx-fastmcp-wrap-resultrf   )r   r.   rk   rl   rm   loggerdebugr   r   r1   r/   json_schema_converterr   r,   r   rD   rp   )r   r   r   success_codesresponse_infostatus_code	resp_infojson_compatible_typesr	   r~   first_content_typeoutput_schemar4   r5   r   wrapped_schemaprocessed_defsri   def_names                      r$   $extract_output_schema_from_responsesr     sn   (  1M M$#%2M % &/oo&7"K%%c** ) '8
 M$@$@ F-777"11,?F . ~-66!$}'C'C"DE--.@A=>P=QR	
 FD11 +62M #5 (z**"..-b1K0 67I7V W ?55e<<P=
  H, #]3!
%)	
 ' +002*002LD&&$'''=f'Et$ 3
 99%@@T !4!4!67+P"8,o,x( 8
 "0grL   )r   r   rK   r   r   )N)T)NN)__doc__typingr   fastmcp.utilities.loggingr   modelsr   r   r   __name__r   r   r   r-   r1   rK   r3   tupler   r   r   __all__ rL   r$   <module>r      sh   ;  0 7 7	H	;Z$%6 ;:;L ;~ 596
sCx.6'*Tz6	#s(^6rKd38n Kc3h K` h!h!h! 4S>4T#s(^ 3445h!VI $sCx. & 15"&zC%&zS#X-z 4Zz 
#s(^d	z|rL   