Access MCP capabilities like logging, progress, and resources within your MCP objects.
Context
object for this purpose.
Context
object provides a clean interface to access MCP features within your functions, including:
Context
. FlashMCP will automatically inject the context instance when your function is called.
Key Points:
ctx
, context
) doesn’t matter, only the type hint Context
is important.Context | None
) or use Annotated[]
and it will still work properly.Context | None=None
to avoid missing argument errors.New in version: 2.2.5
New in version: 2.2.5
New in version: 2.2.11
While the simplest way to access context is through function parameter injection as shown above, there are cases where you need to access the context in code that may not be easy to modify to accept a context parameter, or that is nested deeper within your function calls.
FlashMCP provides dependency functions that allow you to retrieve the active context from anywhere within a server request’s execution flow:
get_context
function should only be used within the context of a server request. Calling it outside of a request will raise a RuntimeError
.get_context
function is server-only and should not be used in client code.ctx.debug(message: str)
: Low-level details useful for debuggingctx.info(message: str)
: General information about executionctx.warning(message: str)
: Potential issues that didn’t prevent executionctx.error(message: str)
: Errors that occurred during executionctx.log(level: Literal["debug", "info", "warning", "error"], message: str, logger_name: str | None = None)
: Generic log method supporting custom logger namesctx.report_progress(progress: float, total: float | None = None)
progress
: Current progress value (e.g., 24)total
: Optional total value (e.g., 100). If provided, clients may interpret this as a percentage.progressToken
in the initial request. If the client doesn’t support progress reporting, these calls will have no effect.
ctx.read_resource(uri: str | AnyUrl) -> list[ReadResourceContents]
uri
: The resource URI to readcontent_list[0].content
and can be text or binary data depending on the resource.
New in version: 2.0.0
Request the client’s LLM to generate text based on provided messages. This is useful when your function needs to leverage the LLM’s capabilities to process data or generate responses.
ctx.sample(messages: str | list[str | SamplingMessage], system_prompt: str | None = None, temperature: float | None = None, max_tokens: int | None = None) -> TextContent | ImageContent
messages
: A string or list of strings/message objects to send to the LLMsystem_prompt
: Optional system prompt to guide the LLM’s behaviortemperature
: Optional sampling temperature (controls randomness)max_tokens
: Optional maximum number of tokens to generate (defaults to 512)ctx.request_id -> str
: Get the unique ID for the current MCP requestctx.client_id -> str | None
: Get the ID of the client making the request, if provided during initializationNew in version: 2.2.7
ctx.get_http_request()
method is deprecated and will be removed in a future version.
Please use the get_http_request()
dependency function instead.
See the HTTP Requests pattern for more details.ctx.FlashMCP -> FlashMCP
: Access the server instance the context belongs toctx.session
: Access the raw mcp.server.session.ServerSession
objectctx.request_context
: Access the raw mcp.shared.context.RequestContext
objectsession
or request_context
requires understanding the low-level MCP Python SDK and may be less stable than using the methods provided directly on the Context
object.