|
| 1 | +"""Tests for the RequestContext and Experimental classes.""" |
| 2 | + |
| 3 | +import pytest |
| 4 | + |
| 5 | +from mcp.shared.context import Experimental |
| 6 | +from mcp.shared.exceptions import McpError |
| 7 | +from mcp.types import ( |
| 8 | + METHOD_NOT_FOUND, |
| 9 | + ClientCapabilities, |
| 10 | + ClientTasksCapability, |
| 11 | + TaskMetadata, |
| 12 | + Tool, |
| 13 | + ToolExecution, |
| 14 | +) |
| 15 | + |
| 16 | +# --- Experimental.is_task --- |
| 17 | + |
| 18 | + |
| 19 | +def test_is_task_true_when_metadata_present() -> None: |
| 20 | + exp = Experimental(task_metadata=TaskMetadata(ttl=60000)) |
| 21 | + assert exp.is_task is True |
| 22 | + |
| 23 | + |
| 24 | +def test_is_task_false_when_no_metadata() -> None: |
| 25 | + exp = Experimental(task_metadata=None) |
| 26 | + assert exp.is_task is False |
| 27 | + |
| 28 | + |
| 29 | +# --- Experimental.client_supports_tasks --- |
| 30 | + |
| 31 | + |
| 32 | +def test_client_supports_tasks_true() -> None: |
| 33 | + exp = Experimental(_client_capabilities=ClientCapabilities(tasks=ClientTasksCapability())) |
| 34 | + assert exp.client_supports_tasks is True |
| 35 | + |
| 36 | + |
| 37 | +def test_client_supports_tasks_false_no_tasks() -> None: |
| 38 | + exp = Experimental(_client_capabilities=ClientCapabilities()) |
| 39 | + assert exp.client_supports_tasks is False |
| 40 | + |
| 41 | + |
| 42 | +def test_client_supports_tasks_false_no_capabilities() -> None: |
| 43 | + exp = Experimental(_client_capabilities=None) |
| 44 | + assert exp.client_supports_tasks is False |
| 45 | + |
| 46 | + |
| 47 | +# --- Experimental.validate_task_mode --- |
| 48 | + |
| 49 | + |
| 50 | +def test_validate_task_mode_always_with_task_is_valid() -> None: |
| 51 | + exp = Experimental(task_metadata=TaskMetadata(ttl=60000)) |
| 52 | + error = exp.validate_task_mode("always", raise_error=False) |
| 53 | + assert error is None |
| 54 | + |
| 55 | + |
| 56 | +def test_validate_task_mode_always_without_task_returns_error() -> None: |
| 57 | + exp = Experimental(task_metadata=None) |
| 58 | + error = exp.validate_task_mode("always", raise_error=False) |
| 59 | + assert error is not None |
| 60 | + assert error.code == METHOD_NOT_FOUND |
| 61 | + assert "requires task-augmented" in error.message |
| 62 | + |
| 63 | + |
| 64 | +def test_validate_task_mode_always_without_task_raises_by_default() -> None: |
| 65 | + exp = Experimental(task_metadata=None) |
| 66 | + with pytest.raises(McpError) as exc_info: |
| 67 | + exp.validate_task_mode("always") |
| 68 | + assert exc_info.value.error.code == METHOD_NOT_FOUND |
| 69 | + |
| 70 | + |
| 71 | +def test_validate_task_mode_never_without_task_is_valid() -> None: |
| 72 | + exp = Experimental(task_metadata=None) |
| 73 | + error = exp.validate_task_mode("never", raise_error=False) |
| 74 | + assert error is None |
| 75 | + |
| 76 | + |
| 77 | +def test_validate_task_mode_never_with_task_returns_error() -> None: |
| 78 | + exp = Experimental(task_metadata=TaskMetadata(ttl=60000)) |
| 79 | + error = exp.validate_task_mode("never", raise_error=False) |
| 80 | + assert error is not None |
| 81 | + assert error.code == METHOD_NOT_FOUND |
| 82 | + assert "does not support task-augmented" in error.message |
| 83 | + |
| 84 | + |
| 85 | +def test_validate_task_mode_never_with_task_raises_by_default() -> None: |
| 86 | + exp = Experimental(task_metadata=TaskMetadata(ttl=60000)) |
| 87 | + with pytest.raises(McpError) as exc_info: |
| 88 | + exp.validate_task_mode("never") |
| 89 | + assert exc_info.value.error.code == METHOD_NOT_FOUND |
| 90 | + |
| 91 | + |
| 92 | +def test_validate_task_mode_none_treated_as_never() -> None: |
| 93 | + exp = Experimental(task_metadata=TaskMetadata(ttl=60000)) |
| 94 | + error = exp.validate_task_mode(None, raise_error=False) |
| 95 | + assert error is not None |
| 96 | + assert "does not support task-augmented" in error.message |
| 97 | + |
| 98 | + |
| 99 | +def test_validate_task_mode_optional_with_task_is_valid() -> None: |
| 100 | + exp = Experimental(task_metadata=TaskMetadata(ttl=60000)) |
| 101 | + error = exp.validate_task_mode("optional", raise_error=False) |
| 102 | + assert error is None |
| 103 | + |
| 104 | + |
| 105 | +def test_validate_task_mode_optional_without_task_is_valid() -> None: |
| 106 | + exp = Experimental(task_metadata=None) |
| 107 | + error = exp.validate_task_mode("optional", raise_error=False) |
| 108 | + assert error is None |
| 109 | + |
| 110 | + |
| 111 | +# --- Experimental.validate_for_tool --- |
| 112 | + |
| 113 | + |
| 114 | +def test_validate_for_tool_with_execution_always() -> None: |
| 115 | + exp = Experimental(task_metadata=None) |
| 116 | + tool = Tool( |
| 117 | + name="test", |
| 118 | + description="test", |
| 119 | + inputSchema={"type": "object"}, |
| 120 | + execution=ToolExecution(task="always"), |
| 121 | + ) |
| 122 | + error = exp.validate_for_tool(tool, raise_error=False) |
| 123 | + assert error is not None |
| 124 | + assert "requires task-augmented" in error.message |
| 125 | + |
| 126 | + |
| 127 | +def test_validate_for_tool_without_execution() -> None: |
| 128 | + exp = Experimental(task_metadata=TaskMetadata(ttl=60000)) |
| 129 | + tool = Tool( |
| 130 | + name="test", |
| 131 | + description="test", |
| 132 | + inputSchema={"type": "object"}, |
| 133 | + execution=None, |
| 134 | + ) |
| 135 | + error = exp.validate_for_tool(tool, raise_error=False) |
| 136 | + assert error is not None |
| 137 | + assert "does not support task-augmented" in error.message |
| 138 | + |
| 139 | + |
| 140 | +def test_validate_for_tool_optional_with_task() -> None: |
| 141 | + exp = Experimental(task_metadata=TaskMetadata(ttl=60000)) |
| 142 | + tool = Tool( |
| 143 | + name="test", |
| 144 | + description="test", |
| 145 | + inputSchema={"type": "object"}, |
| 146 | + execution=ToolExecution(task="optional"), |
| 147 | + ) |
| 148 | + error = exp.validate_for_tool(tool, raise_error=False) |
| 149 | + assert error is None |
| 150 | + |
| 151 | + |
| 152 | +# --- Experimental.can_use_tool --- |
| 153 | + |
| 154 | + |
| 155 | +def test_can_use_tool_always_with_task_support() -> None: |
| 156 | + exp = Experimental(_client_capabilities=ClientCapabilities(tasks=ClientTasksCapability())) |
| 157 | + assert exp.can_use_tool("always") is True |
| 158 | + |
| 159 | + |
| 160 | +def test_can_use_tool_always_without_task_support() -> None: |
| 161 | + exp = Experimental(_client_capabilities=ClientCapabilities()) |
| 162 | + assert exp.can_use_tool("always") is False |
| 163 | + |
| 164 | + |
| 165 | +def test_can_use_tool_optional_without_task_support() -> None: |
| 166 | + exp = Experimental(_client_capabilities=ClientCapabilities()) |
| 167 | + assert exp.can_use_tool("optional") is True |
| 168 | + |
| 169 | + |
| 170 | +def test_can_use_tool_never_without_task_support() -> None: |
| 171 | + exp = Experimental(_client_capabilities=ClientCapabilities()) |
| 172 | + assert exp.can_use_tool("never") is True |
| 173 | + |
| 174 | + |
| 175 | +def test_can_use_tool_none_without_task_support() -> None: |
| 176 | + exp = Experimental(_client_capabilities=ClientCapabilities()) |
| 177 | + assert exp.can_use_tool(None) is True |
0 commit comments