request_context
Experimental request context features.
This module provides the Experimental class which gives access to experimental features within a request context, such as task-augmented request handling.
WARNING: These APIs are experimental and may change without notice.
Experimental
dataclass
Experimental features context for task-augmented requests.
Provides helpers for validating task execution compatibility and running tasks with automatic lifecycle management.
WARNING: This API is experimental and may change without notice.
Source code in src/mcp/server/experimental/request_context.py
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 | |
client_supports_tasks
property
client_supports_tasks: bool
Check if the client declared task support.
validate_task_mode
validate_task_mode(
tool_task_mode: TaskExecutionMode | None,
*,
raise_error: bool = True
) -> ErrorData | None
Validate that the request is compatible with the tool's task execution mode.
Per MCP spec: - "required": Clients MUST invoke as a task. Server returns -32601 if not. - "forbidden" (or None): Clients MUST NOT invoke as a task. Server returns -32601 if they do. - "optional": Either is acceptable.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
tool_task_mode
|
TaskExecutionMode | None
|
The tool's execution.taskSupport value ("forbidden", "optional", "required", or None) |
required |
raise_error
|
bool
|
If True, raises MCPError on validation failure. If False, returns ErrorData. |
True
|
Returns:
| Type | Description |
|---|---|
ErrorData | None
|
None if valid, ErrorData if invalid and raise_error=False |
Raises:
| Type | Description |
|---|---|
MCPError
|
If invalid and raise_error=True |
Source code in src/mcp/server/experimental/request_context.py
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 | |
validate_for_tool
Validate that the request is compatible with the given tool.
Convenience wrapper around validate_task_mode that extracts the mode from a Tool.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
tool
|
Tool
|
The Tool definition |
required |
raise_error
|
bool
|
If True, raises MCPError on validation failure. |
True
|
Returns:
| Type | Description |
|---|---|
ErrorData | None
|
None if valid, ErrorData if invalid and raise_error=False |
Source code in src/mcp/server/experimental/request_context.py
95 96 97 98 99 100 101 102 103 104 105 106 107 108 | |
can_use_tool
can_use_tool(
tool_task_mode: TaskExecutionMode | None,
) -> bool
Check if this client can use a tool with the given task mode.
Useful for filtering tool lists or providing warnings. Returns False if the tool's task mode is "required" but the client doesn't support tasks.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
tool_task_mode
|
TaskExecutionMode | None
|
The tool's execution.taskSupport value |
required |
Returns:
| Type | Description |
|---|---|
bool
|
True if the client can use this tool, False otherwise |
Source code in src/mcp/server/experimental/request_context.py
110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 | |
run_task
async
run_task(
work: Callable[[ServerTaskContext], Awaitable[Result]],
*,
task_id: str | None = None,
model_immediate_response: str | None = None
) -> CreateTaskResult
Create a task, spawn background work, and return CreateTaskResult immediately.
This is the recommended way to handle task-augmented tool calls. It: 1. Creates a task in the store 2. Spawns the work function in a background task 3. Returns CreateTaskResult immediately
The work function receives a ServerTaskContext with: - elicit() for sending elicitation requests - create_message() for sampling requests - update_status() for progress updates - complete()/fail() for finishing the task
When work() returns a Result, the task is auto-completed with that result. If work() raises an exception, the task is auto-failed.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
work
|
Callable[[ServerTaskContext], Awaitable[Result]]
|
Async function that does the actual work |
required |
task_id
|
str | None
|
Optional task ID (generated if not provided) |
None
|
model_immediate_response
|
str | None
|
Optional string to include in _meta as io.modelcontextprotocol/model-immediate-response |
None
|
Returns:
| Type | Description |
|---|---|
CreateTaskResult
|
CreateTaskResult to return to the client |
Raises:
| Type | Description |
|---|---|
RuntimeError
|
If task support is not enabled or task_metadata is missing |
Example
async def handle_tool(ctx: RequestContext, params: CallToolRequestParams) -> CallToolResult:
async def work(task: ServerTaskContext) -> CallToolResult:
result = await task.elicit(
message="Are you sure?",
requested_schema={"type": "object", ...}
)
confirmed = result.content.get("confirm", False)
return CallToolResult(content=[TextContent(text="Done" if confirmed else "Cancelled")])
return await ctx.experimental.run_task(work)
WARNING: This API is experimental and may change without notice.
Source code in src/mcp/server/experimental/request_context.py
127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 | |