@casys/mcp-server 0.24.1 → 0.26.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 +50 -1
- package/mod.ts +2 -0
- package/package.json +1 -1
- package/src/client-auth/provider.ts +157 -3
- package/src/client-auth/types.ts +14 -1
- package/src/mcp-app.ts +529 -108
- package/src/observability/mod.ts +1 -0
- package/src/observability/otel.ts +222 -1
- package/src/testdata/stdio-e2e-server.ts +110 -0
- package/src/types.ts +67 -7
- package/src/validation/schema-validator.ts +14 -1
package/README.md
CHANGED
|
@@ -440,6 +440,54 @@ server.registerResource(
|
|
|
440
440
|
);
|
|
441
441
|
```
|
|
442
442
|
|
|
443
|
+
Handlers return one payload form: `text` (including HTML) **or** `blob` for
|
|
444
|
+
binary content encoded as standard padded base64. Existing text handlers remain
|
|
445
|
+
valid. The framework also checks at runtime that the response URI exactly
|
|
446
|
+
matches the requested URI and that the MIME type is non-empty, which protects
|
|
447
|
+
JavaScript and unchecked TypeScript handlers as well as typed callers.
|
|
448
|
+
|
|
449
|
+
```typescript
|
|
450
|
+
server.registerResource(
|
|
451
|
+
{ uri: "file://reports/latest.pdf", name: "Latest report", size: 184_320 },
|
|
452
|
+
async (uri) => ({
|
|
453
|
+
uri: uri.toString(),
|
|
454
|
+
mimeType: "application/pdf",
|
|
455
|
+
blob: await loadReportAsCanonicalBase64(),
|
|
456
|
+
}),
|
|
457
|
+
);
|
|
458
|
+
```
|
|
459
|
+
|
|
460
|
+
`size` is optional resource metadata shown in `resources/list`; when present it
|
|
461
|
+
must be a non-negative safe integer and is verified on every read against the
|
|
462
|
+
exact UTF-8 byte length of `text` or decoded byte length of `blob`. Supplying
|
|
463
|
+
`mimeType` in the resource metadata likewise binds every response to that exact
|
|
464
|
+
MIME type. If it is absent, it is absent from `resources/list`; the handler
|
|
465
|
+
still declares the MIME type when it serves the bytes. Resource content may
|
|
466
|
+
include `_meta`. Put `annotations`, `icons`, `title`, and resource `_meta` on
|
|
467
|
+
the `MCPResource` registration, where MCP defines those fields.
|
|
468
|
+
|
|
469
|
+
With `resourceCsp`, CSP injection applies only to the `text` branch of an HTML
|
|
470
|
+
resource. Blobs are never decoded, transformed, or re-encoded.
|
|
471
|
+
|
|
472
|
+
Register resources before `start()` / `startHttp()` to install the resource
|
|
473
|
+
handlers and advertise `resources: { listChanged: true }`. They can then be
|
|
474
|
+
added or removed at any time through `unregisterResource(uri)`, which returns
|
|
475
|
+
`true` only once. For a registry that starts empty and discovers resources
|
|
476
|
+
asynchronously, construct with `expectResources: true`; that mode installs the
|
|
477
|
+
same handlers at construction time:
|
|
478
|
+
|
|
479
|
+
```typescript
|
|
480
|
+
const app = new McpApp({
|
|
481
|
+
name: "relay",
|
|
482
|
+
version: "1.0.0",
|
|
483
|
+
expectResources: true,
|
|
484
|
+
});
|
|
485
|
+
|
|
486
|
+
// After start(): list/read/templates handlers are already installed.
|
|
487
|
+
app.registerResource(resource, handler);
|
|
488
|
+
app.unregisterResource(resource.uri); // true, then false if called again
|
|
489
|
+
```
|
|
490
|
+
|
|
443
491
|
#### Capability negotiation (clients that don't support MCP Apps)
|
|
444
492
|
|
|
445
493
|
Not every MCP client renders UI resources. Clients that do advertise the
|
|
@@ -506,11 +554,12 @@ to introspect the protocol target directly.
|
|
|
506
554
|
```typescript
|
|
507
555
|
const server = new McpApp(options: McpAppOptions);
|
|
508
556
|
|
|
509
|
-
// Registration (before start)
|
|
557
|
+
// Registration (before start, unless expectResources: true)
|
|
510
558
|
server.registerTool(tool, handler);
|
|
511
559
|
server.registerTools(tools, handlers);
|
|
512
560
|
server.registerResource(resource, handler);
|
|
513
561
|
server.registerResources(resources, handlers);
|
|
562
|
+
server.unregisterResource(resourceUri); // safe before or after start
|
|
514
563
|
server.use(middleware);
|
|
515
564
|
|
|
516
565
|
// Transport
|
package/mod.ts
CHANGED
|
@@ -66,6 +66,7 @@ export type {
|
|
|
66
66
|
|
|
67
67
|
// Type exports
|
|
68
68
|
export type {
|
|
69
|
+
BlobResourceContent,
|
|
69
70
|
HttpRateLimitContext,
|
|
70
71
|
HttpRateLimitOptions,
|
|
71
72
|
HttpServerInstance,
|
|
@@ -86,6 +87,7 @@ export type {
|
|
|
86
87
|
SamplingParams,
|
|
87
88
|
SamplingResult,
|
|
88
89
|
StructuredToolResult,
|
|
90
|
+
TextResourceContent,
|
|
89
91
|
ToolAnnotations,
|
|
90
92
|
ToolErrorMapper,
|
|
91
93
|
ToolHandler,
|
package/package.json
CHANGED
|
@@ -13,7 +13,10 @@ import type {
|
|
|
13
13
|
OAuthClientMetadata,
|
|
14
14
|
OAuthTokens,
|
|
15
15
|
} from "@modelcontextprotocol/sdk/shared/auth.js";
|
|
16
|
-
import type {
|
|
16
|
+
import type {
|
|
17
|
+
OAuthClientProvider,
|
|
18
|
+
OAuthDiscoveryState,
|
|
19
|
+
} from "@modelcontextprotocol/sdk/client/auth.js";
|
|
17
20
|
import type { OAuthClientConfig } from "./types.js";
|
|
18
21
|
import {
|
|
19
22
|
CimdConfigError,
|
|
@@ -22,12 +25,52 @@ import {
|
|
|
22
25
|
validateCimdClientConfig,
|
|
23
26
|
} from "./client-id-metadata.js";
|
|
24
27
|
|
|
28
|
+
/**
|
|
29
|
+
* Normalise an authorization server identifier before comparing it.
|
|
30
|
+
*
|
|
31
|
+
* `https://as.example.com` and `https://as.example.com/` name the same server,
|
|
32
|
+
* and so do `HTTPS://AS.Example.com` and an explicit `:443`. Comparing raw
|
|
33
|
+
* strings would read those as a server change and wipe valid credentials on
|
|
34
|
+
* every call — a self-inflicted login loop. Falls back to the raw string if it
|
|
35
|
+
* does not parse, which then simply never matches.
|
|
36
|
+
*/
|
|
37
|
+
function canonicalIssuer(value: string): string {
|
|
38
|
+
try {
|
|
39
|
+
return new URL(value).href;
|
|
40
|
+
} catch {
|
|
41
|
+
return value;
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
/**
|
|
46
|
+
* An authorization server URL, safe to put in a log line.
|
|
47
|
+
*
|
|
48
|
+
* A URL may carry `userinfo` credentials, and a query string may carry a token
|
|
49
|
+
* or an internal identifier. Origin and path are enough to tell an operator
|
|
50
|
+
* which server is involved; the rest is only a way for secrets to reach a log
|
|
51
|
+
* aggregator.
|
|
52
|
+
*/
|
|
53
|
+
function redactUrl(value: string): string {
|
|
54
|
+
try {
|
|
55
|
+
const url = new URL(value);
|
|
56
|
+
return `${url.origin}${url.pathname}`;
|
|
57
|
+
} catch {
|
|
58
|
+
return "<unparseable url>";
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
|
|
25
62
|
export class OAuthClientProviderImpl implements OAuthClientProvider {
|
|
26
63
|
private serverUrl: string;
|
|
27
64
|
private config: OAuthClientConfig;
|
|
28
65
|
private _codeVerifier = "";
|
|
29
66
|
private _clientInfo: OAuthClientInformationMixed | undefined;
|
|
30
67
|
private _redirectUrl: string | undefined;
|
|
68
|
+
/**
|
|
69
|
+
* Issuer the SDK resolved for the request currently being authorized
|
|
70
|
+
* (SEP-2352). Set by {@link saveDiscoveryState}, which `auth()` calls after
|
|
71
|
+
* discovery and before it asks for tokens.
|
|
72
|
+
*/
|
|
73
|
+
private _currentIssuer: string | undefined;
|
|
31
74
|
|
|
32
75
|
constructor(serverUrl: string, config: OAuthClientConfig) {
|
|
33
76
|
const mode = resolveClientMode(config);
|
|
@@ -95,9 +138,93 @@ export class OAuthClientProviderImpl implements OAuthClientProvider {
|
|
|
95
138
|
return Promise.resolve();
|
|
96
139
|
}
|
|
97
140
|
|
|
141
|
+
/**
|
|
142
|
+
* Record the authorization server the SDK just resolved (SEP-2352).
|
|
143
|
+
*
|
|
144
|
+
* `auth()` calls this after RFC 9728 discovery and before {@link tokens},
|
|
145
|
+
* which is what makes the check there possible.
|
|
146
|
+
*
|
|
147
|
+
* The binding is the **discovered URL**, not `metadata.issuer`. That
|
|
148
|
+
* distinction is the whole protection: `issuer` is a field the authorization
|
|
149
|
+
* server publishes about itself, so an attacker's server reached at
|
|
150
|
+
* `https://attacker.example.com` can claim
|
|
151
|
+
* `issuer: "https://as-one.example.com"` and, since SDK 1.29 validates the
|
|
152
|
+
* metadata's shape without checking it against the URL it was fetched from,
|
|
153
|
+
* would be handed the credentials minted by the real AS-one. The URL the
|
|
154
|
+
* client actually resolved is the one thing in this exchange the server
|
|
155
|
+
* cannot forge.
|
|
156
|
+
*
|
|
157
|
+
* RFC 8414 does require `issuer` to match the discovery URL. A mismatch is
|
|
158
|
+
* therefore a broken or hostile authorization server, and it is logged —
|
|
159
|
+
* but nothing here depends on the claim being honest.
|
|
160
|
+
*/
|
|
161
|
+
saveDiscoveryState(state: OAuthDiscoveryState): Promise<void> {
|
|
162
|
+
const resolved = canonicalIssuer(state.authorizationServerUrl);
|
|
163
|
+
const claimed = state.authorizationServerMetadata?.issuer;
|
|
164
|
+
|
|
165
|
+
if (claimed !== undefined && canonicalIssuer(claimed) !== resolved) {
|
|
166
|
+
this.log(
|
|
167
|
+
`authorization server at ${redactUrl(state.authorizationServerUrl)} ` +
|
|
168
|
+
`claims issuer "${redactUrl(claimed)}" ` +
|
|
169
|
+
"(RFC 8414 requires them to match); " +
|
|
170
|
+
"binding credentials to the discovered URL",
|
|
171
|
+
);
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
this._currentIssuer = resolved;
|
|
175
|
+
return Promise.resolve();
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
/**
|
|
179
|
+
* Stored tokens, unless they were issued by a different authorization server.
|
|
180
|
+
*
|
|
181
|
+
* SEP-2352 makes this a MUST, and the reason is concrete rather than
|
|
182
|
+
* bureaucratic. `auth()` hands whatever this returns to
|
|
183
|
+
* `refreshAuthorization(authorizationServerUrl, …)`, where the URL comes from
|
|
184
|
+
* the MCP server's own protected-resource metadata. A server that changes —
|
|
185
|
+
* or is made to advertise — a different authorization server would therefore
|
|
186
|
+
* receive a refresh token minted by the previous one. The client must not let
|
|
187
|
+
* the resource server pick who sees its credentials.
|
|
188
|
+
*
|
|
189
|
+
* The check only runs once an issuer is known, which is the case inside
|
|
190
|
+
* `auth()`. A transport asking for the bearer to attach to an ordinary
|
|
191
|
+
* request has no issuer in hand, and that path is not the exposure: those
|
|
192
|
+
* tokens go to the MCP server they were minted for, not to an authorization
|
|
193
|
+
* server.
|
|
194
|
+
*/
|
|
98
195
|
async tokens(): Promise<OAuthTokens | undefined> {
|
|
99
196
|
const stored = await this.config.tokenStore.get(this.serverUrl);
|
|
100
|
-
|
|
197
|
+
if (!stored) return undefined;
|
|
198
|
+
if (this._currentIssuer === undefined) return stored.tokens;
|
|
199
|
+
|
|
200
|
+
if (stored.authServerUrl === undefined) {
|
|
201
|
+
// Written before this binding existed: nothing proves which authorization
|
|
202
|
+
// server minted them. Treated as unusable rather than trusted once —
|
|
203
|
+
// the whole point is to stop handing credentials to an unverified party,
|
|
204
|
+
// and "probably fine" is the assumption this check exists to remove. The
|
|
205
|
+
// cost is one re-authorization per stored credential, at upgrade time.
|
|
206
|
+
await this.config.tokenStore.delete(this.serverUrl);
|
|
207
|
+
this.log(
|
|
208
|
+
"discarding stored credentials with no recorded issuer (SEP-2352); re-authorization required",
|
|
209
|
+
);
|
|
210
|
+
return undefined;
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
if (canonicalIssuer(stored.authServerUrl) !== this._currentIssuer) {
|
|
214
|
+
// A legitimate migration lands here too, and is handled the same way:
|
|
215
|
+
// drop the old credentials and let the flow re-authorize against the new
|
|
216
|
+
// server. What must not happen is the old refresh token travelling there.
|
|
217
|
+
await this.config.tokenStore.delete(this.serverUrl);
|
|
218
|
+
this.log(
|
|
219
|
+
`authorization server changed for ${redactUrl(this.serverUrl)} ` +
|
|
220
|
+
`(${redactUrl(stored.authServerUrl)} → ` +
|
|
221
|
+
`${redactUrl(this._currentIssuer)}); ` +
|
|
222
|
+
"stored credentials discarded, re-authorization required",
|
|
223
|
+
);
|
|
224
|
+
return undefined;
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
return stored.tokens;
|
|
101
228
|
}
|
|
102
229
|
|
|
103
230
|
async saveTokens(tokens: OAuthTokens): Promise<void> {
|
|
@@ -105,8 +232,20 @@ export class OAuthClientProviderImpl implements OAuthClientProvider {
|
|
|
105
232
|
// This get/set is not atomic; concurrent saves for one serverUrl are unsupported
|
|
106
233
|
// because refresh is serialized by the caller.
|
|
107
234
|
const stored = await this.config.tokenStore.get(this.serverUrl);
|
|
235
|
+
|
|
236
|
+
// …but only from the same authorization server. `auth({ authorizationCode })`
|
|
237
|
+
// reaches here without going through `tokens()`, so this is the one path
|
|
238
|
+
// where a credential from a previous issuer can survive: if the code
|
|
239
|
+
// exchange returns no refresh token, carrying the old one over would
|
|
240
|
+
// re-label another server's secret with the new issuer and hand it over on
|
|
241
|
+
// the next refresh. A legacy record has no recorded issuer, so it cannot
|
|
242
|
+
// clear this bar either.
|
|
243
|
+
const inheritable = stored?.authServerUrl !== undefined &&
|
|
244
|
+
this._currentIssuer !== undefined &&
|
|
245
|
+
canonicalIssuer(stored.authServerUrl) === this._currentIssuer;
|
|
246
|
+
|
|
108
247
|
const tokensToStore = tokens.refresh_token === undefined &&
|
|
109
|
-
stored?.tokens.refresh_token !== undefined
|
|
248
|
+
inheritable && stored?.tokens.refresh_token !== undefined
|
|
110
249
|
? { ...tokens, refresh_token: stored.tokens.refresh_token }
|
|
111
250
|
: tokens;
|
|
112
251
|
|
|
@@ -114,9 +253,18 @@ export class OAuthClientProviderImpl implements OAuthClientProvider {
|
|
|
114
253
|
serverUrl: this.serverUrl,
|
|
115
254
|
tokens: tokensToStore,
|
|
116
255
|
obtainedAt: Date.now(),
|
|
256
|
+
// Binds the credentials to their minter. Without it every save produces
|
|
257
|
+
// exactly the legacy record `tokens()` has to throw away.
|
|
258
|
+
...(this._currentIssuer !== undefined
|
|
259
|
+
? { authServerUrl: this._currentIssuer }
|
|
260
|
+
: {}),
|
|
117
261
|
});
|
|
118
262
|
}
|
|
119
263
|
|
|
264
|
+
private log(message: string): void {
|
|
265
|
+
this.config.logger?.(`[client-auth] ${message}`);
|
|
266
|
+
}
|
|
267
|
+
|
|
120
268
|
async redirectToAuthorization(authorizationUrl: URL): Promise<void> {
|
|
121
269
|
await this.config.openBrowser(authorizationUrl.toString());
|
|
122
270
|
}
|
|
@@ -142,5 +290,11 @@ export class OAuthClientProviderImpl implements OAuthClientProvider {
|
|
|
142
290
|
if (scope === "all" || scope === "client") {
|
|
143
291
|
this._clientInfo = undefined;
|
|
144
292
|
}
|
|
293
|
+
if (scope === "all" || scope === "discovery") {
|
|
294
|
+
// Keeping a stale issuer would make the next `tokens()` compare against
|
|
295
|
+
// an authorization server the SDK has already given up on, and discard
|
|
296
|
+
// credentials that are in fact still valid.
|
|
297
|
+
this._currentIssuer = undefined;
|
|
298
|
+
}
|
|
145
299
|
}
|
|
146
300
|
}
|
package/src/client-auth/types.ts
CHANGED
|
@@ -18,7 +18,12 @@ export interface StoredCredentials {
|
|
|
18
18
|
tokens: OAuthTokens;
|
|
19
19
|
/** When the tokens were obtained (Unix epoch ms) */
|
|
20
20
|
obtainedAt: number;
|
|
21
|
-
/**
|
|
21
|
+
/**
|
|
22
|
+
* Issuer that minted these credentials (SEP-2352).
|
|
23
|
+
*
|
|
24
|
+
* Absent on records written before the binding existed; `tokens()` treats
|
|
25
|
+
* that as unusable, since nothing proves who minted them.
|
|
26
|
+
*/
|
|
22
27
|
authServerUrl?: string;
|
|
23
28
|
}
|
|
24
29
|
|
|
@@ -48,6 +53,14 @@ export interface OAuthClientConfigBase {
|
|
|
48
53
|
callbackPort?: number;
|
|
49
54
|
/** Timeout for user to complete OAuth flow (ms, default: 120_000) */
|
|
50
55
|
authTimeout?: number;
|
|
56
|
+
/**
|
|
57
|
+
* Optional sink for credential-lifecycle events.
|
|
58
|
+
*
|
|
59
|
+
* Notably an authorization-server change (SEP-2352): the client silently
|
|
60
|
+
* drops its stored credentials and re-authorizes, and an operator debugging
|
|
61
|
+
* an unexpected login prompt has no other way to see why.
|
|
62
|
+
*/
|
|
63
|
+
logger?: (message: string) => void;
|
|
51
64
|
}
|
|
52
65
|
|
|
53
66
|
/** Static OAuth client registration (default, backwards-compatible mode). */
|