The Context object gives you access to request-scoped utilities while a tool/resource/prompt is executing—primarily logging, progress, and request metadata.
Use get_context() to fetch the active context.
Note: get_context() only works inside an active MCP request handler. Calling it elsewhere raises LookupError.
Get Context
from dedalus_mcp import get_context, tool
@tool(description="Process data with logging")
async def process(data: str) -> str:
ctx = get_context()
await ctx.info("Processing", data={"bytes": len(data)})
return "done"
In tools (and dependency callables), parameters annotated as Context can be auto-injected by the framework—no need to call get_context() manually:
from dedalus_mcp import Context, tool
@tool(description="Same as process(), but Context is injected")
async def process_injected(data: str, ctx: Context) -> str:
await ctx.info("Processing", data={"bytes": len(data)})
return "done"
Available features
| Feature | API | What it does |
|---|
| Logging | ctx.debug(), ctx.info(), ctx.warning(), ctx.error(), ctx.log() | Send log messages to the client |
| Request metadata | ctx.request_id, ctx.session_id, ctx.progress_token | Identify the current request/session and progress token |
| Server/runtime access | ctx.server, ctx.runtime | Access runtime wiring (if present) |
| Auth context | ctx.auth_context | Access the auth context (if authorization is enabled) |
| Progress | ctx.report_progress(...), ctx.progress(...) | Emit progress notifications (if the client provided a progress token) |
| Dispatch (optional) | ctx.dispatch(...) | Send authenticated HTTP requests via the configured dispatch backend (if configured) |
| Connection resolution (optional) | ctx.resolve_client(...) | Resolve a connection handle into a client via the configured resolver (if configured) |
from dedalus_mcp import get_context, tool
@tool(description="Show request metadata")
async def my_tool() -> dict:
ctx = get_context()
return {
"request_id": ctx.request_id,
"session_id": ctx.session_id, # may be None (e.g. stdio)
"progress_token": ctx.progress_token, # may be None if client didn't request progress
}
Authorization context
If authorization is enabled, ctx.auth_context may be set; otherwise it’s None:
from dedalus_mcp import get_context, tool
@tool(description="Show auth context presence")
async def whoami() -> dict:
ctx = get_context()
if ctx.auth_context is None:
return {"user": "anonymous"}
return {"auth_context": "present"}
Progress example
import anyio
from dedalus_mcp import get_context, tool
@tool(description="Process files with progress reporting")
async def process_files(paths: list[str]) -> dict:
ctx = get_context()
processed = 0
async with ctx.progress(total=len(paths)) as tracker:
for _path in paths:
await anyio.sleep(0.01)
processed += 1
await tracker.advance(1)
return {"processed": processed}
Last modified on January 27, 2026