Documentation Index
Fetch the complete documentation index at: https://docs.mcp-use.com/llms.txt
Use this file to discover all available pages before exploring further.
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
ParametersSignatureParameter valueParameter valueParameter 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]\}"
SignatureA 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.ParametersReturnsThe sampling prompt(s). Accepts a simple string, a singleMaximum number of tokens to request from the client LLM.Optional system instructions to prepend.Whether to ask the client to include MCP context.Optional sampling temperature.Optional stop sequences to pass to the client LLM.Optional provider-specific metadata.Optional hint about the desired model.WhenTruereturns the fullCreateMessageResultinstead of
Signature
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():