-
Notifications
You must be signed in to change notification settings - Fork 943
Description
Problem
Currently, changing the set of tools visible to the model requires calling resume_session() with the new tool list. This has a serious structural side-effect: resume_session creates a brand-new CopilotSession Python object and replaces the entry in the internal _sessions dict. Any code that previously called session.on(handler) is now subscribed to a dead, orphaned session object — all future CLI notifications (including session.idle) dispatch to the new session, which has no event handlers. The result is that stream_chat-style patterns block indefinitely if resume_session is called mid-stream.
This makes dynamic tool injection impossible while a session is active, and makes even inter-turn tool updates fragile, since the session object reference must be manually tracked and re-subscribed after every resume_session call.
Proposed Solution
Add a lightweight RPC method — e.g. session.setActiveTools — that filters which registered tools are exposed to the model's context window without replacing the session object:
# Register all tools once, up front
session = await client.create_session(session_id, {
"tools": all_tools, # full palette, all handlers registered
...
})
# Later, filter what the model sees — no session teardown, no re-subscription needed
await session.set_active_tools(["tool_a", "tool_c"])The CLI already owns the tool-call dispatch loop and builds the model prompt internally. This filter would be a server-side context-window hint: include only definitions for the named tools in the next prompt sent to the model. All handlers remain registered; the model simply cannot invoke hidden tools because their definitions are not in its context.
Why This Is Safe and Useful
- No session object replacement — event subscribers remain valid
- Can be called mid-stream (e.g. from an
on_pre_tool_usehook) to reshape the tool palette for the next sub-turn - Enables skill/workflow systems — register a large tool palette once; expose contextually relevant subsets per turn
- No protocol breakage — the existing
toolsregistration increate_session/resume_sessionis unchanged;setActiveToolsis purely additive - Aligns with how VS Code's Copilot Chat already filters agents/tools dynamically in the UI
Current Workaround
Calling resume_session between turns and manually re-subscribing to the new session object. This is error-prone and impossible to do safely mid-stream.