
    ~h#                       d Z ddlmZ ddlZddlmZmZ ddlm	Z	m
Z
mZ ddlmZmZ ddlZddlmZmZmZmZmZ ddlmZ dd	l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# ddl$m%Z% ddl&m'Z' ddl(m)Z)m*Z*m+Z+ er	 eez  ez  Z, e'e-      Z.	 d	 	 	 	 	 	 	 ddZ/ ee   e      Z0e1ez  e2e1ef   z  ee1ez  e2e1ef   z     z  Z3e3e	e3   z  Z4 G d de)      Z G d de#e      Z G d de      Z5y)z!Base classes for FastMCP prompts.    )annotationsN)ABCabstractmethod)	AwaitableCallableSequence)TYPE_CHECKINGAny)EmbeddedResourceImageContentPromptMessageRoleTextContent)Prompt)PromptArgument)FieldTypeAdaptervalidate_call)PromptError)get_context)FastMCPComponent)compress_schema)
get_logger)FastMCPBaseModelfind_kwarg_by_typeget_cached_typeadapterc                `    t        | t              rt        d|       } |d}t        d| |d|S )z.A user-friendly constructor for PromptMessage.texttyper   user)contentrole )
isinstancestrr   r   )r"   r#   kwargss      R/opt/mcp/mcp-sentiment/venv/lib/python3.12/site-packages/fastmcp/prompts/prompt.pyMessager)   #   s9     '368|>t>v>>    c                  j    e Zd ZU dZ ed      Zded<    edd      Zd	ed
<    edd      Zded<   y)r   z+An argument that can be passed to a prompt.zName of the argument)descriptionr&   nameNz%Description of what the argument doesdefaultr,   
str | Noner,   Fz Whether the argument is requiredboolrequired)	__name__
__module____qualname____doc__r   r-   __annotations__r,   r2   r$   r*   r(   r   r   9   sE    5"89D#9#"IK  #EHd r*   r   c                      e Zd ZU dZ edd      Zded<   d
dZe	 	 	 	 d	 	 	 	 	 	 	 	 	 	 	 dd       Z	e
	 d	 	 	 dd	       Zy)r   z7A prompt template that can be rendered with parameters.Nz*Arguments that can be passed to the promptr.   zlist[PromptArgument] | None	argumentsc                    | j                   xs g D cg c].  }t        |j                  |j                  |j                        0 }}| j                  | j                  |d}t        di ||z  S c c}w )z$Convert the prompt to an MCP prompt.r-   r,   r2   )r-   r,   r9   r$   )r9   MCPPromptArgumentr-   r,   r2   	MCPPrompt)self	overridesargr9   r'   s        r(   to_mcp_promptzPrompt.to_mcp_promptL   s|     ~~+
  XXOO
	 
 II++"

 .6I-..
s   3A0c                6    t         j                  | ||||      S )Create a Prompt from a function.

        The function can return:
        - A string (converted to a message)
        - A Message object
        - A dict (converted to a message)
        - A sequence of any of the above
        fnr-   r,   tagsenabled)FunctionPromptfrom_functionrD   s        r(   rI   zPrompt.from_function]   s'      +++D' , 
 	
r*   c                    K   t        d      w)!Render the prompt with arguments.z1Prompt.render() must be implemented by subclasses)NotImplementedError)r>   r9   s     r(   renderzPrompt.renderq   s      ""UVVs   )r?   r
   returnr=   NNNNrE   5Callable[..., PromptResult | Awaitable[PromptResult]]r-   r0   r,   r0   rF   zset[str] | NonerG   zbool | NonerN   rH   Nr9   zdict[str, Any] | NonerN   zlist[PromptMessage])r3   r4   r5   r6   r   r9   r7   rA   staticmethodrI   r   rM   r$   r*   r(   r   r   E   s    A-2"N.I* /"   "& $#
A

  
 	

 
 

 
&  ,0W(W 
W Wr*   r   c                  `    e Zd ZU dZded<   e	 	 	 	 d	 	 	 	 	 	 	 	 	 	 	 dd       Z	 d		 	 	 d
dZy)rH   zA prompt that is a function.rQ   rE   Nc                   ddl m} |xs% t        |dd      xs |j                  j                  }|dk(  rt        d      t        j                  |      }|j                  j                         D ]f  }	|	j                  t        j                  j                  k(  rt        d      |	j                  t        j                  j                  k(  s]t        d       |xs |j                  }t        j                  |      s|j                   }t#        |t$              r|j&                  }t)        |      }
|
j+                         }t-        ||	      }|r|g}nd}t/        ||
      }g }d|v rY|d   j1                         D ]C  \  }}	|j3                  t5        ||	j7                  d      ||j7                  dg       v              E t9        |      } | ||||xs
 t;               |||      S d|      S )rC   r   Contextr3   Nz<lambda>z,You must provide a name for lambda functionsz1Functions with *args are not supported as promptsz4Functions with **kwargs are not supported as prompts
kwarg_type)prune_params
propertiesr,   r2   r;   T)r-   r,   r9   rF   rG   rE   )fastmcp.server.contextrX   getattr	__class__r3   
ValueErrorinspect	signature
parametersvalueskind	ParameterVAR_POSITIONALVAR_KEYWORDr6   	isroutine__call__r%   rT   __func__r   json_schemar   r   itemsappendr   getr   set)clsrE   r-   r,   rF   rG   rX   	func_namesigparamtype_adapterrc   context_kwargr[   r9   
param_names                   r(   rI   zFunctionPrompt.from_function   s   " 	3RGB
D9RR\\=R=R	
"KLL#^^**, 	YEzzW..=== !TUUzzW..::: !WXX		Y "/RZZ   $Bb,'B-b1!--/
 +2'B)?LL$ZlK
 +-	:%%/%=%C%C%E !
E  "'$)IIm$<!+z~~j"/M!M 2#&2G
 	

 9=
 	
r*   c           
     4  K   ddl m} | j                  rT| j                  D ch c]  }|j                  s|j                   }}t        |xs i       }||z
  }|rt        d|       	 |r|j                         ni }t        | j                  |      }|r||vrt               ||<    | j                  di |}	t        j                  |	      r
|	 d{   }	t        |	t        t        z        s|	g}	g }
|	D ]  }	 t        |t               r|
j#                  |       nt        |t$              r(|
j#                  t!        dt'        d|      	             nQt)        j*                  |t$        d
      j-                         }|
j#                  t!        dt'        d|      	              |
S c c}w 7 # t.        $ r t1        d      w xY w# t.        $ rC}t2        j5                  d| j                   d|        t1        d| j                   d      d}~ww xY ww)rK   r   rW   zMissing required arguments: rY   Nr!   r   r   )r#   r"      )fallbackindentz+Could not convert prompt result to message.zError rendering prompt z: .r$   )r]   rX   r9   r2   r-   rp   r`   copyr   rE   r   ra   iscoroutiner%   listtupler   rn   r&   r   pydantic_coreto_jsondecode	Exceptionr   logger	exception)r>   r9   rX   r@   r2   providedmissingr'   rv   resultmessagesmsgr"   es                 r(   rM   zFunctionPrompt.render   s    
 	3 >>,0NNKScllKHK9?+H)G #?y!IJJ-	F)2Y^^%F.tww7KMf!<(3}% TWW&v&F""6*% fdUl3  -/H UU!#}5 ,#C- )%+(3c(J #0"7"7#a# &(   !)%+(3g(NU0 Oa L  &: ! U%&STTU  	F6tyykA3GH 7		{!DEE	Fse   !HF*F*&H*A*G	 F/%G	 ;B+F1&G	 )H/G	 1GG	 		H>HHHrO   rP   rR   rS   )r3   r4   r5   r6   r7   classmethodrI   rM   r$   r*   r(   rH   rH   z   s    &==  "& $#J
AJ
 J
  	J

 J
 J
 
J
 J
\ ,0<F(<F 
<Fr*   rH   rR   )r"   zstr | CONTENT_TYPESr#   zRole | Noner'   r
   rN   r   )6r6   
__future__r   _annotationsra   abcr   r   collections.abcr   r   r   typingr	   r
   r   	mcp.typesr   r   r   r   r   r   r=   r   r<   pydanticr   r   r   fastmcp.exceptionsr   fastmcp.server.dependenciesr   fastmcp.utilities.componentsr   fastmcp.utilities.json_schemar   fastmcp.utilities.loggingr   fastmcp.utilities.typesr   r   r   CONTENT_TYPESr3   r   r)   message_validatorr&   dictSyncPromptResultPromptResultrH   r$   r*   r(   <module>r      s,   ' 2  # 9 9 %  V V ) 9 6 6 * 3 9 9 0  l*-==	H	 7;? ?(3?FI?? /K.}=  
38n s]"T#s(^345   ),<"==	% 	2Ws 2WjNFV NFr*   