Access MCP capabilities like logging, progress, and resources within your MCP objects.
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.
The Context
object provides a clean interface to access MCP features within your functions, including:
To use the context object within any of your functions, simply add a parameter to your function signature and type-hint it as 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:
Important Notes:
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.FastMCP provides several advanced capabilities through the context object. Each capability has dedicated documentation with comprehensive examples and best practices:
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.
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.
See User Elicitation for detailed examples and supported response types.
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.
See LLM Sampling for comprehensive usage and advanced techniques.
Update clients on the progress of long-running operations, enabling progress indicators and better user experience.
See Progress Reporting for detailed patterns and examples.
Read data from resources registered with your FastMCP server, allowing access to files, configuration, or dynamic content.
Method signature:
ctx.read_resource(uri: str | AnyUrl) -> list[ReadResourceContents]
: Returns a list of resource content partsNew 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:
These methods are primarily used internally by FastMCP’s automatic notification system and most users will not need to invoke them directly.
To access the underlying FastMCP server instance, you can use the ctx.fastmcp
property:
Access metadata about the current request and client.
Available Properties:
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)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.
Access MCP capabilities like logging, progress, and resources within your MCP objects.
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.
The Context
object provides a clean interface to access MCP features within your functions, including:
To use the context object within any of your functions, simply add a parameter to your function signature and type-hint it as 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:
Important Notes:
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.FastMCP provides several advanced capabilities through the context object. Each capability has dedicated documentation with comprehensive examples and best practices:
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.
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.
See User Elicitation for detailed examples and supported response types.
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.
See LLM Sampling for comprehensive usage and advanced techniques.
Update clients on the progress of long-running operations, enabling progress indicators and better user experience.
See Progress Reporting for detailed patterns and examples.
Read data from resources registered with your FastMCP server, allowing access to files, configuration, or dynamic content.
Method signature:
ctx.read_resource(uri: str | AnyUrl) -> list[ReadResourceContents]
: Returns a list of resource content partsNew 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:
These methods are primarily used internally by FastMCP’s automatic notification system and most users will not need to invoke them directly.
To access the underlying FastMCP server instance, you can use the ctx.fastmcp
property:
Access metadata about the current request and client.
Available Properties:
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)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.