Skip to content

Commit 6d4d875

Browse files
authored
Add per-user sign-in for OAuth2 connectors (Enterprise) (#1302)
* Add per-user sign-in for OAuth2 connectors (#1300) An OAuth2 connector could only be authorized once, so every user called the provider with the authorizing account and the provider's own permissions were lost. With credentialScope USER (Enterprise), every user connects their own account and each call carries the caller's own token. - New user_connector_credentials table and connectors.credential_scope column, with migration; tokens encrypted with ENCRYPTION_KEY. - Token cache, refresh lock, refused-refresh memo, persistence and credential status keyed per user, so a revoked sign-in blocks and emails only its owner. - Calls without a user, a sign-in or an Enterprise licence fail before anything is sent; nothing falls back to the connector's token. - Response cache kept per user for these connectors. - Endpoints to switch the mode and to read or remove one's own sign-in; any member may start their own authorization on such a connector. - Limited to REST, GraphQL, MCP and OData connectors with browser OAuth2. - Sign-in card on the connector page and docs/connectors/per-user-sign-in.md. * Regenerate llms.txt for the per-user sign-in docs (#1300) * Take the connectors lock in short tries; hide per-user without Enterprise (#1300) - The migration adds credential_scope with lock_timeout 1s in a retry loop: waiting behind a transaction still open on connectors (a token refresh holds one up to 10 s) stalled every connector query queued behind the ALTER for the whole wait; now queries run between tries. - GET my-credential reports whether per-user sign-in is available, and the Sign-in card is not shown on a shared connector without it, so workspaces without Enterprise see the connector page as before. --------- Co-authored-by: helpcode.ai <[email protected]>
1 parent 3b480d4 commit 6d4d875

39 files changed

Lines changed: 2304 additions & 80 deletions

‎docs/connectors/mcp-bridge.md‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,11 @@ The MCP Bridge supports automatic token refresh for OAuth2-protected remote serv
149149
The redirect URI to register with a provider is `<SERVER_URL>/api/mcp-oauth/callback`
150150
(`GET /api/connectors/oauth/redirect-uri` returns it).
151151

152+
With **AnythingMCP Enterprise**, an OAuth2 connector can require every user to
153+
sign in with their own account instead of sharing one authorization, so the
154+
remote server answers each user with their own permissions. See
155+
[Per-user sign-in](per-user-sign-in.md).
156+
152157
### Connector settings (`connector.config`)
153158

154159
A catalog adapter sets these under `connector.config`; they are copied into the connector at
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
# Per-user sign-in for OAuth2 connectors
2+
3+
> Needs **AnythingMCP Enterprise**. Available on AnythingMCP Cloud and on
4+
> self-hosted instances with an Enterprise licence key. The 30-day trial does
5+
> not include it.
6+
7+
By default an OAuth2 connector is authorized **once**: whoever clicks
8+
"Authorize with Provider" signs in, and every user of the workspace then calls
9+
the provider with that one account. Everyone sees what that account can see.
10+
11+
With **per-user sign-in**, every user connects **their own** account. Each tool
12+
call carries the token the provider issued to the caller, so the provider's own
13+
permissions decide what each user sees and may change. This suits providers that
14+
already govern access per user: a remote MCP server such as mfr's, or Microsoft
15+
365, Google and similar APIs.
16+
17+
## Turning it on
18+
19+
1. Open the connector. It must use **OAuth2** with a browser sign-in
20+
(authorization code). API keys, passwords and the client-credentials grant
21+
stay shared: there is no user to sign in.
22+
2. In the **Sign-in** card, the connector's owner or an admin clicks
23+
**Require each user to sign in (Enterprise)** and confirms.
24+
3. Every user opens the connector and clicks **Connect my account**, then signs
25+
in at the provider with their own account.
26+
27+
Switching to per-user removes the authorization stored on the connector. The OAuth
28+
client stays: the client ID and secret, and on MCP servers the dynamically
29+
registered client. Switching back to a shared sign-in removes every user's own
30+
sign-in, and the connector then needs one authorization of its own again.
31+
32+
## What each call does
33+
34+
- **The caller's own sign-in.** A call uses the token of the signed-in user who
35+
makes it, from an MCP client or from **Run Test** and **Test connection** in
36+
the dashboard. The connector's own token is never used, so a missing sign-in
37+
never falls back to someone else's.
38+
- **Not connected yet.** The call fails before anything is sent. The error names
39+
the connector and links to its page, so the model can tell the user where to
40+
connect.
41+
- **No signed-in user.** A call that does not come from a user fails the same
42+
way, for example a workspace API key or a job that runs on its own.
43+
- **Response cache.** For tools with a response cache, entries are kept per user.
44+
One user's cached answer is never served to another.
45+
46+
## Expired or refused sign-ins
47+
48+
Tokens refresh automatically for each user, under that user's own refresh lock.
49+
When the provider refuses a user's refresh token, for example after a password
50+
change, a revocation or a long period of inactivity, the sign-in is marked
51+
**Your sign-in expired** for that user only. That user gets one email, and their
52+
calls fail at once with what to do. Other users are not affected. **Sign in
53+
again** on the connector page clears it.
54+
55+
When the OAuth client itself stops working, for example because a client secret
56+
expired, every user's sign-in fails. The connector's OAuth settings need
57+
fixing, and each user then signs in again.
58+
59+
## Tools of an MCP connector
60+
61+
The tools of a per-user MCP connector are imported when users sign in, with the
62+
first user's account and with each later sign-in. **Re-discover Tools** lists
63+
them with your own sign-in. The remote server still checks every call against the
64+
caller's own permissions.
65+
66+
## API
67+
68+
| Method and path | Who | What |
69+
|---|---|---|
70+
| `PATCH /api/connectors/:id/credential-scope` `{ "credentialScope": "USER" \| "SHARED" }` | owner or admin | Switch the mode (USER needs Enterprise) |
71+
| `POST /api/connectors/:id/oauth/authorize` | any member (per-user connectors) | Start your own sign-in |
72+
| `GET /api/connectors/:id/my-credential` | any member | Whether you are connected; no token is returned |
73+
| `DELETE /api/connectors/:id/my-credential` | any member | Disconnect your own account |
74+
75+
Tokens are stored encrypted with `ENCRYPTION_KEY`, one row per user and
76+
connector (`user_connector_credentials`). They are deleted with the user, the
77+
connector or the workspace. Connector exports never include them.

‎docs/license-faq.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ This split (AGPL core + commercial `ee/`) is the same model used by Cal.com and
6161

6262
## Community, Starter, Team and Enterprise
6363

64-
A self-hosted instance runs **Community** unless a licence key is activated. Community is meant for personal use, education and evaluation, and includes up to 3 active users. **Starter** and **Team** license an instance for small teams at work. **Enterprise** adds more users, single sign-on (Entra ID, Google, Okta, OIDC) and SCIM provisioning; it is quoted rather than bought online ([contact us](https://anythingmcp.com/contact?plan=enterprise)), and administrators can try it for 30 days under **Settings → License**. See [anythingmcp.com/pricing](https://anythingmcp.com/pricing).
64+
A self-hosted instance runs **Community** unless a licence key is activated. Community is meant for personal use, education and evaluation, and includes up to 3 active users. **Starter** and **Team** license an instance for small teams at work. **Enterprise** adds more users, single sign-on (Entra ID, Google, Okta, OIDC), SCIM provisioning and [per-user sign-in for OAuth2 connectors](connectors/per-user-sign-in.md); it is quoted rather than bought online ([contact us](https://anythingmcp.com/contact?plan=enterprise)), and administrators can try it for 30 days under **Settings → License**. See [anythingmcp.com/pricing](https://anythingmcp.com/pricing).
6565

6666
---
6767

‎llms-full.txt‎

Lines changed: 90 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎llms.txt‎

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
-- =============================================================================
2+
-- Migration: per-user connector credentials (Enterprise)
3+
-- =============================================================================
4+
-- `connectors.credential_scope` says whose credential a connector's calls
5+
-- carry: SHARED (the one stored on the connector, as before; the default for
6+
-- every existing row) or USER (each caller's own sign-in to the provider).
7+
--
8+
-- `user_connector_credentials` holds those per-user sign-ins: the tokens the
9+
-- provider issued to one user for one connector, encrypted with
10+
-- ENCRYPTION_KEY like `connectors.auth_config`, and the credential health of
11+
-- that one user (auth_status*), so a revoked token blocks only its owner.
12+
-- Rows go away with the connector, the user or the organization.
13+
--
14+
-- Adding the column is instant (a constant default rewrites nothing), but it
15+
-- needs an exclusive lock on `connectors`. Waiting for it behind a transaction
16+
-- that is still open on the table (a token refresh holds one for up to 10 s
17+
-- while the provider answers) would stall every connector query queued behind
18+
-- the wait, for the whole time. So the lock is asked for 1 s at a time: if it
19+
-- is not free, the attempt gives up, the queued queries run, and it tries
20+
-- again, for up to about two minutes before the migration fails (the release
21+
-- then keeps the old backend serving).
22+
-- =============================================================================
23+
24+
-- CreateEnum
25+
CREATE TYPE "CredentialScope" AS ENUM ('SHARED', 'USER');
26+
27+
-- AlterTable
28+
DO $$
29+
DECLARE
30+
attempt int := 0;
31+
BEGIN
32+
LOOP
33+
BEGIN
34+
PERFORM set_config('lock_timeout', '1s', true);
35+
ALTER TABLE "connectors" ADD COLUMN "credential_scope" "CredentialScope" NOT NULL DEFAULT 'SHARED';
36+
EXIT;
37+
EXCEPTION WHEN lock_not_available THEN
38+
attempt := attempt + 1;
39+
IF attempt >= 60 THEN
40+
RAISE;
41+
END IF;
42+
PERFORM pg_sleep(1);
43+
END;
44+
END LOOP;
45+
PERFORM set_config('lock_timeout', '0', true);
46+
END $$;
47+
48+
-- CreateTable
49+
CREATE TABLE "user_connector_credentials" (
50+
"id" TEXT NOT NULL,
51+
"connector_id" TEXT NOT NULL,
52+
"user_id" TEXT NOT NULL,
53+
"organization_id" TEXT NOT NULL,
54+
"auth_config" TEXT NOT NULL,
55+
"auth_status" TEXT,
56+
"auth_status_code" TEXT,
57+
"auth_status_message" TEXT,
58+
"auth_status_at" TIMESTAMP(3),
59+
"auth_status_ref" TEXT,
60+
"auth_notified_at" TIMESTAMP(3),
61+
"created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
62+
"updated_at" TIMESTAMP(3) NOT NULL,
63+
64+
CONSTRAINT "user_connector_credentials_pkey" PRIMARY KEY ("id")
65+
);
66+
67+
-- CreateIndex
68+
CREATE INDEX "user_connector_credentials_user_id_idx" ON "user_connector_credentials"("user_id");
69+
70+
-- CreateIndex
71+
CREATE INDEX "user_connector_credentials_organization_id_idx" ON "user_connector_credentials"("organization_id");
72+
73+
-- CreateIndex
74+
CREATE UNIQUE INDEX "user_connector_credentials_connector_id_user_id_key" ON "user_connector_credentials"("connector_id", "user_id");
75+
76+
-- AddForeignKey
77+
ALTER TABLE "user_connector_credentials" ADD CONSTRAINT "user_connector_credentials_connector_id_fkey" FOREIGN KEY ("connector_id") REFERENCES "connectors"("id") ON DELETE CASCADE ON UPDATE CASCADE;
78+
79+
-- AddForeignKey
80+
ALTER TABLE "user_connector_credentials" ADD CONSTRAINT "user_connector_credentials_user_id_fkey" FOREIGN KEY ("user_id") REFERENCES "users"("id") ON DELETE CASCADE ON UPDATE CASCADE;
81+
82+
-- AddForeignKey
83+
ALTER TABLE "user_connector_credentials" ADD CONSTRAINT "user_connector_credentials_organization_id_fkey" FOREIGN KEY ("organization_id") REFERENCES "organizations"("id") ON DELETE CASCADE ON UPDATE CASCADE;

‎packages/backend/prisma/rls/disable-rls.sql‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@ DECLARE
1212
'connectors', 'invitation_tokens', 'kg_connector_state', 'kg_edges',
1313
'kg_nodes', 'kg_skill_suggestions', 'kg_value_seen', 'licenses',
1414
'mcp_api_keys', 'mcp_server_configs', 'org_settings',
15-
'organization_members', 'roles', 'tool_invocations', 'users'
15+
'organization_members', 'roles', 'tool_invocations', 'user_connector_credentials',
16+
'users'
1617
];
1718
BEGIN
1819
FOREACH t IN ARRAY org_tables LOOP

‎packages/backend/prisma/rls/enable-rls.sql‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,8 @@ DECLARE
2424
'connectors', 'invitation_tokens', 'kg_connector_state', 'kg_edges',
2525
'kg_nodes', 'kg_skill_suggestions', 'kg_value_seen', 'licenses',
2626
'mcp_api_keys', 'mcp_server_configs', 'org_settings',
27-
'organization_members', 'roles', 'tool_invocations', 'users'
27+
'organization_members', 'roles', 'tool_invocations', 'user_connector_credentials',
28+
'users'
2829
];
2930
BEGIN
3031
FOREACH t IN ARRAY org_tables LOOP

0 commit comments

Comments
 (0)