Reuse MCP handlers across sampling steps (#37273)

## Why

MCP tool handlers and their Code Mode definitions are immutable for the lifetime
of a stable MCP binding, so rebuilding them for every sampling step repeats schema
construction unnecessarily.

## What changed

- Cache MCP handlers per session and reuse them while the same binding remains
  active, invalidating the cache when the binding changes.
- Lazily cache schema-free Code Mode definitions on MCP handlers while continuing
  to rebuild definitions for step-local tools whose specifications may change.
- Reapply current app enablement, tool policy, exposure, and plugin schema budgets
  whenever cached handlers are registered for a step.

## Testing

- Cover handler and Code Mode definition reuse, binding invalidation, changing app
  policy, stable continuation schemas, and step-local custom tool updates.

GitOrigin-RevId: 69c5011fb97c08580ac476e7fb2286b1e92e0359
This commit is contained in:
jif authored and copyberry committed 2026-08-06 13:37:07 +00:00
1 parent bfb6a6ea22
commit e1831db7c3
13 files changed
+439 -85

No files matched your search

+1 -1
View File
@@ -3143,7 +3143,7 @@ impl Session {
self.as_ref(),
turn_context.as_ref(),
&environments,
mcp.as_ref(),
&mcp,
&extension_data,
prepared_recommendations,
)