Skip to content

store every MCP credential in one shape, {auth: 'none' | 'token' | 'oauth', …} - #384

Merged
hkonsti merged 1 commit into
hkonsti/mcp-server-editingfrom
hkonsti/unify-mcp-credentials
Nov 25, 2026
Merged

hkonsti merged 1 commit into
hkonsti/mcp-server-editingfrom
hkonsti/unify-mcp-credentials

Conversation

@hkonsti

@hkonsti hkonsti commented Nov 25, 2026 •

Copy link
Copy Markdown
Collaborator

Stacked on #383.

An integration built on its MCP server stored a key as {auth: 'key', key} and a sign-in's tokens bare, with no auth at all, while a custom server stored {auth: 'none' | 'token' | 'oauth', url, …}. The session code coped by letting any unknown field through (mcpCredentialSchema.loose()), so a custom server's address survived a token refresh. Every MCP credential now has the one shape.

What changes

  • mcp/mcp-credential.ts holds the shapes: {auth: 'none'}, {auth: 'token', token}, {auth: 'oauth', clientId, accessToken, refreshToken, expiresAt}. A built-in integration's is token or oauth; a custom server's is any of the three with its url beside it (and the server's endpoints for oauth), as it already was, so custom credentials are untouched.
  • token rather than bearer: a custom server's token is sent as written when it starts with its scheme (Basic …), so it isn't always a bearer token, and keeping token leaves every custom credential as it is.
  • mcp-session.ts reads with a schema instead of .loose(): withMcpToken with the built-in one, withMcpSignIn with a custom server's sign-in schema, which keeps its address and endpoints when its tokens are refreshed.
  • McpCredential, which was only a sign-in's tokens, is McpTokens.
  • Writers (mcp-connect.ts for keys and sign-ins, take-over-custom.ts, the seed) write the new shape.

Data migration unify-mcp-credentials

  • Rewrites every connection of a built-in MCP integration that has a credential in the old shape, each only while it is still the one read (rewriteConnectionCredential, compare-and-set on the encrypted value), so tokens refreshed meanwhile are never undone. Unlike a refresh it leaves credentialRejectedAt as it was.
  • One changed meanwhile, or not saved, leaves the migration un-noted, to run again on the next start; one that can't be decrypted is logged and passed over, as it was unusable before too.
  • The old shapes are still read (storedBuiltInCredentialSchema), mapped to the new ones. Data migrations run after an instance starts listening, and while a deploy rolls over an instance of the code before may still write them. Once every instance has run the migration, the legacy readers and the migration can go.
  • During that rollover an instance of the old code can't read a key stored in the new shape ({auth: 'token'}), so a call on an Exa, Parallel, Linear or Cloudflare key connection handled by it fails until it is replaced. Sign-ins are fine either way (the old code read them loosely).

Testing

  • bun run typecheck, bun run lint, bun run test pass.
  • New unify-credentials.test.ts: rewrites keys and bare tokens over the one read, leaves current ones, retries on a change meanwhile or a database error, passes over one it can't decrypt.
  • mcp-session.test.ts: bare tokens and {auth: 'key'} are still read, and saved in the new shape on refresh; a custom sign-in keeps its address.
  • Against the local database: data seeded by the code before had its three sign-ins rewritten on start; a key planted in the old shape, marked as turned down, was rewritten with the mark kept.

@hkonsti
hkonsti added this pull request to stack #385 November 25, 2026 22:30
@hkonsti hkonsti changed the title store every MCP credential in one shape, {auth: 'none' | 'token' | 'oauth', …}, built in and custom alike store every MCP credential in one shape, {auth: 'none' | 'token' | 'oauth', …} Nov 25, 2026
…auth', …}, built in and custom alike

* mcp/mcp-credential.ts holds the shapes: a built-in integration's key is {auth: 'token', token} rather than {auth: 'key', key}, and a sign-in's tokens are under {auth: 'oauth'} rather than bare; a custom server's credential is the same with its address beside it, as it already was
* the data migration unify-mcp-credentials rewrites every built-in MCP connection's credential, each only while it is still the one read, leaving whether the provider turned it down as it was; one changed meanwhile has it tried again on the next start
* the old shapes are still read, as they are now, until every instance has run it: an instance of the code before may still write them while a deploy rolls over
* mcp-session.ts reads credentials with a schema rather than letting unknown fields through: built-in integrations with theirs, a custom server's sign-in with its own (withMcpSignIn), which keeps its address and endpoints when its tokens are refreshed
* McpCredential, the tokens of a sign-in, is McpTokens
* the seed writes the new shape
@hkonsti
hkonsti force-pushed the hkonsti/unify-mcp-credentials branch from c7be4fb to 37e64f0 Compare November 25, 2026 22:48
@hkonsti
hkonsti merged commit 0392d68 into main Nov 25, 2026
6 checks passed
hkonsti added a commit that referenced this pull request Nov 25, 2026
 and #384 left (#390)

* read MCP credentials only in the shape they have now; the old shapes only in the data migration, run again for what an instance from before wrote while #384 rolled over

* the old shapes, a key as {auth: 'key', key} and a sign-in's tokens bare, move from mcp-credential.ts into unify-credentials.ts: the session reads a built-in integration's credential with builtInCredentialSchema alone
* the migration runs again as unify-mcp-credentials-again: an instance from before #384 could still write the old shapes while its deploy rolled over, after the first run was noted as done
* Sendable in mcp-session.ts was BuiltInCredential under another name

* connect a custom server and move one on its page through one step; withMcpSignIn is withMcpCredential

* connectServer in custom/route.ts asks the server for its tools and saves the connection, or begins the sign-in it asks for, for a new connection and for one moved on its page alike; what differs is its Destination
* withMcpSignIn sends a token or key as well as a sign-in's access token, so it is withMcpCredential
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant