
    Ki              	      *   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mZ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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,m-Z- d dl"m.Z/ d dl"m0Z1 d dl"m2Z3 d dl4m5Z5 d dl6m7Z7 d dl8m9Z9 d dl:m;Z; d dl<m=Z= d dl>m?Z?m@Z@mAZAmBZBmCZC d dlDmEZEmFZFmGZG d dlHmIZImJZJmKZK d dlHmLZM d dlNmOZO d dlPmQZQ d dlRmSZSmTZT d dlUmVZV  eTeW      ZXdeYd <   eXj                  d!"      Z[deYd#<    eSe[d$%        e;d&e'      Z\ e;d(e]'      Z^ed)   Z_ ed*d'      Z`d+eYd,<    ej                         Zbe G d- d.             Zcej                  ej                  ej                  ej                  ej                  ej                  ej                  ej                  d/Zied9d1       Zje G d2 d0             Zk	 	 d:	 	 	 	 	 	 	 	 	 	 	 d;d3Zld<d4Zm	 	 	 	 d=d5Zn	 	 	 	 d>d6Zo	 	 	 	 d?d7Zp	 	 	 	 d@d8Zqy)A    )annotationsN)Callable	GeneratorMappingSequence)contextmanager)
ContextVarToken)	dataclass)Logger)AnyLiteralcastoverload)LoggingLevelServerSession)ReadResourceContents)request_ctx)RequestContext)CreateMessageResultCreateMessageResultWithToolsGetPromptResultModelPreferencesRootSamplingMessageSamplingMessageContentBlockTextContent
ToolChoiceToolResultContentToolUseContent)Prompt)Resource)Tool)ValidationError)AnyUrl)Request)TypeVar)settings)AcceptedElicitationCancelledElicitationDeclinedElicitationhandle_elicit_acceptparse_elicit_response_type)
SampleStepSamplingResultSamplingTool)_parse_model_preferencescall_sampling_handlerdetermine_handler_mode)execute_tools)FastMCP)compress_schema)_clamp_logger
get_logger)get_cached_typeadapter)namer   logger	to_client)suffixto_client_loggerDEBUG)r;   	max_levelT)defaultResultTautorequirednonecontextzContextVar[Context | None]_current_contextc                  *    e Zd ZU dZded<   dZded<   y)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__rO        b/home/jay/workspace/scripts/.codegraph-venv/lib/python3.12/site-packages/fastmcp/server/context.pyrK   rK   S   s     
H&*E#*rV   rK   )debuginfonoticewarningerrorcriticalalert	emergencyContextc              #     K   t         j                  |       }	 |  t         j                  |       y # t         j                  |       w xY wwN)rI   setreset)rH   tokens     rW   set_contextrf   k   s=       )E&u%u%s   A3 AA

Ac            	      D   e Zd ZdZd-dZed.d       Zd/dZd0dZed1d       Z		 d2	 	 	 	 	 	 	 d3dZ
d4d	Zd5d
Z	 d6	 	 	 	 	 d7dZd8dZ	 	 	 d9	 	 	 	 	 	 	 	 	 d:dZed;d       Zed<d       Zed<d       Zed=d       Z	 	 d2	 	 	 	 	 	 	 d>dZ	 	 d2	 	 	 	 	 	 	 d>dZ	 	 d2	 	 	 	 	 	 	 d>dZ	 	 d2	 	 	 	 	 	 	 d>dZd?dZd0dZd0dZd0dZd0dZddddddddd	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 d@dZeddddddd	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 dAd       Zedddddddd 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 dBd!       Zdddddddd 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 dCd"Ze	 	 	 	 	 	 dDd#       Z 	 e	 	 	 	 	 	 dEd$       Z 	 e	 	 	 	 	 	 dFd%       Z 	 	 d6	 	 	 	 	 dGd&Z dHd'Z!dId(Z"d0d)Z#d0d*Z$d0d+Z%d0d,Z&y)Jr`   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.

    c                r    t        j                  |      | _        g | _        t	               | _        i | _        y rb   )weakrefref_fastmcp_tokensrc   _notification_queue_stateselffastmcps     rW   __init__zContext.__init__   s*    .5kk'.B$&-0U &(rV   c                @    | j                         }|t        d      |S )zGet the FastMCP instance.z'FastMCP instance is no longer available)rk   RuntimeErrorro   s     rW   rq   zContext.fastmcp   s%     --/?HIIrV   c                &  K   t         j                  d      }|$t        j                  |j                        | _        t         j                  |       }| j                  j                  |       ddlm	}m
}m} |j                  t        j                  | j                              | _        | j                  }|j                    |j                  |j                         | _        |j$                   |j                  |j$                        | _        | S w)zFEnter the context manager and set this context as the current context.Nr   _current_docket_current_server_current_worker)rI   getcopydeepcopyrn   rc   rl   appendfastmcp.server.dependenciesrw   rx   ry   ri   rj   rq   _server_token_docket_docket_token_worker_worker_token)rp   parent_contextre   rw   rx   ry   servers          rW   
__aenter__zContext.__aenter__   s     )--d3%--(=(=>DK !$$T*E"	
 	
 -00T\\1JK
 >>%!0!4!4V^^!DD>>%!0!4!4V^^!DDs   DDc                  K   | j                          d{    ddlm}m}m} t        | d      r'|j                  | j                         t        | d       t        | d      r'|j                  | j                         t        | d       t        | d      r'|j                  | j                         t        | d       | j                  r0| j                  j                         }t        j                  |       yy7 w)z9Exit the context manager and reset the most recent token.Nr   rv   r   r   r   )_flush_notificationsr~   rw   rx   ry   hasattrrd   r   delattrr   r   rl   poprI   )rp   exc_typeexc_valexc_tbrw   rx   ry   re   s           rW   	__aexit__zContext.__aexit__   s      '')))	
 	
 4)!!$"4"45D/*4)!!$"4"45D/*4)!!$"4"45D/* <<LL$$&E""5) ) 	*s   C=C;C%C=c                J    	 t        j                         S # t        $ r Y yw xY 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   rz   LookupErrorrp   s    rW   request_contextzContext.request_context   s&    0	??$$ 		s    	""Nc                  K   | j                   r6| 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related_request_id)r   metaprogressTokensessionsend_progress_notification
request_id)rp   r   r   r   r   s        rW   report_progresszContext.report_progress  s}      ##(<(<(A(A   %%33 	 !ll55)# 6 
 	
 	
s   A5A?7A=8A?c                R   K   | j                   j                          d{   S 7 w)zList all available resources from the server.

        Returns:
            List of Resource objects available on the server
        N)rq   _list_resources_mcpr   s    rW   list_resourceszContext.list_resources#  s!      \\557777   '%'c                R   K   | j                   j                          d{   S 7 w)zList all available prompts from the server.

        Returns:
            List of Prompt objects available on the server
        N)rq   _list_prompts_mcpr   s    rW   list_promptszContext.list_prompts+  s!      \\335555r   c                V   K   | j                   j                  ||       d{   S 7 w)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)rq   _get_prompt_mcp)rp   r:   	argumentss      rW   
get_promptzContext.get_prompt3  s%      \\11$	BBBBs    )')c                T   K   | 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
        N)rq   _read_resource_mcp)rp   uris     rW   read_resourcezContext.read_resourceA  s#      \\44S9999s   (&(c                   K   t        ||      }t        || j                  |xs d|| j                         d{    y7 w)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
        )rM   rO   rY   )datar   levellogger_namer   N)rK   _log_to_server_and_clientr   r   )rp   r   r   r   rO   r   s         rW   logzContext.logM  sA     $ 7%0'LL/6##
 	
 	
s   9AAAc                    | j                   r7| j                   j                  r!t        | j                   j                  dd      S dS )zGet the client ID if available.	client_idN)r   r   getattrr   s    rW   r   zContext.client_idi  sD    
 ##(<(<(A(A D((--{DA	
 	
rV   c                n    | j                   t        d      t        | j                   j                        S )zrGet the unique ID for this request.

        Raises RuntimeError if MCP request context is not available.
        zrequest_id is not available because the MCP session has not been established yet. Check `context.request_context` for None before accessing this attribute.)r   rt   rL   r   r   s    rW   r   zContext.request_idr  s<     '\  4''2233rV   c                   | j                   }|t        d      |j                  }t        |dd      }||S |j                  }|r|j
                  j                  d      }|d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)r   rt   r   r   requestheadersrz   uuidr   rL   r   )rp   r   r   
session_idr   r   s         rW   r   zContext.session_id  s    0 **\  %% WmT:
! %% ,,-=>J "UWJ )rV   c                \    | j                   t        d      | j                   j                  S )zAccess to the underlying session for advanced usage.

        Raises RuntimeError if MCP request context is not available.
        zsession is not available because the MCP session has not been established yet. Check `context.request_context` for None before accessing this attribute.)r   rt   r   r   s    rW   r   zContext.session  s7     '\  ##+++rV   c                J   K   | j                  d|||       d{    y7 w)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`.rX   r   r   r   rO   Nr   rp   r   r   rO   s       rW   rX   zContext.debug  /      hh#	  
 	
 	
   #!#c                J   K   | j                  d|||       d{    y7 w)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`.rY   r   Nr   r   s       rW   rY   zContext.info  s/      hh#	  
 	
 	
r   c                J   K   | j                  d|||       d{    y7 w)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`.r[   r   Nr   r   s       rW   r[   zContext.warning  s/      hh#	  
 	
 	
r   c                J   K   | j                  d|||       d{    y7 w)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`.r\   r   Nr   r   s       rW   r\   zContext.error  r   r   c                j   K   | j                   j                          d{   }|j                  S 7 w)zCList the roots available to the server, as indicated by the client.N)r   
list_rootsroots)rp   results     rW   r   zContext.list_roots  s*     ||..00|| 1s   313c                T   K   | j                   j                          d{    y7 w)z4Send a tool list changed notification to the client.N)r   send_tool_list_changedr   s    rW   r   zContext.send_tool_list_changed  s     ll11333   (&(c                T   K   | j                   j                          d{    y7 w)z8Send a resource list changed notification to the client.N)r   send_resource_list_changedr   s    rW   r   z"Context.send_resource_list_changed
  s     ll55777r   c                T   K   | j                   j                          d{    y7 w)z6Send a prompt list changed notification to the client.N)r   send_prompt_list_changedr   s    rW   r   z Context.send_prompt_list_changed  s     ll33555r   c                   K   | j                   r| j                   j                  st        j                  d       y| j                   j                          d{    y7 w)a  Close the current response stream to trigger client reconnection.

        When using StreamableHTTP transport with an EventStore configured, this
        method gracefully closes the HTTP connection for the current request.
        The client will automatically reconnect (after `retry_interval` milliseconds)
        and resume receiving events from where it left off via the EventStore.

        This is useful for long-running operations to avoid load balancer timeouts.
        Instead of holding a connection open for minutes, you can periodically close
        and let the client reconnect.

        Example:
            ```python
            @mcp.tool
            async def long_running_task(ctx: Context) -> str:
                for i in range(100):
                    await ctx.report_progress(i, 100)

                    # Close connection every 30 iterations to avoid LB timeouts
                    if i % 30 == 0 and i > 0:
                        await ctx.close_sse_stream()

                    await do_work()
                return "Done"
            ```

        Note:
            This is a no-op (with a debug log) if not using StreamableHTTP
            transport with an EventStore configured.
        zaclose_sse_stream() called but not applicable (requires StreamableHTTP transport with event_store)N)r   close_sse_streamr;   rX   r   s    rW   r   zContext.close_sse_stream  sN     > ##4+?+?+P+PLLG ""33555s   AA AA T)system_prompttemperature
max_tokensmodel_preferencestoolstool_choicer4   mask_error_detailsc          
       K   t        |      }
t        |      }|r|D cg c]  }|j                          c}nd}|r|D ci c]  }|j                  | c}ni }t	        | t        |            }d}|0|dvrt        d|d      t        t        t        d   |            }||nd}|rt        | |
||||||       d{   }n>| j                  j                  |
|||t        |      ||| j                         d{   }t        |t               xr |j"                  d	k(  }|
j%                  t'        d
|j(                               |st+        ||
      S |st+        ||
      S t-        |      }|rH|	|	nt.        j0                  }t3        |||       d{   }|r|
j%                  t'        d|             t+        ||
      S c c}w c c}w 7 7 7 >w)ao  
        Make a single LLM sampling call.

        This is a stateless function that makes exactly one LLM call and optionally
        executes any requested tools. Use this for fine-grained control over the
        sampling loop.

        Args:
            messages: The message(s) to send. Can be a string, list of strings,
                or list of SamplingMessage objects.
            system_prompt: Optional system prompt for the LLM.
            temperature: Optional sampling temperature.
            max_tokens: Maximum tokens to generate. Defaults to 512.
            model_preferences: Optional model preferences.
            tools: Optional list of tools the LLM can use.
            tool_choice: Tool choice mode ("auto", "required", or "none").
            execute_tools: If True (default), execute tool calls and append results
                to history. If False, return immediately with tool_calls available
                in the step for manual execution.
            mask_error_details: If True, mask detailed error messages from tool
                execution. When None (default), uses the global settings value.
                Tools can raise ToolError to bypass masking.

        Returns:
            SampleStep containing:
            - .response: The raw LLM response
            - .history: Messages including input, assistant response, and tool results
            - .is_tool_use: True if the LLM requested tool execution
            - .tool_calls: List of tool calls (if any)
            - .text: The text content (if any)

        Example:
            messages = "Research X"

            while True:
                step = await ctx.sample_step(messages, tools=[search])

                if not step.is_tool_use:
                    print(step.text)
                    break

                # Continue with tool results
                messages = step.history
        NrD   zInvalid tool_choice: z(. Must be 'auto', 'required', or 'none'.modei   )r   r   r   r   	sdk_toolsr   )messagesr   r   r   r   r   r   r   toolUse	assistantrolecontent)responsehistory)r   user)_prepare_messages_prepare_tools_to_sdk_toolr:   r3   bool
ValueErrorr   r   r   r2   r   create_messager1   r   
isinstancer   
stopReasonr}   r   r   r.   _extract_tool_callsr(   r   run_sampling_tools)rp   r   r   r   r   r   r   r   r4   r   current_messagessampling_toolstr   tool_mapuse_fallbackeffective_tool_choiceeffective_max_tokensr   is_tool_use_responsestep_tool_callseffective_masktool_resultss                          rW   sample_stepzContext.sample_step9  s=    t -X6 (.:H~6!Q^^6d 	 4B/1QVVQY/r 	
 .dD4HI 48"">> +K? ;= =  %/'"<={K%!
 .8-Cz 2 +'/"3#1	 	H "\\88)+'/":;L"M1#'?? 9 	 	H x!=> 1##y0 	 	h6F6FG	

 $x9IJJ x9IJJ .h7 &1 #00 
 "4n" L  ''## , 85EFFo 7 0.		LsM   GGGGA,GG>G GBGG:GGG)r   r   r   r   r   r   c                  K   yw)z<Overload: With result_type, returns SamplingResult[ResultT].NrU   	rp   r   r   r   r   r   r   result_typer   s	            rW   samplezContext.sample          )r   r   r   r   r   r  r   c                  K   yw)z;Overload: Without result_type, returns SamplingResult[str].NrU   r  s	            rW   r  zContext.sample  r  r  c                 K   d}	t        |      }
d}|5|t        ur-t        |      }|
rt        |
      ng }
|
j	                  |       d}|}t        |	      D ]  }| j                  ||||||
||       d{   }||t        ur|j                  r|j                  D ]  }|j                  dk(  st        |      }|j                  }t        |j                         d      }|j                  d      d	k7  rt        |t               r	d
|v r|d
   }	 |j#                  |      }t%        j&                  |j)                  |d            }t+        |||j,                        c c S  |j                  sm|!|t        urt9        d|j:                   d      t+        |j<                  t?        t@        |j<                  r|j<                  nd      |j,                        c S |j,                  }d} t9        d|	 d      7 # t.        $ rY}|j,                  j	                  t1        dt3        d|j4                  t7        dd| d      gd      g             Y d}~d}~ww xY ww)a  
        Send a sampling request to the client and await the response.

        This method runs to completion automatically. When tools are provided,
        it executes a tool loop: if the LLM returns a tool use request, the tools
        are executed and the results are sent back to the LLM. This continues
        until the LLM provides a final text response.

        When result_type is specified, a synthetic `final_response` tool is
        created. The LLM calls this tool to provide the structured response,
        which is validated against the result_type and returned as `.result`.

        For fine-grained control over the sampling loop, use sample_step() instead.

        Args:
            messages: The message(s) to send. Can be a string, list of strings,
                or list of SamplingMessage objects.
            system_prompt: Optional system prompt for the LLM.
            temperature: Optional sampling temperature.
            max_tokens: Maximum tokens to generate. Defaults to 512.
            model_preferences: Optional model preferences.
            tools: Optional list of tools the LLM can use. Accepts plain
                functions or SamplingTools.
            result_type: Optional type for structured output. When specified,
                a synthetic `final_response` tool is created and the LLM's
                response is validated against this type.
            mask_error_details: If True, mask detailed error messages from tool
                execution. When None (default), uses the global settings value.
                Tools can raise ToolError to bypass masking.

        Returns:
            SamplingResult[T] containing:
            - .text: The text representation (raw text or JSON for structured)
            - .result: The typed result (str for text, parsed object for structured)
            - .history: All messages exchanged during sampling
        d   NrF   )r   r   r   r   r   r   r   r   final_responseTprune_titlestypeobjectvaluejsonr   )textr   r   r   tool_resultr  zValidation error: z#. Please try again with valid data.)r  r  )r  	toolUseIdr   isErrorr   z#Expected structured output of type zR, but the LLM returned a text response instead of calling the final_response tool. z&Sampling exceeded maximum iterations ())!r   rL   _create_final_response_toollistr}   ranger   is_tool_use
tool_callsr:   r9   inputr6   json_schemarz   r   dictvalidate_pythonr  dumpsdump_pythonr/   r   r$   r   r   idr   rt   rP   r  r   rC   )rp   r   r   r   r   r   r   r  r   max_iterationsr   r   final_response_toolr   
_iterationstep	tool_calltype_adapter
input_dataoriginal_schemavalidated_resultr  es                          rW   r  zContext.sample  s    b  (. #'"{#'="=k"J5CT.1N!!"56 %K CK/ T	J)))+'%"3$'#5 * 	 	D &;c+AdFVFV!% 1I ~~)99'=k'J &/__
*9(446T+ ,//78C *:t < ': 5)3G)<J /;/K/KJ/W,#':: , 8 89IPV 8 W$D $2%)'7(,$ -1h ##*{#/E&=k>R>R=S T3 3 
 &diiRH LL   $|| KiT	l CNCSSTUVVk	P  /  LL// /)/(91>6?ll0;9?:LQC P\ 9\125. 59)*-&!" sF   A4I%6G=78I%0AI%AH B I% 	I"	AII%I""I%c                   K   y wrb   rU   rp   r   response_types      rW   elicitzContext.elicit  s      r  c                   K   y wrb   rU   r.  s      rW   r0  zContext.elicit  s     
 ORr  c                   K   y wrb   rU   r.  s      rW   r0  zContext.elicit  s     
 QTr  c                v  K   t        |      }| j                  j                  ||j                  | j                         d{   }|j
                  dk(  rt        ||j                        S |j
                  dk(  r
t               S |j
                  dk(  r
t               S t        d|j
                         7 sw)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.
        )r   requestedSchemar   NacceptdeclinecancelzUnexpected elicitation action: )r-   r   r0  schemar   actionr,   r   r+   r*   r   )rp   r   r/  configr   s        rW   r0  zContext.elicit  s     F ,M:||**"MM# + 
 
 ==H$'??]]i'&((]]h&'))>v}}oNOO
s   AB9B7A4B9c                "    || j                   |<   y)z!Set a value in the context state.N)rn   )rp   keyr  s      rW   	set_statezContext.set_state  s     CrV   c                8    | j                   j                  |      S )zIGet a value from the context state. Returns None if the key is not found.)rn   rz   )rp   r<  s     rW   	get_statezContext.get_state  s    {{s##rV   c                :    | j                   j                  d       y)z'Queue a tool list changed notification. notifications/tools/list_changedNrm   addr   s    rW   _queue_tool_list_changedz Context._queue_tool_list_changed  s      $$%GHrV   c                :    | j                   j                  d       y)z+Queue a resource list changed notification.$notifications/resources/list_changedNrB  r   s    rW   _queue_resource_list_changedz$Context._queue_resource_list_changed  s      $$%KLrV   c                :    | j                   j                  d       y)z)Queue a prompt list changed notification."notifications/prompts/list_changedNrB  r   s    rW   _queue_prompt_list_changedz"Context._queue_prompt_list_changed  s      $$%IJrV   c                <  K   t         4 d{    | j                  s	 ddd      d{    y	 d| j                  v r"| j                  j                          d{    d| j                  v r"| j                  j	                          d{    d| j                  v r"| j                  j                          d{    | j                  j                          ddd      d{    y7 7 7 7 e7 7# t        $ r Y &w xY w7 # 1 d{  7  sw Y   yxY ww)zSend all queued notifications.NrA  rF  rI  )_flush_lockrm   r   r   r   r   clear	Exceptionr   s    rW   r   zContext._flush_notifications  s      	 	++	 	 	
59Q9QQ,,==???9T=U=UU,,AACCC74;S;SS,,??AAA((..0	 	 	 	 @CA 	 	 	 	s   DC,DDDC.D+C6C0/C6C2/C6<C4=C6D&D'D.D0C62C64C66	D?DDDDDDDD)rq   r5   )returnr5   )rO  r`   )rO  None)rO  z2RequestContext[ServerSession, Any, Request] | NoneNN)r   floatr   float | Noner   
str | NonerO  rP  )rO  zlist[SDKResource])rO  zlist[SDKPrompt]rb   )r:   rL   r   zdict[str, Any] | NonerO  r   )r   zstr | AnyUrlrO  zlist[ReadResourceContents])NNN)
r   rL   r   zLoggingLevel | Noner   rT  rO   rN   rO  rP  )rO  rT  )rO  rL   )rO  r   )r   rL   r   rT  rO   rN   rO  rP  )rO  z
list[Root])r   %str | Sequence[str | SamplingMessage]r   rT  r   rS  r   
int | Noner   )ModelPreferences | str | list[str] | Noner   2Sequence[SamplingTool | Callable[..., Any]] | Noner   zToolChoiceOption | str | Noner4   r   r   bool | NonerO  r.   )r   rU  r   rT  r   rS  r   rV  r   rW  r   rX  r  ztype[ResultT]r   rY  rO  zSamplingResult[ResultT])r   rU  r   rT  r   rS  r   rV  r   rW  r   rX  r  rP  r   rY  rO  zSamplingResult[str])r   rU  r   rT  r   rS  r   rV  r   rW  r   rX  r  ztype[ResultT] | Noner   rY  rO  z-SamplingResult[ResultT] | SamplingResult[str])r   rL   r/  rP  rO  zPAcceptedElicitation[dict[str, Any]] | DeclinedElicitation | CancelledElicitation)r   rL   r/  ztype[T]rO  zCAcceptedElicitation[T] | DeclinedElicitation | CancelledElicitation)r   rL   r/  z	list[str]rO  zEAcceptedElicitation[str] | DeclinedElicitation | CancelledElicitation)r   rL   r/  z6type[T] | list[str] | dict[str, dict[str, str]] | NonerO  zAcceptedElicitation[T] | AcceptedElicitation[dict[str, Any]] | AcceptedElicitation[str] | AcceptedElicitation[list[str]] | DeclinedElicitation | CancelledElicitation)r<  rL   r  r   rO  rP  )r<  rL   rO  r   )'rP   rQ   rR   rS   rr   propertyrq   r   r   r   r   r   r   r   r   r   r   r   r   r   rX   rY   r[   r\   r   r   r   r   r   r   r   r  r0  r=  r?  rD  rG  rJ  r   rU   rV   rW   r`   r`   t   sV   *X)  @*6  : RV

&2
DN
	
686 =ACC$9C	C
: &*"&*.

 #
  	

 (
 

8 
 
 
4 
4 1 1f 
, 
,  #'*.	

  
 (	

 

& #'*.	

  
 (	

 

& #'*.	

  
 (	

 

& #'*.	

  
 (	

 

 
486%6V %)$(!%GKDH59"*.VG7VG "	VG
 "VG VG EVG BVG 3VG VG (VG 
VGp 
 %)$(!%GKDH*.K7K "	K
 "K K EK BK #K (K 
!K K 
 %)$(!%GKDH *.J7J "	J
 "J J EJ BJ J (J 
J J$ %)$(!%GKDH,0*.ZW7ZW "	ZW
 "ZW ZW EZW BZW *ZW (ZW 
7ZWx  
 	Y  RR R 
M	R R TT !T 
O	T T, QU2P2P N2P
	2Ph!$IMKrV   c                   K   d|j                          d}|r	|d| dz  }t        j                  t        |   | d| j                   | j
                         |j                  || ||       d{    y7 w)	z'Log a message to the server and client.zSending z
 to clientz (r  z: )r   rM   rO   )r   r   r;   r   N)upperr>   r   _mcp_level_to_python_levelrM   rO   send_log_message)r   r   r   r   r   
msg_prefixs         rW   r   r     s      EKKM?*5J;-q))
(/l"TXXJ'jj   
"
"-	 #   s   A0A:2A83A:c                    t        |       }|j                         }t        |d      }|j                  d      dk7  r	dd|idgd}dd}t	        dd	||
      S )zCreate a synthetic 'final_response' tool for structured output.

    This tool is used to capture structured responses from the LLM.
    The tool's schema is derived from the result_type.
    Tr  r  r  r  )r  
propertiesrF   c                     | S rb   rU   )kwargss    rW   r
  z3_create_final_response_tool.<locals>.final_response5  s    rV   r
  z|Call this tool to provide your final response. Use this when you have completed the task and are ready to return the result.)r:   description
parametersfn)rc  r   rO  zdict[str, Any])r9   r  r6   rz   r0   )r  r(  r8  r
  s       rW   r  r  "  sw     *+6L%%'FV$7F zz&X%"F+ 	
 \  rV   c                    t        | t              r&| D ]   }t        |t              s|j                  c S  yt        | t              r| j                  S y)zExtract text from content block(s).

    Returns the text if content is a TextContent or list containing TextContent,
    otherwise returns None.
    N)r   r  r   r  )r   blocks     rW   _extract_text_from_contentri  C  sM     '4  	"E%-zz!	" 	G[	)||rV   c           	         t        | t              rt        t        | d      d      gS | D cg c],  }t        |t              rt        t        |d      d      n|. c}S c c}w )zEConvert various message formats to a list of SamplingMessage objects.r  )r  r  r   )r   r   )r   rL   r   r   )r   ms     rW   r   r   U  sr     (C #?f
 	
 	
  !S! KQV$D6R
 	
 
s   1A"c                    | yg }| D ]j  }t        |t              r|j                  |       %t        |      r%|j                  t        j                  |             Ut        dt        |              |r|S dS )z&Convert tools to SamplingTool objects.Nz'Expected SamplingTool or callable, got )r   r0   r}   callablefrom_function	TypeErrorr  )r   r   r   s      rW   r   r   h  s     })+N Qa&!!!$a[!!,"<"<Q"?@Ed1gYOPPQ ,>55rV   c                    | j                   }t        |t              r!|D cg c]  }t        |t              s| c}S t        |t              r|gS g S c c}w )z#Extract tool calls from a response.)r   r   r  r    )r   r   cs      rW   r   r   {  sP     G'4 "DajN&CDD	G^	,yI Es
   AA)rH   r`   rO  zGenerator[Context, None, None]rQ  )r   rK   r   r   r   r   r   rT  r   rT  rO  rP  )r  r  rO  r0   )r   z?SamplingMessageContentBlock | list[SamplingMessageContentBlock]rO  rT  )r   rU  rO  zlist[SamplingMessage])r   rX  rO  zlist[SamplingTool] | None)r   z2CreateMessageResult | CreateMessageResultWithToolsrO  zlist[ToolUseContent])r
__future__r   r{   r  loggingri   collections.abcr   r   r   r   
contextlibr   contextvarsr	   r
   dataclassesr   r   typingr   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!   	SDKPromptr"   SDKResourcer#   SDKToolpydanticr$   pydantic.networksr%   starlette.requestsr&   typing_extensionsr'   rq   r(   fastmcp.server.elicitationr)   r*   r+   r,   r-   fastmcp.server.samplingr.   r/   r0   fastmcp.server.sampling.runr1   r2   r3   r4   r   fastmcp.server.serverr5   fastmcp.utilities.json_schemar6   fastmcp.utilities.loggingr7   r8   fastmcp.utilities.typesr9   rP   r;   rT   getChildr>   rA   rL   rC   ToolChoiceOptionrI   LockrL  rK   r?   INFOWARNINGERRORCRITICALr]  rf   r`   r   r  ri  r   r   r   rU   rV   rW   <module>r     sF   "     B B % ) !  / /  + A 2 -    * - % $ $ & %   M L 
 * 9 ? :* *!??+?> & >
 % 9 C
)S
) 56 /9)T/R , R ejjl + + + ]]LLll]]  !!	  & & N N Nj #%)
  	
 # 
8BL$
3

&6=66&	@		rV   