
    Ki                       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	m
Z
mZ ddlZddlmZmZmZmZ ddlmZmZ ddl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 m!Z!  G d de      Z" G d de"      Z# G d de"      Z$ ee#e$z     e#e$z        Z%e&e"z  e'e&e
f   z  ee&e"z  e'e&e
f   z     z  Z(e(ee(   z  Z) G d de      Z* G d de      Z+y)z!Base classes for FastMCP prompts.    )annotationsN)	AwaitableCallableSequence)TYPE_CHECKINGAnyLiteral)	BaseModelFieldTypeAdaptervalidate_call)find_context_parameterinject_context)func_metadata)ContentBlockIconTextContent)Context)ServerSessionT)LifespanContextTRequestTc                  :     e Zd ZU dZded<   ded<   d fdZ xZS )Messagez#Base class for all prompt messages.Literal['user', 'assistant']roler   contentc                b    t        |t              rt        d|      }t        |   dd|i| y )Ntexttyper   r    )
isinstancestrr   super__init__selfr   kwargs	__class__s      k/home/jay/workspace/scripts/.codegraph-venv/lib/python3.12/site-packages/mcp/server/fastmcp/prompts/base.pyr%   zMessage.__init__   s.    gs#!vG<G33F3    r   zstr | ContentBlockr(   r   )__name__
__module____qualname____doc____annotations__r%   __classcell__r)   s   @r*   r   r      s    -
&&4 4r+   r   c                  4     e Zd ZU dZdZded<   d fdZ xZS )UserMessagezA message from the user.userr   r   c                (    t        |   dd|i| y Nr   r!   r$   r%   r&   s      r*   r%   zUserMessage.__init__'       33F3r+   r,   r-   r.   r/   r0   r   r1   r%   r2   r3   s   @r*   r5   r5   "   s    ")/D
&/4 4r+   r5   c                  4     e Zd ZU dZdZded<   d fdZ xZS )AssistantMessagezA message from the assistant.	assistantr   r   c                (    t        |   dd|i| y r8   r9   r&   s      r*   r%   zAssistantMessage.__init__0   r:   r+   r,   r;   r3   s   @r*   r=   r=   +   s    ')4D
&44 4r+   r=   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)PromptArgumentz+An argument that can be passed to a prompt.zName of the argumentdescriptionr#   nameNz%Description of what the argument does
str | NonerC   Fz Whether the argument is requireddefaultrC   boolrequired)	r-   r.   r/   r0   r   rD   r1   rC   rI   r!   r+   r*   rA   rA   :   s9    5"89D#9#D6]^K^56XYHdYr+   rA   c                  2   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<    edd      Z	ded<    ed      Z
ded<    edd      Zded<    eddd      Zded<   e	 	 	 	 	 d	 	 	 	 	 	 	 	 	 	 	 	 	 dd       Z	 	 d	 	 	 	 	 ddZy) Promptz7A prompt template that can be rendered with parameters.zName of the promptrB   r#   rD   Nz"Human-readable title of the promptrE   titlez#Description of what the prompt doesrC   z*Arguments that can be passed to the promptzlist[PromptArgument] | None	argumentsT)exclude5Callable[..., PromptResult | Awaitable[PromptResult]]fnz&Optional list of icons for this promptrF   list[Icon] | Noneiconsz-Name of the kwarg that should receive context)rC   rN   context_kwargc           	        |xs |j                   }|dk(  rt        d      |t        |      }t        |||gng       }|j                  j                         }	g }
d|	v r[|	d   j                         D ]E  \  }}||	j                  dg       v }|
j                  t        ||j                  d      |             G t        |      } | |||xs |j                  xs d|
|||	      S )
zCreate 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
        z<lambda>z,You must provide a name for lambda functions)
skip_names
propertiesrI   rC   )rD   rC   rI    )rD   rL   rC   rM   rP   rR   rS   )r-   
ValueErrorr   r   	arg_modelmodel_json_schemaitemsgetappendrA   r   r0   )clsrP   rD   rL   rC   rR   rS   	func_namefunc_arg_metadata
parametersrM   
param_nameparamrI   s                 r*   from_functionzPrompt.from_functionM   s   $ 'BKK	
"KLL  226M **7*C
 '00BBD
 +-	:%%/%=%C%C%E !
E%
B)GG  "'$)IIm$<!) 2#7rzz7R'
 	
r+   c                  K   | j                   rT| j                   D ch c]  }|j                  s|j                   }}t        |xs i       }||z
  }|rt	        d|       	 t        | j                  |xs i || j                        } | j                  di |}t        j                  |      r
| d{   }t        |t        t        z        s|g}g }	|D ]  }
	 t        |
t              r|	j                  |
       nt        |
t              r%|	j                  t         j#                  |
             nt        |
t$              r)t'        d|
      }|	j                  t)        |             nFt+        j,                  |
t$        d      j/                         }|	j                  t        d|	              |	S c c}w 7 # t0        $ r t	        d
|
       w xY w# t0        $ r }t	        d| j                   d|       d}~ww xY ww)z!Render the prompt with arguments.zMissing required arguments: Nr   r   )r      )fallbackindentr6   )r   r   z,Could not convert prompt result to message: zError rendering prompt z: r!   )rM   rI   rD   setrX   r   rP   rS   inspectiscoroutiner"   listtupler   r]   dictmessage_validatorvalidate_pythonr#   r   r5   pydantic_coreto_jsondecode	Exception)r'   rM   contextargrI   providedmissing	call_argsresultmessagesmsgr   es                r*   renderzPrompt.render   s     >>,0NNKScllKHK9?+H)G #?y!IJJ 	I&tww	R$J\J\]I TWW)y)F""6*% fdUl3  ')H [[!#w/ ,#C. (9(I(I#(NO#C-"-6"D G(DE"/"7"7cRS"T"["["] VW(MN[ OI L &( ! [$'STWSX%YZZ[  	I6tyykA3GHH	Isd   HF7F7&H$AG 6F<7%G CF?3G 6H<G ?GG 	H#G>>HH)NNNNN)rP   rO   rD   rE   rL   rE   rC   rE   rR   rQ   rS   rE   returnrK   )NN)rM   zdict[str, Any] | Noneru   z:Context[ServerSessionT, LifespanContextT, RequestT] | Noner   zlist[Message])r-   r.   r/   r0   r   rD   r1   rL   rC   rM   rP   rR   rS   classmethodrd   r~   r!   r+   r*   rK   rK   B   s   A"67D#7d0TUE:U#D6[\K\-24Eq-rI*r@Ed@SB=S$T?ghEh %d8gqu vM:v   "&#'$(9
A9
 9
 	9

  9
 !9
 "9
 
9
 9
z ,0NR.I(.I L.I 
	.Ir+   rK   ),r0   
__future__r   rj   collections.abcr   r   r   typingr   r   r	   rq   pydanticr
   r   r   r   .mcp.server.fastmcp.utilities.context_injectionr   r   *mcp.server.fastmcp.utilities.func_metadatar   	mcp.typesr   r   r   mcp.server.fastmcp.serverr   mcp.server.sessionr   mcp.shared.contextr   r   r   r5   r=   ro   r#   rn   SyncPromptResultPromptResultrA   rK   r!   r+   r*   <module>r      s    ' "  9 9 . .  A A a D 5 511=	4i 	44' 44w 4 @K.> >?N^@^_ =4S>1HS7]TRUWZRZ^=[4\\ ),<"==ZY ZuIY uIr+   