|
| 1 | +"""Built-in tool: get_dash_component.""" |
| 2 | + |
| 3 | +from __future__ import annotations |
| 4 | + |
| 5 | +import json |
| 6 | +from typing import Any |
| 7 | + |
| 8 | +from mcp.types import CallToolResult, TextContent, Tool |
| 9 | +from pydantic import Field, TypeAdapter |
| 10 | +from typing_extensions import Annotated, NotRequired, TypedDict |
| 11 | + |
| 12 | +from dash import get_app |
| 13 | +from dash._layout_utils import find_component |
| 14 | +from dash.mcp.types import ComponentPropertyInfo, ComponentQueryResult |
| 15 | + |
| 16 | + |
| 17 | +class _ComponentQueryInput(TypedDict): |
| 18 | + component_id: Annotated[str, Field(description="The component ID to query")] |
| 19 | + property: NotRequired[ |
| 20 | + Annotated[ |
| 21 | + str, |
| 22 | + Field( |
| 23 | + description="The property name to read (e.g. 'options', 'value'). Omit to list all defined properties." |
| 24 | + ), |
| 25 | + ] |
| 26 | + ] |
| 27 | + |
| 28 | + |
| 29 | +_INPUT_SCHEMA = TypeAdapter(_ComponentQueryInput).json_schema() |
| 30 | +_OUTPUT_SCHEMA = TypeAdapter(ComponentQueryResult).json_schema() |
| 31 | + |
| 32 | +NAME = "get_dash_component" |
| 33 | + |
| 34 | + |
| 35 | +def get_tool_names() -> set[str]: |
| 36 | + return {NAME} |
| 37 | + |
| 38 | + |
| 39 | +def get_tools() -> list[Tool]: |
| 40 | + return [_build_tool()] |
| 41 | + |
| 42 | + |
| 43 | +def _build_tool() -> Tool: |
| 44 | + return Tool( |
| 45 | + name=NAME, |
| 46 | + description=( |
| 47 | + "Get a component's properties, values, and tool relationships. " |
| 48 | + "If property is omitted, returns all defined properties. " |
| 49 | + "If property is specified, returns only that property. " |
| 50 | + "See the dash://components resource for available component IDs." |
| 51 | + ), |
| 52 | + inputSchema=_INPUT_SCHEMA, |
| 53 | + outputSchema=_OUTPUT_SCHEMA, |
| 54 | + ) |
| 55 | + |
| 56 | + |
| 57 | +def call_tool(tool_name: str, arguments: dict[str, Any]) -> CallToolResult: |
| 58 | + comp_id = arguments.get("component_id", "") |
| 59 | + if not comp_id: |
| 60 | + raise ValueError("component_id is required") |
| 61 | + |
| 62 | + prop_filter = arguments.get("property", "") |
| 63 | + component = find_component(comp_id) |
| 64 | + |
| 65 | + if component is None: |
| 66 | + callback_map = get_app().mcp_callback_map |
| 67 | + rendering_tools = [ |
| 68 | + cb.tool_name |
| 69 | + for cb in callback_map |
| 70 | + if any(out["component_id"] == comp_id for out in cb.outputs) |
| 71 | + ] |
| 72 | + msg = f"Component '{comp_id}' not found in static layout." |
| 73 | + if rendering_tools: |
| 74 | + msg += f" However, the following tools would modify it: {rendering_tools}." |
| 75 | + msg += " Use the dash://components resource to see statically available component IDs." |
| 76 | + return CallToolResult( |
| 77 | + content=[TextContent(type="text", text=msg)], |
| 78 | + isError=True, |
| 79 | + ) |
| 80 | + |
| 81 | + callback_map = get_app().mcp_callback_map |
| 82 | + |
| 83 | + properties: dict[str, ComponentPropertyInfo] = {} |
| 84 | + for prop_name in getattr(component, "_prop_names", []): |
| 85 | + if prop_filter and prop_name != prop_filter: |
| 86 | + continue |
| 87 | + |
| 88 | + value = callback_map.get_initial_value(f"{comp_id}.{prop_name}") |
| 89 | + if value is None: |
| 90 | + value = getattr(component, prop_name, None) |
| 91 | + if value is None: |
| 92 | + continue |
| 93 | + |
| 94 | + modified_by: list[str] = [] |
| 95 | + input_to: list[str] = [] |
| 96 | + id_and_prop = f"{comp_id}.{prop_name}" |
| 97 | + for cb in callback_map: |
| 98 | + for out in cb.outputs: |
| 99 | + if out["id_and_prop"] == id_and_prop: |
| 100 | + modified_by.append(cb.tool_name) |
| 101 | + for inp in cb.inputs: |
| 102 | + if inp["id_and_prop"] == id_and_prop: |
| 103 | + input_to.append(cb.tool_name) |
| 104 | + |
| 105 | + properties[prop_name] = ComponentPropertyInfo( |
| 106 | + initial_value=value, |
| 107 | + modified_by_tool=modified_by, |
| 108 | + input_to_tool=input_to, |
| 109 | + ) |
| 110 | + |
| 111 | + labels = callback_map.component_label_map.get(comp_id, []) |
| 112 | + |
| 113 | + structured: ComponentQueryResult = ComponentQueryResult( |
| 114 | + component_id=comp_id, |
| 115 | + component_type=type(component).__name__, |
| 116 | + label=labels if labels else None, |
| 117 | + properties=properties, |
| 118 | + ) |
| 119 | + |
| 120 | + return CallToolResult( |
| 121 | + content=[TextContent(type="text", text=json.dumps(structured, default=str))], |
| 122 | + structuredContent=structured, |
| 123 | + ) |
0 commit comments