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.
安裝
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
- This bundle provides the
mcpAuthservice.headers(serverName)reads the persisted login state (synchronously — the loader evaluates it once at boot). - 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')
- Login state lives in
~/.dsh/mcp-auth/tokens.json(created0600; the directory0700).
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 (includingrefresh_tokenand an absoluteexpires_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
| Route | Config | Works against |
|---|---|---|
| A · CIMD (SEP-991 URL-based client id) | clientMetadataUrl: https, non-root path | servers advertising client_id_metadata_document_supported |
| B · asserted client id | staticClientId | servers supporting neither CIMD nor dynamic registration |
| C · dynamic registration | neither (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-demonever validatesclient_idat all). A real authorization server may refuse it. - An unusable
clientMetadataUrlstops 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
clientInformationoutranks 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:
| Key | Default | Meaning |
|---|---|---|
tokenFile | empty | Overrides the storage path (tests only). |
clientMetadataUrl | empty | Route A: https, non-root path client metadata document URL. |
staticClientId | empty | Route 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 undernode_modules/, and re-runpnpm install.
Security
- The token file is created with owner-only permissions (
0600). - The settings UI reaches this plugin over the dsh Typert Gateway (
/apichannel) — themcp-authRemote 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.1only and accepts astate-checked callback. The lazy-auth listener stays bound for the handle's lifetime (the authorize request and the token exchange must present the sameredirect_uri) and closes when the plugin unloads. - A callback whose
statedoes 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_tokenis read; the refreshed token applies to the next boot. - No interactive refresh UI yet.
- The official
@deepseek-ai/dsh-mcp-clientrow on a static header does no 401-challenge handling (its bundle has zero hits for401|WWW-Authenticate|oauth|authorize) and official packages must not be modified. Resolution: set that row todisabled: trueand let thedsh-mcp-appstool 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 indsh-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@Remotemethod, update both files (and set theimplementationfield 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/).