mirror of
https://github.com/paperclipai/paperclip.git
synced 2026-11-20 18:26:40 +01:00
## Thinking Path
> - Paperclip is the open source app people use to manage AI agents for
work
> - Assistant connections (MCP) let an outside assistant, or another
Paperclip instance, sign in as a person and use the `/mcp/paperclip`
endpoint
> - The feature was behind the experimental `enablePublicMcp` setting.
The setting was off by default and in practice off on Cloud stacks
> - Instance-to-instance work (one person's agent files a task on a
colleague's instance) fails when the receiving instance has the setting
off
> - This pull request moves the feature out of Experimental. It is now
always on, and a stored or Cloud-managed "off" value is ignored
> - The benefit is that every authenticated instance with a public URL,
including Cloud workspaces, accepts assistant connections with no setup
step
## Linked Issues or Issue Description
**Subsystem affected**
Public MCP (`server/src/services/public-mcp/`), instance settings,
Connectors → Assistant Connection (MCP) setup page, Experimental
settings page.
**Problem or motivation**
A remote MCP connector on one instance cannot use another instance's
`/mcp/paperclip` unless an admin on the other instance finds and turns
on an experimental toggle. On Cloud, the toggle is off and is managed by
the fleet. Instance-to-instance delegation therefore does not work by
default.
**Proposed solution**
Remove the gate. Keep `enablePublicMcp` only as a compatibility key, the
same way `enableApps` was graduated: old stored rows, PATCH bodies and
Cloud managed configs still parse, but the effective value is always
`true`.
**Alternatives considered**
An instance-admin kill switch that defaults ON. I did not keep one. Each
person already controls access: a connection exists only after that
person approves OAuth consent for one company, and the person or an
admin can revoke it. Deployment policy (authenticated mode and a valid
public URL) still decides whether the endpoint exists. A second switch
would add a state that can silently break instance-to-instance work
again. A schema migration was also not needed: the value lives in the
`experimental` JSON column, and the stored key becomes inert.
**Roadmap alignment**
Requested by the maintainer to enable instance-to-instance
communication.
## What Changed
- `packages/shared`: `enablePublicMcp` is a deprecated compatibility
key. The schema default is `true`. The feature catalog entry is
"Assistant connections (compatibility)" with both defaults `true`.
- `server` instance settings: normalization always returns
`enablePublicMcp: true`. The Cloud managed overlay skips the key, so a
fleet value of `false` cannot disable it. Removed the key from the Cloud
staging experimental-key list. Removed the client-mirror policy step
that ran only when the toggle changed.
- `server` public MCP: personal discovery, OAuth, tools, setup and event
delivery no longer read instance settings (`isEnabled()` returns `true`
for the personal endpoint without a DB read). Dot, Grok and Muse agent
connections keep their own experimental settings.
- `server` Cloud paths: the client-mirror sync, the hosted event
authority, the ingress-handoff expiry worker and the idle sleep proof
treat the front door as enabled. The warm-standby `503
workspace_unclaimed` behavior is unchanged.
- `server` Dot/Grok gates: they no longer also require
`enablePublicMcp`. Error messages no longer tell people to enable
Assistant connections.
- `ui`: removed the Experimental toggle, the "Enable assistant
connections / Open Experimental settings" block on the Assistant
Connection page, and the "Enable Assistant connections (MCP)" hint in
Dot setup. Updated the Dot and Grok copy.
- Storybook: deleted the Experimental-setting story and removed the
"enable the experiment" step from the journey and presenter.
- Docs: `doc/public-mcp.md` ("Enable an instance", "Start from
Connections", `GET /api/mcp/setup`), `doc/openai-dot-runner.md`,
`doc/idle-grok-dispatch.md`, and the agent-access map.
- Tests: new red/green tests for "enabled with no setting stored" and "a
stored or managed off value is ignored". Removed the tests for the
"disabled" behavior, which no longer exists.
## Verification
- Red/green: with the old source and the new tests,
`instance-settings-readonly.test.ts`,
`instance-settings-service.test.ts` and `public-mcp.test.ts` fail (4
failures, and the public-MCP suite fails in `beforeAll` because
`isEnabled()` was `false` with no setting stored). With the new source
they pass.
- `server`: `public-mcp.test.ts`, instance-settings (readonly, service,
managed overlay), `managed-config`, `cloud-event-authority`,
`events-work`, `dot-runner`, `dot-oauth-onboarding` and
`agent-runtime-assignments`. 354 passed.
- Cloud client-mirror lifecycle
(`client-mirror-lifecycle.postgres.test.ts`, skipped in CI) passed
against a local Postgres 17 with
`PAPERCLIP_MCP_SLEEP_TEST_DATABASE_URL`. 6/6, plus `events-work` 28/28.
- Idle and sleep: `idle-incoming-wake` (service and route),
`idle-deadline-proof`, `idle-incoming-wake-capability`,
`idle-sleep-proof`, `client-mirror-ack`, `recovery-deadline-acceptance`
and `idle-completed-history` pass. The warm-standby test now also
asserts that discovery after the claim does no SQL read.
- `ui`: `InstanceExperimentalSettings`, `AssistantConnection`,
`NewAgentDialog`, `NewAgent` and `ExternalAgentInviteDialog.muse` pass.
267 passed.
- Typecheck: `packages/shared`, `server` and `ui` pass. `node
scripts/check-maps.mjs` passes.
- `pnpm verify`: see the CI checks on this PR.
- After the deploy: confirm
`/.well-known/oauth-protected-resource/mcp/paperclip` and `/mcp/setup`
return 200 on paperclip.staging without changing any setting.
## Risks
- Behavior change: instances that had the setting off now serve
discovery, OAuth and `/mcp/paperclip`. Security invariants are
unchanged: the endpoint is still mounted only in authenticated mode with
a valid public URL, OAuth still needs a real signed-in user's consent,
local implicit board authority cannot approve, and revocation works the
same way.
- Cloud sleep: on a managed stack, the MCP sleep proof blocks sleep
until the client mirror publishes the tenant front door to Cloud. The
client-mirror sync runs at boot and does this once. Stacks that already
had the flag on use this path today. A stack whose Cloud connector
cannot publish the mirror would stay awake. Watch fleet sleep after the
deploy.
- Personal public-MCP requests now read instance settings one time less.
- Fleet admin: the Cloud fleet catalog can still show and set
`enablePublicMcp`. The value is ignored. Removing it from the fleet
catalog is a separate change in the Cloud repo.
- No migration. The `0354` trigger still compares the stored key. That
causes at most one extra connector-runtime invalidation if an old client
writes the key.
> For core feature work, check [`ROADMAP.md`](ROADMAP.md) first and
discuss it in `#dev` before opening the PR. Feature PRs that overlap
with planned core work may need to be redirected — check the roadmap
first. See `CONTRIBUTING.md`.
## Model Used
- Claude Opus 5.5 (`claude-opus-5-5`) in Claude Code, with tool use
(shell, file edits, test execution).
## Checklist
- [x] I have included a thinking path that traces from project context
to this change
- [x] I have specified the model used (with version and capability
details)
- [x] I have checked ROADMAP.md and confirmed this PR does not duplicate
planned core work
- [x] I have searched GitHub for duplicate or related PRs and linked
them above
- [x] I have either (a) linked existing issues with `Fixes: #` / `Closes
#` / `Refs #` OR (b) described the issue in-PR following the relevant
issue template
- [x] I have not referenced internal/instance-local Paperclip issues or
links (only public GitHub `#NNN` / `github.com/paperclipai/paperclip`
URLs)
- [x] My branch name describes the change (e.g. `docs/...`, `fix/...`)
and contains no internal Paperclip ticket id or instance-derived details
- [x] I have run tests locally and they pass
- [x] I have added or updated tests where applicable
- [x] I have updated relevant documentation to reflect my changes
- [x] I have considered and documented any risks above
- [ ] All Paperclip CI gates are green
- [ ] Greptile is 5/5 with no open P2s, recommendations, or follow-ups
- [x] I will address all Greptile and reviewer comments before
requesting merge
🤖 Generated with [Claude Code](https://claude.com/claude-code)
---------
Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
251 lines
20 KiB
TypeScript
251 lines
20 KiB
TypeScript
import { readFile } from "node:fs/promises";
|
|
import { createHash, randomBytes, randomUUID } from "node:crypto";
|
|
import express from "express";
|
|
import request from "supertest";
|
|
import { and, eq, sql } from "drizzle-orm";
|
|
import { afterAll, beforeAll, beforeEach, describe, expect, it, vi } from "vitest";
|
|
import { createDb, authUsers, companies, companyMemberships, agents, dotAgentBindings, dotMailboxItems, mcpEventSubscriptions, mcpOauthGrants, mcpOauthRequests, mcpOauthTokens } from "@paperclipai/db";
|
|
import { startEmbeddedPostgresTestDatabase } from "./helpers/embedded-postgres.js";
|
|
import { createPublicMcpOAuth, publicMcpConfig, hashMcpSecret, DEVICE_GRANT } from "../services/public-mcp/oauth.js";
|
|
import { instanceSettingsService } from "../services/instance-settings.js";
|
|
import { dotRunnerBroker } from "../services/dot-runner-broker.js";
|
|
import { publicMcpManagementRoutes, publicMcpIngressRoutes } from "../routes/public-mcp.js";
|
|
|
|
const config = { origin: "https://paperclip.example", resource: "https://paperclip.example/mcp/runner" };
|
|
const callback = "https://chatgpt.com/connector_platform_oauth_redirect";
|
|
describe("Dot onboarding with an operator-issued pairing capability", () => {
|
|
let temp: Awaited<ReturnType<typeof startEmbeddedPostgresTestDatabase>>;
|
|
let db: ReturnType<typeof createDb>;
|
|
beforeAll(async () => {
|
|
temp = await startEmbeddedPostgresTestDatabase("paperclip-dot-onboarding-");
|
|
db = createDb(temp.connectionString);
|
|
}, 120000);
|
|
beforeEach(async () => { await instanceSettingsService(db).updateExperimental({ enablePublicMcp: true, enableOpenAiDot: true, enableNativeRunner: false }); });
|
|
afterAll(async () => { await temp?.cleanup(); });
|
|
async function fixture(authorizationOrigin?: string, lifecycleState: NonNullable<typeof agents.$inferInsert["lifecycleState"]> = "ready") {
|
|
const userId = randomUUID();
|
|
await db.insert(authUsers).values({ id: userId, name: "Operator", email: userId + "@example.test", createdAt: new Date(), updatedAt: new Date() });
|
|
const [company] = await db.insert(companies).values({ name: "Dot onboarding", issuePrefix: "DO" + randomBytes(3).toString("hex") }).returning();
|
|
await db.insert(companyMemberships).values({ companyId: company!.id, principalType: "user", principalId: userId, membershipRole: "owner", status: "active" });
|
|
const [agent] = await db.insert(agents).values({ companyId: company!.id, name: "Dot", adapterType: "paperclip_runner", adapterConfig: { provider: "openai_dot" }, lifecycleState, status: lifecycleState === "ready" ? "active" : "paused" }).returning();
|
|
const oauth = createPublicMcpOAuth(db, { ...config, ...(authorizationOrigin ? { authorizationOrigin } : {}) });
|
|
const client = await oauth.register({ client_name: "Dot", redirect_uris: [callback], grant_types: ["authorization_code", "refresh_token", DEVICE_GRANT] }, randomUUID());
|
|
const verifier = randomBytes(32).toString("base64url");
|
|
const input = { client_id: client.client_id, redirect_uri: callback, response_type: "code", resource: config.resource,
|
|
scope: "paperclip:agent offline_access", code_challenge: createHash("sha256").update(verifier).digest("base64url"), code_challenge_method: "S256" };
|
|
const begin = async (extra = {}) => (await oauth.authorize({ ...input, ...extra })).split("/").at(-1)!;
|
|
const pairing = await dotRunnerBroker(db).createPairing({ companyId: company!.id, agentId: agent!.id, operatorId: userId });
|
|
const id = await begin();
|
|
return { userId, company: company!, agent: agent!, oauth, client, verifier, input, pairing, id, begin };
|
|
}
|
|
async function connect(f: Awaited<ReturnType<typeof fixture>>) {
|
|
const consent = await f.oauth.consentDotPairing(f.id, f.pairing.pairingCode);
|
|
return f.oauth.token({ grant_type: "authorization_code", client_id: f.client.client_id,
|
|
redirect_uri: callback, resource: config.resource, code: new URL(consent.redirectUrl).searchParams.get("code"), code_verifier: f.verifier });
|
|
}
|
|
|
|
it("invalidates setup when a broker pairing is applied or revoked", async () => {
|
|
const f = await fixture(undefined, "preparing");
|
|
const consent = await f.oauth.consent(f.id, { type: "board", source: "session", userId: f.userId },
|
|
{ decision: "approve", companyId: f.company.id, allowWrites: false });
|
|
const tokens = await f.oauth.token({ grant_type: "authorization_code", client_id: f.client.client_id,
|
|
redirect_uri: callback, resource: config.resource, code: new URL(consent.redirectUrl).searchParams.get("code"), code_verifier: f.verifier });
|
|
const principal = await f.oauth.authenticate(tokens.access_token);
|
|
const broker = dotRunnerBroker(db);
|
|
await broker.pair(principal, f.pairing.pairingCode);
|
|
const [paired] = await db.select().from(agents).where(eq(agents.id, f.agent.id));
|
|
expect(paired.adapterConfig.dotBindingId).toBe(f.pairing.bindingId);
|
|
expect(paired.lifecycleVersion).toBe(f.agent.lifecycleVersion + 1);
|
|
await expect(broker.pair(principal, f.pairing.pairingCode)).rejects.toThrow("Pairing code expired or was consumed");
|
|
|
|
await broker.revoke(f.company.id, f.agent.id, f.userId);
|
|
const [revoked] = await db.select().from(agents).where(eq(agents.id, f.agent.id));
|
|
expect(revoked.adapterConfig).not.toHaveProperty("dotBindingId");
|
|
expect(revoked.lifecycleVersion).toBe(paired.lifecycleVersion + 1);
|
|
await expect(f.oauth.authenticate(tokens.access_token)).rejects.toThrow();
|
|
});
|
|
it.each(["preparing", "verifying"] as const)("allows Dot setup during %s without allowing task access", async (state) => {
|
|
const f = await fixture(undefined, state);
|
|
const tokens = await connect(f);
|
|
const principal = await f.oauth.authenticate(tokens.access_token);
|
|
const broker = dotRunnerBroker(db);
|
|
await db.insert(mcpEventSubscriptions).values({ id: randomUUID(), companyId: f.company.id,
|
|
grantId: principal.grant.id, name: "paperclip.dot.mailbox_updated", bindingId: f.pairing.bindingId,
|
|
arguments: {}, deliveryMaterial: {}, verifiedAt: new Date(), expiresAt: new Date(Date.now() + 60_000) });
|
|
await db.insert(dotMailboxItems).values({ companyId: f.company.id, bindingId: f.pairing.bindingId,
|
|
bindingGeneration: 1, kind: "assignment", sourceEventId: randomUUID(), references: { task: "hidden" } });
|
|
await broker.challenge(f.company.id, f.agent.id);
|
|
const inbox = await broker.mailbox(principal);
|
|
expect(inbox.items.map(item => item.kind)).toEqual(["readiness_challenge"]);
|
|
expect(inbox.nextCursor).toBe(0);
|
|
await expect(broker.confirmChallenge(principal, String(inbox.items[0]!.references.challenge))).resolves.toEqual({ status: "ready" });
|
|
expect(await broker.capabilities(principal)).toMatchObject({ ready: false });
|
|
await expect(broker.tasks(principal)).rejects.toThrow("authority");
|
|
await expect(broker.snapshot(f.company.id, f.agent.id, f.pairing.bindingId)).rejects.toThrow("authority");
|
|
await db.update(agents).set({ lifecycleState: "paused" }).where(eq(agents.id, f.agent.id));
|
|
expect((await broker.mailbox(principal)).items).toEqual([]);
|
|
await expect(broker.confirmChallenge(principal, "already-used")).rejects.toThrow("authority");
|
|
});
|
|
it("refreshes a Dot connection after years of inactivity, still rotates and revokes on replay", async () => {
|
|
const f = await fixture();
|
|
const tokens = await connect(f);
|
|
const [stored] = await db.select().from(mcpOauthTokens).where(eq(mcpOauthTokens.tokenHash, hashMcpSecret(tokens.refresh_token!)));
|
|
expect(stored!.expiresAt).toBeNull();
|
|
const refresh = { grant_type: "refresh_token", client_id: f.client.client_id, resource: config.resource, refresh_token: tokens.refresh_token };
|
|
vi.useFakeTimers({ toFake: ["Date"] });
|
|
try {
|
|
vi.setSystemTime(new Date(Date.now() + 2 * 365 * 24 * 60 * 60_000));
|
|
await expect(f.oauth.authenticate(tokens.access_token)).rejects.toThrow();
|
|
const next = await f.oauth.token(refresh);
|
|
expect(next.refresh_token).not.toBe(tokens.refresh_token);
|
|
expect(next.expires_in).toBe(900);
|
|
expect((await f.oauth.authenticate(next.access_token)).actor).toMatchObject({ type: "agent", agentId: f.agent.id });
|
|
const [rotated] = await db.select().from(mcpOauthTokens).where(eq(mcpOauthTokens.tokenHash, hashMcpSecret(next.refresh_token!)));
|
|
expect(rotated!.expiresAt).toBeNull();
|
|
await expect(f.oauth.token(refresh)).rejects.toThrow();
|
|
await expect(f.oauth.authenticate(next.access_token)).rejects.toThrow();
|
|
expect(await dotRunnerBroker(db).bindingForAgent(f.company.id, f.agent.id)).toBeNull();
|
|
} finally { vi.useRealTimers(); }
|
|
});
|
|
it("upgrades valid Dot refresh tokens without reviving expired, used or revoked credentials", async () => {
|
|
const f = await fixture();
|
|
const tokens = await connect(f);
|
|
const grant = (await f.oauth.authenticate(tokens.access_token)).grant;
|
|
const validExpiry = new Date(Date.now() + 30 * 24 * 60 * 60_000);
|
|
await db.update(mcpOauthTokens).set({ expiresAt: validExpiry }).where(eq(mcpOauthTokens.tokenHash, hashMcpSecret(tokens.refresh_token!)));
|
|
const stale = await fixture();
|
|
const staleTokens = await connect(stale);
|
|
const expired = new Date(0);
|
|
await db.update(mcpOauthTokens).set({ expiresAt: expired }).where(eq(mcpOauthTokens.tokenHash, hashMcpSecret(staleTokens.refresh_token!)));
|
|
const personalId = randomUUID();
|
|
const revokedId = randomUUID();
|
|
const usedHash = randomUUID();
|
|
await db.insert(mcpOauthGrants).values([
|
|
{ ...grant, id: personalId, purpose: "personal", agentId: null, resource: config.origin + "/mcp/paperclip" },
|
|
{ ...grant, id: revokedId, revokedAt: new Date() },
|
|
]);
|
|
await db.insert(mcpOauthTokens).values([
|
|
{ grantId: personalId, tokenHash: randomUUID(), kind: "refresh", expiresAt: validExpiry },
|
|
{ grantId: revokedId, tokenHash: randomUUID(), kind: "refresh", expiresAt: validExpiry },
|
|
{ grantId: grant.id, tokenHash: usedHash, kind: "refresh", expiresAt: validExpiry, usedAt: new Date() },
|
|
]);
|
|
const migration = await readFile(new URL("../../../packages/db/src/migrations/0319_heavy_captain_midlands.sql", import.meta.url), "utf8");
|
|
for (let attempt = 0; attempt < 2; attempt++) {
|
|
for (const statement of migration.split("--> statement-breakpoint")) await db.execute(sql.raw(statement));
|
|
}
|
|
const [personal] = await db.select().from(mcpOauthTokens).where(eq(mcpOauthTokens.grantId, personalId));
|
|
const [revoked] = await db.select().from(mcpOauthTokens).where(eq(mcpOauthTokens.grantId, revokedId));
|
|
const [used] = await db.select().from(mcpOauthTokens).where(eq(mcpOauthTokens.tokenHash, usedHash));
|
|
const [staleToken] = await db.select().from(mcpOauthTokens).where(eq(mcpOauthTokens.tokenHash, hashMcpSecret(staleTokens.refresh_token!)));
|
|
expect(personal!.expiresAt).toEqual(validExpiry);
|
|
expect(revoked!.expiresAt).toEqual(validExpiry);
|
|
expect(used!.expiresAt).toEqual(validExpiry);
|
|
expect(staleToken!.expiresAt).toEqual(expired);
|
|
await expect(stale.oauth.token({ grant_type: "refresh_token", client_id: stale.client.client_id, resource: config.resource, refresh_token: staleTokens.refresh_token })).rejects.toThrow();
|
|
const next = await f.oauth.token({ grant_type: "refresh_token", client_id: f.client.client_id, resource: config.resource, refresh_token: tokens.refresh_token });
|
|
expect((await f.oauth.authenticate(next.access_token)).actor).toMatchObject({ type: "agent", agentId: f.agent.id });
|
|
await f.oauth.revokeToken(next.access_token, f.client.client_id);
|
|
await expect(f.oauth.token({ grant_type: "refresh_token", client_id: f.client.client_id, resource: config.resource, refresh_token: next.refresh_token })).rejects.toThrow();
|
|
});
|
|
it("pins browser ingress without changing the issuer, resource or token endpoint", async () => {
|
|
const browserOrigin = "https://paperclip-browser.example:10000";
|
|
const f = await fixture(browserOrigin);
|
|
const personal = createPublicMcpOAuth(db, { ...config, authorizationOrigin: browserOrigin, resource: config.origin + "/mcp/paperclip" });
|
|
const app = express(); app.use(express.json());
|
|
app.use((req, _res, next) => { req.actor = { type: "none" }; next(); });
|
|
app.use(publicMcpIngressRoutes(f.oauth, vi.fn()));
|
|
app.use("/api", publicMcpManagementRoutes(personal, f.oauth));
|
|
const metadata = await request(app).get("/.well-known/oauth-authorization-server/mcp/runner/oauth");
|
|
expect(metadata.body).toMatchObject({ issuer: config.origin + "/mcp/runner/oauth",
|
|
authorization_endpoint: browserOrigin + "/mcp/runner/oauth/authorize", token_endpoint: config.origin + "/mcp/runner/oauth/token" });
|
|
const authorization = await request(app).get("/mcp/runner/oauth/authorize").query(f.input);
|
|
expect(authorization.status).toBe(303);
|
|
expect(new URL(authorization.headers.location).origin).toBe(browserOrigin);
|
|
expect(new URL(authorization.headers.location).pathname).toMatch(/^\/dot-connect\/pcmcp_request_[A-Za-z0-9_-]{43}$/);
|
|
const path = `/api/mcp/requests/${f.id}/dot-pairing`;
|
|
expect((await request(app).post(path).set("Origin", "https://other.example").send({ pairingCode: f.pairing.pairingCode })).status).toBe(403);
|
|
const approval = await request(app).post(path).set("Origin", browserOrigin).send({ pairingCode: f.pairing.pairingCode });
|
|
expect(approval.status).toBe(200);
|
|
expect(new URL(approval.body.redirectUrl).searchParams.get("iss")).toBe(config.origin + "/mcp/runner/oauth");
|
|
const device = await f.oauth.deviceAuthorize({ client_id: f.client.client_id, resource: config.resource, scope: "paperclip:agent" }, randomUUID());
|
|
expect(device.verification_uri).toBe(browserOrigin + "/mcp-device");
|
|
});
|
|
it("validates a separately configured browser origin", () => {
|
|
expect(publicMcpConfig({ PAPERCLIP_PUBLIC_URL: config.origin, PAPERCLIP_MCP_AUTHORIZATION_ORIGIN: "https://browser.example:10000" }))
|
|
.toMatchObject({ origin: config.origin, resource: config.origin + "/mcp/paperclip", authorizationOrigin: "https://browser.example:10000" });
|
|
for (const invalid of ["http://browser.example", "https://secret@browser.example", "https://browser.example/path", "https://browser.example?next=evil", "https://browser.example#fragment"])
|
|
expect(() => publicMcpConfig({ PAPERCLIP_PUBLIC_URL: config.origin, PAPERCLIP_MCP_AUTHORIZATION_ORIGIN: invalid })).toThrow();
|
|
});
|
|
it.each(["mcp", "dot-mcp"])("connects via %s without a board session, binds the exact agent, and consumes the capability once", async prefix => {
|
|
const f = await fixture();
|
|
const personal = createPublicMcpOAuth(db, { ...config, resource: config.origin + "/mcp/paperclip" });
|
|
const app = express(); app.use(express.json());
|
|
app.use((req, _res, next) => { req.actor = { type: "none" }; next(); });
|
|
app.use("/api", publicMcpManagementRoutes(personal, f.oauth));
|
|
const path = `/api/${prefix}/requests/${f.id}/dot-pairing`;
|
|
expect((await request(app).get(`/api/${prefix}/requests/${f.id}`)).body.agentConnection).toBe(true);
|
|
expect((await request(app).post(path).send({ pairingCode: f.pairing.pairingCode })).status).toBe(403);
|
|
const preview = await request(app).post(path + "/preview").set("Origin", config.origin).send({ pairingCode: f.pairing.pairingCode });
|
|
expect(preview.status).toBe(200);
|
|
expect(preview.body.accessDuration).toBe("Ongoing until revoked. This connection does not expire from inactivity.");
|
|
expect(preview.body).toMatchObject({ company: { id: f.company.id, name: f.company.name }, agent: { id: f.agent.id, name: f.agent.name } });
|
|
expect(JSON.stringify(preview.body)).not.toContain(f.pairing.pairingCode);
|
|
expect(await db.select().from(mcpOauthGrants).where(eq(mcpOauthGrants.userId, f.userId))).toHaveLength(0);
|
|
const approval = await request(app).post(path).set("Origin", config.origin).send({ pairingCode: f.pairing.pairingCode });
|
|
expect(approval.status).toBe(200);
|
|
const redirect = new URL(approval.body.redirectUrl);
|
|
expect(redirect.origin + redirect.pathname).toBe(callback);
|
|
const exchange = { grant_type: "authorization_code", client_id: f.client.client_id, redirect_uri: callback, resource: config.resource,
|
|
code: redirect.searchParams.get("code"), code_verifier: f.verifier };
|
|
await expect(f.oauth.token({ ...exchange, code_verifier: "x".repeat(43) })).rejects.toThrow();
|
|
const tokens = await f.oauth.token(exchange);
|
|
const principal = await f.oauth.authenticate(tokens.access_token);
|
|
expect(principal.actor).toMatchObject({ type: "agent", agentId: f.agent.id, companyId: f.company.id });
|
|
expect(principal.grant.scopes).toEqual(["paperclip:agent", "offline_access"]);
|
|
await expect(personal.authenticate(tokens.access_token)).rejects.toThrow();
|
|
expect(await dotRunnerBroker(db).mailbox(principal)).toMatchObject({ bindingId: f.pairing.bindingId });
|
|
const [binding] = await db.select().from(dotAgentBindings).where(eq(dotAgentBindings.id, f.pairing.bindingId));
|
|
expect(binding).toMatchObject({ status: "connected", pairingCodeHash: null, pairingExpiresAt: null });
|
|
expect((await db.select().from(agents).where(eq(agents.id, f.agent.id)))[0]!.adapterConfig.dotBindingId).toBe(f.pairing.bindingId);
|
|
await expect(f.oauth.consentDotPairing(f.id, f.pairing.pairingCode)).rejects.toThrow();
|
|
await expect(f.oauth.consentDotPairing(await f.begin(), f.pairing.pairingCode)).rejects.toThrow();
|
|
});
|
|
it("does not expose personal assistant requests through the public Dot pairing lane", async () => {
|
|
const f = await fixture();
|
|
const personal = createPublicMcpOAuth(db, { ...config, resource: config.origin + "/mcp/paperclip" });
|
|
const client = await personal.register({ client_name: "Personal", redirect_uris: [callback], grant_types: ["authorization_code"] }, randomUUID());
|
|
const id = (await personal.authorize({ ...f.input, client_id: client.client_id, resource: config.origin + "/mcp/paperclip", scope: "paperclip:read" })).split("/").at(-1)!;
|
|
const app = express(); app.use(express.json());
|
|
app.use((req, _res, next) => { req.actor = { type: "none" }; next(); });
|
|
app.use("/api", publicMcpManagementRoutes(personal, f.oauth));
|
|
expect((await request(app).get(`/api/dot-mcp/requests/${id}`)).status).toBe(404);
|
|
for (const suffix of ["", "/preview"]) {
|
|
expect((await request(app).post(`/api/dot-mcp/requests/${id}/dot-pairing${suffix}`).set("Origin", config.origin).send({ pairingCode: f.pairing.pairingCode })).status).toBe(404);
|
|
}
|
|
expect((await request(app).get(`/api/mcp/requests/${id}`)).status).toBe(200);
|
|
});
|
|
it.each(["expired-code", "expired-request", "revoked", "paused", "viewer", "wrong-company", "dot-disabled"])("rejects %s without creating a grant", async failure => {
|
|
const f = await fixture();
|
|
let id = f.id;
|
|
if (failure === "expired-code") await db.update(dotAgentBindings).set({ pairingExpiresAt: new Date(0) }).where(eq(dotAgentBindings.id, f.pairing.bindingId));
|
|
if (failure === "expired-request") await db.update(mcpOauthRequests).set({ expiresAt: new Date(0) }).where(eq(mcpOauthRequests.id, id));
|
|
if (failure === "revoked") await dotRunnerBroker(db).revoke(f.company.id, f.agent.id, f.userId);
|
|
if (failure === "paused") await db.update(agents).set({ status: "paused" }).where(eq(agents.id, f.agent.id));
|
|
if (failure === "viewer") await db.update(companyMemberships).set({ membershipRole: "viewer" }).where(and(eq(companyMemberships.companyId, f.company.id), eq(companyMemberships.principalId, f.userId)));
|
|
if (failure === "wrong-company") id = await f.begin({ company_id: randomUUID() });
|
|
if (failure === "dot-disabled") await instanceSettingsService(db).updateExperimental({ enableOpenAiDot: false });
|
|
await expect(f.oauth.describeDotPairing(id, f.pairing.pairingCode)).rejects.toThrow();
|
|
await expect(f.oauth.consentDotPairing(id, f.pairing.pairingCode)).rejects.toThrow();
|
|
expect(await db.select().from(mcpOauthGrants).where(eq(mcpOauthGrants.userId, f.userId))).toHaveLength(0);
|
|
});
|
|
it("prevents concurrent requests and the personal endpoint from spending the same capability", async () => {
|
|
const f = await fixture();
|
|
const personal = createPublicMcpOAuth(db, { ...config, resource: config.origin + "/mcp/paperclip" });
|
|
await expect(personal.consentDotPairing(f.id, f.pairing.pairingCode)).rejects.toMatchObject({ status: 403 });
|
|
const other = await f.begin();
|
|
const results = await Promise.allSettled([f.oauth.consentDotPairing(f.id, f.pairing.pairingCode), f.oauth.consentDotPairing(other, f.pairing.pairingCode)]);
|
|
expect(results.filter(result => result.status === "fulfilled")).toHaveLength(1);
|
|
expect(await db.select().from(mcpOauthGrants).where(eq(mcpOauthGrants.userId, f.userId))).toHaveLength(1);
|
|
});
|
|
});
|