MCP Academy · Lesson

Access the Context Object

Reach the request context inside a tool.

Meet the Context Object

The Context object is your tool's gateway to the request: it carries shared state plus helpers for logging and progress.

Ask for It by Type

Add a parameter type-hinted as Context and FastMCP injects it automatically. Clients never see this argument.

from mcp.server.fastmcp import Context

@mcp.tool()
async def greet(name: str, ctx: Context) -> str:
    return "hi " + name

All lessons in this course

  1. The Lifespan Startup Hook
  2. Share State via App Context
  3. Access the Context Object
  4. Clean Shutdown & Resource Release
← Back to MCP Academy