Repository navigation
Reconnect the official MCP server when the account changes - #287
Conversation
The official sigit MCP server took its bearer from the stored token once, at startup, so web_search and the other mcp__sigit__ tools kept using the previous account after a login, logout or account switch until a restart. The server now follows the session. Its bearer is read from the stored credentials on every connect, and each connection remembers the token it used. call_server compares that with the stored token before sending and reconnects on a mismatch, so the old bearer is never sent. Reconnecting also refreshes the tool list and the error shown by /mcp. refresh_session_auth() does the same check without a call, and runs after sign-in, sign-out, /login, /logout and /reload so the tool list updates right away. A user-defined `sigit` entry in mcp.toml keeps its static headers and does not follow the session. Fixes #260 Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
There was a problem hiding this comment.
This change ensures the official MCP server always uses the current signed-in account's bearer token by reconnecting whenever the stored token changes. The fix introduces a follows_session flag on ServerDef, set only for the built-in official server, and changes connection management so that the bearer is refreshed on each connect. The reconnection logic updates the connection, tools, and error fields atomically, and drops the old connection if a reconnect fails. The fix is covered by both unit and integration tests, including a new test that verifies the bearer changes as expected when the session changes. Calls to refresh_session_auth() are added after sign-in, sign-out, and reload events to keep the tool list and /mcp up to date. No findings from earlier reviews remain unresolved, and the changes are well-contained to mcp.rs and call sites.
Automated review by siGit Code Review · commit b92581e · see the review dashboard
The official MCP server read the stored token once at startup. After signing in as another account or signing out,
web_searchand everymcp__sigit__*tool kept sending the old account's bearer until a restart. #259 fixed the same problem for inference by reading the token on each request. An MCP connection fixes its bearer when it connects, so here the fix is to reconnect when the stored token changes.Fixes #260
What changed, all in
crates/cli/src/mcp.rsunless noted:ServerDefhas afollows_sessionflag, set only on the built-in official entry. Its bearer comes fromcredentials::load_token()on every connect. A user-definedsigitentry inmcp.tomlkeeps its static headers and does not follow the session.call_servercompares that with the stored token before sending and reconnects if they differ, so the old bearer is never sent. This also covers asigit loginrun from another terminal.toolsanderrorcan be replaced, since signing in can change whether the handshake works and which tools are listed.reconnect()swaps the connection, tools and error together, and the retry path incall_serveruses it too.refresh_session_auth()does the same check with no call attached. It runs after sign-in, sign-out,/login,/logoutand/reload(inmain.rsandchat.rs), so the tool list and/mcpupdate right away. Correctness doesn't depend on these calls.Tests: two unit tests cover the official and user-defined entries.
tests/acp_mcp_reauth.rsruns the real binary against a fake cloud that serves/mcpand/chat/completions. It checks the bearer on eachtools/callfor token A, then B, then none after the credentials are removed.cargo test,cargo fmt --checkand clippy with-D warningspass.Not done yet: the manual check (sign in as A,
web_search,/loginas B,web_search, and confirm B's token in the cloud logs).