o
    i;                     @   s:   d Z ddlmZ ddlmZmZmZmZ G dd dZdS )z
TaskContext - Pure task state management.

This module provides TaskContext, which manages task state without any
server/session dependencies. It can be used standalone for distributed
workers or wrapped by ServerTaskContext for full server integration.
    )	TaskStore)TASK_STATUS_COMPLETEDTASK_STATUS_FAILEDResultTaskc                   @   s   e Zd ZdZdedefddZedefddZ	edefd	d
Z
edefddZdddZdeddfddZdeddfddZdeddfddZdS )TaskContexta  
    Pure task state management - no session dependencies.

    This class handles:
    - Task state (status, result)
    - Cancellation tracking
    - Store interactions

    For server-integrated features (elicit, create_message, notifications),
    use ServerTaskContext from mcp.server.experimental.

    Example (distributed worker):
        async def worker_job(task_id: str):
            store = RedisTaskStore(redis_url)
            task = await store.get_task(task_id)
            ctx = TaskContext(task=task, store=store)

            await ctx.update_status("Working...")
            result = await do_work()
            await ctx.complete(result)
    taskstorec                 C   s   || _ || _d| _d S )NF)_task_store
_cancelled)selfr   r	    r   Y/home/ubuntu/.local/lib/python3.10/site-packages/mcp/shared/experimental/tasks/context.py__init__$   s   
zTaskContext.__init__returnc                 C   s   | j jS )zThe task identifier.)r
   taskIdr   r   r   r   task_id)   s   zTaskContext.task_idc                 C      | j S )zThe current task state.)r
   r   r   r   r   r   .      zTaskContext.taskc                 C   r   )z(Whether cancellation has been requested.r   r   r   r   r   is_cancelled3   r   zTaskContext.is_cancelledNc                 C   s
   d| _ dS )z
        Request cancellation of this task.

        This sets is_cancelled=True. Task work should check this
        periodically and exit gracefully if set.
        TNr   r   r   r   r   request_cancellation8   s   
z TaskContext.request_cancellationmessagec                    s    | j j| j|dI dH | _dS )zn
        Update the task's status message.

        Args:
            message: The new status message
        )status_messageN)r   update_taskr   r
   )r   r   r   r   r   update_statusA   s
   zTaskContext.update_statusresultc                    s6   | j | j|I dH  | j j| jtdI dH | _dS )zv
        Mark the task as completed with the given result.

        Args:
            result: The task result
        N)status)r   store_resultr   r   r   r
   )r   r   r   r   r   completeM   s   zTaskContext.completeerrorc                    s"   | j j| jt|dI dH | _dS )zt
        Mark the task as failed with an error message.

        Args:
            error: The error message
        )r   r   N)r   r   r   r   r
   )r   r"   r   r   r   failZ   s   zTaskContext.fail)r   N)__name__
__module____qualname____doc__r   r   r   propertystrr   r   boolr   r   r   r   r!   r#   r   r   r   r   r      s    
	r   N)	r'   #mcp.shared.experimental.tasks.storer   	mcp.typesr   r   r   r   r   r   r   r   r   <module>   s    