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
. FastMCP 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.
FastMCP 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.New in version: 2.10.0
Request structured input from clients during tool execution, enabling interactive workflows and progressive disclosure. This is a new feature in the 6/18/2025 MCP spec.
New in version: 2.0.0
Request the client’s LLM to generate text based on provided messages, useful for leveraging AI capabilities within your tools.
ctx.read_resource(uri: str | AnyUrl) -> list[ReadResourceContents]
: Returns a list of resource content partsNew in version: 2.11.0
Store and share data across middleware and tool calls within a request. Context objects maintain a state dictionary that’s especially useful for passing information from middleware to your tools.
To store a value in the context state, use ctx.set_state(key, value)
. To retrieve a value, use ctx.get_state(key)
.
This simplified example shows how to use MCP middleware to store user info in the context state, and how to access that state in a tool:
ctx.set_state(key: str, value: Any) -> None
: Store a value in the context statectx.get_state(key: str) -> Any
: Retrieve a value from the context state (returns None if not found)New in version: 2.9.1
FastMCP automatically sends list change notifications when components (such as tools, resources, or prompts) are added, removed, enabled, or disabled. In rare cases where you need to manually trigger these notifications, you can use the context methods:
ctx.fastmcp
property:
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 initializationctx.session_id -> str | None
: Get the MCP session ID for session-based data sharing (HTTP transports only)New in version: 2.2.11
The recommended way to access the current HTTP request is through the get_http_request()
dependency function:
New in version: 2.2.11
If you only need request headers and want to avoid potential errors, you can use the get_http_headers()
helper:
get_http_headers()
excludes problematic headers like host
and content-length
. To include all headers, use get_http_headers(include_all=True)
.
New in version: 2.11.0
When using authentication with your FastMCP server, you can access the authenticated user’s access token information using the get_access_token()
dependency function:
client_id
or subject from token claimsclaims
field contains all the data from the original token (JWT claims for JWT tokens, or custom data for other token types):