Sign in to MCP servers through an oauth-discovery connection provider
Closed
CH
8d ago+2055+2055+0123456789012345678901234567890123456789-244-244-012345678901234567890123456789
What
Second of three PRs split out of #29669. Based on #30069 (oauth-discovery connection provider). Retarget to main once that one merges.
An MCP server that answers an unauthenticated ping with 401 gets its own oauth-discovery connection provider, generated by Twenty and owned by the server's application:
createMcpServer,updateMcpServerandsyncMcpServerToolsattach the provider when the server asks for a sign-in. A sync also catches servers that were down at creation or started asking later.signInScope(USER: each member signs in,WORKSPACE: one shared account) on the create and update inputs and onMcpServer. Provider scopes are immutable, so changing the scope or the URL replaces the generated provider. The old one is deleted once no server uses it. Providers declared by an app are never deleted.- Calls pick the caller's connected account. A rejected access token is refreshed once before the account is marked signed out.
MCP_SERVER_NAME_MAX_LENGTHmoves totwenty-shared/constantsfor the settings form in PR 3.
Changes from #29669
- Token refreshes are serialized per account for every caller. #29669 added the lock in the MCP client only, but app logic, messaging and calendar also call
resolveTokens. With rotating refresh tokens, an app refresh and an MCP refresh at the same moment would spend the same token, and the loser gotinvalid_grantand an auth-failed account. The lock and the "reuse the winner's tokens" check now live inConnectedAccountRefreshTokensService.refreshAndSaveTokens. - The sign-in check reuses the protected-resource probe from #30069 instead of a second copy of the JSON-RPC ping.
McpServerService(632 lines) is split intoMcpServerSignInService(provider provisioning),McpServerMigrationService(flat maps and migrations) and a lookup util.McpServerClientService(440 lines) is split out intoMcpServerConnectedAccountService.- A unit test for
isMcpServerSignInConnectionProvider, since it decides which providers get deleted. - The unrelated ClickHouse shutdown change from #29669 is left out.
Known limits
syncMcpServerToolsattaches a late sign-in withUSERscope, because a requestedWORKSPACEscope is not stored anywhere when the server did not ask for sign-in at creation. The scope can be changed afterwards on the server.- The messages stored in
toolsSyncErrorare raw English server messages, like every other sync error today. Translating them needs a change to how sync errors are stored. - Deleting a server and its generated provider are two migrations, because deleting the provider cascades to the servers still on it. If the second one fails, the provider is left without a server.
Stack
- #30069: oauth-discovery connection provider
- This PR: MCP server sign-in and refresh serialization
- MCP servers settings UI (based on this branch)
Test
- Unit: mcp-server, connected-account, validators, tool-provider: 62 suites, 381 tests.
- Integration on a fresh DB:
mcp-server-oauth.integration-spec.ts(sign-in, per-member and shared accounts, refresh after a rejected token, concurrent refreshes) plus the developers and connection-provider suites: 15 suites, 139 tests. tsgoclean, oxlint and oxfmt clean.