1b74b06753
Cut comment and docstring volume roughly in half across src, tests, examples, and docs_src: removed comments that restate the adjacent code, leftover development narration, section banners, and self-evident Args/Returns blocks, and compressed the remaining docstrings to a Google-style summary line plus only the detail that earns its place. Kept (and tightened) the load-bearing content: Raises sections, deprecation and version-availability notes, spec/RFC/issue references, why-comments for non-obvious decisions, and all coverage pragmas. The generated mcp_types.v* wire modules are untouched.
137 lines
4.9 KiB
Python
137 lines
4.9 KiB
Python
import pytest
|
|
from mcp_types import (
|
|
ClientCapabilities,
|
|
SamplingCapability,
|
|
SamplingMessage,
|
|
SamplingToolsCapability,
|
|
TextContent,
|
|
Tool,
|
|
ToolChoice,
|
|
ToolResultContent,
|
|
ToolUseContent,
|
|
)
|
|
|
|
from mcp.server.validation import (
|
|
check_sampling_tools_capability,
|
|
validate_sampling_tools,
|
|
validate_tool_use_result_messages,
|
|
)
|
|
from mcp.shared.exceptions import MCPError
|
|
|
|
|
|
def test_check_sampling_tools_capability_returns_false_when_caps_none() -> None:
|
|
assert check_sampling_tools_capability(None) is False
|
|
|
|
|
|
def test_check_sampling_tools_capability_returns_false_when_sampling_none() -> None:
|
|
caps = ClientCapabilities()
|
|
assert check_sampling_tools_capability(caps) is False
|
|
|
|
|
|
def test_check_sampling_tools_capability_returns_false_when_tools_none() -> None:
|
|
caps = ClientCapabilities(sampling=SamplingCapability())
|
|
assert check_sampling_tools_capability(caps) is False
|
|
|
|
|
|
def test_check_sampling_tools_capability_returns_true_when_tools_present() -> None:
|
|
caps = ClientCapabilities(sampling=SamplingCapability(tools=SamplingToolsCapability()))
|
|
assert check_sampling_tools_capability(caps) is True
|
|
|
|
|
|
def test_validate_sampling_tools_no_error_when_tools_none() -> None:
|
|
validate_sampling_tools(None, None, None)
|
|
|
|
|
|
def test_validate_sampling_tools_raises_when_tools_provided_but_no_capability() -> None:
|
|
tool = Tool(name="test", input_schema={"type": "object"})
|
|
with pytest.raises(MCPError) as exc_info:
|
|
validate_sampling_tools(None, [tool], None)
|
|
assert "sampling tools capability" in str(exc_info.value)
|
|
|
|
|
|
def test_validate_sampling_tools_raises_when_tool_choice_provided_but_no_capability() -> None:
|
|
with pytest.raises(MCPError) as exc_info:
|
|
validate_sampling_tools(None, None, ToolChoice(mode="auto"))
|
|
assert "sampling tools capability" in str(exc_info.value)
|
|
|
|
|
|
def test_validate_sampling_tools_no_error_when_capability_present() -> None:
|
|
caps = ClientCapabilities(sampling=SamplingCapability(tools=SamplingToolsCapability()))
|
|
tool = Tool(name="test", input_schema={"type": "object"})
|
|
validate_sampling_tools(caps, [tool], ToolChoice(mode="auto"))
|
|
|
|
|
|
def test_validate_tool_use_result_messages_no_error_for_empty_messages() -> None:
|
|
validate_tool_use_result_messages([])
|
|
|
|
|
|
def test_validate_tool_use_result_messages_no_error_for_simple_text_messages() -> None:
|
|
messages = [
|
|
SamplingMessage(role="user", content=TextContent(type="text", text="Hello")),
|
|
SamplingMessage(role="assistant", content=TextContent(type="text", text="Hi")),
|
|
]
|
|
validate_tool_use_result_messages(messages)
|
|
|
|
|
|
def test_validate_tool_use_result_messages_raises_when_tool_result_mixed_with_other_content() -> None:
|
|
messages = [
|
|
SamplingMessage(
|
|
role="user",
|
|
content=[
|
|
ToolResultContent(type="tool_result", tool_use_id="123"),
|
|
TextContent(type="text", text="also this"),
|
|
],
|
|
),
|
|
]
|
|
with pytest.raises(ValueError, match="only tool_result content"):
|
|
validate_tool_use_result_messages(messages)
|
|
|
|
|
|
def test_validate_tool_use_result_messages_raises_when_tool_result_without_previous_tool_use() -> None:
|
|
messages = [
|
|
SamplingMessage(
|
|
role="user",
|
|
content=ToolResultContent(type="tool_result", tool_use_id="123"),
|
|
),
|
|
]
|
|
with pytest.raises(ValueError, match="previous message containing tool_use"):
|
|
validate_tool_use_result_messages(messages)
|
|
|
|
|
|
def test_validate_tool_use_result_messages_raises_when_previous_message_has_no_tool_use() -> None:
|
|
messages = [
|
|
SamplingMessage(role="assistant", content=TextContent(type="text", text="just text")),
|
|
SamplingMessage(role="user", content=ToolResultContent(type="tool_result", tool_use_id="tool-1")),
|
|
]
|
|
with pytest.raises(ValueError, match="do not match any tool_use in the previous message"):
|
|
validate_tool_use_result_messages(messages)
|
|
|
|
|
|
def test_validate_tool_use_result_messages_raises_when_tool_result_ids_dont_match_tool_use() -> None:
|
|
messages = [
|
|
SamplingMessage(
|
|
role="assistant",
|
|
content=ToolUseContent(type="tool_use", id="tool-1", name="test", input={}),
|
|
),
|
|
SamplingMessage(
|
|
role="user",
|
|
content=ToolResultContent(type="tool_result", tool_use_id="tool-2"),
|
|
),
|
|
]
|
|
with pytest.raises(ValueError, match="do not match"):
|
|
validate_tool_use_result_messages(messages)
|
|
|
|
|
|
def test_validate_tool_use_result_messages_no_error_when_tool_result_matches_tool_use() -> None:
|
|
messages = [
|
|
SamplingMessage(
|
|
role="assistant",
|
|
content=ToolUseContent(type="tool_use", id="tool-1", name="test", input={}),
|
|
),
|
|
SamplingMessage(
|
|
role="user",
|
|
content=ToolResultContent(type="tool_result", tool_use_id="tool-1"),
|
|
),
|
|
]
|
|
validate_tool_use_result_messages(messages)
|