Files
PaperClipAI/packages/shared
Dotta 266eccc524 fix(connections): improve custom remote MCP OAuth draft setup, auto-detection, and recovery UX (#23846)
## Thinking Path

> - Paperclip is the open source app people use to manage AI agents for
work
> - People connect external tools to agents through custom remote MCP
servers in `/apps/byo` and manage them on `/apps/:id/permissions`
> - Unauthenticated custom MCP OAuth drafts failed catalog reads with
`oauth_refresh_missing`, displayed as `Paused`, and left operators on a
dead-end `Retry` error screen with no sign-in, auth-method switch, or
remove controls
> - The `/apps/byo` setup flow also lacked upfront read-only auth
detection, display-name editing, custom draft resume, and detached-tab
OAuth completion polling
> - Custom remote MCP servers must work smoothly out of the box without
dead ends or manual trial-and-error
> - This pull request fixes backend OAuth draft/catalog truthfulness,
adds a read-only custom MCP auto-detection probe, adds dedicated draft
setup and error recovery on `AppDetail`, and improves `/apps/byo`
auto-detection, resume, and OAuth waiting UX
> - The benefit is that operators can connect and recover any
standards-compliant remote MCP server cleanly without hitting false
expired-credential dead ends

## Linked Issues or Issue Description

**What happened?**
Opening an unauthenticated custom remote MCP OAuth draft at
`/apps/:id/permissions` triggered a live remote catalog refresh that
threw `422 oauth_refresh_missing` (`"OAuth credentials have expired and
need to be reconnected."`), overwrote stored health to `failed`,
displayed status `Paused`, and replaced the Permissions tab with a
looping `Retry` box. Operators had no way to start sign-in, change the
sign-in method, see OAuth callback failure details, or remove the
connection. In `/apps/byo`, pasting a URL did not detect auth upfront,
did not allow editing the display name, failed to resume custom MCP
drafts via `?resume=<id>`, duplicated the `Unverified server` badge, and
stopped polling when opening sign-in in a new tab.

**Expected behavior**
Unauthenticated OAuth drafts should remain readable without failing
catalog reads or reporting expired credentials, show `Setup incomplete`
with clear `Sign in`, `Change sign-in method`, `Remove`, and `Connection
details` controls, surface OAuth callback errors inline, and keep
recovery actions visible when catalog loading fails. `/apps/byo` should
detect auth upfront in a read-only probe, allow naming the connection,
resume custom MCP drafts cleanly, and detect OAuth completion even when
sign-in opens in a new tab.

**Steps to reproduce**
1. Create a custom remote MCP OAuth draft via `/apps/byo` or `POST
/api/companies/:companyId/tools/apps/connect` without completing browser
OAuth consent.
2. Open `/<prefix>/apps/<connectionId>/permissions`.
3. Observe `Paused` and `"Couldn't load tools for this app / OAuth
credentials have expired and need to be reconnected. [Retry]"` with no
sign-in or recovery controls.

**Paperclip version or commit**
`master` (`fd8ad7d5b8`)

**Deployment mode**
Local dev (pnpm dev)

## What Changed

- **Backend OAuth draft & catalog truthfulness**
- Export `isConnectionAwaitingFirstSignIn` in
`server/src/services/tool-access/connections/first-sign-in.ts` and skip
remote `refreshCatalog()` in
`server/src/services/tool-access/catalog/reader.ts` when an
unauthenticated OAuth draft has an empty catalog.
- Distinguish never-authorized OAuth connections/grants (`422
oauth_challenge`: `"This app needs you to sign in."`) from genuinely
expired tokens missing a refresh token (`422 oauth_refresh_missing`) in
`server/src/services/tool-access/oauth/grant-refresh.ts` and
`server/src/services/tool-access/oauth/connection-refresh.ts`.
- Annotate `firstSignInPending` on `getConnection` in
`server/src/services/tool-access.ts` to match `listConnections`.
- **Read-only custom MCP auto-detection probe**
- Add `probeGenericMcpSchema` and `GenericMcpProbeResult` in
`packages/shared`, capture optional `resource_name` in
`server/src/services/tool-access/oauth/discovery.ts`, implement
read-only `createGenericMcpProbe` in
`server/src/services/tool-access/connections/probe-mcp.ts`, expose `POST
/api/companies/:companyId/tools/apps/probe-mcp` in
`server/src/routes/tool-access.ts`, and add `toolsApi.probeGenericMcp`
in `ui/src/api/tools.ts`.
- Refresh `tests/runner-e2e/source-sdk-public-inputs.json` for the
shared schema and type additions.
- **App detail setup, recovery, and management UI**
- Show `Setup incomplete` for draft connections in
`ui/src/pages/apps/AppDetail.tsx` and
`ui/src/pages/apps/Connections.tsx`.
- Add `FinishSetupCard` in `AppDetail.tsx` with `Sign in` (shared,
personal, and agent policies), `Change sign-in method`, inline OAuth
return error recovery (`?oauth=failed|denied`), live completion polling,
`Remove`, and `Connection details`.
- Scope catalog fetch errors to the Actions area so `IdentitiesSection`,
`ReconnectCard`, and custom MCP management (`AdvancedPanel`) stay
accessible.
- **BYO setup, resume, and OAuth waiting UX**
- Run background read-only probing on `/apps/byo` Step 2 in
`ConnectionSetupFlow.tsx` and `GenericMcpConnectStep.tsx`, add an
editable Display name input, use `Server URL · Connect` for the BYO
stepper, and support resuming/reconnecting custom MCP drafts via
`?resume=<id>&editAuth=1`.
- Remove the duplicate `Unverified server` badge and stuck disabled
`Opening …` button on `OAuthConnectStateScreen`, use non-workspace copy
for custom MCP servers, and keep polling connection activation after
clicking `Open sign-in in a new tab`.

## Verification

- Ran server and UI regression tests (183/183 passing):
- `pnpm vitest run server/src/__tests__/generic-mcp-connection.test.ts
server/src/__tests__/tool-access-cancelled-first-sign-in.integration.test.ts
ui/src/pages/apps/AppDetail.test.tsx
ui/src/features/connections/ConnectionSetupFlow.generic-mcp.test.tsx
ui/src/features/connections/GenericMcpConnectStep.machine.test.tsx
ui/src/pages/apps/generic-mcp-connect.test.ts`
- Ran package typechecks and UI checks:
  - `pnpm --filter @paperclipai/shared typecheck`
  - `pnpm --filter @paperclipai/server typecheck`
  - `pnpm --filter @paperclipai/ui typecheck`
  - `node scripts/check-query-error-rendering.mjs` (0 findings)
  - `pnpm run check:token-gates` (0 violations in changed files)
- Verified end-to-end locally in an isolated worktree dev server against
live remote OAuth MCP endpoints for draft setup, `Change sign-in method`
resume, upfront read-only auto-detection, and OAuth waiting screen.

## Risks

- Low risk: existing active and expired OAuth connections keep
`oauth_refresh_missing` behavior unchanged (covered by explicit
regression tests).
- The new `/probe-mcp` route uses the same company/management
authorization guards, SSRF/private-network checks, self-loop protection,
and OAuth endpoint policy as `/apps/connect`, performs zero database
writes, and makes zero DCR `POST` requests.
- `packages/shared` exports changed, so
`tests/runner-e2e/source-sdk-public-inputs.json` was regenerated for CI.

## Model Used

- Google Antigravity (`gemini-3-pro`, extended thinking, tool use and
code 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
2026-12-05 00:41:30 -06:00
..