o
    io                  	   @  s  U d dl mZ d dlZd dlZd dlZd dl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 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 d dl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&m'Z'm(Z(m)Z)m*Z*m+Z+m,Z,m-Z-m.Z.m/Z/m0Z0m1Z1 d dl%m2Z3 d dl%m4Z5 d dl%m6Z7 d dl8m9Z9 d dl:m;Z; d dl<m=Z= d dl>Z?d dl?m@Z@ d dlAmBZBmCZCmDZDmEZEmFZF d dlGmHZH d dlImJZJmKZK d dlLmMZM eKeNdZOdePd< eOjQddZRdePd< eJeRd d! e=d"ed#ZSed$dd#ZTd%ePd&< eU ZVeG d'd( d(ZWejXejYejYejZej[ej\ej\ej\d)Z]ed@d-d.Z^eG d/d* d*Z_dAd3d4Z`		dBdCd>d?ZadS )D    )annotationsN)	GeneratorMappingSequence)contextmanager)
ContextVarToken)	dataclass)Enum)Logger)AnyLiteralcast
get_originoverload)LoggingLevelServerSession)ReadResourceContents)request_ctx)RequestContext)AudioContentClientCapabilitiesCreateMessageResultGetPromptResultImageContentIncludeContext	ModelHintModelPreferencesRootSamplingCapabilitySamplingMessageTextContent)CreateMessageRequestParams)Prompt)Resource)AnyUrl)Request)TypeVar)settings)AcceptedElicitationCancelledElicitationDeclinedElicitationScalarElicitationTypeget_elicitation_schema)FastMCP)_clamp_logger
get_logger)get_cached_typeadapternamer   logger	to_client)suffixto_client_loggerDEBUG)r4   	max_levelT)defaultcontextzContextVar[Context | None]_current_contextc                   @  s&   e Zd ZU dZded< dZded< dS )LogDatazData object for passing log arguments to client-side handlers.

    This provides an interface to match the Python standard library logging,
    for compatibility with structured logging.
    strmsgNMapping[str, Any] | Noneextra)__name__
__module____qualname____doc____annotations__rB    rH   rH   \/var/www/html/karishye-ai-python/venv/lib/python3.10/site-packages/fastmcp/server/context.pyr>   E   s   
 r>   )debuginfonoticewarningerrorcriticalalert	emergencyContextreturnGenerator[Context, None, None]c              	   c  s0    t | }z| V  W t | d S t | w N)r=   setreset)r<   tokenrH   rH   rI   set_context]   s
   
rY   c                   @  s  e Zd ZdZdyddZedzddZd{d	d
Zd|ddZed}ddZ		d~dddZ
dddZdddZ	ddd%d&Zdd*d+Z			ddd1d2Zedd3d4Zedd5d6Zedd7d8Zedd:d;Z		d~dd<d=Z		d~dd>d?Z		d~dd@dAZ		d~ddBdCZddEdFZd|dGdHZd|dIdJZd|dKdLZ					dddXdYZedd\d]Z	 edd`d]Z	 eddcd]Z	 	dddfd]ZddhdiZddmdnZ ddodpZ!d|dqdrZ"d|dsdtZ#d|dudvZ$d|dwdxZ%dS )rR   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
    async def my_tool(x: int, ctx: Context) -> str:
        # Log messages to the client
        await ctx.info(f"Processing {x}")
        await ctx.debug("Debug info")
        await ctx.warning("Warning message")
        await ctx.error("Error message")

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

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

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

        # Manage state across the request
        ctx.set_state("key", "value")
        value = ctx.get_state("key")

        return str(x)
    ```

    State Management:
    Context objects maintain a state dictionary that can be used to store and share
    data across middleware and tool calls within a request. When a new context
    is created (nested contexts), it inherits a copy of its parent's state, ensuring
    that modifications in child contexts don't affect parent contexts.

    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.

    fastmcpr.   c                 C  s$   t || _g | _t | _i | _d S rU   )weakrefref_fastmcp_tokensrV   _notification_queue_stateselfrZ   rH   rH   rI   __init__   s   
zContext.__init__rS   c                 C  s   |   }|du rtd|S )zGet the FastMCP instance.Nz'FastMCP instance is no longer available)r]   RuntimeErrorra   rH   rH   rI   rZ      s   zContext.fastmcpc                   s<   t d}|durt|j| _t | }| j| | S )zFEnter the context manager and set this context as the current context.N)r=   getcopydeepcopyr`   rV   r^   append)rb   parent_contextrX   rH   rH   rI   
__aenter__   s   

zContext.__aenter__Nonec                   s2   |   I dH  | jr| j }t| dS dS )z9Exit the context manager and reset the most recent token.N)_flush_notificationsr^   popr=   rW   )rb   exc_typeexc_valexc_tbrX   rH   rH   rI   	__aexit__   s   
zContext.__aexit__2RequestContext[ServerSession, Any, Request] | Nonec                 C  s    zt  W S  ty   Y dS w )a  Access to the underlying request context.

        Returns None when the MCP session has not been established yet.
        Returns the full RequestContext once the MCP session is available.

        For HTTP request access in middleware, use `get_http_request()` from fastmcp.server.dependencies,
        which works whether or not the MCP session is available.

        Example in middleware:
        ```python
        async def on_request(self, context, call_next):
            ctx = context.fastmcp_context
            if ctx.request_context:
                # MCP session available - can access session_id, request_id, etc.
                session_id = ctx.session_id
            else:
                # MCP session not available yet - use HTTP helpers
                from fastmcp.server.dependencies import get_http_request
                request = get_http_request()
            return await call_next(context)
        ```
        N)r   re   LookupErrorrb   rH   rH   rI   request_context   s
   
zContext.request_contextNprogressfloattotalfloat | Nonemessage
str | Nonec                   sL   | j r| j jr| j jjnd}|du rdS | jj||||| jdI dH  dS )zReport progress for the current operation.

        Args:
            progress: Current progress value e.g. 24
            total: Optional total value e.g. 100
        N)progress_tokenrv   rx   rz   related_request_id)ru   metaprogressTokensessionsend_progress_notification
request_id)rb   rv   rx   rz   r|   rH   rH   rI   report_progress   s    zContext.report_progresslist[MCPResource]c                      | j  I dH S )zList all available resources from the server.

        Returns:
            List of Resource objects available on the server
        N)rZ   _list_resources_mcprt   rH   rH   rI   list_resources      zContext.list_resourceslist[MCPPrompt]c                   r   )zList all available prompts from the server.

        Returns:
            List of Prompt objects available on the server
        N)rZ   _list_prompts_mcprt   rH   rH   rI   list_prompts   r   zContext.list_promptsr3   r?   	argumentsdict[str, Any] | Noner   c                   s   | j ||I dH S )zGet a prompt by name with optional arguments.

        Args:
            name: The name of the prompt to get
            arguments: Optional arguments to pass to the prompt

        Returns:
            The prompt result
        N)rZ   _get_prompt_mcp)rb   r3   r   rH   rH   rI   
get_prompt   s   zContext.get_prompturistr | AnyUrllist[ReadResourceContents]c                   s   | j |I dH S )zRead a resource by URI.

        Args:
            uri: Resource URI to read

        Returns:
            The resource content as either text or bytes
        N)rZ   _read_resource_mcp)rb   r   rH   rH   rI   read_resource  s   	zContext.read_resourcelevelLoggingLevel | Nonelogger_namerB   rA   c                   s2   t ||d}t|| j|pd|| jdI dH  dS )a  Send a log message to the client.

        Messages sent to Clients are also logged to the `fastmcp.server.context.to_client` logger with a level of `DEBUG`.

        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
            extra: Optional mapping for additional arguments
        )r@   rB   rK   )datar   r   r   r}   N)r>   _log_to_server_and_clientr   r   )rb   rz   r   r   rB   r   rH   rH   rI   log  s   zContext.logc                 C  s"   | j r| j jrt| j jddS dS )zGet the client ID if available.	client_idN)ru   r~   getattrrt   rH   rH   rI   r   4  s   zContext.client_idc                 C  s   | j du r	tdt| j jS )zrGet the unique ID for this request.

        Raises RuntimeError if MCP request context is not available.
        Nzrequest_id is not available because the MCP session has not been established yet. Check `context.request_context` for None before accessing this attribute.)ru   rd   r?   r   rt   rH   rH   rI   r   =  s
   
zContext.request_idc                 C  sr   | j }|du rtd|j}t|dd}|dur|S |j}|r%|jd}|du r4ddlm} t	| }||_
|S )a  Get the MCP session ID for ALL transports.

        Returns the session ID that can be used as a key for session-based
        data storage (e.g., Redis) to share data between tool calls within
        the same client session.

        Returns:
            The session ID for StreamableHTTP transports, or a generated ID
            for other transports.

        Raises:
            RuntimeError if MCP request context is not available.

        Example:
            ```python
            @server.tool
            def store_data(data: dict, ctx: Context) -> str:
                session_id = ctx.session_id
                redis_client.set(f"session:{session_id}:data", json.dumps(data))
                return f"Data stored for session {session_id}"
            ```
        Nzsession_id is not available because the MCP session has not been established yet. Check `context.request_context` for None before accessing this attribute._fastmcp_idzmcp-session-idr   )uuid4)ru   rd   r   r   requestheadersre   uuidr   r?   r   )rb   r   r   
session_idr   r   rH   rH   rI   r   J  s"   
zContext.session_idr   c                 C  s   | j du r	td| j jS )zAccess to the underlying session for advanced usage.

        Raises RuntimeError if MCP request context is not available.
        Nzsession is not available because the MCP session has not been established yet. Check `context.request_context` for None before accessing this attribute.)ru   rd   r   rt   rH   rH   rI   r   ~  s
   
zContext.sessionc                      | j d|||dI dH  dS )zSend a `DEBUG`-level message to the connected MCP Client.

        Messages sent to Clients are also logged to the `fastmcp.server.context.to_client` logger with a level of `DEBUG`.rJ   r   rz   r   rB   Nr   rb   rz   r   rB   rH   rH   rI   rJ        	zContext.debugc                   r   )zSend a `INFO`-level message to the connected MCP Client.

        Messages sent to Clients are also logged to the `fastmcp.server.context.to_client` logger with a level of `DEBUG`.rK   r   Nr   r   rH   rH   rI   rK     r   zContext.infoc                   r   )zSend a `WARNING`-level message to the connected MCP Client.

        Messages sent to Clients are also logged to the `fastmcp.server.context.to_client` logger with a level of `DEBUG`.rM   r   Nr   r   rH   rH   rI   rM     r   zContext.warningc                   r   )zSend a `ERROR`-level message to the connected MCP Client.

        Messages sent to Clients are also logged to the `fastmcp.server.context.to_client` logger with a level of `DEBUG`.rN   r   Nr   r   rH   rH   rI   rN     r   zContext.error
list[Root]c                   s   | j  I dH }|jS )zCList the roots available to the server, as indicated by the client.N)r   
list_rootsroots)rb   resultrH   rH   rI   r     s   zContext.list_rootsc                      | j  I dH  dS )z4Send a tool list changed notification to the client.N)r   send_tool_list_changedrt   rH   rH   rI   r        zContext.send_tool_list_changedc                   r   )z8Send a resource list changed notification to the client.N)r   send_resource_list_changedrt   rH   rH   rI   r     r   z"Context.send_resource_list_changedc                   r   )z6Send a prompt list changed notification to the client.N)r   send_prompt_list_changedrt   rH   rH   rI   r     r   z Context.send_prompt_list_changedmessages%str | Sequence[str | SamplingMessage]system_promptinclude_contextIncludeContext | Nonetemperature
max_tokens
int | Nonemodel_preferences)ModelPreferences | str | list[str] | None)TextContent | ImageContent | AudioContentc              
     s(  |du rd}t |trtt|ddddg}nt |tr$dd |D }| jjd	ko5| jjt	t
 d
d }| jjdks>|r~| jjdu rHtd| j|t||||t|d| j}	t|	rd|	I dH }	t |	trot|	ddS t |	trw|	jS td|	 | jj|||||t|| jdI dH }
|
jS )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r   typeusercontentrolec                 S  s.   g | ]}t |trtt|d dddn|qS )r   r   r   r   )
isinstancer?   r    r!   ).0mrH   rH   rI   
<listcomp>  s    z"Context.sample.<locals>.<listcomp>fallback)sampling)
capabilityalwaysz Client does not support sampling)systemPromptr   r   	maxTokensmodelPreferencesz$Unexpected sampling handler result: )r   r   r   r   r   r   r}   )r   r?   r    r!   r   rZ   sampling_handler_behaviorr   check_client_capabilityr   r   sampling_handler
ValueErrorSamplingParams_parse_model_preferencesru   inspectisawaitabler   r   create_messager   )rb   r   r   r   r   r   r   sampling_messagesshould_fallbackcreate_message_resultr   rH   rH   rI   sample  sd   







zContext.sampleresponse_typePAcceptedElicitation[dict[str, Any]] | DeclinedElicitation | CancelledElicitationc                      d S rU   rH   rb   rz   r   rH   rH   rI   elicit0  s   zContext.elicittype[T]CAcceptedElicitation[T] | DeclinedElicitation | CancelledElicitationc                   r   rU   rH   r   rH   rH   rI   r   <     	list[str]EAcceptedElicitation[str] | DeclinedElicitation | CancelledElicitationc                   r   rU   rH   r   rH   rH   rI   r   F  r   type[T] | list[str] | NoneAcceptedElicitation[T] | AcceptedElicitation[dict[str, Any]] | AcceptedElicitation[str] | DeclinedElicitation | CancelledElicitationc                   s|  |du rdi d}nGt |tr+tdd |D s td| tt| }t| }n|ttt	t
hv sCt|tu sCt |trGt|trGt| }ttt |}t|}| jj||| jdI dH }|jdkr|durt|}tttt B ||j}t |trtt |jd	S tt tt|d	S |jrtd
|j ttt
tf  i d	S |jdkrt S |jdkrt S td|j )a  
        Send an elicitation request to the client and await the response.

        Call this method at any time to request additional information from
        the user through the client. The client must support elicitation,
        or the request will error.

        Note that the MCP protocol only supports simple object schemas with
        primitive types. You can provide a dataclass, TypedDict, or BaseModel to
        comply. If you provide a primitive type, an object schema with a single
        "value" field will be generated for the MCP interaction and
        automatically deconstructed into the primitive type upon response.

        If the response_type is None, the generated schema will be that of an
        empty object in order to comply with the MCP protocol requirements.
        Clients must send an empty object ("{}")in response.

        Args:
            message: A human-readable message explaining what information is needed
            response_type: The type of the response, which should be a primitive
                type or dataclass or BaseModel. If it is a primitive type, an
                object schema with a single "value" field will be generated.
        Nobject)r   
propertiesc                 s      | ]}t |tV  qd S rU   r   r?   )r   itemrH   rH   rI   	<genexpr>w      z!Context.elicit.<locals>.<genexpr>z5List of options must be a list of strings. Received: )rz   requestedSchemar}   accept)r   z6Elicitation expected an empty response, but received: declinecancelzUnexpected elicitation action: )r   listallr   r   tupler,   boolintrw   r?   r   r   
issubclassr
   r   r:   r-   r   r   r   actionr1   validate_pythonr   r)   valuedictr   r+   r*   )rb   rz   r   schemachoice_literalr   type_adaptervalidated_datarH   rH   rI   r   P  s`   "







r&   c                 C  s"   t jrtjdtdd t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/servers/context#http-requests for more details.   )
stacklevel)	r(   deprecation_warningswarningswarnDeprecationWarningrZ   serverdependenciesget_http_requestrt   rH   rH   rI   r    s   zContext.get_http_requestkeyr   r   c                 C  s   || j |< dS )z!Set a value in the context state.N)r`   )rb   r	  r   rH   rH   rI   	set_state  s   zContext.set_statec                 C  s   | j |S )zIGet a value from the context state. Returns None if the key is not found.)r`   re   )rb   r	  rH   rH   rI   	get_state  s   zContext.get_statec                 C     | j d dS )z'Queue a tool list changed notification. notifications/tools/list_changedNr_   addrt   rH   rH   rI   _queue_tool_list_changed     z Context._queue_tool_list_changedc                 C  r  )z+Queue a resource list changed notification.$notifications/resources/list_changedNr  rt   rH   rH   rI   _queue_resource_list_changed  r  z$Context._queue_resource_list_changedc                 C  r  )z)Queue a prompt list changed notification."notifications/prompts/list_changedNr  rt   rH   rH   rI   _queue_prompt_list_changed  r  z"Context._queue_prompt_list_changedc              	     s   t 4 I dH ^ | js	 W d  I dH  dS z.d| jv r%| j I dH  d| jv r2| j I dH  d| jv r?| j I dH  | j  W n	 tyN   Y nw W d  I dH  dS W d  I dH  dS 1 I dH skw   Y  dS )zSend all queued notifications.Nr  r  r  )_flush_lockr_   r   r   r   r   clear	Exceptionrt   rH   rH   rI   rl     s(   


.zContext._flush_notifications)rZ   r.   )rS   r.   )rS   rR   )rS   rk   )rS   rr   NN)rv   rw   rx   ry   rz   r{   rS   rk   )rS   r   )rS   r   rU   )r3   r?   r   r   rS   r   )r   r   rS   r   )NNN)
rz   r?   r   r   r   r{   rB   rA   rS   rk   )rS   r{   )rS   r?   )rS   r   )rz   r?   r   r{   rB   rA   rS   rk   )rS   r   )NNNNN)r   r   r   r{   r   r   r   ry   r   r   r   r   rS   r   )rz   r?   r   rk   rS   r   )rz   r?   r   r   rS   r   )rz   r?   r   r   rS   r   )rz   r?   r   r   rS   r   )rS   r&   )r	  r?   r   r   rS   rk   )r	  r?   rS   r   )&rC   rD   rE   rF   rc   propertyrZ   rj   rq   ru   r   r   r   r   r   r   r   r   r   r   rJ   rK   rM   rN   r   r   r   r   r   r   r   r  r
  r  r  r  r  rl   rH   rH   rH   rI   rR   f   s    
,

	

	
3



S
[




r   r   ModelPreferences | Nonec                 C  sv   | du rdS t | tr| S t | trtt| dgdS t | tr7tdd | D s-tdtdd | D dS td	)
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.
    Nr2   )hintsc                 s  r   rU   r   r   hrH   rH   rI   r     r   z+_parse_model_preferences.<locals>.<genexpr>zJAll elements of model_preferences list must be strings (model name hints).c                 S  s   g | ]}t |d qS )r2   )r   r  rH   rH   rI   r     s    z,_parse_model_preferences.<locals>.<listcomp>zLmodel_preferences must be one of: ModelPreferences, str, list[str], or None.)r   r   r?   r   r   r   r   )r   rH   rH   rI   r     s   


r   r   r   r   r   r   r   r{   r}   rk   c                   sd   d|   d}|r|d| d7 }tjt| | d| j | jd |j|| ||dI dH  dS )	z'Log a message to the server and client.zSending z
 to clientz ()z: )r   r@   rB   )r   r   r4   r}   N)upperr7   r   _mcp_level_to_python_levelr@   rB   send_log_message)r   r   r   r   r}   
msg_prefixrH   rH   rI   r   
  s   	r   )r<   rR   rS   rT   )r   r   rS   r  r  )r   r>   r   r   r   r   r   r{   r}   r{   rS   rk   )b
__future__r   rf   r   loggingr  r[   collections.abcr   r   r   
contextlibr   contextvarsr   r   dataclassesr	   enumr
   r   typingr   r   r   r   r   anyiomcpr   r    mcp.server.lowlevel.helper_typesr   mcp.server.lowlevel.serverr   mcp.shared.contextr   	mcp.typesr   r   r   r   r   r   r   r   r   r   r    r!   r"   r   r#   	MCPPromptr$   MCPResourcepydantic.networksr%   starlette.requestsr&   typing_extensionsr'   fastmcp.server.dependenciesrZ   r(   fastmcp.server.elicitationr)   r*   r+   r,   r-   fastmcp.server.serverr.   fastmcp.utilities.loggingr/   r0   fastmcp.utilities.typesr1   rC   r4   rG   getChildr7   r:   r=   Lockr  r>   r8   INFOWARNINGERRORCRITICALr!  rY   rR   r   r   rH   rH   rH   rI   <module>   sx    8    
~-