Skip to content

Async-generator tools iterate after context-manager dependencies have closed #5399

Closed

Description

@mikamikasuki

What happened?

An async-generator tool that uses an injected context-manager dependency gets the dependency closed before its first iteration. The call returns an MCP error (I/O operation on closed file) instead of payload. A regular async tool with the same dependency works.

Minimal reproduction

import asyncio
import io
from collections.abc import AsyncIterator
from contextlib import asynccontextmanager
from fastmcp import Client, FastMCP
from fastmcp.dependencies import Depends

@asynccontextmanager
async def open_stream():
    stream = io.StringIO("payload")
    try:
        yield stream
    finally:
        stream.close()

server = FastMCP()

@server.tool
async def read_stream(stream=Depends(open_stream)) -> AsyncIterator[str]:
    yield stream.read()

async def main():
    async with Client(server) as client:
        result = await client.call_tool_mcp("read_stream", {})
        print(result.is_error, result.content[0].text)

asyncio.run(main())

Expected: the result contains payload. Actual: the call returns I/O operation on closed file.

Environment

FastMCP commit c3b81662d31aa003a3451a7dcc7e41d38f3baef8, MCP 2.0.0, Python 3.12.15, macOS 15.0.1 arm64. Reproduced with the in-memory client; no external services are involved.

Activity

  1. added
    bug
    on Oct 3, 2026
  2. DawnofGenX commented on Oct 4, 2026

    @DawnofGenX

    I'd like to work on this.

    The issue is that async-generator tools iterate after context-manager dependencies have closed. I'll investigate the root cause in the tool execution flow and add a regression test.

    I'll start by reproducing the issue with a minimal async-generator tool that uses a context-manager dependency.

  3. mikamikasuki commented on Oct 4, 2026

    @mikamikasuki
    ContributorAuthor

    PR #5438 consolidates #5399 and #5400. It carries forward the production fix and two tests from #5402 and adds broader lifecycle regression coverage. The issue-link check is failing because I am not assigned to either issue. I saw the recent comment expressing interest in #5399, so I will not self-assign or claim it. Could a maintainer advise whether to assign me to #5400 and keep this PR focused there, transfer the additional tests to #5402, or close one PR? I can follow whichever path avoids duplicating work. This is a Codex-assisted change; the PR body discloses that assistance.

  4. added a commit that references this issue on Oct 5, 2026
    e51b1f4
  5. DawnofGenX commented on Oct 7, 2026

    @DawnofGenX

    Thanks for the pointer — I see #5438 consolidated this along with #5399/#5400/#5402. No action needed from me; I'm not pursuing this one.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bug

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions