z-y-q/dsh-mcp-oauth--packages-dsh-mcp-auth ↗★ 0

dsh-mcp-auth

Persistent MCP login state for DeepSeek Harness: OAuth 2.1 / manual header credentials per MCP server, injected into mcp-client rows via !!js. Settings UI talks to the host over Typert Remote.

パッケージ
dsh-mcp-auth
互換性
未検証
Harness ピア範囲
>=0.1.7-rc.2 <0.3.0
Cordis ピア範囲
>=0.1.0-rc.0
バージョン
0.2.1
ライセンス
MIT
最終更新
2026/10/28

インストール

$npx -p @deepseek-ai/dsh dsh plugin --profile web add 'github:z-y-q/dsh-mcp-oauth#17fdde6d3a2fd1cfac47cf66faec55c45e2ca056&path:packages/dsh-mcp-auth'

ドキュメント

README 全文を読む ↗

dsh-mcp-auth

Requires: dsh ≥ v0.1.7-rc.2 (compatible with 0.1.7 / 0.2.x, peer range `>=0.1.7-rc.2 2026-10-20 or later). This is a dsh plugin — install it into a dsh profile and let the dsh host load it; it is not a standalone npm library. Desktop app (DeepSeek Harness.app) is supported: the「MCP 登录」settings section was redesigned (cards, banners, status dots, host-theme aware) and the full OAuth flow is verified on desktop 0.2.1-rc.2.

Persistent MCP login state for DeepSeek Harness: per-server credentials (OAuth 2.1 tokens or manual header values) stored under the dsh home and injected into official mcp-client rows through a !!js config expression. The official packages are never modified.

How it works

  1. This bundle provides the mcpAuth service. headers(serverName) reads the persisted login state (synchronously — the loader evaluates it once at boot).
  2. An mcp-client row waits for the service and injects the headers:
- id: my-server
  name: '@deepseek-ai/dsh-mcp-client'
  inject: [mcpAuth]
  config:
    transport: streamable-http
    serverName: nocodb
    url: https://mcp.example.com/mcp
    headers: !!js ctx.mcpAuth.headers('nocodb')
  1. Login state lives in ~/.dsh/mcp-auth/tokens.json (created 0600; the directory 0700).

Login flows (「MCP 登录」settings section)

  • Manual credentials — paste a header (e.g. Authorization: Bearer … or an API-token header). Stored verbatim and injected as-is.
  • OAuth 2.1 — built on the MCP SDK's auth() state machine: RFC 9728 protected-resource discovery, client registration (see "Client identity" below), PKCE (S256), a throwaway loopback redirect listener, and the platform browser for the authorization step. Tokens (including refresh_token and an absolute expires_at) persist across restarts.

On-demand login (lazy auth)

When a long-lived MCP connection (for instance the one dsh-mcp-apps opens per server) gets a 401, the SDK's streamable-HTTP transport re-runs auth() itself: when no human is needed it simply retries, and when a consent page has to be approved it throws UnauthorizedError and hands control back — the SDK cannot wait for a person. mcpAuth therefore also provides:

lazyAuth(serverName, serverUrl, scope?): Promise

The handle exposes provider (hand it to the transport's authProvider), authorize() (waits for the approval, exchanges the code, persists tokens), authorizationUrl(), lastError() and dispose(). The authorization URL is published through the list projection (so the settings section can show it) and printed to stdout, which is the only way to reach it on a headless host (DSH_MCP_AUTH_NO_BROWSER=1).

For a worked example see dsh-mcp-apps: it installs provider on its own connection, catches UnauthorizedError, calls authorize(), then retries the call once.

⚠️ Installing an authProvider means the static Authorization must be removed from requestInit.headers. The SDK's _commonHeaders() spreads requestInit.headers after the bearer it derives from the provider, so a leftover static header overrides the token that was just obtained and silently defeats the whole flow.

Dead-grant self-healing

Once the server has revoked the session (including the "authorize-to-suicide" shape: a protected revoke tool invoked while logged out triggers a fresh authorization that this very revoke then kills), the SDK's refresh path throws a plain error — not UnauthorizedError — and without handling, that same failure would repeat forever. When the error message contains refresh, revoked or invalid_grant, authorize() drops the dead credentials and re-runs the interactive flow within the same round; the user just approves once more. lastError() always reports why an attempt failed (never silently).

There is also reset(): drops the OAuth state only, keeping manual header credentials.

Client identity

RouteConfigWorks against
A · CIMD (SEP-991 URL-based client id)clientMetadataUrl: https, non-root pathservers advertising client_id_metadata_document_supported
B · asserted client idstaticClientIdservers supporting neither CIMD nor dynamic registration
C · dynamic registrationneither (default)servers offering a registration_endpoint
  • A wins when both are set: a deployment that moved to the portable route should not be dragged back by a leftover staticClientId.
  • B is a shortcut, not a portable solution: it works only where the server accepts the asserted id (the lazy-auth-server-demo never validates client_id at all). A real authorization server may refuse it.
  • An unusable clientMetadataUrl stops startup with the offending value in the message. The MCP SDK accepts only https URLs with a non-root path (isHttpsUrl), with no localhost exemption, so a loopback http metadata document cannot be used — debug locally with B, or host the document on real HTTPS.
  • Already-registered clientInformation outranks the config: a refresh has to name the same client id the grant was issued to.

Config

The bundle row takes no required config. Optional:

KeyDefaultMeaning
tokenFileemptyOverrides the storage path (tests only).
clientMetadataUrlemptyRoute A: https, non-root path client metadata document URL.
staticClientIdemptyRoute B: the asserted client_id.

Install

Add to your profile (~/.dsh/profiles/web/package.json) dependencies:

"dsh-mcp-auth": "^0.2.0"

and list "dsh-mcp-auth" in dsh.profile.bundles, then run pnpm install inside the profile directory. Peer dependencies (@deepseek-ai/cordis, dsh-typert-protocol) do not need manual installation — the dsh host ships them and Node resolves them from your dsh install.

Upgrading from a 0.1.x link: install: change the dependency to the version form above, remove any stale symlink under node_modules/, and re-run pnpm install.

Security

  • The token file is created with owner-only permissions (0600).
  • The settings UI reaches this plugin over the dsh Typert Gateway (/api channel) — the mcp-auth Remote namespace (list / save-headers / forget / oauth-start) sits behind the same browser-trust fence as /api (wrong Origin → 403). As of 0.2.0 there is no separate loopback RPC listener and no per-boot token.
  • The OAuth redirect listener binds 127.0.0.1 only and accepts a state-checked callback. The lazy-auth listener stays bound for the handle's lifetime (the authorize request and the token exchange must present the same redirect_uri) and closes when the plugin unloads.
  • A callback whose state does not match the attempt in flight is rejected with 400 and never completes the flow.
  • Waiting for approval is bounded (5 minutes by default), so closing the tab cannot leave the caller hanging forever.

Known limitations

  • headers() snapshots at boot: a login completed after boot takes effect on the next restart (or the row's own reconnect). Lazy auth is not affected — its transport reads the token from the provider on every request, so the retry after an approval takes effect immediately.
  • Token refresh runs best-effort when an expired token with a refresh_token is read; the refreshed token applies to the next boot.
  • No interactive refresh UI yet.
  • The official @deepseek-ai/dsh-mcp-client row on a static header does no 401-challenge handling (its bundle has zero hits for 401|WWW-Authenticate|oauth|authorize) and official packages must not be modified. Resolution: set that row to disabled: true and let the dsh-mcp-apps tool bridge (registerTools: true) own the tool registration — model-side calls then ride the live-credential connection, restart-free. See the "Live-credential tool bridge" section in dsh-mcp-apps/README.md.

Model experience

No model-visible surface changes: no tools, prompt sections, or session events. Requests and KV-cache behavior are unchanged.

Development

pnpm install --ignore-workspace
pnpm run build   # tsdown twice: node half (@Remote decorator lowering) +
                 # client half; copies the hand-written Typert artifacts
                 # (typert.host / typert.remote-client) into lib/
pnpm test

The Typert artifacts are hand-maintained (src/typert.host.manual.js / typert.remote-client.manual.js): the official generator's package mode only recognizes workspace-internal packages, not a protocol installed from npm. When adding or changing an @Remote method, update both files (and set the implementation field whenever the wire name differs from the JS method name). The 0.1.x rpc-server / settings.register route is retired (_attic-0.1.x/).