How do I keep Pydantic AI Montage tools inside their async lifetime?
A discovered tool can depend on a client that closes before the Agent ever uses it. Trace the asynchronous MCPToolset and FastMCP lifetime explicitly so resource errors remain distinguishable from account rejection, and do not repair one by widening access or hiding the failure. Make resource ownership explicit before handing tools to an Agent. Retaining a callable after its async client closes is a lifecycle defect, not evidence that the Montage account lost coverage or needs broader authorization.
Help me keep Pydantic AI Montage tools inside their async lifetime. Locate client creation, async context entry, toolset attachment and cleanup in the existing application. Inspect actual workspace status and let supported cleanup complete before returning. No writes.Map the lifetime boundary
Locate client creation, async context entry, toolset attachment and cleanup in the existing application. Identify any tool objects that escape after their owning connection closes. The lifetime inventory should identify objects returned from helpers that may still depend on async resources released before the caller uses them.
Follow the installed async contract
Use Pydantic AI and FastMCP documentation matching the project versions. Do not assume another framework’s connection ownership or context nesting applies to this toolset integration. A documented Pydantic AI pattern should not be replaced by a superficially similar context sequence from another framework without checking installed-version ownership rules.
Preserve auth during valid use
Keep the intended user’s approved client alive only for operations requiring it. An unbounded global session is not a safe substitute for correct request-scoped resource ownership. Correct connection ownership should preserve the requesting user boundary instead of avoiding cleanup by turning a private grant into a permanent application-wide session.
Test inside the real boundary
Invoke signals_status directly while the documented async resources are active. Distinguish a closed client, initialization error and server authorization rejection rather than converting all three into missing workspace coverage. A failure from closed resources should remain distinguishable from a server denial, allowing the next intervention to target lifecycle rather than credential replacement.
Confirm result and release
Inspect actual workspace status and let supported cleanup complete before returning. This verifies the direct lifecycle path only; a later Agent run still needs its own approved scope and operational evidence. Successful direct status and completed cleanup are narrower proof than a working future Agent run, which may select different tools and need separate scope.
Keep the boundaries clear.
- Correct connection ownership should preserve the requesting user boundary instead of avoiding cleanup by turning a private grant into a permanent application-wide session.
- Successful direct status and completed cleanup are narrower proof than a working future Agent run, which may select different tools and need separate scope.
Start with a read-only check: ask your agent to report actual source coverage before collecting records or exporting an audience.
Primary references
Documentation reviewed 2026-11-15. Product capabilities, client availability and plans can change. Third-party marks identify their products, not an affiliation.