Wednesday, August 12, 2026

FastMCP renamed to MCPServer and Context object still required

While the Model Context Protocol (MCP) 2026-07-28 specification removed protocol-level sessions (Mcp-Session-Id and the initialize handshake) to make the protocol stateless, the Context object in FastMCP remains a core feature, though its underlying scope and behavior have changed.

How the Context Object Has Changed

From Session-Scoped to Request-Scoped

Previously, the Context object was tied directly to a persistent, long-lived client session. In the new stateless era, each individual MCP request receives a brand new, isolated Context object.

State Isolation

Any state you set dynamically inside a tool via ctx.set_state() is strictly scoped to that single, specific request execution. It does not leak or persist into subsequent tool calls or requests.

Protocol Era Coexistence

The Context object is backward-compatible. Inside your handlers, you can check ctx.request_context.protocol_version to determine if a call arrived via the legacy handshake era (session-backed) or the modern stateless era.

Ambient Cleanup

The ambient server.request_context global ContextVar has been entirely removed in favor of strict, clean parameter injection (ctx: Context) passed directly into your functions.

Why the Context Object Is Still Needed

The Context object does not exist just to manage sessions; it acts as your tool's gateway to operational sub-systems. It is still heavily used for:

  • Logging & Progress: Dispatching real-time ctx.info(), ctx.error(), and ctx.report_progress() updates back to the client UI.
  • Client Sampling: Allowing the server to request completions from the client LLM dynamically using ctx.sample().
  • Lifespan Management: Providing access to persistent backend resources initialized at startup (like database drivers or external APIs) via ctx.request_context.lifespan_context.
Note

As part of the same v2 stateless transition, the high-level framework class FastMCP has been renamed to MCPServer in the official Python SDK.


Migration Details
  • Old Import (v1): from mcp.server.fastmcp import FastMCP
  • New Import (v2): from mcp.server.mcpserver import MCPServer
  • Context Property: ctx.fastmcp changed to ctx.mcp_server

No comments:

Post a Comment

Loop Engineering: Designing the Systems That Prompt Your Agents

Loop Engineering For the last couple of years, the core skill in working with AI was writing a good prompt. You'd craft careful instru...