Jump to content

Connect SuperML | Leeroopedia MCP: Equip your AI agents with best practices, code verification, and debugging knowledge. Powered by Leeroo — building Organizational Superintelligence. Contact us at founders@leeroo.com.

Principle:Microsoft Agent framework Agent Context Management

From Leeroopedia
Property Value
Principle Name Agent Context Management
SDK Microsoft Agent Framework
Repository Microsoft Agent Framework
Source Reference python/packages/core/agent_framework/_agents.py:L730-762
Import from agent_framework import Agent
Domains Agent_Architecture

Overview

Agent Context Management is the principle of managing agent lifecycle through Python's async context manager protocol (async with), ensuring deterministic cleanup of MCP tools and client connections when an agent goes out of scope.

Description

The Agent class implements the asynchronous context manager protocol via __aenter__ and __aexit__ dunder methods. This enables the async with Agent(...) as agent: pattern, which guarantees that all resources held by the agent -- including MCP server connections, chat client sessions, and any other AbstractAsyncContextManager dependencies -- are properly released when the block exits, whether through normal completion or an unhandled exception.

Resource Registration

During entry (__aenter__), the agent iterates over its client and all registered MCP tools. Any object that is an instance of AbstractAsyncContextManager is entered into an internal AsyncExitStack. This defers cleanup responsibility to the exit stack, which calls each context manager's __aexit__ in LIFO order during shutdown.

Deterministic Cleanup

During exit (__aexit__), the agent closes the AsyncExitStack via aclose(). This triggers orderly teardown of all registered context managers, releasing network connections, subprocess handles, and any other OS-level resources. Because cleanup is tied to the async with block boundary, resources are freed at a predictable point in the program's control flow rather than relying on garbage collection.

Why This Matters

Agents that use MCP tools typically hold long-lived connections to external tool servers (often subprocess-based). Without explicit cleanup, these connections may leak, leading to resource exhaustion. The context manager pattern makes correct resource management the default, and incorrect usage (forgetting to clean up) the exception.

Theoretical Basis

This principle applies the Resource Acquisition Is Initialization (RAII) pattern adapted for Python's async runtime. The AsyncExitStack from the standard library (contextlib) provides a composable mechanism for managing multiple async context managers with a single entry/exit lifecycle. The itertools.chain function is used to iterate the client and MCP tools uniformly, and isinstance checks against AbstractAsyncContextManager ensure only context-manager-compatible objects are registered.

The key invariant is: any resource that requires cleanup must implement AbstractAsyncContextManager, and the agent will automatically detect and manage it.

Related Pages

Sources

Type Name URL
Repo Microsoft Agent Framework https://github.com/microsoft/agent-framework

Page Connections

Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment