|
| 1 | +"""Dynamic callback tools: MCP spec compliance, tool naming, output summaries. |
| 2 | +
|
| 3 | +Verifies that generated tools conform to the MCP 2025-11-25 specification |
| 4 | +and Dash-specific conventions. Focuses on shape/structure, tool-name |
| 5 | +sanitization, and ``_OUTPUT_SEMANTICS`` fallback summaries; input-schema |
| 6 | +values are covered by ``test_mcp_input_schemas``. |
| 7 | +
|
| 8 | +Reference: https://modelcontextprotocol.io/specification/2025-11-25/server/tools |
| 9 | +""" |
| 10 | + |
| 11 | +import re |
| 12 | +from unittest.mock import Mock |
| 13 | + |
| 14 | +from dash.mcp.primitives.tools.callback_adapter_collection import ( |
| 15 | + CallbackAdapterCollection, |
| 16 | +) |
| 17 | +from dash.mcp.primitives.tools.descriptions import build_tool_description |
| 18 | + |
| 19 | +from tests.unit.mcp.conftest import ( |
| 20 | + _make_app, |
| 21 | + _tools_list, |
| 22 | +) |
| 23 | + |
| 24 | + |
| 25 | +# --------------------------------------------------------------------------- |
| 26 | +# Helpers |
| 27 | +# --------------------------------------------------------------------------- |
| 28 | + |
| 29 | +_TOOL_NAME_RE = re.compile(r"^[A-Za-z0-9_\-.]+$") |
| 30 | + |
| 31 | + |
| 32 | +def _adapter_with_outputs(outputs, docstring=None): |
| 33 | + adapter = Mock() |
| 34 | + adapter.outputs = outputs |
| 35 | + adapter._docstring = docstring |
| 36 | + return adapter |
| 37 | + |
| 38 | + |
| 39 | +def _out(comp_id, prop, comp_type=None): |
| 40 | + return { |
| 41 | + "id_and_prop": f"{comp_id}.{prop}", |
| 42 | + "component_id": comp_id, |
| 43 | + "property": prop, |
| 44 | + "component_type": comp_type, |
| 45 | + "initial_value": None, |
| 46 | + } |
| 47 | + |
| 48 | + |
| 49 | +# --------------------------------------------------------------------------- |
| 50 | +# MCP spec compliance — every generated tool must satisfy the spec |
| 51 | +# --------------------------------------------------------------------------- |
| 52 | + |
| 53 | + |
| 54 | +def test_mcptc001_all_tools_conform_to_mcp_spec(): |
| 55 | + tools = _tools_list(_make_app()) |
| 56 | + names = [t.name for t in tools] |
| 57 | + |
| 58 | + assert len(names) == len(set(names)), f"Duplicate tool names: {names}" |
| 59 | + |
| 60 | + for tool in tools: |
| 61 | + assert tool.name |
| 62 | + assert tool.inputSchema |
| 63 | + assert 1 <= len(tool.name) <= 128 |
| 64 | + assert _TOOL_NAME_RE.match(tool.name), f"Invalid tool name: {tool.name}" |
| 65 | + |
| 66 | + schema = tool.inputSchema |
| 67 | + assert isinstance(schema, dict) |
| 68 | + assert schema.get("type") == "object" |
| 69 | + assert isinstance(schema.get("properties", {}), dict) |
| 70 | + |
| 71 | + required = set(schema.get("required", [])) |
| 72 | + props = set(schema.get("properties", {}).keys()) |
| 73 | + assert ( |
| 74 | + required <= props |
| 75 | + ), f"{tool.name}: required {required - props} not in properties" |
| 76 | + |
| 77 | + |
| 78 | +# --------------------------------------------------------------------------- |
| 79 | +# Built-in tools |
| 80 | +# --------------------------------------------------------------------------- |
| 81 | + |
| 82 | + |
| 83 | +def test_mcptc002_query_component_always_present(): |
| 84 | + names = {t.name for t in _tools_list(_make_app())} |
| 85 | + assert "get_dash_component" in names |
| 86 | + |
| 87 | + |
| 88 | +def test_mcptc003_query_component_has_required_params(): |
| 89 | + tool = next(t for t in _tools_list(_make_app()) if t.name == "get_dash_component") |
| 90 | + assert "component_id" in tool.inputSchema["properties"] |
| 91 | + assert "property" in tool.inputSchema["properties"] |
| 92 | + assert set(tool.inputSchema.get("required", [])) == {"component_id"} |
| 93 | + |
| 94 | + |
| 95 | +# --------------------------------------------------------------------------- |
| 96 | +# Tool-name sanitization (CallbackAdapterCollection._sanitize_name) |
| 97 | +# --------------------------------------------------------------------------- |
| 98 | + |
| 99 | + |
| 100 | +def test_mcptc004_sanitize_simple_name(): |
| 101 | + assert CallbackAdapterCollection._sanitize_name("update_output") == "update_output" |
| 102 | + |
| 103 | + |
| 104 | +def test_mcptc005_sanitize_special_characters_replaced(): |
| 105 | + assert CallbackAdapterCollection._sanitize_name("my-func.name") == "my_func_name" |
| 106 | + |
| 107 | + |
| 108 | +def test_mcptc006_sanitize_leading_digit(): |
| 109 | + assert CallbackAdapterCollection._sanitize_name("123func") == "cb_123func" |
| 110 | + |
| 111 | + |
| 112 | +def test_mcptc007_sanitize_empty_name(): |
| 113 | + assert CallbackAdapterCollection._sanitize_name("") == "unnamed_callback" |
| 114 | + |
| 115 | + |
| 116 | +def test_mcptc008_sanitize_consecutive_underscores_collapsed(): |
| 117 | + assert CallbackAdapterCollection._sanitize_name("a---b___c") == "a_b_c" |
| 118 | + |
| 119 | + |
| 120 | +def test_mcptc009_sanitize_long_name_truncated_to_64_chars(): |
| 121 | + result = CallbackAdapterCollection._sanitize_name("a" * 200) |
| 122 | + assert len(result) <= 64 |
| 123 | + assert result[-8:].isalnum() |
| 124 | + |
| 125 | + |
| 126 | +def test_mcptc010_sanitize_long_name_uniqueness(): |
| 127 | + result_a = CallbackAdapterCollection._sanitize_name("a" * 200) |
| 128 | + result_b = CallbackAdapterCollection._sanitize_name("b" * 200) |
| 129 | + assert result_a != result_b |
| 130 | + |
| 131 | + |
| 132 | +def test_mcptc011_sanitize_short_name_not_truncated(): |
| 133 | + assert CallbackAdapterCollection._sanitize_name("short_name") == "short_name" |
| 134 | + |
| 135 | + |
| 136 | +# --------------------------------------------------------------------------- |
| 137 | +# Output semantic summary (``_OUTPUT_SEMANTICS`` in description_outputs.py). |
| 138 | +# Other description tests (docstring, output target, multi-output) are |
| 139 | +# covered by test_mcp_tools using real adapters. |
| 140 | +# --------------------------------------------------------------------------- |
| 141 | + |
| 142 | + |
| 143 | +def test_mcptc012_semantic_summary_with_component_type(): |
| 144 | + adapter = _adapter_with_outputs([_out("my-graph", "figure", "Graph")]) |
| 145 | + desc = build_tool_description(adapter) |
| 146 | + assert "Returns chart/visualization data" in desc |
| 147 | + |
| 148 | + |
| 149 | +def test_mcptc013_semantic_summary_fallback_by_property(): |
| 150 | + adapter = _adapter_with_outputs([_out("unknown-id", "figure")]) |
| 151 | + desc = build_tool_description(adapter) |
| 152 | + assert "Returns chart/visualization data" in desc |
0 commit comments