
    k7i\                       S r SSKJr  SSKrSSKrSSKrSSKrSSKJr  SSK	J
r
  SSKJrJr  SSKJr  SSKJrJrJr  SSKrSS	KJr  SS
KJr  SSK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#J$r$  SSK%J&r&  SSK'J(r(  SSK)J*r*J+r+J,r,J-r-J.r.J/r/  \(       a  SSK0J1r1  \(" \25      r3\S   r4S*S jr5\
\*S/S4   r6\
\*S/S4   r7 " S S\Rp                  5      r9 " S S\Rp                  5      r:\" SS9 " S S5      5       r;\;" \9Rx                  S 9/r=      S+S! jr> " S" S#\#5      r? " S$ S%\5      r@ " S& S'\5      rA " S( S)\!5      rBg),z6FastMCP server implementation for OpenAPI integration.    )annotationsN)Counter)Callable)	dataclassfield)Pattern)TYPE_CHECKINGAnyLiteral)ToolAnnotations)AnyUrl)	ToolError)ResourceResourceTemplate)get_http_headers)FastMCP)Tool
ToolResult)openapi)
get_logger)	HTTPRoute_combine_schemas$extract_output_schema_from_responsesformat_array_parameterformat_deep_object_parameter!format_description_with_responses)Context)GETPOSTPUTPATCHDELETEOPTIONSHEADc                    U (       d  g[         R                  " SSU 5      n[         R                  " SSU5      n[         R                  " SSU5      nUR                  S5      nU$ )z
Convert text to a URL-friendly slug format that only contains lowercase
letters, uppercase letters, numbers, and underscores.
 z	[\s\-\.]+_z[^a-zA-Z0-9_]z_+)resubstrip)textslugs     P/home/james-whalen/.local/lib/python3.13/site-packages/fastmcp/server/openapi.py_slugifyr.   ,   s[    
  66,T*D 66"B-D 66%d#D ::c?DK    MCPTypeMCPType | Nonez7OpenAPITool | OpenAPIResource | OpenAPIResourceTemplatec                  (    \ rS rSrSrSrSrSrSrSr	g)	r0   N   a  Type of FastMCP component to create from a route.

Enum values:
    TOOL: Convert the route to a callable Tool
    RESOURCE: Convert the route to a Resource (typically GET endpoints)
    RESOURCE_TEMPLATE: Convert the route to a ResourceTemplate (typically GET with path params)
    EXCLUDE: Exclude the route from being converted to any MCP component
    IGNORE: Deprecated, use EXCLUDE instead
TOOLRESOURCERESOURCE_TEMPLATEEXCLUDE N)
__name__
__module____qualname____firstlineno____doc__r4   r5   r6   r7   __static_attributes__r8   r/   r-   r0   r0   N   s     DH+Gr/   c                  (    \ rS rSrSrSrSrSrSrSr	g)		RouteTypea   zy
Deprecated: Use MCPType instead.

This enum is kept for backward compatibility and will be removed in a future version.
r4   r5   r6   IGNOREr8   N)
r9   r:   r;   r<   r=   r4   r5   r6   rB   r>   r8   r/   r-   r@   r@   a   s     DH+Fr/   r@   T)kw_onlyc                      \ rS rSr% Sr\" SS9rS\S'   \" SS9rS\S	'   \" S
S9r	S\S'   \" \
SS0S9rS\S'   \" S
SS0S9rS\S'   \" \
SS0S9rS\S'   S rSrg
)RouteMapn   zAMapping configuration for HTTP routes to FastMCP component types.*)defaultzlist[HttpMethod] | Literal['*']methodsz.*zPattern[str] | strpatternNzRouteType | MCPType | None
route_typedescriptionz,A set of tags to match. All tags must match.)default_factorymetadataset[str]tagsz(The type of FastMCP component to create.)rH   rN   r1   mcp_typez:A set of tags to apply to the generated FastMCP component.mcp_tagsc                D   U R                   Gc[  U R                  GbM  [        R                  R                  (       a  [
        R                  " S[        SS9  [        U R                  [        5      (       a9  [        R                  R                  (       a  [
        R                  " S[        SS9  U R                  [        R                  :X  a9  [        R                  R                  (       a  [
        R                  " S[        SS9  [        U R                  [        5      (       a4  U R                  R                  nUS:X  a  Sn[        [        U5      U l         O*U R                  U l         OU R                   c  [        S	5      eU R                  c  U R                   U l        gg)
z8Validate and process the route map after initialization.NzThe 'route_type' parameter is deprecated and will be removed in a future version. Use 'mcp_type' instead with the appropriate MCPType value.   )
stacklevelz_The RouteType class is deprecated and will be removed in a future version. Use MCPType instead.zdRouteType.IGNORE is deprecated and will be removed in a future version. Use MCPType.EXCLUDE instead.rB   r7   z`mcp_type` must be provided)rQ   rK   fastmcpsettingsdeprecation_warningswarningswarnDeprecationWarning
isinstancer@   rB   namegetattrr0   
ValueError)selfroute_type_names     r-   __post_init__RouteMap.__post_init__   s+    == T__%@44Q& 	 $//955##88MM/*#$	 )"2"22##88MM7*#$	 $//955"&//"6"6"h.&/O ' A $]]":;; ??""mmDO #r/   )rQ   rK   )r9   r:   r;   r<   r=   r   rI   __annotations__rJ   rK   setrP   rQ   rR   rb   r>   r8   r/   r-   rE   rE   n   s    K/4S/AG,A"'"6G6-24-@J*@!OPD(   %!KL Hn  W
Hh *,r/   rE   rQ   c           	        U GHP  nUR                   S:X  d  U R                  UR                   ;   d  M0  [        UR                  [        5      (       a&  UR                  R                  U R                  5      nO+[        R
                  " UR                  U R                  5      nU(       d  M  UR                  (       a@  [        U R                  =(       d    / 5      nUR                  R                  U5      (       d  M  UR                  c   e[        R                  SU R                   SU R                   SUR                  R                   35        Us  $    [        [         R"                  S9$ )a  
Determines the FastMCP component type based on the route and mappings.

Args:
    route: HTTPRoute object
    mappings: List of RouteMap objects in priority order

Returns:
    The RouteMap that matches the route, or a catchall "Tool" RouteMap if no match is found.
rG   Route  z matched mapping to rf   )rI   methodr\   rJ   r   searchpathr(   rP   re   issubsetrQ   loggerdebugr]   rE   r0   r4   )routemappings	route_mappattern_matchesroute_tags_sets        r-   _determine_route_typeru      s    	#u||y7H7H'H)++W55"+"3"3":":5::"F"$))I,=,=uzz"J >>%()9r%:N$>>22>BB  !))555U\\N!EJJ<7KIL^L^LcLcKde ! 1 6 W\\**r/   c                  x   ^  \ rS rSrSr     S                   SU 4S jjjrS	S jrS
S jrSrU =r	$ )OpenAPITool   z*Tool implementation for OpenAPI endpoints.c           
     r   > [         TU ]  UUUUU=(       d
    [        5       U	U
S9  Xl        X l        Xl        g )N)r]   rL   
parametersoutput_schemarP   r   
serializer)super__init__re   _client_route_timeout)r`   clientrp   r]   rL   rz   r{   rP   timeoutr   r|   	__class__s              r-   r~   OpenAPITool.__init__   sC     	#!'#! 	 	
 r/   c                    SU R                   < SU R                  R                   SU R                  R                   S3$ )@Custom representation to prevent recursion errors when printing.zOpenAPITool(name=z	, method=, path=))r]   r   rj   rl   r`   s    r-   __repr__OpenAPITool.__repr__  s;    "499-y9K9K8LGTXT_T_TdTdSeefggr/   c           	       ^'#    0 n[        5       nU R                  R                  (       a  U R                  R                  R                  (       a~  [	        [        U R                  R                  R                  5      5      nU R                  R                  R                  U   n[        UR                  S0 5      R                  5       5      nU R                  R                   HX  nUR                  nUR                   SUR                   3nUR                  U;   a  XvR                  4X('   XvR                  4X''   MZ     U R                  R                  n	0 n
U R                  R                   H  nUR                  S:X  d  M  UR                   SUR                   3nX;   a$  UR                  U5      b  X   XR                  '   MY  UR                  U;   d  Mk  UR                  UR                  5      c  M  XR                     XR                  '   M     U R                  R                   Vs1 s H4  nUR                  S:X  d  M  UR                  (       d  M(  UR                  iM6     nnXR                  5       -
  nU(       a  [        SU 35      eU
R                  5        H  u  m'n[	        U'4S jU R                  R                   5       S5      nU(       ah  [        U[         5      (       aS  UR"                  nUR                  S5      S:H  nU(       a,  [%        UT'S	S
9nU	R'                  ST' S3[)        U5      5      n	M  U	R'                  ST' S3[)        U5      5      n	M     0 nU R                  R                   GHX  nUR                  S:X  d  M  UR                   SUR                   3nSnUR                  U5      nX;   a9  Ub6  US:w  a0  [        U[         [*        -  5      (       a  [-        U5      S:X  d  X   nOmUR                  UR                  5      nUR                  U;   aB  Ub?  US:w  a9  [        U[         [*        -  5      (       a  [-        U5      S:X  d  XR                     nUc  M  UR.                  =(       d    SnUR0                  b  UR0                  OSnUS:X  a  [        U[*        5      (       a  [-        U5      S:  ay  U(       a)  [3        XR                  5      nUR5                  U5        GM~  [6        R9                  SUR                   S35        [:        R<                  " U5      UUR                  '   GM  [        U[         5      (       am  UR"                  R                  S5      S:X  aN  [-        U5      S:  a?  U(       a  UUUR                  '   GM#  [%        XR                  SS
9nUUUR                  '   GMI  UUUR                  '   GM[     0 n0 nU R                  R                   H  nUR                  S:X  d  M  UR                   SUR                   3nSnX;   a  UR                  U5      b  X   nO:UR                  U;   a*  UR                  UR                  5      b  XR                     nUc  M  [)        U5      UUR                  R?                  5       '   M     UR5                  U5        [A        5       nUR5                  U5        SnU R                  R                  (       a  U R                  R                  R                  (       a  [        5       nU R                  R                   HZ  nUR                  U;   a,  URC                  UR                   SUR                   35        M?  URC                  UR                  5        M\     UR                  5        VVs0 s H  u  nnUU;  d  M  UU_M     n nnU (       a  U n U RD                  RG                  U R                  RH                  U	UUUU RJ                  S9I Sh  vN n!U!RM                  5          U!R;                  5       n"Sn#U RN                  b(  U RN                  R                  S5      (       a  SU"0n#OU"n#O[        U"[*        5      (       d  SU"0n#OU"n#[Q        U#S9$ s  snf s  snnf  N! [:        RR                   a    [Q        U!RT                  S9s $ f = f! [V        RX                   a  n$SU$RZ                  R\                   SU$RZ                  R^                   3n% U$RZ                  R;                  5       n&U%SU& 3-  n%OW! [:        RR                  [`        4 a:    U$RZ                  RT                  (       a  U%SU$RZ                  RT                   3-  n% Of = f[a        U%5      U$eSn$A$f[V        Rb                   a  n$[a        SU$< 35      U$eSn$A$ff = f7f)z:Execute the HTTP request based on the route configuration.
properties__rl   Nz"Missing required path parameters: c              3  J   >#    U  H  oR                   T:X  d  M  Uv   M     g 7fN)r]   ).0p
param_names     r-   	<genexpr>"OpenAPITool.run.<locals>.<genexpr>?  s     K2qff
6J2s   #	#typearrayF)is_query_parameter{}queryr&   r   formT
deepObjectz3deepObject style with explode=false for parameter 'z5' is not standard. Using JSON serialization fallback.header)rj   urlparamsheadersjsonr   zx-fastmcp-wrap-resultresult)structured_content)contentHTTP error :  - Request error: )2re   r   request_bodycontent_schemanextitergetkeysrz   r]   locationrl   requiredr   itemsr\   listschema_r   replacestrdictlenstyleexploder   updatern   warningr   dumpslowerr   addr   requestrj   r   raise_for_statusr{   r   JSONDecodeErrorr+   httpxHTTPStatusErrorresponsestatus_codereason_phraser_   RequestError)(r`   	argumentsparam_mapping
body_propscontent_typebody_schemaparamoriginal_namesuffixed_namerl   path_paramsr   required_path_paramsmissing_paramsparam_value
param_infoschemais_arrayformatted_valuequery_paramssuffixed_value
name_valueparam_styleparam_explodedeep_obj_paramsr   openapi_headersmcp_headers	json_dataparams_to_excludekvbody_paramsr   r   structured_outputeerror_message
error_datar   s(                                          @r-   runOpenAPITool.run  sa    
  U
;;##(@(@(O(OT[[%=%=%L%L MNL++22AA,OK[__\2>CCEFJ [[++E!JJM$zzl"U^^,<=M zzZ'0=~~/N,,9>>+JM( , {{ ''AzzV##$66("QZZL 9!.!m4@*3*BK'VVy(Y]]166-B-N*3FF*;K' ( [[++ 
+zzV# ()

 AFF+ 	  

 .0@0@0BB@@PQRR'2'8'8':#J KDKK22KTJ jd;;#++!::f-8 '=#ZE'O  <<"ZL(;S=QRD <<"ZL 3S5EFD- (;2 ''AzzW$#$66("QZZL 9"!*}!=!.&2&",">4$;??/14 #,":K!*qvv!6J)+&2&",&z4$;??C
OWXDX '0&7* )6   &'YY%:		 " $|3&{D99,q0( /K +VV/O )//@ #NN"UVWV\V\U] ^E !F 48::k3JL0 #;55IIMM&1W<,q0( 4?L0 /E +VV/O 4CL0 0;QVV,Y (^  ''AzzX%#$66("QZZL 9" ".!m4@"+":KVVy(Y]]166-B-N"+FF"3K*69+6FOAFFLLN3 (  	' '({# 	;;##(@(@(O(O !$[[++FFj(%))QVVHBqzzl*CD%))!&&1 , "+!2!2Aa?P6P1!2   '	3	=!\\11{{))# 2  H %%'9! %)!%%1))--.EFF-5v,>),2)#FD11)16(:%(.%!5FGGE 
v> '' 9!(--889 $$ 	3 ajj445R

8P8P7QR =ZZ__.
3zl!33((*5 =::??!s1::??*;%<<M= ]+2!! 	=qe451<	=s   E<gAgg76g-bbb(D%gCg/FgA4g?D)g(b9b g?c bc "A+b gc 'cc gcc f?0f"d0/f0Afffff?*f::f??gr   r   r   )NNNNN)r   httpx.AsyncClientrp   openapi.HTTPRouter]   r   rL   r   rz   dict[str, Any]r{   zdict[str, Any] | NonerP   set[str] | Noner   float | Noner   zToolAnnotations | Noner|   zCallable[[Any], str] | Nonereturnr   )r   r   r   r   )
r9   r:   r;   r<   r=   r~   r   r   r>   __classcell__r   s   @r-   rw   rw      s    4 04 $ $.226 !  !  	 
   #  -      ,  0   4hB= B=r/   rw   c                  l   ^  \ rS rSrSr   S               SU 4S jjjrS	S jrS
S jrSrU =r	$ )OpenAPIResourcei  z.Resource implementation for OpenAPI endpoints.c	                x   > Uc
  [        5       n[        T	U ]	  [        U5      UUUUS9  Xl        X l        Xl        g )N)urir]   rL   	mime_typerP   )re   r}   r~   r   r   r   r   )
r`   r   rp   r   r]   rL   r   rP   r   r   s
            r-   r~   OpenAPIResource.__init__  sG     <5Ds# 	 	
 r/   c                n    SU R                   < SU R                  < SU R                  R                   S3$ )r   zOpenAPIResource(name=z, uri=r   r   )r]   r   r   rl   r   s    r-   r   OpenAPIResource.__repr__&  s1    &tyym6$((WT[[M]M]L^^_``r/   c                  ^#     U R                   R                  n[        U R                  5      nSU;   a  SU;   a  UR	                  S5      n[        U5      S:  a  0 n[        R                  " SU5      nU(       aD  UR                  SS9  [        U5      S-
  n[        U5       H  u  pxXv:  d  M  USU-
     n	XU'   M     UR                  5        H$  u  pUR                  SU S3[        U	5      5      nM&     0 n
U R                   R                   Hi  nUR                  S	:X  d  M  [        U S
UR                   35      (       d  M5  [!        U S
UR                   35      nUc  MS  US:w  d  M[  XUR                  '   Mk     0 n[#        5       nUR%                  U5        U R&                  R)                  U R                   R*                  UU
UU R,                  S9I Sh  vN nUR/                  5         UR0                  R3                  SS5      R5                  5       mST;   a&  UR7                  5       n[6        R8                  " U5      $ [;        U4S jS 5       5      (       a  UR<                  $ UR>                  $  N! [@        RB                   a  nSURD                  RF                   SURD                  RH                   3n URD                  R7                  5       nUSU 3-  nOW! [6        RJ                  [L        4 a:    URD                  R<                  (       a  USURD                  R<                   3-  n Of = f[M        U5      UeSnAf[@        RN                   a  n[M        SU< 35      UeSnAff = f7f)z2Fetch the resource data by making an HTTP request.r   r   /   z\{([^}]+)\}T)reverser   r'   Nr&   )rj   r   r   r   r   zcontent-typeapplication/jsonc              3  ,   >#    U  H	  oT;   v   M     g 7fr   r8   )r   ctr   s     r-   r   'OpenAPIResource.read.<locals>.<genexpr>l  s     O2NB<'2Ns   )ztext/zapplication/xmlr   r   r   r   )(r   rl   r   r   splitr   r(   findallsort	enumerater   r   rz   r   hasattrr]   r^   r   r   r   r   rj   r   r   r   r   r   r   r   anyr+   r   r   r   r   r   r   r   r_   r   )r`   rl   resource_uripartsr   param_matchesexpected_param_countir   r   r   r   valuer   r   r   r   r   r   r   r   s                       @r-   readOpenAPIResource.read*  s    U	=;;##Dtxx=L d{sd{$**3/u:>"$K %'JJ~t$DM$%**4*8/25zA~, .7}-EMA 7.3BFm:EJ 7 .F 4?3D3D3F/
#||bB,?[AQR 4G L//>>W,5::,?O1P1P#DAejj\*:;E(Ub[38UZZ0	 0 G*,KNN;'!\\11{{))# 2  H %%' $++//CIIKL!\1!zz&))O2NOOO}}$'''), $$ 	3 ajj445R

8P8P7QR =ZZ__.
3zl!33((*5 =::??!s1::??*;%<<M= ]+2!! 	=qe451<	=s   M1B'I7 0A1I7 %I7 I7 #I7 +A-I7 I5A)I7 M1%I7 (M1)I7 4M15I7 7M.0M<"KMAL30M2L33MM.M))M..M1r   )r  NN)r   r   rp   r   r   r   r]   r   rL   r   r   r   rP   r   r   r   r   )r   zstr | bytes)
r9   r:   r;   r<   r=   r~   r   r  r>   r   r   s   @r-   r   r     s{    8 , $ $ !  !  	 
            0aW= W=r/   r   c                  ~   ^  \ rS rSrSr  S               SU 4S jjjrS	S jr S
       SS jjrSrU =r	$ )OpenAPIResourceTemplatei  z7Resource template implementation for OpenAPI endpoints.c	                f   > Uc
  [        5       n[        T	U ]	  UUUUUS9  Xl        X l        Xl        g )N)uri_templater]   rL   rz   rP   )re   r}   r~   r   r   r   )
r`   r   rp   r  r]   rL   rz   rP   r   r   s
            r-   r~    OpenAPIResourceTemplate.__init__  sC     <5D%#! 	 	
 r/   c                n    SU R                   < SU R                  < SU R                  R                   S3$ )r   zOpenAPIResourceTemplate(name=z, uri_template=r   r   )r]   r  r   rl   r   s    r-   r    OpenAPIResourceTemplate.__repr__  s8    .tyym?4K\K\J__fgkgrgrgwgwfxxyzzr/   c                  #    / nUR                  5        H  u  pVUR                  U SU 35        M     [        U R                  U R                  UU R
                   SSR                  U5       3U R                  =(       d    SU R                  R                   3S[        U R                  R                  =(       d    / 5      U R                  S9$ 7f)z,Create a resource with the given parameters.=-zResource for r  )r   rp   r   r]   rL   r   rP   r   )r   appendr   r   r   r]   joinrL   rl   re   rP   r   )r`   r   r   context	uri_partskeyr  s          r-   create_resource'OpenAPIResourceTemplate.create_resource  s      	 ,,.JCuAeW-. ) <<++II;a 345((NmDKK<L<L;M,N(T[[%%+,MM	
 		
s   CCr   )NN)r   r   rp   r   r  r   r]   r   rL   r   rz   r   rP   r   r   r   r   r   )r   r   r   r   r"  zContext | Noner   r   )
r9   r:   r;   r<   r=   r~   r   r%  r>   r   r   s   @r-   r  r    s    A !% $ !  !  	 
     #       0{ #'	

 
  	

 

 
r/   r  c                     ^  \ rS rSrSr       S
                   SU 4S jjjr S     SS jjr      SS jr      SS jr      SS jr	      SS jr
S	rU =r$ )FastMCPOpenAPIi  a
  
FastMCP server implementation that creates components from an OpenAPI schema.

This class parses an OpenAPI specification and creates appropriate FastMCP components
(Tools, Resources, ResourceTemplates) based on route mappings.

Example:
    ```python
    from fastmcp.server.openapi import FastMCPOpenAPI, RouteMap, MCPType
    import httpx

    # Define custom route mappings
    custom_mappings = [
        # Map all user-related endpoints to ResourceTemplate
        RouteMap(
            methods=["GET", "POST", "PATCH"],
            pattern=r".*/users/.*",
            mcp_type=MCPType.RESOURCE_TEMPLATE
        ),
        # Map all analytics endpoints to Tool
        RouteMap(
            methods=["GET"],
            pattern=r".*/analytics/.*",
            mcp_type=MCPType.TOOL
        ),
    ]

    # Create server with custom mappings and route mapper
    server = FastMCPOpenAPI(
        openapi_spec=spec,
        client=httpx.AsyncClient(),
        name="API Server",
        route_maps=custom_mappings,
    )
    ```
c
                  > [         TU ]  " SSU=(       d    S0U
D6  X l        Xl        X`l        [        5       [        5       [        5       [        5       S.U l        [        R                  " U5      nSnU=(       d    / [        -   nU GH  n[        X5      nUR                  c   eUR                  nUbJ   U" X5      nUb>  Un[        R                  SUR                   SUR                   SUR                    35        U R'                  X5      n[)        UR*                  5      UR,                  -  U=(       d
    [)        5       -  nU[.        R0                  :X  a  U R3                  UUUS9  M  U[.        R4                  :X  a  U R7                  UUUS9  GM  U[.        R8                  :X  a  U R;                  UUUS9  GM5  U[.        R<                  :X  d  GML  [        R?                  SUR                   SUR                   35        US-  nGM     [        R?                  S[A        U5      U-
   S35        g! ["         a>  n[        R%                  S	UR                   SUR                   S
U S35         SnAGNwSnAff = f)a  
Initialize a FastMCP server from an OpenAPI schema.

Args:
    openapi_spec: OpenAPI schema as a dictionary or file path
    client: httpx AsyncClient for making HTTP requests
    name: Optional name for the server
    route_maps: Optional list of RouteMap objects defining route mappings
    route_map_fn: Optional callable for advanced route type mapping.
        Receives (route, mcp_type) and returns MCPType or None.
        Called on every route, including excluded ones.
    mcp_component_fn: Optional callable for component customization.
        Receives (route, component) and can modify the component in-place.
        Called on every created component.
    mcp_names: Optional dictionary mapping operationId to desired component names.
        If an operationId is not in the dictionary, falls back to using the
        operationId up to the first double underscore. If no operationId exists,
        falls back to slugified summary or path-based naming.
        All names are truncated to 56 characters maximum.
    tags: Optional set of tags to add to all components. Components always receive any tags
        from the route.
    timeout: Optional timeout (in seconds) for all requests
    **settings: Additional settings for FastMCP
r]   zOpenAPI FastMCP)toolresourceresource_templatepromptr   Nrh   ri   z* mapping customized by route_map_fn: type=zError in route_map_fn for r   z. Using default values.)rP   zExcluding route: r  z$Created FastMCP OpenAPI server with z routesr8   )!r}   r~   r   r   _mcp_component_fnr   _used_namesr   parse_openapi_to_http_routesDEFAULT_ROUTE_MAPPINGSru   rQ   rn   ro   rj   rl   r]   	Exceptionr   _generate_default_namere   rP   rR   r0   r4   _create_openapi_toolr5   _create_openapi_resourcer6   _create_openapi_templater7   infor   )r`   openapi_specr   r]   
route_mapsroute_map_fnmcp_component_fn	mcp_namesrP   r   rW   http_routesnum_excludedrp   rr   rK   r   r   component_name
route_tagsr   s                       r-   r~   FastMCPOpenAPI.__init__  sL   J 	Dd7&7D8D!1 I	!(i	
 ::<H  &B*@@
 E-e@I %%111"++J ')%<F)%+
$U\\N!EJJ< @$$.OO#46 "88JNUZZ9+=+==OJW\\)))%j)Qw///--e^*-Uw888--e^*-Uw./~QuzzlKL!K !N 	23{3Cl3R2SSZ[	
+ ! NN4U\\N!EJJ<rRSQT U0 1 s   5A	H22
I:<3I55I:c                X   SnU=(       d    0 nUR                   (       a>  UR                   U;   a  X!R                      nOMUR                   R                  S5      S   nO.UR                  =(       d    UR                   SUR                   3n[        U5      n[        U5      S:  a  USS nU$ )zEGenerate a default name from the route using the configured strategy.r&   r   r   r'   8   N)operation_idr  summaryrj   rl   r.   r   )r`   rp   mcp_names_mapr]   s       r-   r3  %FastMCPOpenAPI._generate_default_nameE  s     %+ !!]2$%7%78 ))//5a8==Bu||nAejj\$BD~ t9r>9Dr/   c           	         U R                   U   U==   S-  ss'   U R                   U   U   S:X  a  U$ U SU R                   U   U    3n[        R                  SU SUSS  SU S35        U$ )	z
Ensure the name is unique within its component type by appending numbers if needed.

Args:
    name: The proposed name
    component_type: The type of component ("tools", "resources", or "templates")

Returns:
    str: A unique name for the component
r  r'   zName collision detected: 'z' already exists as a Nr  z	. Using 'z
' instead.)r/  rn   ro   )r`   r]   component_typenew_names       r-   _get_unique_nameFastMCPOpenAPI._get_unique_name^  s      	(.!3.N+D1Q6K q!1!1.!A$!G HIHLL,TF2HX[Y[I\H] ^":/
 r/   c                   [        U5      n[        UR                  UR                  UR                  5      nU R                  US5      nUR                  =(       d/    UR                  =(       d    SUR                   SUR                   3n[        UUR                  UR                  UR                  S9n[        U R                  UUUUU[        UR                   =(       d    / 5      U-  U R"                  S9n	U R$                  b+   U R%                  X5        [&        R)                  SU S35        U	R.                  nXR0                  R2                  U'   [&        R)                  SU SUR                   SUR                   SUR                    35        g! [*         a&  n
[&        R-                  S	U S
U
 S35         Sn
A
NSn
A
ff = f)z?Creates and registers an OpenAPITool with enhanced description.r*  z	Executes ri   base_description	responsesrz   r   )r   rp   r]   rL   rz   r{   rP   r   NzTool  customized by component_fnzError in component_fn for tool r   . Using component as-is.zRegistered TOOL:  () with tags: )r   r   rP  schema_definitionsopenapi_versionrK  rL   rE  rj   rl   r   rz   r   rw   r   re   rP   r   r.  rn   ro   r2  r   r]   _tool_manager_tools)r`   rp   r]   rP   combined_schemar{   	tool_namerO  enhanced_descriptionr*  r   final_tool_names               r-   r4  #FastMCPOpenAPI._create_openapi_tool|  s    +51 =OOU55u7L7L

 ))$7	  7}}75<<.%**6 	  A-oo''++	 
 <<,&'UZZ%2&-MM	
 !!-&&u3uYK/JKL )) 6:!!/205<<.%**][`[e[eZfg	
  5i[1# F- . s   *F 
GF??Gc                "   U R                  US5      nSU 3nUR                  =(       d"    UR                  =(       d    SUR                   3n[	        UUR
                  UR                  UR                  S9n[        U R                  UUUU[        UR                  =(       d    / 5      U-  U R                  S9nU R                  b+   U R                  X5        [        R                  SU S35        [%        UR&                  5      n
XR(                  R*                  U
'   [        R                  SU
 SUR,                   SUR                   SUR                   35        g! [          a&  n	[        R#                  S	U S
U	 S35         Sn	A	NSn	A	ff = f)zCCreates and registers an OpenAPIResource with enhanced description.r+  resource://zRepresents rN  )r   rp   r   r]   rL   rP   r   Nz	Resource rQ  z#Error in component_fn for resource r   rR  zRegistered RESOURCE: rS  ri   rT  )rK  rL   rE  rl   r   rP  rz   r   r   r   re   rP   r   r.  rn   ro   r2  r   r   r   _resource_manager
_resourcesrj   )r`   rp   r]   rP   resource_namer  rO  r[  r+  r   final_resource_uris              r-   r5  'FastMCPOpenAPI._create_openapi_resource  s    --dJ?$]O4LLK

|2L 	
  A-oo''++	 
 #<<,UZZ%2&-MM
 !!-&&u7y6QRS !. AI))*<=#$6#7r%,,qTabgblblamn	
  9,r! M- . s   *E 
F(F		Fc                ,   U R                  US5      nUR                   Vs/ s H   oUR                  S:X  d  M  UR                  PM"     nnUR	                  5         SU 3nU(       a  USSR                  S U 5       5      -   -  nUR                  =(       d"    UR                  =(       d    SUR                   3n[        UUR                  UR                  UR                  S9n	SUR                   Vs0 s H  nUR                  S:X  d  M  UR                  0 [        UR                  [        5      (       a  UR                  R                  5       O0 EUR                  (       a=  [        UR                  [        5      (       a  S	UR                  ;   d  S	UR                  0O0 E_M     snUR                   Vs/ s H3  oUR                  S:X  d  M  UR                   (       d  M'  UR                  PM5     snS
.n
[#        U R$                  UUUU	U
['        UR(                  =(       d    / 5      U-  U R*                  S9nU R,                  b+   U R-                  X5        [.        R1                  SU S35        UR6                  nXR8                  R:                  U'   [.        R1                  SU SUR<                   SUR                   SUR(                   35        gs  snf s  snf s  snf ! [2         a&  n[.        R5                  SU SU S35         SnANSnAff = f)zKCreates and registers an OpenAPIResourceTemplate with enhanced description.r,  rl   r_  r   c              3  .   #    U  H  nS U S3v   M     g7f)r   r   Nr8   )r   r   s     r-   r   :FastMCPOpenAPI._create_openapi_template.<locals>.<genexpr>  s     .O;aA3bz;s   zTemplate for rN  objectrL   )r   r   r   )r   rp   r  r]   rL   rz   rP   r   Nz	Template rQ  z#Error in component_fn for template r   rR  zRegistered TEMPLATE: rS  ri   rT  )rK  rz   r   r]   r
  r!  rL   rE  rl   r   rP  r   r\   r   r   copyr   r  r   re   rP   r   r.  rn   ro   r2  r   r  r`  
_templatesrj   )r`   rp   r]   rP   template_namer   r   uri_template_strrO  r[  template_params_schematemplater   final_template_uris                 r-   r6  'FastMCPOpenAPI._create_openapi_template  s    --d4GH','7'7P'7!::;Ovqvv'7P(8chh.O;.O&O OO NNM%**2N 	
  A-oo''++	 
  )) *A::'
 
+5aii+F+Fqyy~~'B
 ==&qyy$77MQYY<V '6
  
 
 *  !& 0 0 01JJ&4HQZZ 0#"
, +<<),-UZZ%2&-MM	
 !!-&&u7y)9(::UVW &22 AI))*<=#$6#7r%,,qTabgblblamn	
I Q**  9:J9K2aS Q- . sA   KK2K
BK6KK K*K# #
L-LL)r   r.  r   r/  )NNNNNNN)r8  r   r   r   r]   z
str | Noner9  zlist[RouteMap] | Noner:  zRouteMapFn | Noner;  zComponentFn | Noner<  dict[str, str] | NonerP   r   r   r   rW   r
   r   )rp   r   rF  rq  r   r   )r]   r   rI  z:Literal['tool', 'resource', 'resource_template', 'prompt']r   r   )rp   r   r]   r   rP   rO   )r9   r:   r;   r<   r=   r~   r3  rK  r4  r5  r6  r>   r   r   s   @r-   r(  r(    sN   #R  ,0*./3+/ $ $a
$a
 "a
 	a

 *a
 (a
 -a
 )a
 a
 a
 a
 a
H PT&7L	2 S 
	<<
 <
 <
 	<
|3
 3
 3
 	3
jP
 P
 P
 	P
 P
r/   r(  )r+   r   r   r   )rp   r   rq   zlist[RouteMap]r   rE   )Cr=   
__future__r   enumr   r(   rY   collectionsr   collections.abcr   dataclassesr   r   r   typingr	   r
   r   r   	mcp.typesr   pydantic.networksr   rV   fastmcp.exceptionsr   fastmcp.resourcesr   r   fastmcp.server.dependenciesr   fastmcp.server.serverr   fastmcp.tools.toolr   r   fastmcp.utilitiesr   fastmcp.utilities.loggingr   fastmcp.utilities.openapir   r   r   r   r   r   fastmcp.serverr   r9   rn   
HttpMethodr.   
RouteMapFnComponentFnEnumr0   r@   rE   r4   r1  ru   rw   r   r  r(  r8   r/   r-   <module>r     s[   < "   	   $ (  . .  % $  ( 8 8 ) / % 0  &	H	OP
0 y),.>>?
A 		
dii &
		 
 4?, ?, ?,J gll# 
*+*+*+ *+Zc=$ c=L	v=h v=r5
. 5
pC
W C
r/   