[v1.x] Scope experimental tasks to the session that created them (#2720)
Some checks failed
Main branch checks / checks (push) Has been cancelled

This commit is contained in:
Max 2026-05-29 18:05:58 +01:00 • committed by GitHub
commit 62137874ff
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -3,6 +3,7 @@
import pytest
from mcp.server.experimental.request_context import Experimental
from mcp.server.experimental.task_context import ServerTaskContext
from mcp.shared.exceptions import McpError
from mcp.types import (
METHOD_NOT_FOUND,
@ -11,6 +12,7 @@ from mcp.types import (
TASK_REQUIRED,
ClientCapabilities,
ClientTasksCapability,
Result,
TaskMetadata,
Tool,
ToolExecution,
@ -164,3 +166,30 @@ def test_can_use_tool_forbidden_without_task_support() -> None:
def test_can_use_tool_none_without_task_support() -> None:
exp = Experimental(_client_capabilities=ClientCapabilities())
assert exp.can_use_tool(None) is True
@pytest.mark.anyio
async def test_run_task_with_an_explicit_task_id_emits_a_deprecation_warning() -> None:
"""An explicitly provided task ID is not associated with the creating session, so passing one is deprecated."""
exp = Experimental(task_metadata=TaskMetadata(ttl=60000))
async def work(task: ServerTaskContext) -> Result:
raise AssertionError("unreachable") # pragma: no cover
with pytest.warns(DeprecationWarning, match="not associated with the session"):
# Task support is not configured, so the call fails after the
# deprecated argument has been reported.
with pytest.raises(RuntimeError, match="Task support not enabled"):
# The deliberate use of the deprecated overload is the point of this test.
await exp.run_task(work, task_id="explicitly-chosen") # pyright: ignore[reportDeprecated]
@pytest.mark.anyio
async def test_run_task_without_a_task_id_does_not_warn() -> None:
exp = Experimental(task_metadata=TaskMetadata(ttl=60000))
async def work(task: ServerTaskContext) -> Result:
raise AssertionError("unreachable") # pragma: no cover
with pytest.raises(RuntimeError, match="Task support not enabled"):
await exp.run_task(work)