Skip to content

Commit ff68b71

Browse files
committed
feat(mcp): add settings panel controls for dynamic tool loading
1 parent 61fe02f commit ff68b71

39 files changed

Lines changed: 857 additions & 28 deletions

‎README.md‎

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,28 @@ Zoo Code adapts to how you work:
102102
Learn more: [Using Modes](https://docs.zoocode.dev/basic-usage/using-modes) •
103103
[Custom Modes](https://docs.zoocode.dev/advanced-usage/custom-modes)
104104

105+
## Dynamic MCP Tool Loading
106+
107+
When a mode can use many MCP tools, Zoo Code sends only the tools that match the task. This keeps each request small.
108+
109+
> **Kill-switch:** to turn off dynamic tool loading, set **Search turns** (`mcp.dynamicLoad.searchTurns`) to `0`. Zoo Code then sends all MCP tools.
110+
111+
1. At the start of a task, Zoo Code ranks the MCP tools against the task text with BM25 keyword search. It attaches the best matches.
112+
2. For each new user message in the search window, Zoo Code ranks the tools again and adds the best new matches. It does not remove tools.
113+
3. After the search window, the selection does not grow by itself. The model can call the `mcp_load` tool with a query to attach more tools.
114+
4. When the context condenses, the selection starts again from step 1.
115+
116+
Set these values in **Settings > MCP Servers > Dynamic tool loading**:
117+
118+
| Setting | Default | Meaning |
119+
| ----------------------------------------------------- | ------- | ------------------------------------------------------------------------------------------------------ |
120+
| Search turns (`mcp.dynamicLoad.searchTurns`) | 7 | User messages in the search window. `0` turns off dynamic loading. `1` keeps only the first selection. |
121+
| Tool threshold (`mcp.dynamicLoad.threshold`) | 50 | Dynamic loading starts when the mode can use more MCP tools than this number. |
122+
| Initial tools (`mcp.dynamicLoad.initialK`) | 30 | Tools attached at the start of a task and after the context condenses. |
123+
| Tools per `mcp_load` call (`mcp.dynamicLoad.searchK`) | 10 | The maximum number of tools that one `mcp_load` call attaches. |
124+
125+
The search window stops adding tools at 60 attached tools. `mcp_load` can attach more. See [the design notes](docs/architecture/mcp-dynamic-tool-loading.md).
126+
105127
## Resources
106128

107129
- **[Documentation](https://docs.zoocode.dev):** The official guide to

‎docs/architecture/mcp-dynamic-tool-loading.md‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,10 @@ The gate limits advertised definitions; it does not add an execution permission
5757
boundary. Existing tool authorization still applies to calls by name.
5858

5959
The settings schema and extension-state round trip are implemented for
60-
`threshold`, `initialK`, `searchTurns`, and `searchK`. SettingsView controls are
61-
deferred to #583.
60+
`threshold`, `initialK`, `searchTurns`, and `searchK`. The MCP settings panel
61+
edits all four through the SettingsView `cachedState` and saves them with
62+
`updateSettings`. The panel accepts a value only when the host schema accepts it.
63+
`docs/architecture/tla/McpDynamicLoadSettings.tla` models this round trip.
6264

6365
## Loading more tools
6466

‎docs/architecture/tla/McpDynamicLoadSettings.NoZero.cfg‎

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎docs/architecture/tla/McpDynamicLoadSettings.Omitted.cfg‎

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎docs/architecture/tla/McpDynamicLoadSettings.Raw.cfg‎

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎docs/architecture/tla/McpDynamicLoadSettings.cfg‎

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
---- MODULE McpDynamicLoadSettings ----
2+
\* Source: the dynamic tool loading inputs in McpView. They write SettingsView cachedState.
3+
\* Path: SettingsView.handleSubmit sends updateSettings. webviewMessageHandler validates each key and stores it.
4+
\* Sink: ClineProvider.getStateToPostToWebview posts resolveMcpDynamicLoadSettings of the stored value.
5+
\* Failure 1: the panel shows a saved value that the host dropped, so the runtime uses a different value.
6+
\* Failure 2: the panel refuses a value that the schema accepts, for example the kill-switch searchTurns 0.
7+
\* Invariant 1: RoundTrip. With no unsaved change, each shown value equals the effective stored value.
8+
\* Invariant 2: ValidReachable. The panel accepts each value that the schema accepts.
9+
\* Oracle: the schema. SchemaAccepts holds for an integer at or above the minimum of the key.
10+
\* Scope: two keys. searchTurns has minimum 0 like threshold. initialK has minimum 1 like searchK.
11+
\* Host readers of searchTurns are checked in McpToolSelectionLifecycle (SharedDefault). This file is the webview.
12+
\* Expect: Raw and Omitted fail RoundTrip. NoZero fails ValidReachable. Fixed holds.
13+
\* Expect: a null result for the fixed code. The UI uses the same schema as the host.
14+
\* Assumption: React restores a controlled input after a rejected edit. So the shown value is cachedState.
15+
\* Result: Fixed holds in 150 states. Raw and Omitted fail RoundTrip. NoZero fails ValidReachable.
16+
\* Result: null result for the fixed code. The first Fixed run failed TypeOK. It was a spec error, not a code defect.
17+
\* Replay: parseInt in parseMcpDynamicLoadInput (Raw) fails 7 tests. A minimum of 1 (NoZero) fails 3 tests.
18+
\* Replay: removal of initialK from handleSubmit (Omitted) fails the SettingsView save test.
19+
\* Property test: McpDynamicLoadSettings.spec.tsx. Accepted values resolve to themselves. Rejected integers resolve to the default.
20+
\* Finding: Save writes the shown default to storage. A later change to a default does not reach that user.
21+
\* Other SettingsView settings do the same. This change does not fix it.
22+
\* Finding: the issue asks the panel to describe an embeddings ranker. createRanker returns only BM25 (#576).
23+
\* The panel and README describe BM25 only.
24+
\* Cost: each TLC config takes under 1 second. Agent time was not measured.
25+
26+
EXTENDS Integers, TLC
27+
\* Unset and Cleared are model values. Cleared is an empty input. parseInt gives NaN for it.
28+
CONSTANTS Keys, Min, Default, Typed, Unset, Cleared, Variant
29+
VARIABLES stored, cached, dirty, rejectedValid
30+
vars == <<stored, cached, dirty, rejectedValid>>
31+
32+
SchemaAccepts(k, v) == v # Cleared /\ v >= Min[k]
33+
UiAccepts(k, v) ==
34+
CASE Variant = "Raw" -> v # Cleared
35+
[] Variant = "NoZero" -> v # Cleared /\ v >= 1
36+
[] OTHER -> SchemaAccepts(k, v)
37+
Sent == IF Variant = "Omitted" THEN Keys \ {"initialK"} ELSE Keys
38+
Effective(k) == IF stored[k] = Unset THEN Default[k] ELSE stored[k]
39+
Posted == [k \in Keys |-> Effective(k)]
40+
41+
Init == /\ stored \in [Keys -> {Unset} \cup (Typed \ {Cleared})]
42+
/\ \A k \in Keys : IF stored[k] = Unset THEN TRUE ELSE SchemaAccepts(k, stored[k])
43+
/\ cached = Posted
44+
/\ dirty = FALSE
45+
/\ rejectedValid = FALSE
46+
47+
Edit(k, v) ==
48+
/\ IF UiAccepts(k, v)
49+
THEN /\ cached' = [cached EXCEPT ![k] = v]
50+
/\ dirty' = (dirty \/ cached[k] # v)
51+
/\ UNCHANGED rejectedValid
52+
ELSE /\ rejectedValid' = (rejectedValid \/ SchemaAccepts(k, v))
53+
/\ UNCHANGED <<cached, dirty>>
54+
/\ UNCHANGED stored
55+
56+
\* The host keeps the old value of a key that fails validation.
57+
Save == /\ dirty
58+
/\ stored' = [k \in Keys |->
59+
IF k \in Sent /\ SchemaAccepts(k, cached[k]) THEN cached[k] ELSE stored[k]]
60+
/\ dirty' = FALSE
61+
/\ UNCHANGED <<cached, rejectedValid>>
62+
63+
\* Reload and Discard both copy the posted state into cachedState.
64+
Reload == /\ cached' = Posted
65+
/\ dirty' = FALSE
66+
/\ UNCHANGED <<stored, rejectedValid>>
67+
68+
Next == (\E k \in Keys, v \in Typed : Edit(k, v)) \/ Save \/ Reload
69+
Spec == Init /\ [][Next]_vars
70+
71+
Shown == Typed \cup {Default[k] : k \in Keys}
72+
TypeOK == /\ stored \in [Keys -> {Unset} \cup Shown]
73+
/\ cached \in [Keys -> Shown]
74+
/\ dirty \in BOOLEAN
75+
/\ rejectedValid \in BOOLEAN
76+
RoundTrip == ~dirty => cached = Posted
77+
ValidReachable == ~rejectedValid
78+
====
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
---- MODULE McpDynamicLoadSettingsMC ----
2+
\* Checking domain for McpDynamicLoadSettings. The default 3 differs from each typed value.
3+
EXTENDS McpDynamicLoadSettings
4+
5+
MCKeys == {"searchTurns", "initialK"}
6+
MCMin == [k \in MCKeys |-> IF k = "searchTurns" THEN 0 ELSE 1]
7+
MCDefault == [k \in MCKeys |-> 3]
8+
\* -1 and 0 are the boundary values.
9+
MCTyped == {Cleared, -1, 0, 1, 2}
10+
====

‎webview-ui/playwright/gallery/stories.tsx‎

Lines changed: 24 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
import { globalSettingsSchema, resolveMcpDynamicLoadSettings, type ExtensionState } from "@roo-code/types"
2+
3+
import { useAppTranslation } from "@src/i18n/TranslationContext"
4+
import { Input, StandardTooltip } from "@src/components/ui"
5+
6+
const FIELDS = [
7+
{ key: "mcp.dynamicLoad.searchTurns", name: "searchTurns" },
8+
{ key: "mcp.dynamicLoad.threshold", name: "threshold" },
9+
{ key: "mcp.dynamicLoad.initialK", name: "initialK" },
10+
{ key: "mcp.dynamicLoad.searchK", name: "searchK" },
11+
] as const
12+
13+
export type McpDynamicLoadKey = (typeof FIELDS)[number]["key"]
14+
export type McpDynamicLoadValues = Pick<ExtensionState, McpDynamicLoadKey>
15+
16+
/** Spec: McpDynamicLoadSettings, UiAccepts. webviewMessageHandler validates with the same schema. */
17+
export function parseMcpDynamicLoadInput(key: McpDynamicLoadKey, text: string): number | undefined {
18+
if (text.trim() === "") return undefined
19+
return globalSettingsSchema.shape[key].safeParse(Number(text)).data
20+
}
21+
22+
interface McpDynamicLoadSettingsProps {
23+
values: McpDynamicLoadValues
24+
setValue: (key: McpDynamicLoadKey, value: number) => void
25+
}
26+
27+
export const McpDynamicLoadSettings = ({ values, setValue }: McpDynamicLoadSettingsProps) => {
28+
const { t } = useAppTranslation()
29+
const resolved = resolveMcpDynamicLoadSettings(
30+
values["mcp.dynamicLoad.threshold"],
31+
values["mcp.dynamicLoad.initialK"],
32+
values["mcp.dynamicLoad.searchTurns"],
33+
values["mcp.dynamicLoad.searchK"],
34+
)
35+
36+
return (
37+
<div className="mt-5 flex flex-col gap-3" data-testid="mcp-dynamic-load-settings">
38+
<div>
39+
<h4 className="m-0 font-medium">{t("mcp:dynamicLoad.title")}</h4>
40+
<p className="mt-1 mb-0 text-sm text-vscode-descriptionForeground">
41+
{t("mcp:dynamicLoad.description")}
42+
</p>
43+
</div>
44+
{FIELDS.map(({ key, name }) => (
45+
<label key={key} className="flex items-center justify-between gap-4">
46+
<span className="flex items-center gap-1">
47+
{t(`mcp:dynamicLoad.${name}.label`)}
48+
<StandardTooltip content={t(`mcp:dynamicLoad.${name}.tooltip`)}>
49+
<span className="codicon codicon-info text-vscode-descriptionForeground" />
50+
</StandardTooltip>
51+
</span>
52+
<Input
53+
type="number"
54+
min={globalSettingsSchema.shape[key].unwrap().minValue ?? undefined}
55+
step={1}
56+
className="w-24 rounded px-2 py-1 text-right"
57+
value={resolved[name]}
58+
onChange={(e) => {
59+
const value = parseMcpDynamicLoadInput(key, e.target.value)
60+
if (value !== undefined) setValue(key, value)
61+
}}
62+
onClick={(e) => e.currentTarget.select()}
63+
data-testid={`mcp-dynamic-load-${name}`}
64+
/>
65+
</label>
66+
))}
67+
</div>
68+
)
69+
}

0 commit comments

Comments
 (0)