D0: rmcp conditional-tools spike findings and recommendation
This commit is contained in:
1 parent
efbf1ca163
commit
d2d6e7c872
1 file changed
+39
@@ -42,3 +42,42 @@ Both snapshot variants pass; each tool with `peer` returns the peer's data via t
|
|||||||
## Log
|
## Log
|
||||||
|
|
||||||
- 2026-10-19: Split from the single phase plan 2026-10-19.
|
- 2026-10-19: Split from the single phase plan 2026-10-19.
|
||||||
|
- 2026-10-19: **D0 rmcp conditional-tools spike: supported natively, no fallback needed.** Tested against pinned `rmcp` 3.5.1. `ToolRouter` exposes `disable_route`, `enable_route` and `with_disabled`; `list_all()` filters disabled names, `get()` returns `None` and `call()` rejects them with `invalid_params("tool not found")`. The `#[tool_handler]` macro routes `list_tools` through `router.list_all()` and `call_tool` through `router.call()`, and `TrackerService` already stores its router in a field built by `new()`. `StreamableHttpService` constructs `TrackerService` per session, so the visible set is computed per session from `AppState` with no restart. `disable_route` also records names that have no route yet, so gating can be written before the two tools exist or in either order.
|
||||||
|
|
||||||
|
Throwaway proof (run as `cargo test --lib mcp::tests::spike_conditional_tool_list`, then removed; `src/` unchanged):
|
||||||
|
|
||||||
|
```rust
|
||||||
|
#[test]
|
||||||
|
fn spike_conditional_tool_list() {
|
||||||
|
use rmcp::handler::server::router::tool::ToolRoute;
|
||||||
|
use rmcp::model::Tool;
|
||||||
|
let peer_route = |name: &'static str| {
|
||||||
|
ToolRoute::new_dyn(
|
||||||
|
Tool::new(name, name, Arc::new(Default::default())),
|
||||||
|
|_ctx| Box::pin(async { Ok(CallToolResult::default().into()) }),
|
||||||
|
)
|
||||||
|
};
|
||||||
|
let mut router = TrackerService::tool_router();
|
||||||
|
router.add_route(peer_route("list_peers"));
|
||||||
|
router.add_route(peer_route("get_peer"));
|
||||||
|
assert_eq!(router.list_all().len(), 10);
|
||||||
|
router.disable_route("list_peers");
|
||||||
|
router.disable_route("get_peer");
|
||||||
|
assert_eq!(router.list_all().len(), 8);
|
||||||
|
assert!(router.get("list_peers").is_none());
|
||||||
|
assert!(!router.has_route("get_peer"));
|
||||||
|
router.enable_route("list_peers");
|
||||||
|
router.enable_route("get_peer");
|
||||||
|
assert_eq!(router.list_all().len(), 10);
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
Result: passed; 10 visible -> 8 after disabling the two peer tools -> 10 after re-enabling.
|
||||||
|
|
||||||
|
Recommendation for D2:
|
||||||
|
|
||||||
|
1. Keep all ten `#[tool]` methods on one impl (one `#[tool_router]`). In `TrackerService::new`, after `Self::tool_router()`, do `if state.peers.is_none() { tool_router.disable_route("list_peers"); tool_router.disable_route("get_peer"); }`. This yields exactly 8 tools by default and 10 when peers are enabled. Do **not** adopt the plan's fallback (always list 10, error when disabled); it is unnecessary and changes the contract.
|
||||||
|
2. Rewrite the snapshot test to build services, not the static router: peers-disabled variant via `TrackerService::new(fake_state(...))` asserts 8; peers-enabled variant via `fake_state_with_peers(...)` asserts 10. `TrackerService::tool_router()` alone always returns all 10 and would no longer guard the gated list.
|
||||||
|
3. Schema caveat: `disable_route` hides a tool but cannot alter its static `Tool` attr. Adding `peer: Option<String>` to the eight params structs therefore changes their `input_schema` even when peers are disabled, which contracts section 7 forbids. Recommended fix: a small helper in `TrackerService::new` that, when `state.peers.is_none()`, removes the `"peer"` property from each of the eight `input_schema`s (`ToolRouter.map` and `ToolRoute.attr` are public; `Arc::make_mut(&mut route.attr.input_schema).remove("peer")`); the field is optional so `required` is untouched. This is deterministic and covered by asserting the two snapshot variants' property sets. It relies on public fields of the pinned rmcp version, so keep the pin and the snapshot test. If that is judged too brittle, the alternative is a contracts section 7 change to always advertise `peer` (optional, ignored when peers are disabled); ask the contract owner first.
|
||||||
|
4. Not a risk for the rest of D3: `list_resources`, `list_prompts` and `get_info`/instructions are hand-written in this repo, so `peers://status`, the peer-aware prompt argument and the conditional instructions text are plain `state.peers.is_some()` branches, not router features.
|
||||||
|
5. Runtime toggling is not required (peers are declared at startup; SIGHUP rotates tokens, not `enabled`), but `disable_route`/`enable_route` can emit `notifications/tools/list_changed` via `bind_peer_notifier` if a later phase needs it.
|
||||||
Reference in new issue
Block a user