
    i              	         U d dl m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 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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, d dl-m.Z. d dl/m0Z0m1Z1m2Z2m3Z3m4Z4 d dl5m6Z6 d dl7m8Z8m9Z9m:Z: d dl;m<Z<m=Z= d dl>m?Z?m@Z@ d dlAmBZB d dlAmCZD d dlAmEZF d dlAmGZH d dlAmIZJ d dlAmKZL d dlMmNZNmOZO d d lPmQZQ  eOeR!      ZSd"eTd#<   eSj                  d$%      ZVd"eTd&<    eNeVd'(        e*d)e*      ZW e*d+eX*      ZYd d,l;mZZZ  ed-d*      Z[d.eTd/<   ed0   Z\ ed1d*      Z]d2eTd3<   	 	 	 	 d=d4Z^d>d5Z_e G d6 d7             Z`ej                  ej                  ej                  ej                  ej                  ej                  ej                  ej                  d8Zfe
d?d:       Zge G d; d9             Zh	 	 d@	 	 	 	 	 	 	 	 	 	 	 dAd<Ziy)B    )annotationsN)Callable	GeneratorMappingSequence)contextmanager)
ContextVarToken)	dataclass)Logger)AnyLiteraloverload)LoggingLevelServerSession)request_ctx)RequestContext)GetPromptResultModelPreferencesRootSamplingMessage)Prompt)Resource)AnyUrl)Request)TypeVar)SharedContext)ResourceResult)AcceptedElicitationCancelledElicitationDeclinedElicitationhandle_elicit_acceptparse_elicit_response_type)MiddlewareServerSession)
SampleStepSamplingResultSamplingTool)sample_implsample_step_impl)FastMCP
StateValue)
Visibility)disable_components)enable_components)get_session_transforms)get_visibility_rules)reset_visibility)_clamp_logger
get_logger)VersionSpec)namer   logger	to_client)suffixto_client_loggerDEBUG)r6   	max_levelT)defaultResultT)ToolChoiceOptioncontextzContextVar[Context | None]_current_context)stdiossezstreamable-http	transportz ContextVar[TransportType | None]_current_transportc                ,    t         j                  |       S )z8Set the current transport type. Returns token for reset.)rE   set)rD   s    f/Users/bowang/.openclaw/workspace/ChatDev/.venv/lib/python3.12/site-packages/fastmcp/server/context.pyset_transportrI   X   s     !!),,    c                .    t         j                  |        y)z"Reset transport to previous value.N)rE   reset)tokens    rH   reset_transportrN   _   s    U#rJ   c                  *    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__rT    rJ   rH   rP   rP   d   s     
H&*E#*rJ   rP   )debuginfonoticewarningerrorcriticalalert	emergencyContextc              #     K   t         j                  |       }	 |  t         j                  |       y # t         j                  |       w xY wwN)rA   rG   rL   )r@   rM   s     rH   set_contextrf   |   s=       )E&u%u%s   A3 AA

Ac            
         e Zd ZU dZdZded<   	 d@ddd	 	 	 	 	 	 	 dAdZedBd       ZedCd	       Z	edCd
       Z
edDd       ZdEdZdFdZedGd       ZedHd       Z	 dI	 	 	 	 	 	 	 dJdZ	 	 	 	 	 	 	 	 dKdZdLdZdMdZ	 d@	 	 	 	 	 dNdZdOdZ	 	 	 dP	 	 	 	 	 	 	 	 	 dQdZedRd       ZdSdZedCd       ZedTd       ZedTd       ZedUd       Z	 	 dI	 	 	 	 	 	 	 dVdZ	 	 dI	 	 	 	 	 	 	 dVdZ	 	 dI	 	 	 	 	 	 	 dVdZ	 	 dI	 	 	 	 	 	 	 dVd Z dWd!Z!	 	 	 	 dXd"Z"dFd#Z#ddddddd$ddd%		 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 dYd&Z$e%dddddddd'	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 dZd(       Z&e%ddddddddd)	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 d[d*       Z&ddddddddd)	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 d\d+Z&e%	 	 	 	 	 	 d]d,       Z'	 e%	 	 	 	 	 	 d^d-       Z'	 e%	 	 	 	 	 	 d_d.       Z'	 e%	 	 	 	 	 	 d`d/       Z'	 e%	 	 	 	 	 	 dad0       Z'	 e%	 	 	 	 	 	 dbd1       Z'	 	 d@	 	 	 	 	 dcd2Z'	 	 	 	 	 	 ddd3Z(ded4Z)d$d5	 	 	 	 	 	 	 dfd6Z*dgd7Z+dhd8Z,did9Z-djd:Z.dddddd;d<	 	 	 	 	 	 	 	 	 	 	 	 	 dkd=Z/dddddd;d<	 	 	 	 	 	 	 	 	 	 	 	 	 dkd>Z0dFd?Z1y)lrc   ak  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 session (persists across requests)
        await ctx.set_state("key", "value")
        value = await ctx.get_state("key")

        # Store non-serializable values for the current request only
        await ctx.set_state("client", http_client, serializable=False)

        return str(x)
    ```

    State Management:
    Context provides session-scoped state that persists across requests within
    the same MCP session. State is automatically keyed by session, ensuring
    isolation between different clients.

    State set during `on_initialize` middleware will persist to subsequent tool
    calls when using the same session object (STDIO, SSE, single-server HTTP).
    For distributed/serverless HTTP deployments where different machines handle
    the init and tool calls, state is isolated by the mcp-session-id header.

    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.

    iQ int_STATE_TTL_SECONDSN)task_idorigin_request_idc               ~    t        j                  |      | _        || _        g | _        || _        || _        i | _        y re   )weakrefref_fastmcp_session_tokens_task_id_origin_request_id_request_state)selffastmcpsessionrj   rk   s        rH   __init__zContext.__init__   s9     /6kk'.B.5$&$+.?.0rJ   c                    | j                   duS )a<  True when this context is running in a background task (Docket worker).

        When True, certain operations like elicit() and sample() will use
        task-aware implementations that can pause the task and wait for
        client input.

        Example:
            ```python
            @server.tool(task=True)
            async def my_task(ctx: Context) -> str:
                # Works transparently in both foreground and background task modes
                result = await ctx.elicit("Need input", str)
                return str(result)
            ```
        Nrr   ru   s    rH   is_background_taskzContext.is_background_task   s    " }}D((rJ   c                    | j                   S )zGet the background task ID if running in a background task.

        Returns None if not running in a background task context.
        rz   r{   s    rH   rj   zContext.task_id   s     }}rJ   c                p    | j                   t        | j                   j                        S | j                  S )a
  Get the request ID that originated this execution, if available.

        In foreground request mode, this is the current request_id.
        In background task mode, this is the request_id captured when the task
        was submitted, if one was available.
        )request_contextrQ   
request_idrs   r{   s    rH   rk   zContext.origin_request_id   s3     +t++6677&&&rJ   c                @    | j                         }|t        d      |S )zGet the FastMCP instance.z'FastMCP instance is no longer available)ro   RuntimeError)ru   rv   s     rH   rv   zContext.fastmcp   s%     --/?HIIrJ   c                |  K   t         j                  d      }||j                  | _        t         j                  |       }| j                  j                  |       ddlm}m}m	}m
} |j                  t        j                  | j                              | _        | j                  } |       rZ|j                   |j                  |j                        | _        |j"                   |j                  |j"                        | _        | S t'               | _        | j(                  j+                          d{    | S 7 w)zFEnter the context manager and set this context as the current context.Nr   )_current_docket_current_server_current_workeris_docket_available)rA   getrt   rG   rq   appendfastmcp.server.dependenciesr   r   r   r   rm   rn   rv   _server_token_docket_docket_token_worker_worker_tokenr   _shared_context
__aenter__)ru   parentrM   r   r   r   r   servers           rH   r   zContext.__aenter__   s     "%%d+"("7"7D !$$T*E"	
 	
 -00T\\1JK
  ~~)%4%8%8%H"~~)%4%8%8%H"  $1?D &&11333 4s   D1D<3D:4D<c                  K   ddl m}m}m} t	        | d      r|j                  | j                         | `t	        | d      r|j                  | j                         | `t	        | d      r'| j                  j                  |||       d{    | `t	        | d      r|j                  | j                         | `
| j                  r0| j                  j                         }t        j                  |       yy7 lw)z9Exit the context manager and reset the most recent token.r   )r   r   r   r   r   r   Nr   )r   r   r   r   hasattrrL   r   r   r   	__aexit__r   rq   poprA   )ru   exc_typeexc_valexc_tbr   r   r   rM   s           rH   r   zContext.__aexit__$  s     	
 	
 4)!!$"4"45"4)!!$"4"45"4*+&&007FKKK$4)!!$"4"45" <<LL$$&E""5)  Ls   B	C:C8A-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   r   LookupErrorr{   s    rH   r   zContext.request_context@  s&    0	??$$ 		s    	""c                n    | j                   }|| j                  j                  }||S i S |j                  S )a  Access the server's lifespan context.

        Returns the context dict yielded by the server's lifespan function.
        Returns an empty dict if no lifespan was configured or if the MCP
        session is not yet established.

        In background tasks (Docket workers), where request_context is not
        available, falls back to reading from the FastMCP server's lifespan
        result directly.

        Example:
        ```python
        @server.tool
        def my_tool(ctx: Context) -> str:
            db = ctx.lifespan_context.get("db")
            if db:
                return db.query("SELECT 1")
            return "No database connection"
        ```
        )r   rv   _lifespan_resultlifespan_context)ru   rcresults      rH   r   zContext.lifespan_context]  sA    , !!: \\22F!I"""rJ   c                  K   | j                   r6| j                   j                  r | j                   j                  j                  nd}|3| j                  j	                  ||||| j
                         d{    yddlm}  |       sy	 ddlm	} |j                         }|,|j                  j                  t        |             d{    t        |      }t        |dd      }	||	z
  }
|
dkD  r#|j                  j                  |
       d{    ||_        |$|j                  j#                  |       d{    yy7 7 y7 67 # t$        $ r Y yw xY ww)ai  Report progress for the current operation.

        Works in both foreground (MCP progress notifications) and background
        (Docket task execution) contexts.

        Args:
            progress: Current progress value e.g. 24
            total: Optional total value e.g. 100
            message: Optional status message describing current progress
        N)progress_tokenprogresstotalmessagerelated_request_idr   )r   )current_execution_fastmcp_last_progress)r   metaprogressTokenrw   send_progress_notificationr   r   r   docket.dependenciesr   r   r   	set_totalrh   getattr	incrementr   set_messager   )ru   r   r   r   r   r   r   	executioncurrentlastdeltas              rH   report_progresszContext.report_progress}  sb      ##(<(<(A(A   %%33 	 %,,99-!#'?? :     	D"$	=)--/I
  ((223u:>>>(mG	+CQGDdNEqy((225999/6I,"((44W=== #E2 ? : > 		sm   A4E6E7E?E
 
EAE
 E+E
 ;E<E
  EE
 E
 E
 
	EEEEc                  K   g }d}t               }	  ||      } ||       d{   }|j                   ||             |j                  s	 |S |j                  |v r	 |S |j                  |j                         |j                  }w7 cw)aV  Generic pagination helper for list operations.

        Args:
            request_factory: Function that creates a request from a cursor
            call_method: Async method to call with the request
            extract_items: Function to extract items from the result

        Returns:
            List of all items across all pages
        N)rG   extend
nextCursoradd)	ru   request_factorycall_methodextract_items	all_itemscursorseen_cursorsrequestr   s	            rH   _paginate_listzContext._paginate_list  s        "	!!$%f-G&w//F]623$$
 	   L0  V../&&F /s   #BB	A$Bc                r   K   | j                  d | j                  j                  d        d{   S 7 w)zList all available resources from the server.

        Returns:
            List of Resource objects available on the server
        c                    t         j                  j                  | r%t         j                  j                  |             S d       S Nr   )params)mcptypesListResourcesRequestPaginatedRequestParamsr   s    rH   <lambda>z(Context.list_resources.<locals>.<lambda>  sC    399+I+I yy77v7F ,J ,  ,J ,rJ   c                    | j                   S re   )	resourcesr   s    rH   r   z(Context.list_resources.<locals>.<lambda>  s
    )9)9rJ   r   r   r   N)r   rv   _list_resources_mcpr{   s    rH   list_resourceszContext.list_resources  sA      ((
 889 ) 
 
 	
 
   .757c                r   K   | j                  d | j                  j                  d        d{   S 7 w)zList all available prompts from the server.

        Returns:
            List of Prompt objects available on the server
        c                    t         j                  j                  | r%t         j                  j                  |             S d       S r   )r   r   ListPromptsRequestr   r   s    rH   r   z&Context.list_prompts.<locals>.<lambda>  sC    399+G+G yy77v7F ,H ,  ,H ,rJ   c                    | j                   S re   )promptsr   s    rH   r   z&Context.list_prompts.<locals>.<lambda>  s    rJ   r   N)r   rv   _list_prompts_mcpr{   s    rH   list_promptszContext.list_prompts  sA      ((
 667 ) 
 
 	
 
r   c                   K   | j                   j                  ||       d{   }t        |t        j                  j
                        rt        d      |j                         S 7 C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
        NHUnexpected CreateTaskResult: Context calls should not have task metadata)rv   render_prompt
isinstancer   r   CreateTaskResultr   to_mcp_prompt_result)ru   r5   	argumentsr   s       rH   
get_promptzContext.get_prompt  s\      ||11$	BBfcii889Z  **,, Cs    A(A&AA(c                   K   | j                   j                  t        |             d{   }t        |t        j
                  j                        rt        d      |S 7 5w)zRead a resource by URI.

        Args:
            uri: Resource URI to read

        Returns:
            ResourceResult with contents
        Nr   )rv   read_resourcerQ   r   r   r   r   r   )ru   urir   s      rH   r   zContext.read_resource  sS      ||11#c(;;fcii889Z   <s   (A"A 6A"c                   K   t        ||      }| j                  }t        || j                  |xs d||       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
        )rR   rT   r\   )datarw   levellogger_namer   N)rP   rk   _log_to_server_and_clientrw   )ru   r   r   r   rT   r   r   s          rH   logzContext.log  sI     $ 7%0!33'LL/6#1
 	
 	
s   ;AAAc                *    t         j                         S )zGet the current transport type.

        Returns the transport type used to run this server: "stdio", "sse",
        or "streamable-http". Returns None if called outside of a server context.
        )rE   r   r{   s    rH   rD   zContext.transport:  s     "%%''rJ   c                |    | j                   }|y|j                  }t        |t              sy|j	                  |      S )a  Check whether the connected client supports a given MCP extension.

        Inspects the ``extensions`` extra field on ``ClientCapabilities``
        sent by the client during initialization.

        Returns ``False`` when no session is available (e.g., outside a
        request context) or when the client did not advertise the extension.

        Example::

            from fastmcp.server.apps import UI_EXTENSION_ID

            @mcp.tool
            async def my_tool(ctx: Context) -> str:
                if ctx.client_supports_extension(UI_EXTENSION_ID):
                    return "UI-capable client"
                return "text-only client"
        F)r   rw   r   r$   client_supports_extension)ru   extension_idr   rw   s       rH   r   z!Context.client_supports_extensionC  s?    & !!:**'#:;00>>rJ   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   r   r{   s    rH   r   zContext.client_id^  sD    
 ##(<(<(A(A D((--{DA	
 	
rJ   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   r   rQ   r   r{   s    rH   r   zContext.request_idg  s<     '\  4''2233rJ   c                :   ddl m} | j                  }||j                  }n$| j                  | j                  }nt        d      t        |dd      }||S |)|j                  }|r|j                  j                  d      }|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 no session is 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}"
            ```
        r   )uuid4Nzmsession_id is not available because no session exists. This typically means you're outside a request context._fastmcp_state_prefixzmcp-session-id)uuidr   r   rw   rp   r   r   r   headersr   rQ   r   )ru   r   r   rw   
session_idr   s         rH   r   zContext.session_idt  s    0 	 **"!))G]]&mmGI  W&=tD
! "!))G$__001AB
 UWJ )3%rJ   c                    | j                   r| j                  | j                  S | j                  | j                  j                  S | j                  | j                  S t	        d      )a  Access to the underlying session for advanced usage.

        In request mode: Returns the session from the active request context.
        In background task mode: Returns the session stored at Context creation.

        Raises RuntimeError if no session is 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|   rp   r   rw   r   r{   s    rH   rw   zContext.session  si     ""t}}'@==  +''/// ==$== X
 	
rJ   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`.r[   r   r   r   rT   Nr   ru   r   r   rT   s       rH   r[   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`.r\   r   Nr   r   s       rH   r\   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       rH   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       rH   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)rw   
list_rootsroots)ru   r   s     rH   r  zContext.list_roots  s*     ||..00|| 1s   313c                   K   | j                   j                  t        j                  j	                  |             d{    y7 w)zSend a notification to the client immediately.

        Args:
            notification: An MCP notification instance (e.g., ToolListChangedNotification())
        N)rw   send_notificationr   r   ServerNotification)ru   notifications     rH   r  zContext.send_notification  s/      ll,,SYY-I-I,-WXXXs   <AAA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_streamr6   r[   r{   s    rH   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_choiceexecute_toolsmask_error_detailstool_concurrencyc       	        J   K   t        | |||||||||	|
       d{   S 7 w)a  
        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.
            tool_concurrency: Controls parallel execution of tools:
                - None (default): Sequential execution (one at a time)
                - 0: Unlimited parallel execution
                - N > 0: Execute at most N tools concurrently
                If any tool has sequential=True, all tools execute sequentially
                regardless of this setting.

        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
        )
messagesr  r  r  r  r  r  auto_execute_toolsr  r  N)r)   )ru   r  r  r  r  r  r  r  r  r  r  s              rH   sample_stepzContext.sample_step>  sA     @ &'#!/#,1-
 
 	
 
s   #!#)r  r  r  r  r  r  r  c                  K   yw)z<Overload: With result_type, returns SamplingResult[ResultT].NrZ   
ru   r  r  r  r  r  r  result_typer  r  s
             rH   samplezContext.sample          )r  r  r  r  r  r  r  r  c                  K   yw)z;Overload: Without result_type, returns SamplingResult[str].NrZ   r  s
             rH   r  zContext.sample  r  r  c               H   K   t        | |||||||||	
       d{   S 7 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.
            tool_concurrency: Controls parallel execution of tools:
                - None (default): Sequential execution (one at a time)
                - 0: Unlimited parallel execution
                - N > 0: Execute at most N tools concurrently
                If any tool has sequential=True, all tools execute sequentially
                regardless of this setting.

        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

        Note:
            Background task support for sampling is planned for a future release.
            Currently, sampling in background tasks requires using the low-level
            session.create_message() API directly.
        )	r  r  r  r  r  r  r  r  r  N)r(   r  s
             rH   r  zContext.sample  s>     z !'#!/#1-
 
 	
 
s   " "c                   K   y wre   rZ   ru   r   response_types      rH   elicitzContext.elicit        r  c                   K   y wre   rZ   r"  s      rH   r$  zContext.elicit  s     
 ORr  c                   K   y wre   rZ   r"  s      rH   r$  zContext.elicit       
 QTr  c                   K   y wre   rZ   r"  s      rH   r$  zContext.elicit  r(  r  c                   K   y wre   rZ   r"  s      rH   r$  zContext.elicit   r%  r  c                   K   y wre   rZ   r"  s      rH   r$  zContext.elicit,  r%  r  c                  K   t        |      }| j                  r&| j                  ||j                         d{   }n:| 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 7 u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.

        Note:
            This method works transparently in both request and background task
            contexts. In background task mode (SEP-1686), it will set the task
            status to "input_required" and wait for the client to provide input.
        )r   schemaN)r   requestedSchemar   acceptdeclinecancelzUnexpected elicitation action: )r#   r|   _elicit_for_taskr-  rw   r$  r   actionr"   contentr!   r    
ValueError)ru   r   r#  configr   s        rH   r$  zContext.elicit9  s     Z ,M:""00}} 1  F  <<.. &#'?? /  F ==H$'??]]i'&((]]h&'))>v}}oNOO's"   8C-C):C-5C+6A4C-+C-c                   K   | j                   st        d      ddlm}  || j                  | j
                  ||| j                         d{   S 7 w)a  Send an elicitation request from a background task (SEP-1686).

        This method handles elicitation when running in a Docket worker context,
        where there's no active MCP request. It:
        1. Sets the task status to "input_required"
        2. Sends the elicitation request with task metadata
        3. Waits for the client to provide input via tasks/sendInput
        4. Returns the result and resumes task execution

        Args:
            message: The message to display to the user
            schema: The JSON schema for the expected response

        Returns:
            ElicitResult with the user's response

        Raises:
            RuntimeError: If not running in a background task context
        z<_elicit_for_task called but not in a background task contextr   )elicit_for_task)rj   rw   r   r-  rv   N)r|   r    fastmcp.server.tasks.elicitationr8  rr   rp   rv   )ru   r   r-  r8  s       rH   r2  zContext._elicit_for_task  sY     0 &&N 
 	E$MMMMLL
 
 	
 
s   AAAAc                $    | j                    d| S )z.Create session-prefixed key for state storage.:)r   )ru   keys     rH   _make_state_keyzContext._make_state_key  s    //"!C5))rJ   )serializablec                 K   | j                  |      }|s|| j                  |<   y| j                  j                  |d       	 | j                  j                  j                  |t        |      | j                         d{    y7 # t        $ r4}dt        |      j                         v rt        d|d|d      | d}~ww xY ww)a  Set a value in the state store.

        By default, values are stored in the session-scoped state store and
        persist across requests within the same MCP session. Values must be
        JSON-serializable (dicts, lists, strings, numbers, etc.).

        For non-serializable values (e.g., HTTP clients, database connections),
        pass ``serializable=False``. These values are stored in a request-scoped
        dict and only live for the current MCP request (tool call, resource
        read, or prompt render). They will not be available in subsequent
        requests.

        The key is automatically prefixed with the session identifier.
        N)value)r<  r@  ttl	serializezValue for state key z$ is not serializable. Use set_state(z, value, serializable=False) to store non-serializable values. Note: non-serializable state is request-scoped and will not persist across requests.)r=  rt   r   rv   _state_storeputr+   ri   	ExceptionrQ   lower	TypeError)ru   r<  r@  r>  prefixed_keyes         rH   	set_statezContext.set_state  s     " ++C005D-d3	,,++//  u-++ 0   
  	 c!flln,*3' 2%%(G ,KL
  	s<   A C
?B
 BB
 C
B
 
	C/CCC
c                   K   | j                  |      }|| j                  v r| j                  |   S | j                  j                  j	                  |       d{   }||j
                  S dS 7 w)zGet a value from the state store.

        Checks request-scoped state first (set with ``serializable=False``),
        then falls back to the session-scoped state store.

        Returns None if the key is not found.
        r<  N)r=  rt   rv   rC  r   r@  )ru   r<  rH  r   s       rH   	get_statezContext.get_state  sq      ++C04...&&|44||00444FF%1v||;t; Gs   AA1A/A1c                   K   | j                  |      }| j                  j                  |d       | j                  j                  j                  |       d{    y7 w)zrDelete a value from the state store.

        Removes from both request-scoped and session-scoped stores.
        NrL  )r=  rt   r   rv   rC  delete)ru   r<  rH  s      rH   delete_statezContext.delete_state  sO     
 ++C0d3ll''..<.@@@s   AA!AA!c                4   K   t        |        d{   S 7 w)z.Load visibility rule dicts from session state.N)_get_visibility_rulesr{   s    rH   rR  zContext._get_visibility_rules  s     *40000   c                4   K   t        |        d{   S 7 w)z<Get session-specific Visibility transforms from state store.N)_get_session_transformsr{   s    rH   rU  zContext._get_session_transforms  s     ,T2222rS  Fnameskeysversiontags
components	match_allc          	     D   K   t        | ||||||       d{    y7 w)aJ  Enable components matching criteria for this session only.

        Session rules override global transforms. Rules accumulate - each call
        adds a new rule to the session. Later marks override earlier ones
        (Visibility transform semantics).

        Sends notifications to this session only: ToolListChangedNotification,
        ResourceListChangedNotification, and PromptListChangedNotification.

        Args:
            names: Component names or URIs to match.
            keys: Component keys to match (e.g., {"tool:my_tool@v1"}).
            version: Component version spec to match.
            tags: Tags to match (component must have at least one).
            components: Component types to match (e.g., {"tool", "prompt"}).
            match_all: If True, matches all components regardless of other criteria.
        rV  N)_enable_componentsru   rW  rX  rY  rZ  r[  r\  s          rH   r.   zContext.enable_components  s/     8 !!
 	
 	
     c          	     D   K   t        | ||||||       d{    y7 w)aK  Disable components matching criteria for this session only.

        Session rules override global transforms. Rules accumulate - each call
        adds a new rule to the session. Later marks override earlier ones
        (Visibility transform semantics).

        Sends notifications to this session only: ToolListChangedNotification,
        ResourceListChangedNotification, and PromptListChangedNotification.

        Args:
            names: Component names or URIs to match.
            keys: Component keys to match (e.g., {"tool:my_tool@v1"}).
            version: Component version spec to match.
            tags: Tags to match (component must have at least one).
            components: Component types to match (e.g., {"tool", "prompt"}).
            match_all: If True, matches all components regardless of other criteria.
        rV  N)_disable_componentsr_  s          rH   r-   zContext.disable_components  s/     8 "!
 	
 	
r`  c                6   K   t        |        d{    y7 w)a  Clear all session visibility rules.

        Use this to reset session visibility back to global defaults.

        Sends notifications to this session only: ToolListChangedNotification,
        ResourceListChangedNotification, and PromptListChangedNotification.
        N)_reset_visibilityr{   s    rH   r1   zContext.reset_visibilityD  s       %%%s   re   )rv   r*   rw   zServerSession | Nonerj   
str | Nonerk   re  )returnbool)rf  re  )rf  r*   )rf  rc   )rf  None)rf  z2RequestContext[ServerSession, Any, Request] | None)rf  dict[str, Any]NN)r   floatr   float | Noner   re  rf  rh  )r   zCallable[[str | None], Any]r   zCallable[[Any], Any]r   zCallable[[Any], list[Any]]rf  z	list[Any])rf  zlist[SDKResource])rf  zlist[SDKPrompt])r5   rQ   r   zdict[str, Any] | Nonerf  r   )r   zstr | AnyUrlrf  r   )NNN)
r   rQ   r   zLoggingLevel | Noner   re  rT   rS   rf  rh  )rf  zTransportType | None)r   rQ   rf  rg  )rf  rQ   )rf  r   )r   rQ   r   re  rT   rS   rf  rh  )rf  z
list[Root])r	  z mcp.types.ServerNotificationTyperf  rh  )r  %str | Sequence[str | SamplingMessage]r  re  r  rl  r  
int | Noner  )ModelPreferences | str | list[str] | Noner  2Sequence[SamplingTool | Callable[..., Any]] | Noner  zToolChoiceOption | str | Noner  rg  r  bool | Noner  rn  rf  r%   )r  rm  r  re  r  rl  r  rn  r  ro  r  rp  r  ztype[ResultT]r  rq  r  rn  rf  zSamplingResult[ResultT])r  rm  r  re  r  rl  r  rn  r  ro  r  rp  r  rh  r  rq  r  rn  rf  zSamplingResult[str])r  rm  r  re  r  rl  r  rn  r  ro  r  rp  r  ztype[ResultT] | Noner  rq  r  rn  rf  z-SamplingResult[ResultT] | SamplingResult[str])r   rQ   r#  rh  rf  zPAcceptedElicitation[dict[str, Any]] | DeclinedElicitation | CancelledElicitation)r   rQ   r#  ztype[T]rf  zCAcceptedElicitation[T] | DeclinedElicitation | CancelledElicitation)r   rQ   r#  z	list[str]rf  EAcceptedElicitation[str] | DeclinedElicitation | CancelledElicitation)r   rQ   r#  zdict[str, dict[str, str]]rf  rr  )r   rQ   r#  zlist[list[str]]rf  KAcceptedElicitation[list[str]] | DeclinedElicitation | CancelledElicitation)r   rQ   r#  zlist[dict[str, dict[str, str]]]rf  rs  )r   rQ   r#  zjtype[T] | list[str] | dict[str, dict[str, str]] | list[list[str]] | list[dict[str, dict[str, str]]] | Nonerf  zAcceptedElicitation[T] | AcceptedElicitation[dict[str, Any]] | AcceptedElicitation[str] | AcceptedElicitation[list[str]] | DeclinedElicitation | CancelledElicitation)r   rQ   r-  ri  rf  zmcp.types.ElicitResult)r<  rQ   rf  rQ   )r<  rQ   r@  r   r>  rg  rf  rh  )r<  rQ   rf  r   )r<  rQ   rf  rh  )rf  zlist[dict[str, Any]])rf  zlist[Visibility])rW  set[str] | NonerX  rt  rY  zVersionSpec | NonerZ  rt  r[  z=set[Literal['tool', 'resource', 'template', 'prompt']] | Noner\  rg  rf  rh  )2rU   rV   rW   rX   ri   rY   rx   propertyr|   rj   rk   rv   r   r   r   r   r   r   r   r   r   r   r   rD   r   r   r   r   rw   r[   r\   r^   r_   r  r  r  r  r   r  r$  r2  r=  rJ  rM  rP  rR  rU  r.   r-   r1   rZ   rJ   rH   rc   rc      s   1h $#
 )-1
 #(,11 &1
 1 &1" ) )$   	' 	'  %N*8  8 # #@ RV<<&2<DN<	<|4 * 2	
 
>
 
" =A--$9-	-&& &*"&*.

 #
  	

 (
 

: ( (?6 
 
 
4 
4 6 6p 
 
: #'*.	

  
 (	

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

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

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

 

 
Y<Y	Y%6V %)$(!%GKDH59"*.'+L
7L
 "	L

 "L
 L
 EL
 BL
 3L
 L
 (L
 %L
 
L
\ 
 %)$(!%GKDH*.'+K7K "	K
 "K K EK BK #K (K %K 
!K K 
 %)$(!%GKDH *.'+J7J "	J
 "J J EJ BJ J (J %J 
J J& %)$(!%GKDH,0*.'+H
7H
 "	H

 "H
 H
 EH
 BH
 *H
 (H
 %H
 
7H
T  
 	Y  RR R 
M	R R TT !T 
O	T T, TT 1T 
O	T T1  '
 	T H  7
 	T  DPDPDP	DPL&
&
 &
 
 	&
P*
 =A(("(59(	(T<A13 "& $&* $$
 $
 	$

 $$
 $
$
 $
 
$
R "& $&* $$
 $
 	$

 $$
 $
$
 $
 
$
L&rJ   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 ()z: )r   rR   rT   )r   r   r6   r   N)upperr9   r   _mcp_level_to_python_levelrR   rT   send_log_message)r   rw   r   r   r   
msg_prefixs         rH   r   r   O  s      EKKM?*5J;-q))
(/l"TXXJ'jj   
"
"-	 #   s   A0A:2A83A:)rD   TransportTyperf  Token[TransportType | None])rM   r}  rf  rh  )r@   rc   rf  zGenerator[Context, None, None]rj  )r   rP   rw   r   r   r   r   re  r   re  rf  rh  )j
__future__r   loggingrm   collections.abcr   r   r   r   
contextlibr   contextvarsr	   r
   dataclassesr   r   typingr   r   r   	mcp.typesr   r   r   mcp.server.lowlevel.serverr   mcp.shared.contextr   r   r   r   r   r   	SDKPromptr   SDKResourcepydantic.networksr   starlette.requestsr   typing_extensionsr   uncalled_forr   fastmcp.resources.resourcer   fastmcp.server.elicitationr   r    r!   r"   r#   fastmcp.server.low_levelr$   fastmcp.server.samplingr%   r&   r'   fastmcp.server.sampling.runr(   r)   fastmcp.server.serverr*   r+   $fastmcp.server.transforms.visibilityr,   r-   rb  r.   r^  r/   rU  r0   rR  r1   rd  fastmcp.utilities.loggingr2   r3   fastmcp.utilities.versionsr4   rU   r6   rY   getChildr9   r<   rQ   r>   r?   rA   r|  rE   rI   rN   rP   r:   INFOWARNINGERRORCRITICALry  rf   rc   r   rZ   rJ   rH   <module>r     s   "   B B % ) !  ) )  + 2 -  * - $ & % & 5  = L L 6 @ 2* *!??+?> & >
 % 9 C
)S
) 9/9)T/R , R9:7A8 4 
-- -$
 + + + ]]LLll]]  !!	  & & F& F& F&Z& #%)
  	
 # 
rJ   