WeKnora/internal/mcp
Ochan Kwon be581c41e0
fix(mcp): close only the authorizing user's connection after OAuth (#4138)
An OAuth MCP service keeps one cached connection per user. After a
successful OAuth callback, after a token is revoked, and when an agent
resumes from an in-conversation authorization, the service's connections
were recycled with CloseClient, which disconnects every user's
connection and cancels connection attempts still in flight. Closing a
Streamable HTTP connection cancels the requests it has in flight, so one
user authorizing again cancelled other users' running tool calls on the
same server process. The agent retries a failed call once on a fresh
connection, but a long call fails, and a non-idempotent tool can run
twice.

A connection is not affected when another user's token changes: each
one reads its own user's token from the store on every request. Add
MCPManager.CloseClientForPrincipal, which closes only that user's
connection, and use it when a token is revoked.

After an authorization, the callback and the in-conversation retry call
CloseClientsAfterAuthorization instead. It closes the authorizing user's
connection, since the new authorization may be for another account of
the upstream service, and every connection of the service in the tenant
that was built with an OAuth client ID other than the registration
stored now. The registration is shared by all users of the service and
can be replaced by any replica. A connection still holding the old
client ID can fail its next refresh with invalid_client, and handling
that failure deletes the stored registration, by then the replacement.
If the registration cannot be read, the whole service is closed as
before, unless the caller's context has ended, in which case only the
authorizing user's connection is closed. The callback bounds this
cleanup with its own timeout.

This runs in the process that handles the callback and, for an
in-conversation authorization, in the process where the agent waits.
Connections cached by other replicas are not examined, as before, and
neither are other users' connection attempts still in flight.

CompleteAuthorization now returns the tenant, service and principal of
the attempt, and connections report the client ID they were built with.
Disconnecting happens after the manager lock is released, because
closing a transport can wait on the server.
2026-10-12 11:19:42 +08:00
..
client.go fix(mcp): close only the authorizing user's connection after OAuth (#4138) 2026-10-12 11:19:42 +08:00
client_param_headers_test.go feat(mcp): support 2026-07-28 alongside legacy clients (#4095) 2026-10-10 15:15:06 +08:00
client_protocol_test.go feat(mcp): support 2026-07-28 alongside legacy clients (#4095) 2026-10-10 15:15:06 +08:00
client_session_test.go feat(mcp): support 2026-07-28 alongside legacy clients (#4095) 2026-10-10 15:15:06 +08:00
client_structured_result_test.go fix(mcp): preserve structured tool results for agents 2026-10-10 20:27:34 +08:00
client_test.go feat(mcp)!: persist tool catalogs and add guided configuration (#3118) 2026-09-09 13:34:56 +08:00
client_tools_test.go feat(mcp): support 2026-07-28 alongside legacy clients (#4095) 2026-10-10 15:15:06 +08:00
errors.go refactor: Clean up code by removing unnecessary blank lines and improving comments for better readability and maintainability 2025-11-28 16:10:37 +08:00
manager.go fix(mcp): close only the authorizing user's connection after OAuth (#4138) 2026-10-12 11:19:42 +08:00
manager_close_principal_test.go fix(mcp): close only the authorizing user's connection after OAuth (#4138) 2026-10-12 11:19:42 +08:00
manager_closeall_test.go fix(mcp): close MCP connections on shutdown without blocking exit 2026-09-25 18:22:14 +08:00
manager_disconnect_test.go fix(mcp): 隔离慢连接清理对其他服务的阻塞 2026-10-10 20:12:58 +08:00
manager_startup_test.go feat(mcp): support 2026-07-28 alongside legacy clients (#4095) 2026-10-10 15:15:06 +08:00
oauth_lifecycle.go fix(mcp): close only the authorizing user's connection after OAuth (#4138) 2026-10-12 11:19:42 +08:00
oauth_lifecycle_test.go fix(mcp): coordinate OAuth token refresh and authorization polling 2026-07-23 15:35:24 +08:00
oauth_manager.go fix(mcp): close only the authorizing user's connection after OAuth (#4138) 2026-10-12 11:19:42 +08:00
oauth_manager_test.go fix(mcp): close only the authorizing user's connection after OAuth (#4138) 2026-10-12 11:19:42 +08:00
oauth_principal_test.go fix(mcp): coordinate OAuth token refresh and authorization polling 2026-07-23 15:35:24 +08:00
oauth_redirect_security_test.go fix: align client sessions and integration request handling (#3305) 2026-09-16 16:20:57 +08:00
oauth_state.go fix(mcp): coordinate OAuth token refresh and authorization polling 2026-07-23 15:35:24 +08:00
oauth_state_test.go fix(mcp): coordinate OAuth token refresh and authorization polling 2026-07-23 15:35:24 +08:00
oauth_tokenstore.go fix(mcp): coordinate OAuth token refresh and authorization polling 2026-07-23 15:35:24 +08:00
security.go fix(security): harden outbound URL SSRF protection (#2626) 2026-08-10 13:24:14 +08:00
security_test.go fix(security): harden outbound URL SSRF protection (#2626) 2026-08-10 13:24:14 +08:00
tool_content_test.go fix(mcp): pass embedded resources in tool results to the agent (#3766) 2026-10-08 01:25:04 +08:00
types.go fix(mcp): preserve structured tool results for agents 2026-10-10 20:27:34 +08:00