
    kKj8                       d Z ddlmZ ddlZddlmZmZmZ ddlm	Z	 ddl
mZmZmZmZmZ ddlmZmZmZ ddlmZ dd	lmZ dd
lmZ erddlmZ ddlmZ  ee      Z ededef         Z dddZ!	 	 	 	 	 	 	 	 	 	 ddZ" G d de      Z#y)u4  FastMCPApp — a Provider that represents a composable MCP application.

FastMCPApp binds entry-point tools (model calls these) together with backend
tools (the UI calls these via CallTool).  Backend tools are tagged with
``meta["fastmcp"]["app"]`` so they can be found through the provider chain
even when transforms (namespace, visibility, etc.) have renamed or hidden
them — the server sets a context var that tells ``Provider.get_tool`` to
fall back to a direct lookup for app-visible tools.

Usage::

    from fastmcp import FastMCP, FastMCPApp

    app = FastMCPApp("Dashboard")

    @app.ui()
    def show_dashboard() -> Component:
        return Column(...)

    @app.tool()
    def save_contact(name: str, email: str) -> str:
        return name

    server = FastMCP("Platform")
    server.add_provider(app)
    )annotationsN)AsyncIteratorCallableSequence)asynccontextmanager)TYPE_CHECKINGAnyLiteralTypeVaroverload)AnyFunctionIconToolAnnotations)Provider)	AuthCheck)
get_loggerLocalProviderToolF.)boundc                :     ddl mm d fddfd}|S )u  Create a CallTool resolver that prefixes tool names with a hash.

    Structurally identical to the old ``___`` resolver — ``app_name`` is
    the FastMCPApp's name, known at serialization time from the tool's
    ``meta["fastmcp"]["app"]`` tag. The only change is the wire format:
    ``<hash>_<local_name>`` instead of ``<app_name>___<local_name>``.

    The dispatcher recognizes the hashed form and routes it via
    ``get_tool_by_hash`` which walks the provider tree recursively —
    same pattern as ``get_app_tool``.
    r   )hashed_backend_nameparse_hashed_backend_namec                2    r |       | S  |       S | S N )
local_nameapp_namer   r   s    a/Users/ahmed/devFolder/Ultron/claude-voice/.venv/lib/python3.12/site-packages/fastmcp/apps/app.py_prefixz_make_resolver.<locals>._prefixH   s,     )4@!!&x<<    c                &   ddl m} t        | t              r | |             S d }	 ddlm}  ||       }|t        |dd       }| | |            S t        | dd       }| | |            S t        d|       # t        $ r Y Ww xY w)Nr   )ResolvedToolname)get_fastmcp_metar'   __name__zCannot resolve tool reference: )	prefab_ui.appr%   
isinstancestrfastmcp.decoratorsr(   	Exceptiongetattr
ValueError)fnr%   fmetar(   r'   fn_namer"   s         r!   _resolve_tool_refz)_make_resolver.<locals>._resolve_tool_refQ   s    .b#WR[11	;$R(E &ufd;D#77"j$/WW%566:2&ABB  		s   B 	BB)r   r,   returnr,   )r1   r	   r5   r	   )#fastmcp.server.providers.addressingr   r   )r    r4   r"   r   r   s   ` @@@r!   _make_resolverr7   7   s    
C2 r#   c                    t        j                  |       r	 | |      S t        | t              r|t	        d      | n| |nt	        d| dt        |              dfd}|S )zEShared dispatch logic for @app.tool() and @app.ui() calling patterns.zECannot specify both a name as first argument and as keyword argument.zFirst argument to @z* must be a function, string, or None, got c                     |       S r   r   )r1   register	tool_names    r!   	decoratorz&_dispatch_decorator.<locals>.decorator   s    I&&r#   )r1   r   r5   r   )inspect	isroutiner+   r,   	TypeErrortype)
name_or_fnr'   r:   decorator_namer<   r;   s     `  @r!   _dispatch_decoratorrC   m   s     $
D))*c"W  !+				!.!1 2
#$&
 	

' r#   c                  :    e Zd ZdZd fdZddZedddddd	 	 	 	 	 	 	 	 	 	 	 	 	 dd       Ze	 ddddddd	 	 	 	 	 	 	 	 	 	 	 	 	 dd       Z	 ddddddd	 	 	 	 	 	 	 	 	 	 	 	 	 dd	Zeddddddddd
	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 dd       Ze	 dddddddddd
	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 d d       Z	 dddddddddd
	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 d!dZ	 	 	 	 d"dZ	d#dZ
dd$dZd%dZdd&dZd%dZdd&dZd%dZdd'dZed(d       Z	 d	 	 	 	 	 d)dZ xZS )*
FastMCPAppaO  A Provider that represents an MCP application.

    Binds together entry-point tools (``@app.ui``), backend tools
    (``@app.tool``), and the Prefab renderer resource.  Backend tools
    are tagged with ``meta["fastmcp"]["app"]`` so ``Provider.get_tool``
    can find them by original name even when transforms have been applied.
    c                X    ddl m} t        |           || _         |d      | _        y )Nr   r   error)on_duplicate)'fastmcp.server.providers.local_providerr   super__init__r'   _local)selfr'   r   	__class__s      r!   rK   zFastMCPApp.__init__   s$    I	%2%Hr#   c                "    d| j                   dS )NzFastMCPApp()r&   rM   s    r!   __repr__zFastMCPApp.__repr__   s    TYYM++r#   NF)r'   descriptionmodelauthtimeoutc                    y r   r   rM   rA   r'   rS   rT   rU   rV   s          r!   toolzFastMCPApp.tool   s     r#   c                    y r   r   rX   s          r!   rY   zFastMCPApp.tool   s     r#   c               N     |rddgndgd fd}t        |||d      S )a  Register a backend tool that the UI calls via CallTool.

        Backend tools default to ``visibility=["app"]``.  Pass ``model=True``
        to also expose the tool to the model (``visibility=["app", "model"]``).

        Supports multiple calling patterns::

            @app.tool
            def save(name: str): ...

            @app.tool()
            def save(name: str): ...

            @app.tool("custom_name")
            def save(name: str): ...
        apprT   c                <   ddl m} |xs t        | dd       }|t        d|       ddlm}m} ddlm}  |      } ||      j                   |j                  |      dd	} |j                  | ||

      }	j                  j                  |	       | S )Nr   r   r)   zCannot determine tool name for 	AppConfigapp_config_to_meta_dict	hash_tool)
visibilityr\   
_tool_hashuifastmcp)r'   rS   metarV   rU   )fastmcp.tools.baser   r/   r0   fastmcp.apps.configr_   r`   r6   rb   r'   from_functionrL   _add_component)r1   r;   r   resolved_namer_   r`   rb   
app_configri   tool_objrU   rS   rM   rV   rc   s             r!   	_registerz"FastMCPApp.tool.<locals>._register   s    /%FZ)FM$ #B2&!IJJNE"j9J-j999"+DII}"E$D *t))"'H KK&&x0Ir#   rY   r1   r   r;   
str | Noner5   r   rC   )	rM   rA   r'   rS   rT   rU   rV   rq   rc   s	   `  ` `` @r!   rY   zFastMCPApp.tool   s5    6 !&UGE7 		 	< #:tYGGr#   )r'   rS   titletagsiconsr   rU   rV   c                    y r   r   
rM   rA   r'   rS   ru   rv   rw   r   rU   rV   s
             r!   rg   zFastMCPApp.ui  s     r#   c                    y r   r   ry   s
             r!   rg   zFastMCPApp.ui  s     r#   c          	     H    	 d 	fd}
t        |||
d      S )a!  Register a UI entry-point tool that the model calls.

        Entry-point tools default to ``visibility=["model"]`` and auto-wire
        the Prefab renderer resource and CSP. They are tagged with the app
        name so structured content includes ``_meta.fastmcp.app``.

        Supports multiple calling patterns::

            @app.ui
            def dashboard() -> Component: ...

            @app.ui()
            def dashboard() -> Component: ...

            @app.ui("my_dashboard")
            def dashboard() -> Component: ...
        c                <   ddl m}m} ddlm} ddlm} ddlm} |xs t        | dd       xs d} ||dg	      } ||      j                   |j                  |      d
d}	 |j                  | ||	
      }
j                  j                  |
       | S )Nr   r^   ra   )PREFAB_RENDERER_URIr   r)   unknownrT   )resource_urirc   rd   rf   )	r'   rS   ru   rv   rw   r   ri   rV   rU   )rk   r_   r`   r6   rb   8fastmcp.server.providers.local_provider.decorators.toolsr}   rj   r   r/   r'   rl   rL   rm   )r1   r;   r_   r`   rb   r}   r   resolvedro   ri   rp   r   rU   rS   rw   rM   rv   rV   ru   s              r!   rq   z FastMCPApp.ui.<locals>._register>  s    NE 0 NGB
D$ANYH"0#9J .j999"+DIIx"@$D *t))''H KK&&x0Ir#   rg   rr   rt   )rM   rA   r'   rS   ru   rv   rw   r   rU   rV   rq   s   `  ``````` r!   rg   zFastMCPApp.ui  s!    >$	 $	L #:tYEEr#   c                   ddl m} t        ||      s |j                  |      }ddlm} |j                  rt        |j                        ni }|j                  di       }| j                  |d<    || j                  |j                        |d<   |j                  di       }d|vrdg|d<   ||_        | j                  j                  |       |S )	zoAdd a tool to this app programmatically.

        The tool is tagged with this app's name for routing.
        r   r   ra   rh   r\   re   rg   rc   )rj   r   r+   _ensure_toolr6   rb   ri   dict
setdefaultr'   rL   rm   )rM   rY   r   rb   ri   fmrg   s          r!   add_toolzFastMCPApp.add_toolj  s     	,$%$4$$T*DA"&))tDII__Y+II5	$TYY		:<__T2&r! %wB|	""4(r#   c                R   K   | j                   j                          d {   S 7 wr   )rL   _list_toolsrQ   s    r!   r   zFastMCPApp._list_tools  s     [[,,....   '%'c                V   K   | j                   j                  ||       d {   S 7 wr   )rL   	_get_toolrM   r'   versions      r!   r   zFastMCPApp._get_tool  s#     [[**49999    )')c                R   K   | j                   j                          d {   S 7 wr   )rL   _list_resourcesrQ   s    r!   r   zFastMCPApp._list_resources  s     [[002222r   c                V   K   | j                   j                  ||       d {   S 7 wr   )rL   _get_resourcerM   urir   s      r!   r   zFastMCPApp._get_resource  s#     [[..sG<<<<r   c                R   K   | j                   j                          d {   S 7 wr   )rL   _list_resource_templatesrQ   s    r!   r   z#FastMCPApp._list_resource_templates  s     [[99;;;;r   c                V   K   | j                   j                  ||       d {   S 7 wr   )rL   _get_resource_templater   s      r!   r   z!FastMCPApp._get_resource_template  s#     [[77WEEEEr   c                R   K   | j                   j                          d {   S 7 wr   )rL   _list_promptsrQ   s    r!   r   zFastMCPApp._list_prompts  s     [[..0000r   c                V   K   | j                   j                  ||       d {   S 7 wr   )rL   _get_promptr   s      r!   r   zFastMCPApp._get_prompt  s#     [[,,T7;;;;r   c                  K   | j                   j                         4 d {    d  d d d       d {    y 7 7 # 1 d {  7  sw Y   y xY wwr   )rL   lifespanrQ   s    r!   r   zFastMCPApp.lifespan  s5     ;;'')) *))))))s>   A<AA A>AA AA	AAc                ~    ddl m}  || j                        }|j                  |         |j                  dd|i| y)z>Create a temporary FastMCP server and run this app standalone.r   )FastMCP	transportNr   )fastmcp.server.serverr   r'   add_providerrun)rM   r   kwargsr   servers        r!   r   zFastMCPApp.run  s9     	2#D!

1Y1&1r#   )r'   r,   r5   None)r5   r,   )rA   r   r'   rs   rS   rs   rT   boolrU   "AuthCheck | list[AuthCheck] | NonerV   float | Noner5   r   r   )rA   rs   r'   rs   rS   rs   rT   r   rU   r   rV   r   r5   Callable[[F], F])rA   str | AnyFunction | Noner'   rs   rS   rs   rT   r   rU   r   rV   r   r5   r	   )rA   r   r'   rs   rS   rs   ru   rs   rv   set[str] | Nonerw   list[Icon] | Noner   ToolAnnotations | NonerU   r   rV   r   r5   r   )rA   rs   r'   rs   rS   rs   ru   rs   rv   r   rw   r   r   r   rU   r   rV   r   r5   r   )rA   r   r'   rs   rS   rs   ru   rs   rv   r   rw   r   r   r   rU   r   rV   r   r5   r	   )rY   zTool | Callable[..., Any]r5   r   )r5   zSequence[Tool])r'   r,   r   r	   r5   zTool | None)r5   zSequence[Any])r   r,   r   r	   r5   
Any | None)r'   r,   r   r	   r5   r   )r5   zAsyncIterator[None])r   z9Literal['stdio', 'http', 'sse', 'streamable-http'] | Noner   r	   r5   r   )r)   
__module____qualname____doc__rK   rR   r   rY   rg   r   r   r   r   r   r   r   r   r   r   r   r   __classcell__)rN   s   @r!   rE   rE      s   I, 
  "&37 $		 		
  	 	 1	 	 
	 	  "&	  "&37 $		 		
  	 	 1	 	 
	 	 04<H  "&37 $<H,<H 	<H
  <H <H 1<H <H 
<HD 
  "&  $#'.237 $ 	
     ! , 1  
   "&  "&  $#'.237 $ 	
     ! , 1  
   04EF  "&  $#'.237 $EF,EF 	EF
  EF EF EF !EF ,EF 1EF EF 
EFV' 
>/:3=<F1<   PT
2L
2 
2 
	
2r#   rE   r   )r    rs   r5   r	   )
rA   r   r'   rs   r:   z Callable[[Any, str | None], Any]rB   r,   r5   r	   )$r   
__future__r   r=   collections.abcr   r   r   
contextlibr   typingr   r	   r
   r   r   	mcp.typesr   r   r   fastmcp.server.providers.baser   fastmcp.utilities.authorizationr   fastmcp.utilities.loggingr   rI   r   rj   r   r)   loggerr   r7   rC   rE   r   r#   r!   <module>r      s   6 #  = = * A A 8 8 2 5 0E'	H	CxS)*3l(
 / 	
 	Fd2 d2r#   