
    ~h'                    ,   U d dl mZ d dlZd dlmZ d dlmZ d dl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mZmZmZmZmZmZ d dlmZ d dlmZ d dlZ d dl!m"Z" d dl#m$Z$  e$e%      Z& e	dd      Z'de(d<   edd       Z)e G d d             Z*y)    )annotationsN)	Generator)contextmanager)
ContextVarToken)	dataclass)LoggingLevel)ReadResourceContents)RequestContext)CreateMessageResultImageContent	ModelHintModelPreferencesRootSamplingMessageTextContent)AnyUrl)Request)FastMCP)
get_loggercontext)defaultzContextVar[Context | None]_current_contextContextc              #     K   t         j                  |       }	 |  t         j                  |       y # t         j                  |       w xY wwN)r   setreset)r   tokens     R/opt/mcp/mcp-sentiment/venv/lib/python3.12/site-packages/fastmcp/server/context.pyset_contextr!   !   s=       )E&u%u%s   A3 AA

Ac                     e Zd ZdZddZddZddZedd       Z	 d	 	 	 	 	 	 	 ddZ	ddZ
	 	 d	 	 	 	 	 	 	 dd	Zedd
       Zedd       Zed        Zdd dZdd dZdd dZdd dZd!dZ	 	 	 	 d"	 	 	 	 	 	 	 	 	 	 	 d#dZd$dZ	 	 	 	 d%dZy)&r   a  Context object providing access to MCP capabilities.

    This provides a cleaner interface to MCP's RequestContext functionality.
    It gets injected into tool and resource functions that request it via type hints.

    To use context in a tool function, add a parameter with the Context type annotation:

    ```python
    @server.tool
    def my_tool(x: int, ctx: Context) -> str:
        # Log messages to the client
        ctx.info(f"Processing {x}")
        ctx.debug("Debug info")
        ctx.warning("Warning message")
        ctx.error("Error message")

        # Report progress
        ctx.report_progress(50, 100, "Processing")

        # Access resources
        data = ctx.read_resource("resource://data")

        # Get request info
        request_id = ctx.request_id
        client_id = ctx.client_id

        return str(x)
    ```

    The context parameter name can be anything as long as it's annotated with Context.
    The context is optional - tools that don't need it can omit the parameter.

    c                     || _         g | _        y r   )fastmcp_tokens)selfr$   s     r    __init__zContext.__init__N   s    $&    c                f    t         j                  |       }| j                  j                  |       | S )zFEnter the context manager and set this context as the current context.)r   r   r%   append)r&   r   s     r    	__enter__zContext.__enter__R   s+     !$$T*E"r(   c                |    | j                   r0| j                   j                         }t        j                  |       yy)z9Exit the context manager and reset the most recent token.N)r%   popr   r   )r&   exc_typeexc_valexc_tbr   s        r    __exit__zContext.__exit__Y   s.    <<LL$$&E""5) r(   c                B    | j                   j                  j                  S )z)Access to the underlying request context.)r$   _mcp_serverrequest_contextr&   s    r    r4   zContext.request_context_   s     ||''777r(   Nc                   K   | j                   j                  r | j                   j                  j                  nd}|y| j                   j                  j	                  ||||       d{    y7 w)zReport progress for the current operation.

        Args:
            progress: Current progress value e.g. 24
            total: Optional total value e.g. 100
        N)progress_tokenprogresstotalmessage)r4   metaprogressTokensessionsend_progress_notification)r&   r8   r9   r:   r7   s        r    report_progresszContext.report_progressd   sw      ##((   %%33 	 !""**EE)	 F 
 	
 	
s   A(A2*A0+A2c                z   K   | j                   J d       | j                   j                  |       d{   S 7 w)zRead a resource by URI.

        Args:
            uri: Resource URI to read

        Returns:
            The resource content as either text or bytes
        Nz-Context is not available outside of a request)r$   _mcp_read_resource)r&   uris     r    read_resourcezContext.read_resource~   s9      ||'X)XX'\\44S9999s   2;9;c                x   K   |d}| j                   j                  j                  |||       d{    y7 w)a-  Send a log message to the client.

        Args:
            message: Log message
            level: Optional log level. One of "debug", "info", "notice", "warning", "error", "critical",
                "alert", or "emergency". Default is "info".
            logger_name: Optional logger name
        Ninfo)leveldatalogger)r4   r=   send_log_message)r&   r:   rF   logger_names       r    logzContext.log   sB      =E""**;;gk < 
 	
 	
s   0:8:c                t    | j                   j                  r!t        | j                   j                  dd      S dS )zGet the client ID if available.	client_idN)r4   r;   getattrr5   s    r    rM   zContext.client_id   s<    
 ##(( D((--{DA	
 	
r(   c                @    t        | j                  j                        S )z#Get the unique ID for this request.)strr4   
request_idr5   s    r    rQ   zContext.request_id   s     4''2233r(   c                .    | j                   j                  S )z4Access to the underlying session for advanced usage.)r4   r=   r5   s    r    r=   zContext.session   s     ##+++r(   c                H   K   | j                  d||       d{    y7 w)zSend a debug log message.debugrF   r:   rJ   NrK   r&   r:   rJ   s      r    rT   zContext.debug        hhWg;hOOO   " "c                H   K   | j                  d||       d{    y7 w)zSend an info log message.rE   rU   NrV   rW   s      r    rE   zContext.info   s     hhVW+hNNNrY   c                H   K   | j                  d||       d{    y7 w)zSend a warning log message.warningrU   NrV   rW   s      r    r\   zContext.warning   s     hhY[hQQQrY   c                H   K   | j                  d||       d{    y7 w)zSend an error log message.errorrU   NrV   rW   s      r    r^   zContext.error   rX   rY   c                ~   K   | j                   j                  j                          d{   }|j                  S 7 w)zCList the roots available to the server, as indicated by the client.N)r4   r=   
list_rootsroots)r&   results     r    r`   zContext.list_roots   s3     ++33>>@@|| As   (=;=c           	       K   |d}t        |t              rt        t        |d      d      g}nGt        |t              r7|D cg c],  }t        |t              rt        t        |d      d      n|. }}| j
                  j                  j                  |||| j                  |             d{   }|j                  S c c}w 7 w)a  
        Send a sampling request to the client and await the response.

        Call this method at any time to have the server request an LLM
        completion from the client. The client must be appropriately configured,
        or the request will error.
        Ni   text)rd   typeuser)contentrole)messagessystem_prompttemperature
max_tokensmodel_preferences)

isinstancerP   r   r   listr4   r=   create_message_parse_model_preferencesrg   )	r&   ri   rj   rk   rl   rm   sampling_messagesmrb   s	            r    samplezContext.sample   s       Jh$'XFC&!
 $'
 "	!  a%  (HvV! ! -1,@,@,H,H,W,W&'#!";;<MN -X -
 '
 ~~!'
s   AC1C6>C4C
5Cc                    t        j                  dt        d       t        j                  j
                  j                         S )z!Get the active starlette request.zContext.get_http_request() is deprecated and will be removed in a future version. Use get_http_request() from fastmcp.server.dependencies instead. See https://gofastmcp.com/patterns/http-requests for more details.   )
stacklevel)warningswarnDeprecationWarningr$   serverdependenciesget_http_requestr5   s    r    r}   zContext.get_http_request   s9     	Q 	
 ~~**;;==r(   c                <   |yt        |t              r|S t        |t              rt        t        |      g      S t        |t              rAt        d |D              st        d      t        |D cg c]  }t        |       c}      S t        d      c c}w )a  
        Validates and converts user input for model_preferences into a ModelPreferences object.

        Args:
            model_preferences (ModelPreferences | str | list[str] | None):
                The model preferences to use. Accepts:
                - ModelPreferences (returns as-is)
                - str (single model hint)
                - list[str] (multiple model hints)
                - None (no preferences)

        Returns:
            ModelPreferences | None: The parsed ModelPreferences object, or None if not provided.

        Raises:
            ValueError: If the input is not a supported type or contains invalid values.
        N)name)hintsc              3  <   K   | ]  }t        |t                y wr   )rn   rP   ).0hs     r    	<genexpr>z3Context._parse_model_preferences.<locals>.<genexpr>  s     Eaz!S)Es   zJAll elements of model_preferences list must be strings (model name hints).zLmodel_preferences must be one of: ModelPreferences, str, list[str], or None.)rn   r   rP   r   ro   all
ValueError)r&   rm   r   s      r    rq   z Context._parse_model_preferences   s    ( $)+;<$$)3/#9:K+L*MNN)40E3DEE 3  $2CDQya(D  ^  Es   3B)r$   r   )returnr   )r   None)r   r   )NN)r8   floatr9   float | Noner:   
str | Noner   r   )rB   zstr | AnyUrlr   zlist[ReadResourceContents])r:   rP   rF   zLoggingLevel | NonerJ   r   r   r   )r   r   )r   rP   r   )r:   rP   rJ   r   r   r   )r   z
list[Root])NNNN)ri   z!str | list[str | SamplingMessage]rj   r   rk   r   rl   z
int | Nonerm   )ModelPreferences | str | list[str] | Noner   zTextContent | ImageContent)r   r   )rm   r   r   zModelPreferences | None)__name__
__module____qualname____doc__r'   r+   r1   propertyr4   r?   rC   rK   rM   rQ   r=   rT   rE   r\   r^   r`   rt   r}   rq    r(   r    r   r   *   sQ    D'* 8 8
 RV

&2
DN
	
4
: &*"&	

 #
  	

 

( 
 
 4 4 , ,
PORP %)$(!%GK)3) ") "	)
 ) E) 
$)V>(!J(	 (r(   )r   r   r   zGenerator[Context, None, None])+
__future__r   _annotationsrx   collections.abcr   
contextlibr   contextvarsr   r   dataclassesr   mcpr	    mcp.server.lowlevel.helper_typesr
   mcp.shared.contextr   	mcp.typesr   r   r   r   r   r   r   pydantic.networksr   starlette.requestsr   fastmcp.server.dependenciesr$   fastmcp.server.serverr   fastmcp.utilities.loggingr   r   rH   r   __annotations__r!   r   r   r(   r    <module>r      s    2  % % ) !  A -   % & " ) 0	H	/9)T/R , R & & } } }r(   