Scope toolkit tool reads and stop gating reads on TTL-expired catalogs

Merged
RH
2mo ago
+706-189

Toolkit MCP sessions were slower than default sessions in production: session init p95 was about 3.3 s versus 0.3 s, and search or describe inside a toolkit session paid the same cost. Two structural causes.

  1. A toolkit session listed connections by loading every tool row in the workspace and resolving policy for each, then keeping the few the toolkit allowed. ToolPolicyProvider.prepare now returns the dynamic-tool prefixes the allowlist can reach, and core restricts the tool query to those prefixes. The read scales with the toolkit, not the workspace.

  2. Every tools read waited up to the 2 s grace budget for re-listing MCP catalogs that were merely older than the freshness TTL. Those rebuilds now run in the background and the read answers from the persisted rows. Stale-marked and config-revised catalogs still gate the read within the grace budget. Visibility-only readers (connections and integrations under a toolkit) no longer wait on the sync at all.

Also digests the toolkit rules once per operation instead of re-sorting and re-classifying them for every tool.

Tests: unit tests for the scope helper, scoped reads under a prepared provider, the toolkit scope derivation, and the split between expired and stale-marked sync. The MCP catalog TTL test now asserts eventual convergence. Ran selfhost e2e toolkits-mcp scenarios.