View the source code for this module on GitHub: https://github.com/mcp-use/mcp-use/blob/main/libraries/python/mcp_use/server/context.py
Context
from mcp_use.server.context import Context
method init
ParametersSignatureRequestContext[ServerSessionT, LifespanContextT, RequestT] | Nonedefault:"None"Parameter valueFastMCP | Nonedefault:"None"Parameter valueAnyrequiredParameter value
def __init__(request_context: RequestContext[ServerSessionT, LifespanContextT, RequestT] | None = None, fastmcp: FastMCP | None = None, kwargs: Any):
method list_roots
Request the list of roots from the client.Roots represent directories or files that the client has access to
and wants to make available to the server.Example:@mcp.tool()
async def analyze_workspace(ctx: Context) -> str:
roots = await ctx.list_roots()
if not roots:
return "No roots provided by client"
return f"Client has \{len(roots)\} root(s): \{[str(r.uri) for r in roots]\}"
Signaturelist[Root]A list of Root objects, each with a ‘uri’ (file:// URI) and optional ‘name’ field.
def list_roots():
method sample
Request a client-side LLM sampling invocation.ParametersReturnsstr | SamplingMessage | Sequence[SamplingMessage | str]requiredThe sampling prompt(s). Accepts a simple string, a singleintdefault:"512"Maximum number of tokens to request from the client LLM.str | Nonedefault:"None"Optional system instructions to prepend.bool | Nonedefault:"None"Whether to ask the client to include MCP context.float | Nonedefault:"None"Optional sampling temperature.Sequence[str] | Nonedefault:"None"Optional stop sequences to pass to the client LLM.dict[str, Any] | Nonedefault:"None"Optional provider-specific metadata.ModelPreferences | Nonedefault:"None"Optional hint about the desired model.booldefault:"False"WhenTruereturns the fullCreateMessageResultinstead of
SignatureCreateMessageResult
def sample(
messages: str | SamplingMessage | Sequence[SamplingMessage | str],
max_tokens: int = 512,
system_prompt: str | None = None,
include_context: bool | None = None,
temperature: float | None = None,
stop_sequences: Sequence[str] | None = None,
metadata: dict[str,
Any] | None = None,
model_preferences: ModelPreferences | None = None,
raw: bool = False
):
method send_prompt_list_changed
Notify the client that the prompt list changed.Signaturedef send_prompt_list_changed():
method send_resource_list_changed
Notify the client that the resource list changed.Signaturedef send_resource_list_changed():
method send_tool_list_changed
Notify the client that the tool list changed.Signaturedef send_tool_list_changed():