@keycardai/mcp 0.12.1 → 2.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +74 -0
- package/dist/cjs/client/auth/providers/base.d.ts +52 -9
- package/dist/cjs/client/auth/providers/base.d.ts.map +1 -1
- package/dist/cjs/client/auth/providers/base.js +33 -11
- package/dist/cjs/client/auth/providers/base.js.map +1 -1
- package/dist/cjs/server/auth/toolScopes.d.ts +54 -0
- package/dist/cjs/server/auth/toolScopes.d.ts.map +1 -0
- package/dist/cjs/server/auth/toolScopes.js +59 -0
- package/dist/cjs/server/auth/toolScopes.js.map +1 -0
- package/dist/cjs/shared/auth.d.ts +8 -8
- package/dist/esm/client/auth/providers/base.d.ts +52 -9
- package/dist/esm/client/auth/providers/base.d.ts.map +1 -1
- package/dist/esm/client/auth/providers/base.js +33 -11
- package/dist/esm/client/auth/providers/base.js.map +1 -1
- package/dist/esm/server/auth/toolScopes.d.ts +54 -0
- package/dist/esm/server/auth/toolScopes.d.ts.map +1 -0
- package/dist/esm/server/auth/toolScopes.js +55 -0
- package/dist/esm/server/auth/toolScopes.js.map +1 -0
- package/dist/esm/shared/auth.d.ts +8 -8
- package/package.json +11 -5
package/README.md
CHANGED
|
@@ -90,6 +90,31 @@ app.get(
|
|
|
90
90
|
|
|
91
91
|
### MCP Client Provider
|
|
92
92
|
|
|
93
|
+
Implements the `OAuthClientProvider` interface from the MCP v2 client SDK
|
|
94
|
+
(`@modelcontextprotocol/client`, an optional peer dependency).
|
|
95
|
+
|
|
96
|
+
**Migrating from v1:** the client half now targets MCP v2, which shipped as a
|
|
97
|
+
package split rather than a version bump — there is no `@modelcontextprotocol/sdk`
|
|
98
|
+
2.x. Install the new client package alongside this one:
|
|
99
|
+
|
|
100
|
+
```bash
|
|
101
|
+
npm install @modelcontextprotocol/client
|
|
102
|
+
```
|
|
103
|
+
|
|
104
|
+
This applies equally if you consume `BaseOAuthClientProvider` through
|
|
105
|
+
`@keycardai/sdk`, which re-exports it. Without the package installed, the
|
|
106
|
+
provider's types resolve to `any` under `skipLibCheck` or fail with TS2307
|
|
107
|
+
without it. Consumers who need the v1 SDK should pin the previous
|
|
108
|
+
`@keycardai/mcp` minor instead; v1 and v2 are not supported side by side.
|
|
109
|
+
|
|
110
|
+
**Servers migrating to the v2 packages need zod 4.** This package keeps its
|
|
111
|
+
own zod 3 dependency (its v2 imports are type-only, so the majors coexist),
|
|
112
|
+
but that does not extend to your application code: v2's
|
|
113
|
+
`registerTool(name, { inputSchema })` types the zod overload against zod 4
|
|
114
|
+
internals, so a zod 3 schema fails to typecheck with a misleading
|
|
115
|
+
"missing properties from ZodType" error. Upgrade your app's zod to `^4`
|
|
116
|
+
when you move your server onto `@modelcontextprotocol/server`.
|
|
117
|
+
|
|
93
118
|
```typescript
|
|
94
119
|
import { BaseOAuthClientProvider } from "@keycardai/mcp/client/auth/providers/base";
|
|
95
120
|
|
|
@@ -101,6 +126,9 @@ class MyOAuthProvider extends BaseOAuthClientProvider {
|
|
|
101
126
|
client_name: "My MCP Client",
|
|
102
127
|
},
|
|
103
128
|
"your-client-id",
|
|
129
|
+
{
|
|
130
|
+
redirectUrl: "http://localhost:3000/callback",
|
|
131
|
+
},
|
|
104
132
|
);
|
|
105
133
|
}
|
|
106
134
|
|
|
@@ -111,6 +139,51 @@ class MyOAuthProvider extends BaseOAuthClientProvider {
|
|
|
111
139
|
}
|
|
112
140
|
```
|
|
113
141
|
|
|
142
|
+
Notes on the v2 provider contract:
|
|
143
|
+
|
|
144
|
+
- `redirectUrl` returns `undefined` when no redirect URL is configured. The
|
|
145
|
+
MCP SDK reads that as a non-interactive provider (`client_credentials`,
|
|
146
|
+
`jwt-bearer`) and skips the authorization redirect leg.
|
|
147
|
+
- Token and client credential values persisted by the SDK carry an
|
|
148
|
+
SDK-stamped authorization-server `issuer` field (`StoredOAuthTokens`,
|
|
149
|
+
`StoredOAuthClientInformation`), and the persistence methods receive an
|
|
150
|
+
optional context with the resolved issuer. The provider forwards that
|
|
151
|
+
context to the configured `tokensStore`.
|
|
152
|
+
- Passing a `discoveryStateStore` in the options enables SEP-2352 discovery
|
|
153
|
+
caching and the callback-leg authorization-server binding check. The store
|
|
154
|
+
must persist with the same durability as the code verifier store: without
|
|
155
|
+
it the SDK re-runs discovery on the callback leg and cannot verify that
|
|
156
|
+
the authorization server matches the one recorded before the redirect.
|
|
157
|
+
|
|
158
|
+
### `req.auth` typing conflict with `@modelcontextprotocol/express`
|
|
159
|
+
|
|
160
|
+
The MCP v2 `@modelcontextprotocol/express` package globally augments
|
|
161
|
+
Express's `Request` interface with `auth?: AuthInfo` (the MCP SDK token
|
|
162
|
+
shape). Once that package is anywhere in your compilation, the
|
|
163
|
+
augmentation is active for every Express request type in the project.
|
|
164
|
+
|
|
165
|
+
This collides with `@keycardai/express`, which types `req.auth` as
|
|
166
|
+
Keycard's `AccessToken`:
|
|
167
|
+
|
|
168
|
+
- The `AuthenticatedRequest` interface (`auth: AccessToken`) extends the
|
|
169
|
+
augmented `Request` whose `auth` is `AuthInfo | undefined`, which is
|
|
170
|
+
TS2430 (incompatible member types: Keycard's `resource` is a string,
|
|
171
|
+
the MCP shape's is a `URL`). With `skipLibCheck: true` the error inside
|
|
172
|
+
the library declaration is suppressed and the
|
|
173
|
+
`req as AuthenticatedRequest` cast pattern keeps working; declaring an
|
|
174
|
+
equivalent interface in your own source surfaces the error.
|
|
175
|
+
- A user-level `declare global namespace Express` augmentation of
|
|
176
|
+
`req.auth` as `AccessToken` does not error, it silently loses:
|
|
177
|
+
the MCP package augments the more derived
|
|
178
|
+
`express-serve-static-core` `Request`, so `req.auth` resolves to
|
|
179
|
+
`AuthInfo | undefined` and Keycard token fields stop typechecking.
|
|
180
|
+
|
|
181
|
+
Workarounds until the two packages agree on a shared property: keep
|
|
182
|
+
`skipLibCheck: true` and the `AuthenticatedRequest` cast pattern instead
|
|
183
|
+
of global augmentation, or isolate the MCP express integration in a
|
|
184
|
+
separate TypeScript project so only one declaration of `req.auth` is in
|
|
185
|
+
scope per compilation.
|
|
186
|
+
|
|
114
187
|
## API Overview
|
|
115
188
|
|
|
116
189
|
### Server Auth Middleware
|
|
@@ -120,6 +193,7 @@ class MyOAuthProvider extends BaseOAuthClientProvider {
|
|
|
120
193
|
| `requireBearerAuth` | `@keycardai/mcp/server/auth/middleware/bearerAuth` | Express middleware — verifies JWT bearer tokens, checks scopes, validates resource claims |
|
|
121
194
|
| `mcpAuthMetadataRouter` | `@keycardai/mcp/server/auth/router` | Express router for `.well-known/oauth-protected-resource` and `.well-known/oauth-authorization-server` |
|
|
122
195
|
| `JWTOAuthTokenVerifier` | `@keycardai/mcp/server/auth/verifiers/jwt` | Token verifier implementing the MCP SDK's `OAuthTokenVerifier` interface |
|
|
196
|
+
| `requireToolScopes`, `missingToolScopes` | `@keycardai/mcp/server/auth/toolScopes` | Per-tool scope checks inside MCP tool handlers via `ctx.http.authInfo` (route-level `requiredScopes` cannot vary per tool on a multiplexed `/mcp` route) |
|
|
123
197
|
|
|
124
198
|
### Delegated Access
|
|
125
199
|
|
|
@@ -1,18 +1,41 @@
|
|
|
1
|
-
import { OAuthClientProvider } from '@modelcontextprotocol/
|
|
2
|
-
import { OAuthClientMetadata, OAuthClientInformation, OAuthTokens, OAuthMetadata } from '@modelcontextprotocol/sdk/shared/auth.js';
|
|
1
|
+
import type { AuthorizationServerMetadata, OAuthClientInformationContext, OAuthClientMetadata, OAuthClientProvider, OAuthDiscoveryState, StoredOAuthClientInformation, StoredOAuthTokens } from '@modelcontextprotocol/client';
|
|
3
2
|
import type { PrivateKeyring } from "@keycardai/oauth/keyring";
|
|
4
3
|
export interface OAuthTokensStore {
|
|
5
|
-
get(): Promise<
|
|
6
|
-
save(tokens:
|
|
4
|
+
get(ctx?: OAuthClientInformationContext): Promise<StoredOAuthTokens | undefined>;
|
|
5
|
+
save(tokens: StoredOAuthTokens, ctx?: OAuthClientInformationContext): void | Promise<void>;
|
|
7
6
|
}
|
|
8
7
|
export interface OAuthCodeVerifierStore {
|
|
9
8
|
get(): string | Promise<string>;
|
|
10
9
|
save(codeVerifier: string): void | Promise<void>;
|
|
11
10
|
}
|
|
11
|
+
/**
|
|
12
|
+
* Persists OAuth discovery state (SEP-2352) so the MCP SDK can skip
|
|
13
|
+
* redundant RFC 9728 / RFC 8414 discovery requests and verify that the
|
|
14
|
+
* authorization server on the redirect callback leg matches the one
|
|
15
|
+
* recorded before the redirect.
|
|
16
|
+
*
|
|
17
|
+
* Implementations MUST persist with the same durability as the code
|
|
18
|
+
* verifier store: the state has to survive the authorization redirect
|
|
19
|
+
* round-trip.
|
|
20
|
+
*/
|
|
21
|
+
/**
|
|
22
|
+
* Single-slot by design: `get`/`save` take no key, so one provider instance
|
|
23
|
+
* holds discovery state for one in-flight authorization at a time. Two
|
|
24
|
+
* concurrent flows against different authorization servers through the same
|
|
25
|
+
* provider will overwrite each other, and the second callback leg then fails
|
|
26
|
+
* the SEP-2352 binding check with `AuthorizationServerMismatchError`. Give each
|
|
27
|
+
* concurrent flow its own provider instance, or back the store with per-flow
|
|
28
|
+
* storage keyed outside this interface.
|
|
29
|
+
*/
|
|
30
|
+
export interface OAuthDiscoveryStateStore {
|
|
31
|
+
get(): OAuthDiscoveryState | undefined | Promise<OAuthDiscoveryState | undefined>;
|
|
32
|
+
save(state: OAuthDiscoveryState): void | Promise<void>;
|
|
33
|
+
}
|
|
12
34
|
export interface BaseOAuthClientProviderOptions {
|
|
13
35
|
redirectUrl?: string | URL;
|
|
14
36
|
tokensStore?: OAuthTokensStore;
|
|
15
37
|
codeVerifierStore?: OAuthCodeVerifierStore;
|
|
38
|
+
discoveryStateStore?: OAuthDiscoveryStateStore;
|
|
16
39
|
privateKeyring?: PrivateKeyring;
|
|
17
40
|
}
|
|
18
41
|
export declare class BaseOAuthClientProvider implements OAuthClientProvider {
|
|
@@ -22,13 +45,33 @@ export declare class BaseOAuthClientProvider implements OAuthClientProvider {
|
|
|
22
45
|
protected privateKeyring: PrivateKeyring | undefined;
|
|
23
46
|
protected tokensStore: OAuthTokensStore | undefined;
|
|
24
47
|
protected codeVerifierStore: OAuthCodeVerifierStore | undefined;
|
|
48
|
+
protected discoveryStateStore: OAuthDiscoveryStateStore | undefined;
|
|
49
|
+
/**
|
|
50
|
+
* SEP-2352 discovery-state persistence. Defined only when a
|
|
51
|
+
* `discoveryStateStore` is configured: the MCP SDK treats the presence of
|
|
52
|
+
* `saveDiscoveryState` as a promise that discovery state is durably
|
|
53
|
+
* persisted, and fails the authorization callback leg when the method
|
|
54
|
+
* exists but no recorded state can be read back. A provider without a
|
|
55
|
+
* store must therefore not define these members at all.
|
|
56
|
+
*/
|
|
57
|
+
saveDiscoveryState?: (state: OAuthDiscoveryState) => void | Promise<void>;
|
|
58
|
+
discoveryState?: () => OAuthDiscoveryState | undefined | Promise<OAuthDiscoveryState | undefined>;
|
|
25
59
|
constructor(metadata: OAuthClientMetadata, clientId?: string, options?: BaseOAuthClientProviderOptions);
|
|
26
|
-
addClientAuthentication(headers: Headers, params: URLSearchParams,
|
|
27
|
-
|
|
60
|
+
addClientAuthentication(headers: Headers, params: URLSearchParams, url: string | URL, metadata?: AuthorizationServerMetadata): Promise<void>;
|
|
61
|
+
/**
|
|
62
|
+
* Returns `undefined` when no redirect URL was configured. The MCP SDK
|
|
63
|
+
* reads that as a non-interactive provider (`client_credentials`,
|
|
64
|
+
* `jwt-bearer`) and skips the authorization redirect leg.
|
|
65
|
+
*/
|
|
66
|
+
get redirectUrl(): string | URL | undefined;
|
|
28
67
|
get clientMetadata(): OAuthClientMetadata;
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
68
|
+
/**
|
|
69
|
+
* This provider holds a single credential set, so the authorization-server
|
|
70
|
+
* binding context is ignored (permitted by the MCP SDK contract).
|
|
71
|
+
*/
|
|
72
|
+
clientInformation(ctx?: OAuthClientInformationContext): StoredOAuthClientInformation | undefined | Promise<StoredOAuthClientInformation | undefined>;
|
|
73
|
+
tokens(ctx?: OAuthClientInformationContext): Promise<StoredOAuthTokens | undefined>;
|
|
74
|
+
saveTokens(tokens: StoredOAuthTokens, ctx?: OAuthClientInformationContext): void | Promise<void>;
|
|
32
75
|
redirectToAuthorization(authorizationUrl: URL): void | Promise<void>;
|
|
33
76
|
saveCodeVerifier(codeVerifier: string): void | Promise<void>;
|
|
34
77
|
codeVerifier(): string | Promise<string>;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"base.d.ts","sourceRoot":"","sources":["../../../../../src/client/auth/providers/base.ts"],"names":[],"mappings":"AAAA,OAAO,
|
|
1
|
+
{"version":3,"file":"base.d.ts","sourceRoot":"","sources":["../../../../../src/client/auth/providers/base.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EACV,2BAA2B,EAC3B,6BAA6B,EAC7B,mBAAmB,EACnB,mBAAmB,EACnB,mBAAmB,EACnB,4BAA4B,EAC5B,iBAAiB,EAClB,MAAM,8BAA8B,CAAC;AACtC,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,0BAA0B,CAAC;AAG/D,MAAM,WAAW,gBAAgB;IAC/B,GAAG,CAAC,GAAG,CAAC,EAAE,6BAA6B,GAAG,OAAO,CAAC,iBAAiB,GAAG,SAAS,CAAC,CAAC;IACjF,IAAI,CAAC,MAAM,EAAE,iBAAiB,EAAE,GAAG,CAAC,EAAE,6BAA6B,GAAG,IAAI,GAAG,OAAO,CAAC,IAAI,CAAC,CAAC;CAC5F;AAED,MAAM,WAAW,sBAAsB;IACrC,GAAG,IAAI,MAAM,GAAG,OAAO,CAAC,MAAM,CAAC,CAAC;IAChC,IAAI,CAAC,YAAY,EAAE,MAAM,GAAG,IAAI,GAAG,OAAO,CAAC,IAAI,CAAC,CAAC;CAClD;AAED;;;;;;;;;GASG;AACH;;;;;;;;GAQG;AACH,MAAM,WAAW,wBAAwB;IACvC,GAAG,IAAI,mBAAmB,GAAG,SAAS,GAAG,OAAO,CAAC,mBAAmB,GAAG,SAAS,CAAC,CAAC;IAClF,I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,SAAS,CAE1C;IAED,IAAI,cAAc,IAAI,mBAAmB,CAExC;IAED;;;OAGG;IACH,iBAAiB,CAAC,GAAG,CAAC,EAAE,6BAA6B,GAAG,4BAA4B,GAAG,SAAS,GAAG,OAAO,CAAC,4BAA4B,GAAG,SAAS,CAAC;IAQpJ,MAAM,CAAC,GAAG,CAAC,EAAE,6BAA6B,GAAG,OAAO,CAAC,iBAAiB,GAAG,SAAS,CAAC;IAOnF,UAAU,CAAC,MAAM,EAAE,iBAAiB,EAAE,GAAG,CAAC,EAAE,6BAA6B,GAAG,IAAI,GAAG,OAAO,CAAC,IAAI,CAAC;IAOhG,uBAAuB,CAAC,gBAAgB,EAAE,GAAG,GAAG,IAAI,GAAG,OAAO,CAAC,IAAI,CAAC;IAIpE,gBAAgB,CAAC,YAAY,EAAE,MAAM,GAAG,IAAI,GAAG,OAAO,CAAC,IAAI,CAAC;IAO5D,YAAY,IAAI,MAAM,GAAG,OAAO,CAAC,MAAM,CAAC;CAMzC"}
|
|
@@ -10,17 +10,30 @@ class BaseOAuthClientProvider {
|
|
|
10
10
|
this.tokensStore = options?.tokensStore;
|
|
11
11
|
this.codeVerifierStore = options?.codeVerifierStore;
|
|
12
12
|
this.privateKeyring = options?.privateKeyring;
|
|
13
|
+
// Assigned as own properties, and only when a store exists, because SEP-2352
|
|
14
|
+
// keys off whether these members are *defined*: the v2 client throws
|
|
15
|
+
// AuthorizationServerMismatchError if saveDiscoveryState exists but no state
|
|
16
|
+
// reads back. Declaring them as prototype methods would make them always
|
|
17
|
+
// defined and break that check. The tradeoff is that an own property shadows
|
|
18
|
+
// a subclass's prototype override of the same name, so a subclass wanting
|
|
19
|
+
// custom discovery-state behavior should supply a discoveryStateStore rather
|
|
20
|
+
// than override saveDiscoveryState/discoveryState.
|
|
21
|
+
const discoveryStateStore = options?.discoveryStateStore;
|
|
22
|
+
if (discoveryStateStore) {
|
|
23
|
+
this.discoveryStateStore = discoveryStateStore;
|
|
24
|
+
this.saveDiscoveryState = (state) => discoveryStateStore.save(state);
|
|
25
|
+
this.discoveryState = () => discoveryStateStore.get();
|
|
26
|
+
}
|
|
13
27
|
// workaround to bind function to this context, since underlying
|
|
14
28
|
// MCP library calls it without a context.
|
|
15
29
|
this.addClientAuthentication = this.addClientAuthentication.bind(this);
|
|
16
30
|
}
|
|
17
|
-
async addClientAuthentication(headers, params,
|
|
31
|
+
async addClientAuthentication(headers, params, url, metadata) {
|
|
18
32
|
const clientInfo = await this.clientInformation();
|
|
19
33
|
if (!clientInfo) {
|
|
20
34
|
throw new Error("Client information not available for authentication");
|
|
21
35
|
}
|
|
22
36
|
const clientInformation = { ...this._metadata, ...clientInfo };
|
|
23
|
-
const supportedMethods = metadata?.token_endpoint_auth_methods_supported ?? [];
|
|
24
37
|
const authMethod = clientInformation.token_endpoint_auth_method || 'client_secret_basic';
|
|
25
38
|
switch (authMethod) {
|
|
26
39
|
case 'private_key_jwt':
|
|
@@ -28,9 +41,12 @@ class BaseOAuthClientProvider {
|
|
|
28
41
|
if (!this.privateKeyring) {
|
|
29
42
|
throw new Error("Private keyring not initialized");
|
|
30
43
|
}
|
|
44
|
+
// The MCP SDK passes the token endpoint URL being called as `url`;
|
|
45
|
+
// prefer the metadata value when present since it is the validated
|
|
46
|
+
// RFC 8414 document entry.
|
|
31
47
|
const tokenUrl = metadata?.token_endpoint
|
|
32
48
|
? new URL(metadata.token_endpoint)
|
|
33
|
-
: new URL(
|
|
49
|
+
: new URL(url);
|
|
34
50
|
const now = Date.now();
|
|
35
51
|
const signer = new jwt_js_1.JSONWebTokenSigner(this.privateKeyring);
|
|
36
52
|
// RFC 7523 section 3: for client authentication both "iss" and
|
|
@@ -64,32 +80,38 @@ class BaseOAuthClientProvider {
|
|
|
64
80
|
break;
|
|
65
81
|
}
|
|
66
82
|
}
|
|
83
|
+
/**
|
|
84
|
+
* Returns `undefined` when no redirect URL was configured. The MCP SDK
|
|
85
|
+
* reads that as a non-interactive provider (`client_credentials`,
|
|
86
|
+
* `jwt-bearer`) and skips the authorization redirect leg.
|
|
87
|
+
*/
|
|
67
88
|
get redirectUrl() {
|
|
68
|
-
if (!this._redirectUrl) {
|
|
69
|
-
throw new Error("Attempt to access redirectUrl before it was set");
|
|
70
|
-
}
|
|
71
89
|
return this._redirectUrl;
|
|
72
90
|
}
|
|
73
91
|
get clientMetadata() {
|
|
74
92
|
return this._metadata;
|
|
75
93
|
}
|
|
76
|
-
|
|
94
|
+
/**
|
|
95
|
+
* This provider holds a single credential set, so the authorization-server
|
|
96
|
+
* binding context is ignored (permitted by the MCP SDK contract).
|
|
97
|
+
*/
|
|
98
|
+
clientInformation(ctx) {
|
|
77
99
|
if (this._clientId) {
|
|
78
100
|
return { ...this.clientMetadata, client_id: this._clientId };
|
|
79
101
|
}
|
|
80
102
|
return undefined;
|
|
81
103
|
}
|
|
82
|
-
tokens() {
|
|
104
|
+
tokens(ctx) {
|
|
83
105
|
if (!this.tokensStore) {
|
|
84
106
|
throw new Error("OAuth tokens store not initialized");
|
|
85
107
|
}
|
|
86
|
-
return this.tokensStore.get();
|
|
108
|
+
return this.tokensStore.get(ctx);
|
|
87
109
|
}
|
|
88
|
-
saveTokens(tokens) {
|
|
110
|
+
saveTokens(tokens, ctx) {
|
|
89
111
|
if (!this.tokensStore) {
|
|
90
112
|
throw new Error("OAuth tokens store not initialized");
|
|
91
113
|
}
|
|
92
|
-
return this.tokensStore.save(tokens);
|
|
114
|
+
return this.tokensStore.save(tokens, ctx);
|
|
93
115
|
}
|
|
94
116
|
redirectToAuthorization(authorizationUrl) {
|
|
95
117
|
throw new Error('redirectToAuthorization not implemented');
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"base.js","sourceRoot":"","sources":["../../../../../src/client/auth/providers/base.ts"],"names":[],"mappings":";;;
|
|
1
|
+
{"version":3,"file":"base.js","sourceRoot":"","sources":["../../../../../src/client/auth/providers/base.ts"],"names":[],"mappings":";;;AAUA,8CAAuD;AA4CvD,MAAa,uBAAuB;IAsBlC,Y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,gBAAgB,CAAC,YAAoB;QACnC,IAAI,CAAC,IAAI,CAAC,iBAAiB,EAAE,CAAC;YAC5B,MAAM,IAAI,KAAK,CAAC,2CAA2C,CAAC,CAAC;QAC/D,CAAC;QACD,OAAO,IAAI,CAAC,iBAAiB,CAAC,IAAI,CAAC,YAAY,CAAC,CAAC;IACnD,CAAC;IAED,YAAY;QACV,IAAI,CAAC,IAAI,CAAC,iBAAiB,EAAE,CAAC;YAC5B,MAAM,IAAI,KAAK,CAAC,2CAA2C,CAAC,CAAC;QAC/D,CAAC;QACD,OAAO,IAAI,CAAC,iBAAiB,CAAC,GAAG,EAAE,CAAC;IACtC,CAAC;CACF;AAvKD,0DAuKC"}
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
import type { AuthInfo } from "../../shared/auth.js";
|
|
2
|
+
/**
|
|
3
|
+
* The slice of the MCP v2 tool handler context that carries HTTP transport
|
|
4
|
+
* authentication. Structurally compatible with the `ctx` argument the
|
|
5
|
+
* `@modelcontextprotocol/server` request handlers receive, so handlers can
|
|
6
|
+
* pass their context straight through without depending on that package's
|
|
7
|
+
* types here.
|
|
8
|
+
*/
|
|
9
|
+
export interface ToolAuthContext {
|
|
10
|
+
http?: {
|
|
11
|
+
authInfo?: AuthInfo;
|
|
12
|
+
};
|
|
13
|
+
}
|
|
14
|
+
/**
|
|
15
|
+
* Returns the required scopes the current tool call's access token does not
|
|
16
|
+
* carry. An unauthenticated call (no `ctx.http.authInfo`) grants nothing, so
|
|
17
|
+
* every required scope is reported missing.
|
|
18
|
+
*
|
|
19
|
+
* All MCP tools share one HTTP route, so route-level `requiredScopes` on
|
|
20
|
+
* `requireBearerAuth` can only express scopes common to every tool. Per-tool
|
|
21
|
+
* requirements are checked inside the tool handler with this helper.
|
|
22
|
+
*/
|
|
23
|
+
export declare function missingToolScopes(ctx: ToolAuthContext, requiredScopes: readonly string[]): string[];
|
|
24
|
+
/**
|
|
25
|
+
* Asserts that the current tool call is authenticated and its access token
|
|
26
|
+
* carries every required scope, returning the validated `AuthInfo`.
|
|
27
|
+
*
|
|
28
|
+
* Throws `InsufficientScopeError` when the call is unauthenticated or scopes
|
|
29
|
+
* are missing, which stops the handler and fails the tool call.
|
|
30
|
+
*
|
|
31
|
+
* What the client actually receives is a plain tool error, not an OAuth
|
|
32
|
+
* challenge. `@modelcontextprotocol/server` catches every handler throw except
|
|
33
|
+
* `UrlElicitationRequiredError` and flattens it into
|
|
34
|
+
* `{ content: [{ type: "text", ... }], isError: true }` at HTTP 200. So the
|
|
35
|
+
* `insufficient_scope` code, the 403, and the `WWW-Authenticate` header do not
|
|
36
|
+
* reach the wire from here — only the error's message text does. This helper
|
|
37
|
+
* enforces the requirement; it cannot signal it in a machine-readable way.
|
|
38
|
+
*
|
|
39
|
+
* That makes it unsuitable on its own for driving a scope step-up: the client
|
|
40
|
+
* cannot distinguish missing scopes from any other tool failure. For an
|
|
41
|
+
* interactive step-up, v2's own primitive is `UrlElicitationRequiredError`,
|
|
42
|
+
* the one error type it re-throws rather than flattening. For a structured
|
|
43
|
+
* in-band signal, use `missingToolScopes` and return a tool result your client
|
|
44
|
+
* knows how to interpret.
|
|
45
|
+
*
|
|
46
|
+
* ```ts
|
|
47
|
+
* server.registerTool("delete_repo", config, async (args, ctx) => {
|
|
48
|
+
* const authInfo = requireToolScopes(ctx, ["repo:delete"]);
|
|
49
|
+
* // ...
|
|
50
|
+
* });
|
|
51
|
+
* ```
|
|
52
|
+
*/
|
|
53
|
+
export declare function requireToolScopes(ctx: ToolAuthContext, requiredScopes: readonly string[]): AuthInfo;
|
|
54
|
+
//# sourceMappingURL=toolScopes.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"toolScopes.d.ts","sourceRoot":"","sources":["../../../../src/server/auth/toolScopes.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,QAAQ,EAAE,MAAM,sBAAsB,CAAC;AAErD;;;;;;GAMG;AACH,MAAM,WAAW,eAAe;IAC9B,IAAI,CAAC,EAAE;QACL,QAAQ,CAAC,EAAE,QAAQ,CAAC;KACrB,CAAC;CACH;AAED;;;;;;;;GAQG;AACH,wBAAgB,iBAAiB,CAC/B,GAAG,EAAE,eAAe,EACpB,cAAc,EAAE,SAAS,MAAM,EAAE,GAChC,MAAM,EAAE,CAGV;AAED;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA4BG;AACH,wBAAgB,iBAAiB,CAC/B,GAAG,EAAE,eAAe,EACpB,cAAc,EAAE,SAAS,MAAM,EAAE,GAChC,QAAQ,CAgBV"}
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.missingToolScopes = missingToolScopes;
|
|
4
|
+
exports.requireToolScopes = requireToolScopes;
|
|
5
|
+
const errors_1 = require("@keycardai/oauth/errors");
|
|
6
|
+
/**
|
|
7
|
+
* Returns the required scopes the current tool call's access token does not
|
|
8
|
+
* carry. An unauthenticated call (no `ctx.http.authInfo`) grants nothing, so
|
|
9
|
+
* every required scope is reported missing.
|
|
10
|
+
*
|
|
11
|
+
* All MCP tools share one HTTP route, so route-level `requiredScopes` on
|
|
12
|
+
* `requireBearerAuth` can only express scopes common to every tool. Per-tool
|
|
13
|
+
* requirements are checked inside the tool handler with this helper.
|
|
14
|
+
*/
|
|
15
|
+
function missingToolScopes(ctx, requiredScopes) {
|
|
16
|
+
const grantedScopes = new Set(ctx.http?.authInfo?.scopes ?? []);
|
|
17
|
+
return requiredScopes.filter((scope) => !grantedScopes.has(scope));
|
|
18
|
+
}
|
|
19
|
+
/**
|
|
20
|
+
* Asserts that the current tool call is authenticated and its access token
|
|
21
|
+
* carries every required scope, returning the validated `AuthInfo`.
|
|
22
|
+
*
|
|
23
|
+
* Throws `InsufficientScopeError` when the call is unauthenticated or scopes
|
|
24
|
+
* are missing, which stops the handler and fails the tool call.
|
|
25
|
+
*
|
|
26
|
+
* What the client actually receives is a plain tool error, not an OAuth
|
|
27
|
+
* challenge. `@modelcontextprotocol/server` catches every handler throw except
|
|
28
|
+
* `UrlElicitationRequiredError` and flattens it into
|
|
29
|
+
* `{ content: [{ type: "text", ... }], isError: true }` at HTTP 200. So the
|
|
30
|
+
* `insufficient_scope` code, the 403, and the `WWW-Authenticate` header do not
|
|
31
|
+
* reach the wire from here — only the error's message text does. This helper
|
|
32
|
+
* enforces the requirement; it cannot signal it in a machine-readable way.
|
|
33
|
+
*
|
|
34
|
+
* That makes it unsuitable on its own for driving a scope step-up: the client
|
|
35
|
+
* cannot distinguish missing scopes from any other tool failure. For an
|
|
36
|
+
* interactive step-up, v2's own primitive is `UrlElicitationRequiredError`,
|
|
37
|
+
* the one error type it re-throws rather than flattening. For a structured
|
|
38
|
+
* in-band signal, use `missingToolScopes` and return a tool result your client
|
|
39
|
+
* knows how to interpret.
|
|
40
|
+
*
|
|
41
|
+
* ```ts
|
|
42
|
+
* server.registerTool("delete_repo", config, async (args, ctx) => {
|
|
43
|
+
* const authInfo = requireToolScopes(ctx, ["repo:delete"]);
|
|
44
|
+
* // ...
|
|
45
|
+
* });
|
|
46
|
+
* ```
|
|
47
|
+
*/
|
|
48
|
+
function requireToolScopes(ctx, requiredScopes) {
|
|
49
|
+
const authInfo = ctx.http?.authInfo;
|
|
50
|
+
if (!authInfo) {
|
|
51
|
+
throw new errors_1.InsufficientScopeError("Tool call is not authenticated; no access token information is available on the request context");
|
|
52
|
+
}
|
|
53
|
+
const missingScopes = missingToolScopes(ctx, requiredScopes);
|
|
54
|
+
if (missingScopes.length > 0) {
|
|
55
|
+
throw new errors_1.InsufficientScopeError(`Tool call requires additional scopes: ${missingScopes.join(" ")}`);
|
|
56
|
+
}
|
|
57
|
+
return authInfo;
|
|
58
|
+
}
|
|
59
|
+
//# sourceMappingURL=toolScopes.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"toolScopes.js","sourceRoot":"","sources":["../../../../src/server/auth/toolScopes.ts"],"names":[],"mappings":";;AAyBA,8CAMC;AA+BD,8CAmBC;AAjFD,oDAAiE;AAgBjE;;;;;;;;GAQG;AACH,SAAgB,iBAAiB,CAC/B,GAAoB,EACpB,cAAiC;IAEjC,MAAM,aAAa,GAAG,IAAI,GAAG,CAAC,GAAG,CAAC,IAAI,EAAE,QAAQ,EAAE,MAAM,IAAI,EAAE,CAAC,CAAC;IAChE,OAAO,cAAc,CAAC,MAAM,CAAC,CAAC,KAAK,EAAE,EAAE,CAAC,CAAC,aAAa,CAAC,GAAG,CAAC,KAAK,CAAC,CAAC,CAAC;AACrE,CAAC;AAED;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA4BG;AACH,SAAgB,iBAAiB,CAC/B,GAAoB,EACpB,cAAiC;IAEjC,MAAM,QAAQ,GAAG,GAAG,CAAC,IAAI,EAAE,QAAQ,CAAC;IACpC,IAAI,CAAC,QAAQ,EAAE,CAAC;QACd,MAAM,IAAI,+BAAsB,CAC9B,iGAAiG,CAClG,CAAC;IACJ,CAAC;IAED,MAAM,aAAa,GAAG,iBAAiB,CAAC,GAAG,EAAE,cAAc,CAAC,CAAC;IAC7D,IAAI,aAAa,CAAC,MAAM,GAAG,CAAC,EAAE,CAAC;QAC7B,MAAM,IAAI,+BAAsB,CAC9B,yCAAyC,aAAa,CAAC,IAAI,CAAC,GAAG,CAAC,EAAE,CACnE,CAAC;IACJ,CAAC;IAED,OAAO,QAAQ,CAAC;AAClB,CAAC"}
|
|
@@ -2,8 +2,8 @@
|
|
|
2
2
|
* Information about a validated access token, provided to request handlers.
|
|
3
3
|
*
|
|
4
4
|
* Structurally compatible with the official MCP SDK's `AuthInfo`
|
|
5
|
-
* (`@modelcontextprotocol/
|
|
6
|
-
*
|
|
5
|
+
* (`@modelcontextprotocol/server`): values of either type are interchangeable
|
|
6
|
+
* in both directions via structural typing.
|
|
7
7
|
*/
|
|
8
8
|
export interface AuthInfo {
|
|
9
9
|
/**
|
|
@@ -37,8 +37,8 @@ export interface AuthInfo {
|
|
|
37
37
|
* Verifies bearer access tokens and returns information about them.
|
|
38
38
|
*
|
|
39
39
|
* Structurally compatible with the official MCP SDK's `OAuthTokenVerifier`
|
|
40
|
-
* (`@modelcontextprotocol/
|
|
41
|
-
*
|
|
40
|
+
* (`@modelcontextprotocol/server`): implementations of either interface are
|
|
41
|
+
* interchangeable via structural typing.
|
|
42
42
|
*/
|
|
43
43
|
export interface OAuthTokenVerifier {
|
|
44
44
|
/**
|
|
@@ -50,10 +50,10 @@ export interface OAuthTokenVerifier {
|
|
|
50
50
|
* RFC 9728 OAuth Protected Resource Metadata.
|
|
51
51
|
*
|
|
52
52
|
* Structurally compatible with the official MCP SDK's
|
|
53
|
-
* `OAuthProtectedResourceMetadata` (`@modelcontextprotocol/
|
|
54
|
-
*
|
|
55
|
-
*
|
|
56
|
-
*
|
|
53
|
+
* `OAuthProtectedResourceMetadata` (`@modelcontextprotocol/server`): values of
|
|
54
|
+
* either type are interchangeable in both directions via structural typing.
|
|
55
|
+
* The index signature mirrors the SDK schema's passthrough of unrecognized
|
|
56
|
+
* metadata fields.
|
|
57
57
|
*/
|
|
58
58
|
export interface OAuthProtectedResourceMetadata {
|
|
59
59
|
resource: string;
|
|
@@ -1,18 +1,41 @@
|
|
|
1
|
-
import { OAuthClientProvider } from '@modelcontextprotocol/
|
|
2
|
-
import { OAuthClientMetadata, OAuthClientInformation, OAuthTokens, OAuthMetadata } from '@modelcontextprotocol/sdk/shared/auth.js';
|
|
1
|
+
import type { AuthorizationServerMetadata, OAuthClientInformationContext, OAuthClientMetadata, OAuthClientProvider, OAuthDiscoveryState, StoredOAuthClientInformation, StoredOAuthTokens } from '@modelcontextprotocol/client';
|
|
3
2
|
import type { PrivateKeyring } from "@keycardai/oauth/keyring";
|
|
4
3
|
export interface OAuthTokensStore {
|
|
5
|
-
get(): Promise<
|
|
6
|
-
save(tokens:
|
|
4
|
+
get(ctx?: OAuthClientInformationContext): Promise<StoredOAuthTokens | undefined>;
|
|
5
|
+
save(tokens: StoredOAuthTokens, ctx?: OAuthClientInformationContext): void | Promise<void>;
|
|
7
6
|
}
|
|
8
7
|
export interface OAuthCodeVerifierStore {
|
|
9
8
|
get(): string | Promise<string>;
|
|
10
9
|
save(codeVerifier: string): void | Promise<void>;
|
|
11
10
|
}
|
|
11
|
+
/**
|
|
12
|
+
* Persists OAuth discovery state (SEP-2352) so the MCP SDK can skip
|
|
13
|
+
* redundant RFC 9728 / RFC 8414 discovery requests and verify that the
|
|
14
|
+
* authorization server on the redirect callback leg matches the one
|
|
15
|
+
* recorded before the redirect.
|
|
16
|
+
*
|
|
17
|
+
* Implementations MUST persist with the same durability as the code
|
|
18
|
+
* verifier store: the state has to survive the authorization redirect
|
|
19
|
+
* round-trip.
|
|
20
|
+
*/
|
|
21
|
+
/**
|
|
22
|
+
* Single-slot by design: `get`/`save` take no key, so one provider instance
|
|
23
|
+
* holds discovery state for one in-flight authorization at a time. Two
|
|
24
|
+
* concurrent flows against different authorization servers through the same
|
|
25
|
+
* provider will overwrite each other, and the second callback leg then fails
|
|
26
|
+
* the SEP-2352 binding check with `AuthorizationServerMismatchError`. Give each
|
|
27
|
+
* concurrent flow its own provider instance, or back the store with per-flow
|
|
28
|
+
* storage keyed outside this interface.
|
|
29
|
+
*/
|
|
30
|
+
export interface OAuthDiscoveryStateStore {
|
|
31
|
+
get(): OAuthDiscoveryState | undefined | Promise<OAuthDiscoveryState | undefined>;
|
|
32
|
+
save(state: OAuthDiscoveryState): void | Promise<void>;
|
|
33
|
+
}
|
|
12
34
|
export interface BaseOAuthClientProviderOptions {
|
|
13
35
|
redirectUrl?: string | URL;
|
|
14
36
|
tokensStore?: OAuthTokensStore;
|
|
15
37
|
codeVerifierStore?: OAuthCodeVerifierStore;
|
|
38
|
+
discoveryStateStore?: OAuthDiscoveryStateStore;
|
|
16
39
|
privateKeyring?: PrivateKeyring;
|
|
17
40
|
}
|
|
18
41
|
export declare class BaseOAuthClientProvider implements OAuthClientProvider {
|
|
@@ -22,13 +45,33 @@ export declare class BaseOAuthClientProvider implements OAuthClientProvider {
|
|
|
22
45
|
protected privateKeyring: PrivateKeyring | undefined;
|
|
23
46
|
protected tokensStore: OAuthTokensStore | undefined;
|
|
24
47
|
protected codeVerifierStore: OAuthCodeVerifierStore | undefined;
|
|
48
|
+
protected discoveryStateStore: OAuthDiscoveryStateStore | undefined;
|
|
49
|
+
/**
|
|
50
|
+
* SEP-2352 discovery-state persistence. Defined only when a
|
|
51
|
+
* `discoveryStateStore` is configured: the MCP SDK treats the presence of
|
|
52
|
+
* `saveDiscoveryState` as a promise that discovery state is durably
|
|
53
|
+
* persisted, and fails the authorization callback leg when the method
|
|
54
|
+
* exists but no recorded state can be read back. A provider without a
|
|
55
|
+
* store must therefore not define these members at all.
|
|
56
|
+
*/
|
|
57
|
+
saveDiscoveryState?: (state: OAuthDiscoveryState) => void | Promise<void>;
|
|
58
|
+
discoveryState?: () => OAuthDiscoveryState | undefined | Promise<OAuthDiscoveryState | undefined>;
|
|
25
59
|
constructor(metadata: OAuthClientMetadata, clientId?: string, options?: BaseOAuthClientProviderOptions);
|
|
26
|
-
addClientAuthentication(headers: Headers, params: URLSearchParams,
|
|
27
|
-
|
|
60
|
+
addClientAuthentication(headers: Headers, params: URLSearchParams, url: string | URL, metadata?: AuthorizationServerMetadata): Promise<void>;
|
|
61
|
+
/**
|
|
62
|
+
* Returns `undefined` when no redirect URL was configured. The MCP SDK
|
|
63
|
+
* reads that as a non-interactive provider (`client_credentials`,
|
|
64
|
+
* `jwt-bearer`) and skips the authorization redirect leg.
|
|
65
|
+
*/
|
|
66
|
+
get redirectUrl(): string | URL | undefined;
|
|
28
67
|
get clientMetadata(): OAuthClientMetadata;
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
68
|
+
/**
|
|
69
|
+
* This provider holds a single credential set, so the authorization-server
|
|
70
|
+
* binding context is ignored (permitted by the MCP SDK contract).
|
|
71
|
+
*/
|
|
72
|
+
clientInformation(ctx?: OAuthClientInformationContext): StoredOAuthClientInformation | undefined | Promise<StoredOAuthClientInformation | undefined>;
|
|
73
|
+
tokens(ctx?: OAuthClientInformationContext): Promise<StoredOAuthTokens | undefined>;
|
|
74
|
+
saveTokens(tokens: StoredOAuthTokens, ctx?: OAuthClientInformationContext): void | Promise<void>;
|
|
32
75
|
redirectToAuthorization(authorizationUrl: URL): void | Promise<void>;
|
|
33
76
|
saveCodeVerifier(codeVerifier: string): void | Promise<void>;
|
|
34
77
|
codeVerifier(): string | Promise<string>;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"base.d.ts","sourceRoot":"","sources":["../../../../../src/client/auth/providers/base.ts"],"names":[],"mappings":"AAAA,OAAO,
|
|
1
|
+
{"version":3,"file":"base.d.ts","sourceRoot":"","sources":["../../../../../src/client/auth/providers/base.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EACV,2BAA2B,EAC3B,6BAA6B,EAC7B,mBAAmB,EACnB,mBAAmB,EACnB,mBAAmB,EACnB,4BAA4B,EAC5B,iBAAiB,EAClB,MAAM,8BAA8B,CAAC;AACtC,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,0BAA0B,CAAC;AAG/D,MAAM,WAAW,gBAAgB;IAC/B,GAAG,CAAC,GAAG,CAAC,EAAE,6BAA6B,GAAG,OAAO,CAAC,iBAAiB,GAAG,SAAS,CAAC,CAAC;IACjF,IAAI,CAAC,MAAM,EAAE,iBAAiB,EAAE,GAAG,CAAC,EAAE,6BAA6B,GAAG,IAAI,GAAG,OAAO,CAAC,IAAI,CAAC,CAAC;CAC5F;AAED,MAAM,WAAW,sBAAsB;IACrC,GAAG,IAAI,MAAM,GAAG,OAAO,CAAC,MAAM,CAAC,CAAC;IAChC,IAAI,CAAC,YAAY,EAAE,MAAM,GAAG,IAAI,GAAG,OAAO,CAAC,IAAI,CAAC,CAAC;CAClD;AAED;;;;;;;;;GASG;AACH;;;;;;;;GAQG;AACH,MAAM,WAAW,wBAAwB;IACvC,GAAG,IAAI,mBAAmB,G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AAC;IAwDhB;;;;OAIG;IACH,IAAI,WAAW,IAAI,MAAM,GAAG,GAAG,GAAG,SAAS,CAE1C;IAED,IAAI,cAAc,IAAI,mBAAmB,CAExC;IAED;;;OAGG;IACH,iBAAiB,CAAC,GAAG,CAAC,EAAE,6BAA6B,GAAG,4BAA4B,GAAG,SAAS,GAAG,OAAO,CAAC,4BAA4B,GAAG,SAAS,CAAC;IAQpJ,MAAM,CAAC,GAAG,CAAC,EAAE,6BAA6B,GAAG,OAAO,CAAC,iBAAiB,GAAG,SAAS,CAAC;IAOnF,UAAU,CAAC,MAAM,EAAE,iBAAiB,EAAE,GAAG,CAAC,EAAE,6BAA6B,GAAG,IAAI,GAAG,OAAO,CAAC,IAAI,CAAC;IAOhG,uBAAuB,CAAC,gBAAgB,EAAE,GAAG,GAAG,IAAI,GAAG,OAAO,CAAC,IAAI,CAAC;IAIpE,gBAAgB,CAAC,YAAY,EAAE,MAAM,GAAG,IAAI,GAAG,OAAO,CAAC,IAAI,CAAC;IAO5D,YAAY,IAAI,MAAM,GAAG,OAAO,CAAC,MAAM,CAAC;CAMzC"}
|
|
@@ -7,17 +7,30 @@ export class BaseOAuthClientProvider {
|
|
|
7
7
|
this.tokensStore = options?.tokensStore;
|
|
8
8
|
this.codeVerifierStore = options?.codeVerifierStore;
|
|
9
9
|
this.privateKeyring = options?.privateKeyring;
|
|
10
|
+
// Assigned as own properties, and only when a store exists, because SEP-2352
|
|
11
|
+
// keys off whether these members are *defined*: the v2 client throws
|
|
12
|
+
// AuthorizationServerMismatchError if saveDiscoveryState exists but no state
|
|
13
|
+
// reads back. Declaring them as prototype methods would make them always
|
|
14
|
+
// defined and break that check. The tradeoff is that an own property shadows
|
|
15
|
+
// a subclass's prototype override of the same name, so a subclass wanting
|
|
16
|
+
// custom discovery-state behavior should supply a discoveryStateStore rather
|
|
17
|
+
// than override saveDiscoveryState/discoveryState.
|
|
18
|
+
const discoveryStateStore = options?.discoveryStateStore;
|
|
19
|
+
if (discoveryStateStore) {
|
|
20
|
+
this.discoveryStateStore = discoveryStateStore;
|
|
21
|
+
this.saveDiscoveryState = (state) => discoveryStateStore.save(state);
|
|
22
|
+
this.discoveryState = () => discoveryStateStore.get();
|
|
23
|
+
}
|
|
10
24
|
// workaround to bind function to this context, since underlying
|
|
11
25
|
// MCP library calls it without a context.
|
|
12
26
|
this.addClientAuthentication = this.addClientAuthentication.bind(this);
|
|
13
27
|
}
|
|
14
|
-
async addClientAuthentication(headers, params,
|
|
28
|
+
async addClientAuthentication(headers, params, url, metadata) {
|
|
15
29
|
const clientInfo = await this.clientInformation();
|
|
16
30
|
if (!clientInfo) {
|
|
17
31
|
throw new Error("Client information not available for authentication");
|
|
18
32
|
}
|
|
19
33
|
const clientInformation = { ...this._metadata, ...clientInfo };
|
|
20
|
-
const supportedMethods = metadata?.token_endpoint_auth_methods_supported ?? [];
|
|
21
34
|
const authMethod = clientInformation.token_endpoint_auth_method || 'client_secret_basic';
|
|
22
35
|
switch (authMethod) {
|
|
23
36
|
case 'private_key_jwt':
|
|
@@ -25,9 +38,12 @@ export class BaseOAuthClientProvider {
|
|
|
25
38
|
if (!this.privateKeyring) {
|
|
26
39
|
throw new Error("Private keyring not initialized");
|
|
27
40
|
}
|
|
41
|
+
// The MCP SDK passes the token endpoint URL being called as `url`;
|
|
42
|
+
// prefer the metadata value when present since it is the validated
|
|
43
|
+
// RFC 8414 document entry.
|
|
28
44
|
const tokenUrl = metadata?.token_endpoint
|
|
29
45
|
? new URL(metadata.token_endpoint)
|
|
30
|
-
: new URL(
|
|
46
|
+
: new URL(url);
|
|
31
47
|
const now = Date.now();
|
|
32
48
|
const signer = new JSONWebTokenSigner(this.privateKeyring);
|
|
33
49
|
// RFC 7523 section 3: for client authentication both "iss" and
|
|
@@ -61,32 +77,38 @@ export class BaseOAuthClientProvider {
|
|
|
61
77
|
break;
|
|
62
78
|
}
|
|
63
79
|
}
|
|
80
|
+
/**
|
|
81
|
+
* Returns `undefined` when no redirect URL was configured. The MCP SDK
|
|
82
|
+
* reads that as a non-interactive provider (`client_credentials`,
|
|
83
|
+
* `jwt-bearer`) and skips the authorization redirect leg.
|
|
84
|
+
*/
|
|
64
85
|
get redirectUrl() {
|
|
65
|
-
if (!this._redirectUrl) {
|
|
66
|
-
throw new Error("Attempt to access redirectUrl before it was set");
|
|
67
|
-
}
|
|
68
86
|
return this._redirectUrl;
|
|
69
87
|
}
|
|
70
88
|
get clientMetadata() {
|
|
71
89
|
return this._metadata;
|
|
72
90
|
}
|
|
73
|
-
|
|
91
|
+
/**
|
|
92
|
+
* This provider holds a single credential set, so the authorization-server
|
|
93
|
+
* binding context is ignored (permitted by the MCP SDK contract).
|
|
94
|
+
*/
|
|
95
|
+
clientInformation(ctx) {
|
|
74
96
|
if (this._clientId) {
|
|
75
97
|
return { ...this.clientMetadata, client_id: this._clientId };
|
|
76
98
|
}
|
|
77
99
|
return undefined;
|
|
78
100
|
}
|
|
79
|
-
tokens() {
|
|
101
|
+
tokens(ctx) {
|
|
80
102
|
if (!this.tokensStore) {
|
|
81
103
|
throw new Error("OAuth tokens store not initialized");
|
|
82
104
|
}
|
|
83
|
-
return this.tokensStore.get();
|
|
105
|
+
return this.tokensStore.get(ctx);
|
|
84
106
|
}
|
|
85
|
-
saveTokens(tokens) {
|
|
107
|
+
saveTokens(tokens, ctx) {
|
|
86
108
|
if (!this.tokensStore) {
|
|
87
109
|
throw new Error("OAuth tokens store not initialized");
|
|
88
110
|
}
|
|
89
|
-
return this.tokensStore.save(tokens);
|
|
111
|
+
return this.tokensStore.save(tokens, ctx);
|
|
90
112
|
}
|
|
91
113
|
redirectToAuthorization(authorizationUrl) {
|
|
92
114
|
throw new Error('redirectToAuthorization not implemented');
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"base.js","sourceRoot":"","sources":["../../../../../src/client/auth/providers/base.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"base.js","sourceRoot":"","sources":["../../../../../src/client/auth/providers/base.ts"],"names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CAAC;IAED,uBAAuB,CAAC,gBAAqB;QAC3C,MAAM,IAAI,KAAK,CAAC,yCAAyC,CAAC,CAAC;IAC7D,CAAC;IAED,gBAAgB,CAAC,YAAoB;QACnC,IAAI,CAAC,IAAI,CAAC,iBAAiB,EAAE,CAAC;YAC5B,MAAM,IAAI,KAAK,CAAC,2CAA2C,CAAC,CAAC;QAC/D,CAAC;QACD,OAAO,IAAI,CAAC,iBAAiB,CAAC,IAAI,CAAC,YAAY,CAAC,CAAC;IACnD,CAAC;IAED,YAAY;QACV,IAAI,CAAC,IAAI,CAAC,iBAAiB,EAAE,CAAC;YAC5B,MAAM,IAAI,KAAK,CAAC,2CAA2C,CAAC,CAAC;QAC/D,CAAC;QACD,OAAO,IAAI,CAAC,iBAAiB,CAAC,GAAG,EAAE,CAAC;IACtC,CAAC;CACF"}
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
import type { AuthInfo } from "../../shared/auth.js";
|
|
2
|
+
/**
|
|
3
|
+
* The slice of the MCP v2 tool handler context that carries HTTP transport
|
|
4
|
+
* authentication. Structurally compatible with the `ctx` argument the
|
|
5
|
+
* `@modelcontextprotocol/server` request handlers receive, so handlers can
|
|
6
|
+
* pass their context straight through without depending on that package's
|
|
7
|
+
* types here.
|
|
8
|
+
*/
|
|
9
|
+
export interface ToolAuthContext {
|
|
10
|
+
http?: {
|
|
11
|
+
authInfo?: AuthInfo;
|
|
12
|
+
};
|
|
13
|
+
}
|
|
14
|
+
/**
|
|
15
|
+
* Returns the required scopes the current tool call's access token does not
|
|
16
|
+
* carry. An unauthenticated call (no `ctx.http.authInfo`) grants nothing, so
|
|
17
|
+
* every required scope is reported missing.
|
|
18
|
+
*
|
|
19
|
+
* All MCP tools share one HTTP route, so route-level `requiredScopes` on
|
|
20
|
+
* `requireBearerAuth` can only express scopes common to every tool. Per-tool
|
|
21
|
+
* requirements are checked inside the tool handler with this helper.
|
|
22
|
+
*/
|
|
23
|
+
export declare function missingToolScopes(ctx: ToolAuthContext, requiredScopes: readonly string[]): string[];
|
|
24
|
+
/**
|
|
25
|
+
* Asserts that the current tool call is authenticated and its access token
|
|
26
|
+
* carries every required scope, returning the validated `AuthInfo`.
|
|
27
|
+
*
|
|
28
|
+
* Throws `InsufficientScopeError` when the call is unauthenticated or scopes
|
|
29
|
+
* are missing, which stops the handler and fails the tool call.
|
|
30
|
+
*
|
|
31
|
+
* What the client actually receives is a plain tool error, not an OAuth
|
|
32
|
+
* challenge. `@modelcontextprotocol/server` catches every handler throw except
|
|
33
|
+
* `UrlElicitationRequiredError` and flattens it into
|
|
34
|
+
* `{ content: [{ type: "text", ... }], isError: true }` at HTTP 200. So the
|
|
35
|
+
* `insufficient_scope` code, the 403, and the `WWW-Authenticate` header do not
|
|
36
|
+
* reach the wire from here — only the error's message text does. This helper
|
|
37
|
+
* enforces the requirement; it cannot signal it in a machine-readable way.
|
|
38
|
+
*
|
|
39
|
+
* That makes it unsuitable on its own for driving a scope step-up: the client
|
|
40
|
+
* cannot distinguish missing scopes from any other tool failure. For an
|
|
41
|
+
* interactive step-up, v2's own primitive is `UrlElicitationRequiredError`,
|
|
42
|
+
* the one error type it re-throws rather than flattening. For a structured
|
|
43
|
+
* in-band signal, use `missingToolScopes` and return a tool result your client
|
|
44
|
+
* knows how to interpret.
|
|
45
|
+
*
|
|
46
|
+
* ```ts
|
|
47
|
+
* server.registerTool("delete_repo", config, async (args, ctx) => {
|
|
48
|
+
* const authInfo = requireToolScopes(ctx, ["repo:delete"]);
|
|
49
|
+
* // ...
|
|
50
|
+
* });
|
|
51
|
+
* ```
|
|
52
|
+
*/
|
|
53
|
+
export declare function requireToolScopes(ctx: ToolAuthContext, requiredScopes: readonly string[]): AuthInfo;
|
|
54
|
+
//# sourceMappingURL=toolScopes.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"toolScopes.d.ts","sourceRoot":"","sources":["../../../../src/server/auth/toolScopes.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,QAAQ,EAAE,MAAM,sBAAsB,CAAC;AAErD;;;;;;GAMG;AACH,MAAM,WAAW,eAAe;IAC9B,IAAI,CAAC,EAAE;QACL,QAAQ,CAAC,EAAE,QAAQ,CAAC;KACrB,CAAC;CACH;AAED;;;;;;;;GAQG;AACH,wBAAgB,iBAAiB,CAC/B,GAAG,EAAE,eAAe,EACpB,cAAc,EAAE,SAAS,MAAM,EAAE,GAChC,MAAM,EAAE,CAGV;AAED;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA4BG;AACH,wBAAgB,iBAAiB,CAC/B,GAAG,EAAE,eAAe,EACpB,cAAc,EAAE,SAAS,MAAM,EAAE,GAChC,QAAQ,CAgBV"}
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
import { InsufficientScopeError } from "@keycardai/oauth/errors";
|
|
2
|
+
/**
|
|
3
|
+
* Returns the required scopes the current tool call's access token does not
|
|
4
|
+
* carry. An unauthenticated call (no `ctx.http.authInfo`) grants nothing, so
|
|
5
|
+
* every required scope is reported missing.
|
|
6
|
+
*
|
|
7
|
+
* All MCP tools share one HTTP route, so route-level `requiredScopes` on
|
|
8
|
+
* `requireBearerAuth` can only express scopes common to every tool. Per-tool
|
|
9
|
+
* requirements are checked inside the tool handler with this helper.
|
|
10
|
+
*/
|
|
11
|
+
export function missingToolScopes(ctx, requiredScopes) {
|
|
12
|
+
const grantedScopes = new Set(ctx.http?.authInfo?.scopes ?? []);
|
|
13
|
+
return requiredScopes.filter((scope) => !grantedScopes.has(scope));
|
|
14
|
+
}
|
|
15
|
+
/**
|
|
16
|
+
* Asserts that the current tool call is authenticated and its access token
|
|
17
|
+
* carries every required scope, returning the validated `AuthInfo`.
|
|
18
|
+
*
|
|
19
|
+
* Throws `InsufficientScopeError` when the call is unauthenticated or scopes
|
|
20
|
+
* are missing, which stops the handler and fails the tool call.
|
|
21
|
+
*
|
|
22
|
+
* What the client actually receives is a plain tool error, not an OAuth
|
|
23
|
+
* challenge. `@modelcontextprotocol/server` catches every handler throw except
|
|
24
|
+
* `UrlElicitationRequiredError` and flattens it into
|
|
25
|
+
* `{ content: [{ type: "text", ... }], isError: true }` at HTTP 200. So the
|
|
26
|
+
* `insufficient_scope` code, the 403, and the `WWW-Authenticate` header do not
|
|
27
|
+
* reach the wire from here — only the error's message text does. This helper
|
|
28
|
+
* enforces the requirement; it cannot signal it in a machine-readable way.
|
|
29
|
+
*
|
|
30
|
+
* That makes it unsuitable on its own for driving a scope step-up: the client
|
|
31
|
+
* cannot distinguish missing scopes from any other tool failure. For an
|
|
32
|
+
* interactive step-up, v2's own primitive is `UrlElicitationRequiredError`,
|
|
33
|
+
* the one error type it re-throws rather than flattening. For a structured
|
|
34
|
+
* in-band signal, use `missingToolScopes` and return a tool result your client
|
|
35
|
+
* knows how to interpret.
|
|
36
|
+
*
|
|
37
|
+
* ```ts
|
|
38
|
+
* server.registerTool("delete_repo", config, async (args, ctx) => {
|
|
39
|
+
* const authInfo = requireToolScopes(ctx, ["repo:delete"]);
|
|
40
|
+
* // ...
|
|
41
|
+
* });
|
|
42
|
+
* ```
|
|
43
|
+
*/
|
|
44
|
+
export function requireToolScopes(ctx, requiredScopes) {
|
|
45
|
+
const authInfo = ctx.http?.authInfo;
|
|
46
|
+
if (!authInfo) {
|
|
47
|
+
throw new InsufficientScopeError("Tool call is not authenticated; no access token information is available on the request context");
|
|
48
|
+
}
|
|
49
|
+
const missingScopes = missingToolScopes(ctx, requiredScopes);
|
|
50
|
+
if (missingScopes.length > 0) {
|
|
51
|
+
throw new InsufficientScopeError(`Tool call requires additional scopes: ${missingScopes.join(" ")}`);
|
|
52
|
+
}
|
|
53
|
+
return authInfo;
|
|
54
|
+
}
|
|
55
|
+
//# sourceMappingURL=toolScopes.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"toolScopes.js","sourceRoot":"","sources":["../../../../src/server/auth/toolScopes.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,sBAAsB,EAAE,MAAM,yBAAyB,CAAC;AAgBjE;;;;;;;;GAQG;AACH,MAAM,UAAU,iBAAiB,CAC/B,GAAoB,EACpB,cAAiC;IAEjC,MAAM,aAAa,GAAG,IAAI,GAAG,CAAC,GAAG,CAAC,IAAI,EAAE,QAAQ,EAAE,MAAM,IAAI,EAAE,CAAC,CAAC;IAChE,OAAO,cAAc,CAAC,MAAM,CAAC,CAAC,KAAK,EAAE,EAAE,CAAC,CAAC,aAAa,CAAC,GAAG,CAAC,KAAK,CAAC,CAAC,CAAC;AACrE,CAAC;AAED;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA4BG;AACH,MAAM,UAAU,iBAAiB,CAC/B,GAAoB,EACpB,cAAiC;IAEjC,MAAM,QAAQ,GAAG,GAAG,CAAC,IAAI,EAAE,QAAQ,CAAC;IACpC,IAAI,CAAC,QAAQ,EAAE,CAAC;QACd,MAAM,IAAI,sBAAsB,CAC9B,iGAAiG,CAClG,CAAC;IACJ,CAAC;IAED,MAAM,aAAa,GAAG,iBAAiB,CAAC,GAAG,EAAE,cAAc,CAAC,CAAC;IAC7D,IAAI,aAAa,CAAC,MAAM,GAAG,CAAC,EAAE,CAAC;QAC7B,MAAM,IAAI,sBAAsB,CAC9B,yCAAyC,aAAa,CAAC,IAAI,CAAC,GAAG,CAAC,EAAE,CACnE,CAAC;IACJ,CAAC;IAED,OAAO,QAAQ,CAAC;AAClB,CAAC"}
|
|
@@ -2,8 +2,8 @@
|
|
|
2
2
|
* Information about a validated access token, provided to request handlers.
|
|
3
3
|
*
|
|
4
4
|
* Structurally compatible with the official MCP SDK's `AuthInfo`
|
|
5
|
-
* (`@modelcontextprotocol/
|
|
6
|
-
*
|
|
5
|
+
* (`@modelcontextprotocol/server`): values of either type are interchangeable
|
|
6
|
+
* in both directions via structural typing.
|
|
7
7
|
*/
|
|
8
8
|
export interface AuthInfo {
|
|
9
9
|
/**
|
|
@@ -37,8 +37,8 @@ export interface AuthInfo {
|
|
|
37
37
|
* Verifies bearer access tokens and returns information about them.
|
|
38
38
|
*
|
|
39
39
|
* Structurally compatible with the official MCP SDK's `OAuthTokenVerifier`
|
|
40
|
-
* (`@modelcontextprotocol/
|
|
41
|
-
*
|
|
40
|
+
* (`@modelcontextprotocol/server`): implementations of either interface are
|
|
41
|
+
* interchangeable via structural typing.
|
|
42
42
|
*/
|
|
43
43
|
export interface OAuthTokenVerifier {
|
|
44
44
|
/**
|
|
@@ -50,10 +50,10 @@ export interface OAuthTokenVerifier {
|
|
|
50
50
|
* RFC 9728 OAuth Protected Resource Metadata.
|
|
51
51
|
*
|
|
52
52
|
* Structurally compatible with the official MCP SDK's
|
|
53
|
-
* `OAuthProtectedResourceMetadata` (`@modelcontextprotocol/
|
|
54
|
-
*
|
|
55
|
-
*
|
|
56
|
-
*
|
|
53
|
+
* `OAuthProtectedResourceMetadata` (`@modelcontextprotocol/server`): values of
|
|
54
|
+
* either type are interchangeable in both directions via structural typing.
|
|
55
|
+
* The index signature mirrors the SDK schema's passthrough of unrecognized
|
|
56
|
+
* metadata fields.
|
|
57
57
|
*/
|
|
58
58
|
export interface OAuthProtectedResourceMetadata {
|
|
59
59
|
resource: string;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@keycardai/mcp",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "2.0.0",
|
|
4
4
|
"description": "[Preview] High-security OAuth implementation for Model Context Protocol",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"repository": {
|
|
@@ -40,6 +40,11 @@
|
|
|
40
40
|
"require": "./dist/cjs/server/auth/errors.js",
|
|
41
41
|
"types": "./dist/esm/server/auth/errors.d.ts"
|
|
42
42
|
},
|
|
43
|
+
"./server/auth/toolScopes": {
|
|
44
|
+
"import": "./dist/esm/server/auth/toolScopes.js",
|
|
45
|
+
"require": "./dist/cjs/server/auth/toolScopes.js",
|
|
46
|
+
"types": "./dist/esm/server/auth/toolScopes.d.ts"
|
|
47
|
+
},
|
|
43
48
|
"./shared/auth": {
|
|
44
49
|
"import": "./dist/esm/shared/auth.js",
|
|
45
50
|
"require": "./dist/cjs/shared/auth.js",
|
|
@@ -75,20 +80,21 @@
|
|
|
75
80
|
"cors": "^2.8.5",
|
|
76
81
|
"express": "^5.1.0",
|
|
77
82
|
"zod": "^3.25.74",
|
|
78
|
-
"@keycardai/express": "0.
|
|
83
|
+
"@keycardai/express": "0.9.0",
|
|
79
84
|
"@keycardai/oauth": "0.20.0"
|
|
80
85
|
},
|
|
81
86
|
"peerDependencies": {
|
|
82
|
-
"@modelcontextprotocol/
|
|
87
|
+
"@modelcontextprotocol/client": "^2.0.0"
|
|
83
88
|
},
|
|
84
89
|
"peerDependenciesMeta": {
|
|
85
|
-
"@modelcontextprotocol/
|
|
90
|
+
"@modelcontextprotocol/client": {
|
|
86
91
|
"optional": true
|
|
87
92
|
}
|
|
88
93
|
},
|
|
89
94
|
"devDependencies": {
|
|
90
95
|
"@jest/globals": "^30.0.4",
|
|
91
|
-
"@modelcontextprotocol/
|
|
96
|
+
"@modelcontextprotocol/client": "^2.0.0",
|
|
97
|
+
"@modelcontextprotocol/server": "^2.0.0",
|
|
92
98
|
"@types/cors": "^2.8.19",
|
|
93
99
|
"@types/express": "^5.0.3",
|
|
94
100
|
"jest": "^30.0.4",
|