← All Labs Lab 15 / 28 MCP OAuth sign-in once, refresh after
Lab 15 · Tools & UI · Proposed

MCP OAuth sign-in once, refresh after

RemoteMcpClient signs in to an mcp-serve OAuth 2.1 server once, and after its access token expires it keeps calling tools by refreshing, without a second sign-in.

Proposed Needs: tool-calling Source: examples/15-mcp-oauth-refresh

Not built yet. PROMPT.md is the prompt to build it from.

An agent that uses an OAuth-protected MCP server shouldn't send its person back to a sign-in page every hour. This lab signs RemoteMcpClient (packages/protocols/src/mcp/client/remote.ts) in to an mcp-serve server (createMcpServeMount, MemoryStore, a stub upstream identity provider) once, expires the access token, and checks the next tool call succeeds by refreshing, with no second authorization request.

The checks the build should end with:

CheckWhat it shows
signed-in-onceThe first connect runs one authorization request and a tool call returns the signed-in user
refreshed-silentlyAfter the access token expires, the next tool call succeeds, the server saw a refresh_token grant, and there was no second authorization request
old-access-refusedThe expired access token on its own gets 401 (the control: refresh is what made the call work)

Moved out

  • ui:// views from an MCP tool are already covered by Lab 13 (summary to the model, view to the user) and the preserve-mcp-ui-embedded-resources client tests.
  • Discovery labelling a 401 as oauth-required is covered by packages/protocols/src/discovery/discover.test.ts since D-30.

Known going in

  • mcp-serve's access-token lifetime is a constant (ACCESS_TOKEN_TTL = 3600 in oauth/token.ts), so the lab expires the token through the store, not by waiting.
  • The client stores tokens in a file (authStorePath, default ~/.umwelten/mcp-auth/); the lab must point it at a temp dir.

Prompt

Coding-Agent Prompt · Ready to Paste
Build `examples/15-mcp-oauth-refresh/run.ts`: a self-contained, offline lab that checks RemoteMcpClient signs in to an OAuth 2.1 MCP server once and then survives access-token expiry by refreshing, without a second sign-in.

Read first: `packages/protocols/src/mcp/client/remote.ts` (RemoteMcpClient options `authStorePath`, `oauthPort`, `onAuthorizationUrl`, `interactiveAuth`), `packages/protocols/src/mcp-serve/mount.ts`, `server.ts`, `memory-store.ts` and `oauth/token.ts`, and `packages/protocols/src/mcp-serve/mount.test.ts` for how its tests stub the upstream identity provider.

1. On loopback ephemeral ports, start an mcp-serve server (createMcpServeMount + MemoryStore) with one tool, `whoami`, that returns the signed-in user id, and a stub upstream identity provider that approves immediately.
2. Connect RemoteMcpClient with a temp `authStorePath` and an `onAuthorizationUrl` that follows the authorization URL with fetch (redirect: "manual") through the upstream stub back to the client's loopback callback, so no browser opens. Count authorization requests and token-endpoint grants by type.
3. Expire the issued access token through the MemoryStore (ACCESS_TOKEN_TTL is a constant), then call `whoami` again on the same client.
4. Print one PASS/FAIL line per check and exit 1 on any failure: signed-in-once (one authorization request; `whoami` returns the user); refreshed-silently (the call after expiry succeeds, the server saw one refresh_token grant, still one authorization request); old-access-refused (the expired access token alone gets 401).
5. Strict TypeScript, no `any`; clean up servers and the temp dir in `finally`. Add README frontmatter (status built), and a FINDINGS.md with what building it taught us.
Works in Pi, Claude Code, Codex & Antigravity Offline mock first → --live opt-in