|
4 | 4 |
|
5 | 5 | from typing import TYPE_CHECKING |
6 | 6 |
|
| 7 | +from .base import ToolDescriptionSource |
| 8 | + |
7 | 9 | if TYPE_CHECKING: |
8 | 10 | from dash.mcp.primitives.tools.callback_adapter import CallbackAdapter |
9 | 11 |
|
10 | 12 | _OUTPUT_SEMANTICS: dict[tuple[str | None, str], str] = { |
11 | | - ("Graph", "figure"): "Returns chart/visualization data", |
12 | 13 | ("DataTable", "data"): "Returns tabular data", |
13 | 14 | ("DataTable", "columns"): "Returns table column definitions", |
14 | | - ("Dropdown", "options"): "Returns selection options", |
15 | | - ("Dropdown", "value"): "Updates a selection value", |
16 | | - ("RadioItems", "options"): "Returns selection options", |
17 | | - ("Checklist", "options"): "Returns selection options", |
18 | | - ("Store", "data"): "Returns stored data", |
| 15 | + ("Store", "data"): "Returns data to be remembered client-side", |
19 | 16 | ("Download", "data"): "Returns downloadable content", |
20 | 17 | ("Markdown", "children"): "Returns formatted text", |
21 | 18 | (None, "figure"): "Returns chart/visualization data", |
22 | | - (None, "data"): "Returns data", |
23 | | - (None, "options"): "Returns selection options", |
| 19 | + (None, "options"): "Returns available options", |
24 | 20 | (None, "columns"): "Returns column definitions", |
25 | 21 | (None, "children"): "Returns content", |
26 | | - (None, "value"): "Returns a value", |
| 22 | + (None, "value"): "Returns the current value", |
27 | 23 | (None, "style"): "Updates styling", |
28 | 24 | (None, "disabled"): "Updates enabled/disabled state", |
29 | 25 | } |
30 | 26 |
|
31 | 27 |
|
32 | | -def output_summary(adapter: CallbackAdapter) -> list[str]: |
| 28 | +class OutputSummaryDescription(ToolDescriptionSource): |
33 | 29 | """Produce a short summary of what the callback outputs represent.""" |
34 | | - outputs = adapter.outputs |
35 | | - if not outputs: |
36 | | - return ["Dash callback"] |
37 | | - |
38 | | - lines: list[str] = [] |
39 | | - for out in outputs: |
40 | | - comp_id = out["component_id"] |
41 | | - prop = out["property"] |
42 | | - comp_type = out.get("component_type") |
43 | | - |
44 | | - semantic = _OUTPUT_SEMANTICS.get((comp_type, prop)) |
45 | | - if semantic is None: |
46 | | - semantic = _OUTPUT_SEMANTICS.get((None, prop)) |
47 | | - |
48 | | - if semantic is not None: |
49 | | - lines.append(f"- {comp_id}.{prop}: {semantic}") |
50 | | - else: |
51 | | - lines.append(f"- {comp_id}.{prop}") |
52 | | - |
53 | | - n = len(outputs) |
54 | | - if n == 1: |
55 | | - return [lines[0].lstrip("- ")] |
56 | | - header = f"Returns {n} output{'s' if n > 1 else ''}:" |
57 | | - return [header] + lines |
| 30 | + |
| 31 | + @classmethod |
| 32 | + def describe(cls, callback: CallbackAdapter) -> list[str]: |
| 33 | + outputs = callback.outputs |
| 34 | + if not outputs: |
| 35 | + return ["Dash callback"] |
| 36 | + |
| 37 | + lines: list[str] = [] |
| 38 | + for out in outputs: |
| 39 | + comp_id = out["component_id"] |
| 40 | + prop = out["property"] |
| 41 | + comp_type = out.get("component_type") |
| 42 | + |
| 43 | + semantic = _OUTPUT_SEMANTICS.get((comp_type, prop)) |
| 44 | + if semantic is None: |
| 45 | + semantic = _OUTPUT_SEMANTICS.get((None, prop)) |
| 46 | + |
| 47 | + if semantic is not None: |
| 48 | + lines.append(f"- {comp_id}.{prop}: {semantic}") |
| 49 | + else: |
| 50 | + lines.append(f"- {comp_id}.{prop}") |
| 51 | + |
| 52 | + n = len(outputs) |
| 53 | + if n == 1: |
| 54 | + return [lines[0].lstrip("- ")] |
| 55 | + header = f"Returns {n} output{'s' if n > 1 else ''}:" |
| 56 | + return [header] + lines |
0 commit comments