|
| 1 | +"""MCP tools (Phase 1, read-only) over the ModelChoice bridge. |
| 2 | +
|
| 3 | +Each tool obtains a process-global :class:`ModelChoiceBridge` via |
| 4 | +``get_bridge()`` (lazy — the first call attaches to Excel). Tests inject |
| 5 | +a fake with ``set_bridge_for_testing`` to avoid touching COM. |
| 6 | +""" |
| 7 | + |
| 8 | +from __future__ import annotations |
| 9 | + |
| 10 | +from typing import Annotated |
| 11 | + |
| 12 | +from pydantic import Field |
| 13 | + |
| 14 | +from modelchoice_mcp.bridge import ModelChoiceBridge |
| 15 | +from modelchoice_mcp.schemas import ( |
| 16 | + BranchView, |
| 17 | + NodeResultView, |
| 18 | + NodeView, |
| 19 | + RollupResponse, |
| 20 | + TreeList, |
| 21 | + TreeStructure, |
| 22 | + TreeSummary, |
| 23 | +) |
| 24 | +from modelchoice_mcp.server import mcp |
| 25 | +from modelchoice_mcp.tree import DecisionTree, parse_model, rollup |
| 26 | + |
| 27 | +_bridge: ModelChoiceBridge | None = None |
| 28 | + |
| 29 | + |
| 30 | +def get_bridge() -> ModelChoiceBridge: |
| 31 | + global _bridge |
| 32 | + if _bridge is None: |
| 33 | + _bridge = ModelChoiceBridge() |
| 34 | + return _bridge |
| 35 | + |
| 36 | + |
| 37 | +def set_bridge_for_testing(bridge: ModelChoiceBridge | None) -> None: |
| 38 | + global _bridge |
| 39 | + _bridge = bridge |
| 40 | + |
| 41 | + |
| 42 | +def _counts(tree: DecisionTree) -> tuple[int, int, int]: |
| 43 | + d = sum(1 for n in tree.nodes.values() if n.kind == "decision") |
| 44 | + c = sum(1 for n in tree.nodes.values() if n.kind == "chance") |
| 45 | + t = sum(1 for n in tree.nodes.values() if n.kind == "terminal") |
| 46 | + return d, c, t |
| 47 | + |
| 48 | + |
| 49 | +@mcp.tool( |
| 50 | + description=( |
| 51 | + "ModelChoice: List the decision trees stored in a workbook, with a " |
| 52 | + "summary of each (model name, root node, and node-type counts). Trees " |
| 53 | + "live in the workbook's hidden ModelChoice store; the add-in does not " |
| 54 | + "need to be loaded. Omit workbook_name for the active workbook." |
| 55 | + ) |
| 56 | +) |
| 57 | +def list_trees(workbook_name: str | None = None) -> TreeList: |
| 58 | + trees = get_bridge().list_trees(workbook_name) |
| 59 | + summaries: list[TreeSummary] = [] |
| 60 | + for name, model_json in trees.items(): |
| 61 | + t = parse_model(model_json) |
| 62 | + d, c, term = _counts(t) |
| 63 | + root = t.nodes[t.root_id] |
| 64 | + summaries.append( |
| 65 | + TreeSummary( |
| 66 | + name=name, |
| 67 | + model_name=t.model_name, |
| 68 | + root_id=t.root_id, |
| 69 | + root_name=root.name, |
| 70 | + node_count=len(t.nodes), |
| 71 | + decision_count=d, |
| 72 | + chance_count=c, |
| 73 | + terminal_count=term, |
| 74 | + ) |
| 75 | + ) |
| 76 | + return TreeList(workbook=workbook_name, count=len(summaries), trees=summaries) |
| 77 | + |
| 78 | + |
| 79 | +@mcp.tool( |
| 80 | + description=( |
| 81 | + "ModelChoice: Read the full structure of one decision tree — every " |
| 82 | + "node (decision / chance / terminal) with its branches, probabilities, " |
| 83 | + "and branch values. Pass tree_name to pick a specific tree, else the " |
| 84 | + "first/active one. Read-only." |
| 85 | + ) |
| 86 | +) |
| 87 | +def get_tree( |
| 88 | + tree_name: Annotated[ |
| 89 | + str | None, Field(description="Tree sheet name, e.g. 'MC_Tree_1'. Omit for the first tree.") |
| 90 | + ] = None, |
| 91 | + workbook_name: str | None = None, |
| 92 | +) -> TreeStructure: |
| 93 | + bridge = get_bridge() |
| 94 | + trees = bridge.list_trees(workbook_name) |
| 95 | + if tree_name is None: |
| 96 | + tree_name = next(iter(trees)) |
| 97 | + t = parse_model(trees[tree_name]) |
| 98 | + nodes = [ |
| 99 | + NodeView( |
| 100 | + id=n.id, |
| 101 | + name=n.name, |
| 102 | + kind=n.kind, |
| 103 | + value=n.value, |
| 104 | + branches=[ |
| 105 | + BranchView( |
| 106 | + name=b.name, child_id=b.child_id, value=b.value, probability=b.probability |
| 107 | + ) |
| 108 | + for b in n.branches |
| 109 | + ], |
| 110 | + ) |
| 111 | + for n in t.nodes.values() |
| 112 | + ] |
| 113 | + return TreeStructure( |
| 114 | + name=tree_name, |
| 115 | + model_name=t.model_name, |
| 116 | + root_id=t.root_id, |
| 117 | + maximize=t.maximize, |
| 118 | + node_count=len(t.nodes), |
| 119 | + nodes=nodes, |
| 120 | + ) |
| 121 | + |
| 122 | + |
| 123 | +@mcp.tool( |
| 124 | + description=( |
| 125 | + "ModelChoice: Roll a decision tree back to its expected values and " |
| 126 | + "OPTIMAL POLICY — the decision recommendation. Returns the root " |
| 127 | + "expected value, the optimal sequence of decisions, a plain-English " |
| 128 | + "recommendation, and the per-node expected values. Computed directly " |
| 129 | + "from the stored model (terminal payoff = accumulated branch values; " |
| 130 | + "chance = probability-weighted EV; decision = best EV by maximize/" |
| 131 | + "minimize) — reproduces ModelChoice's rollback without the add-in." |
| 132 | + ) |
| 133 | +) |
| 134 | +def roll_up( |
| 135 | + tree_name: Annotated[ |
| 136 | + str | None, Field(description="Tree sheet name. Omit for the first tree.") |
| 137 | + ] = None, |
| 138 | + workbook_name: str | None = None, |
| 139 | +) -> RollupResponse: |
| 140 | + bridge = get_bridge() |
| 141 | + trees = bridge.list_trees(workbook_name) |
| 142 | + if tree_name is None: |
| 143 | + tree_name = next(iter(trees)) |
| 144 | + t = parse_model(trees[tree_name]) |
| 145 | + r = rollup(t) |
| 146 | + |
| 147 | + direction = "maximize" if t.maximize else "minimize" |
| 148 | + if r.optimal_path: |
| 149 | + choices = " → ".join(r.optimal_path) |
| 150 | + rec = ( |
| 151 | + f"Optimal decision ({direction} EV): take {choices}. " |
| 152 | + f"Expected value {r.expected_value:,.2f}." |
| 153 | + ) |
| 154 | + else: |
| 155 | + rec = ( |
| 156 | + f"No decision nodes to optimize; expected value {r.expected_value:,.2f} " |
| 157 | + f"({direction})." |
| 158 | + ) |
| 159 | + |
| 160 | + nodes = [ |
| 161 | + NodeResultView( |
| 162 | + id=nr.id, |
| 163 | + name=nr.name, |
| 164 | + kind=nr.kind, |
| 165 | + expected_value=nr.expected_value, |
| 166 | + optimal_branch_name=nr.optimal_branch_name, |
| 167 | + ) |
| 168 | + for nr in r.node_results.values() |
| 169 | + ] |
| 170 | + return RollupResponse( |
| 171 | + name=tree_name, |
| 172 | + model_name=t.model_name, |
| 173 | + maximize=t.maximize, |
| 174 | + expected_value=r.expected_value, |
| 175 | + optimal_path=r.optimal_path, |
| 176 | + recommendation=rec, |
| 177 | + nodes=nodes, |
| 178 | + ) |
| 179 | + |
| 180 | + |
| 181 | +__all__ = ["get_bridge", "get_tree", "list_trees", "roll_up", "set_bridge_for_testing"] |
0 commit comments