
    kKj!                        d Z ddlmZ ddlmZmZ ddlmZmZm	Z	 erddl
mZ ddlmZ ddlmZ dZdd	Zdd
Z	 	 	 	 	 	 ddZddZddZddZ	 	 	 	 	 	 ddZddZy)a  On-demand Prefab renderer resource synthesis.

Tools marked as Prefab (via ``app=True``, ``PrefabAppConfig``, etc.) carry
a placeholder ``meta.ui.resourceUri`` and optionally a hash in
``meta.fastmcp._tool_hash``. This module synthesizes per-tool renderer
resources on demand at ``list_resources`` and ``read_resource`` time
without storing or materializing anything.

Each tool's resource URI is ``ui://prefab/tool/<hash>/renderer.html``
where the hash comes from the tool's own meta (set at registration from
the app name + tool name). CSP on the resource is the tool's
``meta.ui.csp`` merged with the renderer defaults across all four
``*_domains`` fields.
    )annotations)TYPE_CHECKINGAny)HASH_LENGTH	hash_toolparse_hashed_resource_uri)Resource)FastMCPToolzui://prefab/renderer.htmlc                    | j                   }|sy|j                  d      }t        |t              sy|j                  d      t        k(  S )zGTrue if *tool* was marked as needing a Prefab renderer at registration.FuiresourceUri)metaget
isinstancedictPREFAB_PLACEHOLDER_URI)toolr   r   s      z/Users/ahmed/devFolder/Ultron/claude-voice/.venv/lib/python3.12/site-packages/fastmcp/server/providers/prefab_synthesis.py_is_prefab_toolr   #   s@    99D	$Bb$66- $:::    c                h   | j                   xs i }|j                  d      }t        |t              rl|j                  d      }t        |t              rt        |      t        k(  r|S |j                  d      }t        |t              rt        || j                        S t        d| j                        S )zJRead the stored hash from tool meta, or compute from app name + tool name.fastmcp
_tool_hashapp )	r   r   r   r   strlenr   r   name)r   r   fastmcp_metahr   s        r   _get_tool_hashr#   .   s    99?D88I&L,%\*a#a&K"7Hu%c3S$)),,R##r   c                x    | |y t        | xs g       }|xs g D ]  }||vs|j                  |        |xs d S N)listappend)baseextracombineditems       r   _merge_domain_listsr,   >   sO     |DJBHxOOD!  tr   c                   	 ddl m}m} ddlm}m}m}m} ddl	m
} t        |       }|y |       xs i }	i }
| j                  rYt        | j                  j                  d      t              r0| j                  d   j                  d      }t        |t              r|}
dd}t!        |	j                  d	       ||
d	d
            t!        |	j                  d       ||
dd            t!        |	j                  d       ||
dd            t!        |	j                  d       ||
dd            d}t#        |j%                               r |di |nd}d}| j                  ret        | j                  j                  d      t              r<| j                  d   j                  d      }t        |t              rddlm}  |di |} |||      }d| d} ||d| j(                   d |       |d ||      i      S # t        $ r Y yw xY w)zWSynthesize a TextResource for a prefab tool. Returns None if prefab_ui isn't installed.r   )get_renderer_cspget_renderer_htmlN)UI_MIME_TYPE	AppConfigResourceCSPapp_config_to_meta_dict)TextResourcer   cspc                r    | j                  |      }|| j                  |      }t        |t              r|S d S r%   )r   r   r&   )dsnakecamelvals       r   _getz&_build_resource_for_tool.<locals>._gete   s5    eeEl;%%,C d+s55r   connect_domainsconnectDomainsresource_domainsresourceDomainsframe_domainsframeDomainsbase_uri_domainsbaseUriDomains)r<   r>   r@   rB   permissions)ResourcePermissions)r5   rD   ui://prefab/tool//renderer.htmlzPrefab Renderer ())urir    text	mime_typer   )r7   zdict[str, Any]r8   r   r9   r   returnlist[str] | None )prefab_ui.rendererr.   r/   ImportErrorfastmcp.apps.configr0   r1   r2   r3   fastmcp.resources.typesr4   r#   r   r   r   r   r,   anyvaluesrE   r    )r   r.   r/   r0   r1   r2   r3   r4   	tool_hashdefaultsuser_csprawr;   mergedresource_cspuser_permissions	raw_permsrE   resource_apprI   s                       r   _build_resource_for_toolr^   J   s   J  5t$I  017RH!HyyZ		d 3T:iio!!%(c4 H6 /LL*+,.>?
 0LL+,-/@A
 -LL)?N;
 0LL+,-/?@
F& -0,@;((dL yyZ		d 3T:IIdO''6	i&?2?Y?$L i[
7C 1- +L9: E  s   G9 9	HHc                    ddl m ddlm} ddlm ddlm ddlm	 g dfd| j                  D ]
  } |        S )	zTEnumerate all prefab tools across the server's providers (sync walk of _components).r   )
FastMCPApp)Provider)LocalProvider)_WrappedProviderr   c                   | }t        |
      r|j                  }t        |
      rg }t        |      r|j                  |       t        |      r|j                  |j                         |D ]4  }j	                  	fd|j
                  j                         D               6 ddlm} ddl	m
} t        ||      r|j                  D ]
  } |        t        ||      r$|j                  j                  D ]
  } |        y y )Nc              3  P   K   | ]  }t        |      rt        |      r|  y wr%   )r   r   ).0	componentr   s     r   	<genexpr>z=_walk_prefab_tools.<locals>._walk_provider.<locals>.<genexpr>   s*      !9Ii.?93M !9s   #&r   )AggregateProvider)FastMCPProvider)r   _innerr'   _localextend_componentsrT   "fastmcp.server.providers.aggregateri   )fastmcp.server.providers.fastmcp_providerrj   	providersserver)providerinnersourcessrcri   rj   childr`   rb   r   rc   _walk_providerresultss          r   rx   z*_walk_prefab_tools.<locals>._walk_provider   s     01LLE  01 (*e]+NN5!eZ(NN5<<(CNN !$!7!7!9   	IMe./u% ) e_-//u% 0 .r   )rs   ra   rL   None)fastmcp.apps.appr`   fastmcp.server.providers.basera   'fastmcp.server.providers.local_providerrb   )fastmcp.server.providers.wrapped_providerrc   fastmcp.tools.baser   rq   )	rr   ra   rs   r`   rb   r   rc   rx   ry   s	      @@@@@@r   _walk_prefab_toolsr      sB    +6EJ'G& &> $$x  % Nr   c                   K   g }t               }t        |       D ]D  }t        |      }|||v r|j                  |       t	        |      }|4|j                  |       F |S w)zCReturn fresh synthetic Prefab resources for all prefab tools. Pure.)setr   r#   addr^   r'   )rr   	resourcesseen_hashesr   r"   resources         r   synthesize_prefab_resourcesr      sl      "IEK"6*4 9[(+D1X& + s   AA#A#c                ~   K   t        |      }|yt        |       D ]  }t        |      |k(  st        |      c S  yw)z9Intercept a Prefab renderer URI and synthesize on demand.N)r   r   r#   r^   )rr   rI   digestr   s       r   !synthesize_prefab_resource_by_urir      sF      's+F~"6*$6)+D11 + s   +==c                <   t        |       s| S t        |       }|| S | j                  J t        | j                  d         }d| d|d<   |j	                  dd       |j	                  dd       t        | j                        }||d<   | j                  d|i	      S )
u   Return a model_copy with the per-tool URI and CSP stripped.

    Reads the hash from the tool's own meta. If no hash is found,
    returns the tool unchanged. Produces a fresh copy — the original
    Tool object is untouched.
    Nr   rF   rG   r   r5   rD   r   )update)r   r#   r   r   pop
model_copy)r   rU   new_uinew_metas       r   rewrite_tool_meta_for_wirer      s     4 t$I99   $))D/"F/	{.IF=
JJud
JJ}d#DIIHHTN??68"4?55r   N)r   r   rL   bool)r   r   rL   z
str | None)r(   rM   r)   rM   rL   rM   )r   r   rL   Resource | None)rr   r
   rL   z
list[Tool])rr   r
   rL   zlist[Resource])rr   r
   rI   r   rL   r   )r   r   rL   r   )__doc__
__future__r   typingr   r   #fastmcp.server.providers.addressingr   r   r   fastmcp.resources.baser	   fastmcp.server.serverr
   r   r   r   r   r#   r,   r^   r   r   r   r   rN   r   r   <module>r      s    # %  /-' 5 ;$ 	
	#3		L^,^



6r   