
    i                        d Z ddlm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 ed   Z e	d	
      Z eej!                         dz        ZdZe G d d             Ze G d d             Zy)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)	forbiddenoptionalrequired   )secondsi  i`  c                  .    e Zd ZU dZdZded<   dZded<   y)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        k/Users/bowang/.openclaw/workspace/ChatDev/.venv/lib/python3.12/site-packages/fastmcp/server/tasks/config.pyr   r      s     CFJr   r   c                  P    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y
)
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_intervalc                &     | |rd      S 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   )clsvalues     r   	from_boolzTaskConfig.from_boolN   s     e
====r   c                     | j                   dk7  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_tasksZ   s     yyK''r   c                &   | j                         syddlm}  |d| d       |}t        j                  |      st        |      r|j                  }t        |t              r|j                  }t        j                  |      st        d| d      y)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.dependenciesr(   inspect	isroutinecallable__call__
isinstancestaticmethod__func__iscoroutinefunction
ValueError)r%   fnnamer(   fn_to_checks        r   validate_functionzTaskConfig.validate_functionb   s    $ ""$ 	?24&:;   $"++Kk<0%..K**;7D6 < <  8r   N)r"   boolreturnr   )r9   r8   )r4   zCallable[..., Any]r5   strr9   None)r   r   r   r   r   r   DEFAULT_POLL_INTERVALr   classmethodr#   r&   r7   r   r   r   r   r   (   s:    B  D(4M94	> 	>(&r   r   )r   
__future__r   r+   collections.abcr   dataclassesr   datetimer   typingr   r   r   r<   inttotal_secondsDEFAULT_POLL_INTERVAL_MSDEFAULT_TTL_MSr   r   r   r   r   <module>rG      s    #  $ !   67 "!, 4BBDtKL     _ _ _r   