Skip to main content
When defining FastMCP tools, resources, resource templates, or prompts, your functions might need to interact with the underlying MCP session or access advanced server capabilities. FastMCP provides the Context object for this purpose.
You access Context through FastMCP’s dependency injection system. For other injectable values like HTTP requests, access tokens, and custom dependencies, see Dependency Injection.

What Is Context?

The Context object provides a clean interface to access MCP features within your functions, including:
  • Logging: Send debug, info, warning, and error messages back to the client
  • Progress Reporting: Update the client on the progress of long-running operations
  • Resource Access: List and read data from resources registered with the server
  • Prompt Access: List and retrieve prompts registered with the server
  • LLM Sampling: Request the client’s LLM to generate text based on provided messages
  • User Elicitation: Request structured input from users during tool execution
  • Session State: Store data that persists across requests within an MCP session
  • Session Visibility: Control which components are visible to the current session
  • Request Information: Access metadata about the current request
  • Server Access: When needed, access the underlying FastMCP server instance

Accessing the Context

The preferred way to access context is using the CurrentContext() dependency:
This works with tools, resources, and prompts:
Key Points:
  • Dependency parameters are automatically excluded from the MCP schema—clients never see them.
  • Context methods are async, so your function usually needs to be async as well.
  • Each MCP request receives a new context object. Context is scoped to a single request; state or data set in one request will not be available in subsequent requests.
  • Context is only available during a request; attempting to use context methods outside a request will raise errors.

Legacy Type-Hint Injection

For backwards compatibility, you can still access context by simply adding a parameter with the Context type hint. FastMCP will automatically inject the context instance:
This approach still works for tools, resources, and prompts. The parameter name doesn’t matter—only the Context type hint is important. The type hint can also be a union (Context | None) or use Annotated[].

Via get_context() Function

For code nested deeper within your function calls where passing context through parameters is inconvenient, use get_context() to retrieve the active context from anywhere within a request’s execution flow:
Important Notes:
  • The get_context() function should only be used within the context of a server request. Calling it outside of a request will raise a RuntimeError.
  • The get_context() function is server-only and should not be used in client code.

Context Capabilities

FastMCP provides several advanced capabilities through the context object. Each capability has dedicated documentation with comprehensive examples and best practices:

Logging

Send debug, info, warning, and error messages back to the MCP client for visibility into function execution.
See Server Logging for complete documentation and examples.

Client Elicitation

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.
See User Elicitation for detailed examples and supported response types.

LLM Sampling

Request the client’s LLM to generate text based on provided messages, useful for leveraging AI capabilities within your tools.
See LLM Sampling for comprehensive usage and advanced techniques.

Progress Reporting

Update clients on the progress of long-running operations, enabling progress indicators and better user experience.
See Progress Reporting for detailed patterns and examples.

Resource Access

List and read data from resources registered with your FastMCP server, allowing access to files, configuration, or dynamic content.
Method signatures:
  • ctx.list_resources() -> list[MCPResource]: Returns list of all available resources
  • ctx.read_resource(uri: str | AnyUrl) -> list[ReadResourceContents]: Returns a list of resource content parts

Prompt Access

List and retrieve prompts registered with your FastMCP server, allowing tools and middleware to discover and use available prompts programmatically.
Method signatures:
  • ctx.list_prompts() -> list[MCPPrompt]: Returns list of all available prompts
  • ctx.get_prompt(name: str, arguments: dict[str, Any] | None = None) -> GetPromptResult: Get a specific prompt with optional arguments

Session State

Store data that persists across multiple requests within the same MCP session. Session state is automatically keyed by the client’s session, ensuring isolation between different clients.
Each client session has its own isolated state—two different clients calling increment_counter will each have their own counter. Method signatures:
  • await ctx.set_state(key, value, *, serializable=True): Store a value in session state
  • await ctx.get_state(key): Retrieve a value (returns None if not found)
  • await ctx.delete_state(key): Remove a value from session state
State methods are async and require await. State expires after 1 day to prevent unbounded memory growth.

Non-Serializable Values

By default, state values must be JSON-serializable (dicts, lists, strings, numbers, etc.) so they can be persisted across requests. For non-serializable values like HTTP clients or database connections, pass serializable=False:
Values stored with serializable=False only live for the current MCP request (a single tool call, resource read, or prompt render). They will not be available in subsequent requests within the session.

Custom Storage Backends

By default, session state uses an in-memory store suitable for single-server deployments. For distributed or serverless deployments, provide a custom storage backend:
Any backend compatible with the py-key-value-aio AsyncKeyValue protocol works. See Storage Backends for more options including Redis, DynamoDB, and MongoDB.

State and Mounted Servers

Each FastMCP instance has its own session state store. When you mount() a child server, state set on the parent is not visible to tools on the child, and vice versa:
To share state across the mount boundary, pass the same store to both servers:
Alternatively, state set with serializable=False lives on the request context and is inherited by mounted children automatically — use it when the value is request-scoped and does not need to persist across tool calls.

State During Initialization

State set during on_initialize middleware persists to subsequent tool calls when using the same session object (STDIO, SSE, single-server HTTP). For distributed/serverless HTTP deployments where different machines handle init and tool calls, state is isolated by the mcp-session-id header.

Session Visibility

Tools can customize which components are visible to their current session using ctx.enable_components(), ctx.disable_components(), and ctx.reset_visibility(). These methods apply visibility rules that affect only the calling session, leaving other sessions unchanged. See Per-Session Visibility for complete documentation, filter criteria, and patterns like namespace activation.

Change Notifications

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’s notification methods:
These methods are primarily used internally by FastMCP’s automatic notification system and most users will not need to invoke them directly.

FastMCP Server

To access the underlying FastMCP server instance, you can use the ctx.fastmcp property:

Transport

The ctx.transport property indicates which transport is being used to run the server. This is useful when your tool needs to behave differently depending on whether the server is running over STDIO, SSE, or Streamable HTTP. For example, you might want to return shorter responses over STDIO or adjust timeout behavior based on transport characteristics. The transport type is set once when the server starts and remains constant for the server’s lifetime. It returns None when called outside of a server context (for example, in unit tests or when running code outside of an MCP request).
Property signature: ctx.transport -> Literal["stdio", "sse", "streamable-http"] | None

MCP Request

Access metadata about the current request and client.
Available Properties:
  • ctx.request_id -> str: Get the unique ID for the current MCP request
  • ctx.client_id -> str | None: Get the ID of the client making the request, if provided during initialization
  • ctx.session_id -> str: Get the MCP session ID for session-based data sharing. Raises RuntimeError if the MCP session is not yet established.

Request Context Availability

The ctx.request_context property provides access to the underlying MCP request context, but returns None when the MCP session has not been established yet. This typically occurs:
  • During middleware execution in the on_request hook before the MCP handshake completes
  • During the initialization phase of client connections
The MCP request context is distinct from the HTTP request. For HTTP transports, HTTP request data may be available even when the MCP session is not yet established. To safely access the request context in situations where it may not be available:
For HTTP request access that works regardless of MCP session availability (when using HTTP transports), use the HTTP request helpers like get_http_request() and get_http_headers().

Client Metadata

Clients can send contextual information with their requests using the meta parameter. This metadata is accessible through ctx.request_context.meta and is available for all MCP operations (tools, resources, prompts). The meta field is None when clients don’t provide metadata. When provided, metadata is accessible via attribute access (e.g., meta.user_id) rather than dictionary access. The structure of metadata is determined by the client making the request.
The MCP request is part of the low-level MCP SDK and intended for advanced use cases. Most users will not need to use it directly.