
    kKjX                    l    d Z ddlmZ ddlmZmZmZ ddlmZm	Z	m
Z
 ddlmZmZ  ed      Z	 	 	 	 d	dZy)
zALifespan utilities for combining async context manager lifespans.    )annotations)AsyncIteratorCallableMapping)AbstractAsyncContextManagerAsyncExitStackasynccontextmanager)AnyTypeVarAppTc                 &     t         d fd       }|S )a  Combine multiple lifespans into a single lifespan.

    Useful when mounting FastMCP into FastAPI and you need to run
    both your app's lifespan and the MCP server's lifespan.

    Works with both FastAPI-style lifespans (yield None) and FastMCP-style
    lifespans (yield dict). Results are merged; later lifespans override
    earlier ones on key conflicts.

    Lifespans are entered in order and exited in reverse order (LIFO).

    Example:
        ```python
        from fastmcp import FastMCP
        from fastmcp.utilities.lifespan import combine_lifespans
        from fastapi import FastAPI

        mcp = FastMCP("Tools")
        mcp_app = mcp.http_app()

        app = FastAPI(lifespan=combine_lifespans(app_lifespan, mcp_app.lifespan))
        app.mount("/mcp", mcp_app)  # MCP endpoint at /mcp
        ```

    Args:
        *lifespans: Lifespan context manager factories to combine.

    Returns:
        A combined lifespan context manager factory.
    c                 K   i }t               4 d {   }D ]5  }|j                   ||              d {   }|%|j                  |       7 | d d d       d {    y 7 T7 27 	# 1 d {  7  sw Y   y xY ww)N)r   enter_async_contextupdate)appmergedstacklsresult	lifespanss        k/Users/ahmed/devFolder/Ultron/claude-voice/.venv/lib/python3.12/site-packages/fastmcp/utilities/lifespan.pycombinedz#combine_lifespans.<locals>.combined.   sp     !#!##u$88CAA%MM&)   L $##A $###sZ   BA)B A/A+A/ A/B#A-$B+A/-B/B5A86B=B)r   r   returnzAsyncIterator[dict[str, Any]])r	   )r   r   s   ` r   combine_lifespansr      s    D   O    N)r   zGCallable[[AppT], AbstractAsyncContextManager[Mapping[str, Any] | None]]r   z=Callable[[AppT], AbstractAsyncContextManager[dict[str, Any]]])__doc__
__future__r   collections.abcr   r   r   
contextlibr   r   r	   typingr
   r   r   r    r   r   <module>r"      s8    G " < < W W v,W,B,r   