
    $j2                    &   d Z ddlmZ ddlZddlZddlmZ ddlmZ ddl	m
Z
 ddlmZmZ ddlmZ ed	         Z e
d
          Z ee                                dz            ZdZe G d d                      Ze G d d                      ZdS )zTaskConfig for MCP SEP-1686 background task execution modes.

This module defines the configuration for how tools, resources, and prompts
handle task-augmented execution as specified in SEP-1686.
    )annotationsN)Callable)	dataclass)	timedelta)AnyLiteral)is_coroutine_function)	forbiddenoptionalrequired   )secondsi  i`  c                  0    e Zd ZU dZdZded<   dZded<   dS )TaskMetaae  Metadata for task-augmented execution requests.

    When passed to call_tool/read_resource/get_prompt, signals that
    the operation should be submitted as a background task.

    Attributes:
        ttl: Client-requested TTL in milliseconds. If None, uses server default.
        fn_key: Docket routing key. Auto-derived from component name if None.
    Nz
int | Nonettlz
str | Nonefn_key)__name__
__module____qualname____doc__r   __annotations__r        c/home/rurouni/.hermes/hermes-agent/venv/lib/python3.11/site-packages/fastmcp/server/tasks/config.pyr   r      s@           CFr   r   c                  X    e Zd ZU dZdZded<   eZded<   edd
            Z	ddZ
ddZdS )
TaskConfiga  Configuration for MCP background task execution (SEP-1686).

    Controls how a component handles task-augmented requests:

    - "forbidden": Component does not support task execution. Clients must not
      request task augmentation; server returns -32601 if they do.
    - "optional": Component supports both synchronous and task execution.
      Client may request task augmentation or call normally.
    - "required": Component requires task execution. Clients must request task
      augmentation; server returns -32601 if they don't.

    Important:
        Task-enabled components must be available at server startup to be
        registered with all Docket workers. Components added dynamically after
        startup will not be registered for background execution.

    Example:
        ```python
        from fastmcp import FastMCP
        from fastmcp.server.tasks import TaskConfig

        mcp = FastMCP("MyServer")

        # Background execution required
        @mcp.tool(task=TaskConfig(mode="required"))
        async def long_running_task(): ...

        # Supports both modes (default when task=True)
        @mcp.tool(task=TaskConfig(mode="optional"))
        async def flexible_task(): ...
        ```
    r   TaskModemoder   poll_intervalvalueboolreturnc                "     | |rdnd          S )zConvert boolean task flag to TaskConfig.

        Args:
            value: True for "optional" mode, False for "forbidden" mode.

        Returns:
            TaskConfig with appropriate mode.
        r   r
   r   r   )clsr    s     r   	from_boolzTaskConfig.from_boolQ   s      se<

====r   c                    | j         dk    S )zCheck if this component supports task execution.

        Returns:
            True if mode is "optional" or "required", False if "forbidden".
        r
   r$   )selfs    r   supports_taskszTaskConfig.supports_tasks]   s     yK''r   fnCallable[..., Any]namestrNonec                d   |                                  sdS ddlm}  |d| d           |}t          j        |          s0t          |t          j                  st          |          r|j	        }t          |t                    r|j        }t          |          st          d| d          dS )a  Validate that function is compatible with this task config.

        Task execution requires:
        1. fastmcp[tasks] to be installed (pydocket)
        2. Async functions

        Raises ImportError if mode is "optional" or "required" but pydocket
        is not installed. Raises ValueError if function is synchronous.

        Args:
            fn: The function to validate (handles callable classes and staticmethods).
            name: Name for error messages.

        Raises:
            ImportError: If task execution is enabled but pydocket not installed.
            ValueError: If task execution is enabled but function is sync.
        Nr   )require_docketz`task=True` on function ''z`' uses a sync function but has task execution enabled. Background tasks require async functions.)r)   fastmcp.server.dependenciesr0   inspect	isroutine
isinstance	functoolspartialcallable__call__staticmethod__func__r	   
ValueError)r(   r*   r,   r0   fn_to_checks        r   validate_functionzTaskConfig.validate_functione   s    $ ""$$ 	F 	?>>>>>:4:::;;; !"%%	&r9#455	& 	&
 +Kk<00 	/%.K$[11 	<D < < <  	 	r   N)r    r!   r"   r   )r"   r!   )r*   r+   r,   r-   r"   r.   )r   r   r   r   r   r   DEFAULT_POLL_INTERVALr   classmethodr&   r)   r>   r   r   r   r   r   +   s          B  D4M4444	> 	> 	> [	>( ( ( (* * * * * *r   r   )r   
__future__r   r6   r3   collections.abcr   dataclassesr   datetimer   typingr   r   fastmcp.utilities.async_utilsr	   r   r?   inttotal_secondsDEFAULT_POLL_INTERVAL_MSDEFAULT_TTL_MSr   r   r   r   r   <module>rK      s]    # " " " " "      $ $ $ $ $ $ ! ! ! ! ! !               ? ? ? ? ? ? 67 "	!,,, 34BBDDtKLL          c c c c c c c c c cr   