Access the Context Object
Reach the request context inside a tool.
Access the Context Object is a free MCP Academy lesson on CoddyKit — lesson 3 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the MCP Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
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 " + nameReach Your Shared State
Drill into ctx.request_context.lifespan_context to get the exact object your lifespan yielded at startup.
app = ctx.request_context.lifespan_context
rows = await app.db.query("...")Use the Live Connection
Now your tool runs work against the shared pool instead of opening a brand new connection on every call.
Log Through the Context
Call ctx.info to send a log line to the client, which is far more useful than a hidden print on the server.
await ctx.info("loading user " + str(user_id))Report Progress Too
For slow work, ctx.report_progress streams percent-done updates so the host can show the user a moving bar.
await ctx.report_progress(progress=3, total=10)It's Injected, Not Passed
The model never fills in ctx. FastMCP recognizes the type hint and supplies it, so it stays out of your tool schema.
Async Tools Love Context
Most context helpers are awaitable, so define context-using tools as async functions to call them cleanly.
Name It Anything
The parameter name is up to you; the type hint is what matters. People often just call it ctx by convention.
One Context per Request
Each tool call gets its own request context, but they all point at the same long-lived lifespan state underneath.
Read Request Metadata
The context also exposes request metadata, so a tool can inspect details about the specific call it is handling.
Quick Check
How do you get the Context object inside a FastMCP tool?
Recap: The Context Object
You reach shared state via ctx.request_context.lifespan_context and use ctx for logging and progress. Next: shutting down cleanly. 🔌
Frequently asked questions
Is the “Access the Context Object” lesson free?
Yes — the full text of “Access the Context Object” is free to read here on the web, and the MCP Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the MCP Academy course, upgrade to CoddyKit PRO.
What will I learn in “Access the Context Object”?
Reach the request context inside a tool. You practise MCP Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start MCP Academy?
No prior experience is required. MCP Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 3 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Access the Context Object” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this MCP Academy lesson?
Yes. Every MCP Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- The Lifespan Startup Hook
- Share State via App Context
- Access the Context Object
- Clean Shutdown & Resource Release