@openrouter/mcp 0.0.1 → 1.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.
Files changed (64) hide show
  1. package/README.md +156 -6
  2. package/esm/auth/auth-resolver.d.ts +1 -1
  3. package/esm/auth/auth-resolver.d.ts.map +1 -1
  4. package/esm/auth/auth-types.d.ts +9 -1
  5. package/esm/auth/auth-types.d.ts.map +1 -1
  6. package/esm/auth/auth-types.js.map +1 -1
  7. package/esm/build-tools.d.ts +4 -2
  8. package/esm/build-tools.d.ts.map +1 -1
  9. package/esm/build-tools.js +3 -0
  10. package/esm/build-tools.js.map +1 -1
  11. package/esm/cache/cache-types.d.ts +17 -0
  12. package/esm/cache/cache-types.d.ts.map +1 -1
  13. package/esm/cache/cache-types.js +11 -3
  14. package/esm/cache/cache-types.js.map +1 -1
  15. package/esm/cache/serialize.d.ts +4 -4
  16. package/esm/cache/serialize.d.ts.map +1 -1
  17. package/esm/cache/serialize.js +17 -6
  18. package/esm/cache/serialize.js.map +1 -1
  19. package/esm/close-quietly.d.ts +24 -0
  20. package/esm/close-quietly.d.ts.map +1 -0
  21. package/esm/close-quietly.js +30 -0
  22. package/esm/close-quietly.js.map +1 -0
  23. package/esm/create-mcp-tools.d.ts.map +1 -1
  24. package/esm/create-mcp-tools.js +23 -2
  25. package/esm/create-mcp-tools.js.map +1 -1
  26. package/esm/elicitation.d.ts +1 -1
  27. package/esm/elicitation.d.ts.map +1 -1
  28. package/esm/errors.d.ts +47 -0
  29. package/esm/errors.d.ts.map +1 -1
  30. package/esm/errors.js +41 -0
  31. package/esm/errors.js.map +1 -1
  32. package/esm/handle.d.ts +25 -0
  33. package/esm/handle.d.ts.map +1 -1
  34. package/esm/handle.js +116 -14
  35. package/esm/handle.js.map +1 -1
  36. package/esm/index.d.ts +3 -3
  37. package/esm/index.d.ts.map +1 -1
  38. package/esm/index.js +1 -1
  39. package/esm/index.js.map +1 -1
  40. package/esm/mcp-connection.d.ts +114 -7
  41. package/esm/mcp-connection.d.ts.map +1 -1
  42. package/esm/mcp-connection.js +364 -34
  43. package/esm/mcp-connection.js.map +1 -1
  44. package/esm/rehydrate.d.ts +40 -5
  45. package/esm/rehydrate.d.ts.map +1 -1
  46. package/esm/rehydrate.js +381 -55
  47. package/esm/rehydrate.js.map +1 -1
  48. package/esm/resource-tools.d.ts +1 -1
  49. package/esm/resource-tools.d.ts.map +1 -1
  50. package/esm/resource-tools.js +24 -2
  51. package/esm/resource-tools.js.map +1 -1
  52. package/esm/tool-wrapper.d.ts +17 -2
  53. package/esm/tool-wrapper.d.ts.map +1 -1
  54. package/esm/tool-wrapper.js +16 -3
  55. package/esm/tool-wrapper.js.map +1 -1
  56. package/esm/transport-types.d.ts +43 -1
  57. package/esm/transport-types.d.ts.map +1 -1
  58. package/esm/types.d.ts +56 -7
  59. package/esm/types.d.ts.map +1 -1
  60. package/esm/version.d.ts +3 -0
  61. package/esm/version.d.ts.map +1 -0
  62. package/esm/version.js +5 -0
  63. package/esm/version.js.map +1 -0
  64. package/package.json +13 -9
package/esm/errors.d.ts CHANGED
@@ -24,12 +24,59 @@ export declare class MCPCacheError extends MCPError {
24
24
  cause?: unknown;
25
25
  });
26
26
  }
27
+ /**
28
+ * Raised when the tool list was read successfully but writing the snapshot back
29
+ * to the caller's `MCPCacheStore` failed.
30
+ *
31
+ * Distinct from the read failing, because the consequences differ completely: the
32
+ * connection is live and its tools are current, so the only thing lost is a cache
33
+ * entry. Callers who treat a store outage as fatal can catch this; callers who
34
+ * would rather keep working with a warm handle can ignore it.
35
+ *
36
+ * Subclasses {@link MCPCacheError}, so existing catch sites keep working.
37
+ */
38
+ export declare class MCPCacheWriteError extends MCPCacheError {
39
+ constructor(message: string, options?: {
40
+ cause?: unknown;
41
+ });
42
+ }
43
+ /**
44
+ * Raised when a snapshot is older than `staleness.maxAgeMs` and the re-list that
45
+ * would have refreshed it failed — so the only tools available are ones the
46
+ * caller declared too old to use.
47
+ *
48
+ * Subclasses {@link MCPCacheError}, so existing `catch (e instanceof
49
+ * MCPCacheError)` sites keep working. Catch this specifically to fall back to
50
+ * stale-but-usable tools instead of failing.
51
+ */
52
+ export declare class MCPStaleSnapshotError extends MCPCacheError {
53
+ constructor(message: string, options?: {
54
+ cause?: unknown;
55
+ });
56
+ }
27
57
  /**
28
58
  * Raised when connecting to the MCP server fails across all attempted transports.
29
59
  */
30
60
  export declare class MCPConnectionError extends MCPError {
61
+ /**
62
+ * Every failure behind this one, in attempt order.
63
+ *
64
+ * `cause` holds only the last attempt, which loses information: an
65
+ * `UnauthorizedError` from the Streamable HTTP attempt matters even when the
66
+ * SSE fallback then failed for an unrelated reason (a URL that simply isn't an
67
+ * SSE endpoint answering 404). Named `errors` to match `AggregateError`, so it
68
+ * reads the way callers expect rather than as a bespoke field.
69
+ *
70
+ * Always populated: a single-attempt failure contributes its one underlying
71
+ * error, so `errors[errors.length - 1]` and `cause` agree and a caller can
72
+ * iterate `errors` alone without special-casing the single-attempt shape —
73
+ * which matters most on the auth short-circuit, where exactly one attempt was
74
+ * made and its rejection is the thing worth finding.
75
+ */
76
+ readonly errors: readonly unknown[];
31
77
  constructor(message: string, options?: {
32
78
  cause?: unknown;
79
+ errors?: readonly unknown[];
33
80
  });
34
81
  }
35
82
  //# sourceMappingURL=errors.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"errors.d.ts","sourceRoot":"","sources":["../src/errors.ts"],"names":[],"mappings":"AAAA;;GAEG;AACH,qBAAa,QAAS,SAAQ,KAAK;gBAE/B,OAAO,EAAE,MAAM,EACf,OAAO,CAAC,EAAE;QACR,KAAK,CAAC,EAAE,OAAO,CAAC;KACjB;CAKJ;AAED;;;GAGG;AACH,qBAAa,gBAAiB,SAAQ,QAAQ;IAC5C,QAAQ,CAAC,QAAQ,EAAE,MAAM,CAAC;gBAGxB,QAAQ,EAAE,MAAM,EAChB,OAAO,EAAE,MAAM,EACf,OAAO,CAAC,EAAE;QACR,KAAK,CAAC,EAAE,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IAC9C;;;;;;;;;;;;;;OAcG;IACH,QAAQ,CAAC,MAAM,EAAE,SAAS,OAAO,EAAE,CAAC;gBAGlC,OAAO,EAAE,MAAM,EACf,OAAO,CAAC,EAAE;QACR,KAAK,CAAC,EAAE,OAAO,CAAC;QAChB,MAAM,CAAC,EAAE,SAAS,OAAO,EAAE,CAAC;KAC7B;CAcJ"}
package/esm/errors.js CHANGED
@@ -27,6 +27,38 @@ export class MCPCacheError extends MCPError {
27
27
  this.name = 'MCPCacheError';
28
28
  }
29
29
  }
30
+ /**
31
+ * Raised when the tool list was read successfully but writing the snapshot back
32
+ * to the caller's `MCPCacheStore` failed.
33
+ *
34
+ * Distinct from the read failing, because the consequences differ completely: the
35
+ * connection is live and its tools are current, so the only thing lost is a cache
36
+ * entry. Callers who treat a store outage as fatal can catch this; callers who
37
+ * would rather keep working with a warm handle can ignore it.
38
+ *
39
+ * Subclasses {@link MCPCacheError}, so existing catch sites keep working.
40
+ */
41
+ export class MCPCacheWriteError extends MCPCacheError {
42
+ constructor(message, options) {
43
+ super(message, options);
44
+ this.name = 'MCPCacheWriteError';
45
+ }
46
+ }
47
+ /**
48
+ * Raised when a snapshot is older than `staleness.maxAgeMs` and the re-list that
49
+ * would have refreshed it failed — so the only tools available are ones the
50
+ * caller declared too old to use.
51
+ *
52
+ * Subclasses {@link MCPCacheError}, so existing `catch (e instanceof
53
+ * MCPCacheError)` sites keep working. Catch this specifically to fall back to
54
+ * stale-but-usable tools instead of failing.
55
+ */
56
+ export class MCPStaleSnapshotError extends MCPCacheError {
57
+ constructor(message, options) {
58
+ super(message, options);
59
+ this.name = 'MCPStaleSnapshotError';
60
+ }
61
+ }
30
62
  /**
31
63
  * Raised when connecting to the MCP server fails across all attempted transports.
32
64
  */
@@ -34,6 +66,15 @@ export class MCPConnectionError extends MCPError {
34
66
  constructor(message, options) {
35
67
  super(message, options);
36
68
  this.name = 'MCPConnectionError';
69
+ // Default a single-attempt failure to its own cause, so `errors` is a
70
+ // uniform "every attempt" list rather than empty-unless-multi.
71
+ this.errors =
72
+ options?.errors ??
73
+ (options?.cause !== undefined
74
+ ? [
75
+ options.cause,
76
+ ]
77
+ : []);
37
78
  }
38
79
  }
39
80
  //# sourceMappingURL=errors.js.map
package/esm/errors.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"errors.js","sourceRoot":"","sources":["../src/errors.ts"],"names":[],"mappings":"AAAA;;GAEG;AACH,MAAM,OAAO,QAAS,SAAQ,KAAK;IACjC,YACE,OAAe,EACf,OAEC;QAED,KAAK,CAAC,OAAO,EAAE,OAAO,CAAC,CAAC;QACxB,IAAI,CAAC,IAAI,GAAG,UAAU,CAAC;IACzB,CAAC;CACF;AAED;;;GAGG;AACH,MAAM,OAAO,gBAAiB,SAAQ,QAAQ;IAG5C,YACE,QAAgB,EAChB,OAAe,EACf,OAEC;QAED,KAAK,CAAC,OAAO,EAAE,OAAO,CAAC,CAAC;QACxB,IAAI,CAAC,IAAI,GAAG,kBAAkB,CAAC;QAC/B,IAAI,CAAC,QAAQ,GAAG,QAAQ,CAAC;IAC3B,CAAC;CACF;AAED;;GAEG;AACH,MAAM,OAAO,aAAc,SAAQ,QAAQ;IACzC,YACE,OAAe,EACf,OAEC;QAED,KAAK,CAAC,OAAO,EAAE,OAAO,CAAC,CAAC;QACxB,IAAI,CAAC,IAAI,GAAG,eAAe,CAAC;IAC9B,CAAC;CACF;AAED;;GAEG;AACH,MAAM,OAAO,kBAAmB,SAAQ,QAAQ;IAC9C,YACE,OAAe,EACf,OAEC;QAED,KAAK,CAAC,OAAO,EAAE,OAAO,CAAC,CAAC;QACxB,IAAI,CAAC,IAAI,GAAG,oBAAoB,CAAC;IACnC,CAAC;CACF"}
1
+ {"version":3,"file":"errors.js","sourceRoot":"","sources":["../src/errors.ts"],"names":[],"mappings":"AAAA;;GAEG;AACH,MAAM,OAAO,QAAS,SAAQ,KAAK;IACjC,YACE,OAAe,EACf,OAEC;QAED,KAAK,CAAC,OAAO,EAAE,OAAO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;qBACd;oBACH,CAAC,CAAC,EAAE,CAAC,CAAC;IACZ,CAAC;CACF"}
package/esm/handle.d.ts CHANGED
@@ -9,6 +9,18 @@ export declare function normalizeUrl(url: string | URL): URL;
9
9
  * `tools/list` is paginated: each response may carry a `nextCursor` that must be
10
10
  * passed back as `{ cursor }` to fetch the next page. We accumulate every page so
11
11
  * servers that paginate their tool list aren't silently truncated.
12
+ *
13
+ * Always `cacheMode: 'refresh'`. SDK v2 keeps a per-client response cache
14
+ * honouring the server's `ttlMs` on `tools/list` (up to 24h), so the default
15
+ * `'use'` would serve a still-fresh cached list without a round trip. That is
16
+ * fine for an opportunistic read but wrong for every caller here: `refresh()`
17
+ * documents a forced re-read, `freshConnect` is establishing the tool set for the
18
+ * first time, and the stale-replay path exists precisely because the age of the
19
+ * cached list is no longer acceptable. `'refresh'` fetches and re-stores, so the
20
+ * cache stays warm for the SDK's own readers.
21
+ *
22
+ * `'refresh'` rather than `'bypass'` deliberately — bypass would skip the write
23
+ * too, leaving a stale entry behind for anything that later reads with `'use'`.
12
24
  */
13
25
  export declare function listToolDefs(connection: MCPConnection, signal: AbortSignal | undefined): Promise<McpToolDef[]>;
14
26
  interface HandleContext {
@@ -21,6 +33,19 @@ export interface MakeHandleArgs {
21
33
  options: CreateMCPToolsOptions;
22
34
  context: HandleContext;
23
35
  initialToolDefs: McpToolDef[];
36
+ /**
37
+ * `cachedAt` to carry forward when `initialToolDefs` came from a snapshot
38
+ * rather than a live `listTools()`. Omit on a cold connect so the write
39
+ * stamps now.
40
+ *
41
+ * Without this a replay would restamp `cachedAt` on the write-back below,
42
+ * resetting the staleness clock on a tool set that was never re-listed —
43
+ * so `staleness.maxAgeMs` could never fire on a repeatedly-rehydrated
44
+ * snapshot. Age tracks when the tools were last actually discovered, not
45
+ * when they were last replayed. `refresh()` clears it, because that path
46
+ * does re-list.
47
+ */
48
+ replayedCachedAt?: number;
24
49
  }
25
50
  /**
26
51
  * Connect, discover tools via `listTools()`, and build a handle WITHOUT
@@ -1 +1 @@
1
- {"version":3,"file":"handle.d.ts","sourceRoot":"","sources":["../src/handle.ts"],"names":[],"mappings":"AAMA,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,qBAAqB,CAAC;AAEzD,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,mBAAmB,CAAC;AACpD,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,sBAAsB,CAAC;AAC7D,OAAO,KAAK,EAAE,qBAAqB,EAAE,cAAc,EAAE,MAAM,YAAY,CAAC;AAExE,wBAAgB,YAAY,CAAC,GAAG,EAAE,MAAM,GAAG,GAAG,GAAG,GAAG,CAEnD;AAcD;;;;;;GAMG;AACH,wBAAsB,YAAY,CAChC,UAAU,EAAE,aAAa,EACzB,MAAM,EAAE,WAAW,GAAG,SAAS,GAC9B,OAAO,CAAC,UAAU,EAAE,CAAC,CAsCvB;AAOD,UAAU,aAAa;IACrB,GAAG,EAAE,GAAG,CAAC;IACT,SAAS,EAAE,gBAAgB,CAAC;IAC5B,QAAQ,EAAE,MAAM,CAAC;CAClB;AAED,MAAM,WAAW,cAAc;IAC7B,UAAU,EAAE,aAAa,CAAC;IAC1B,OAAO,EAAE,qBAAqB,CAAC;IAC/B,OAAO,EAAE,aAAa,CAAC;IACvB,eAAe,EAAE,UAAU,EAAE,CAAC;CAC/B;AAED;;;;;;;;GAQG;AACH,wBAAsB,YAAY,CAChC,OAAO,EAAE,qBAAqB,EAC9B,GAAG,EAAE,GAAG,EACR,QAAQ,EAAE,MAAM,GACf,OAAO,CAAC,cAAc,CAAC,CAsCzB;AAED;;;GAGG;AACH,wBAAsB,UAAU,CAAC,IAAI,EAAE,cAAc,GAAG,OAAO,CAAC,cAAc,CAAC,CAmE9E"}
1
+ {"version":3,"file":"handle.d.ts","sourceRoot":"","sources":["../src/handle.ts"],"names":[],"mappings":"AAQA,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,qBAAqB,CAAC;AAEzD,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,mBAAmB,CAAC;AACpD,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,sBAAsB,CAAC;AAC7D,OAAO,KAAK,EAAE,qBAAqB,EAAE,cAAc,EAAE,MAAM,YAAY,CAAC;AAExE,wBAAgB,YAAY,CAAC,GAAG,EAAE,MAAM,GAAG,GAAG,GAAG,GAAG,CAEnD;AAiCD;;;;;;;;;;;;;;;;;;GAkBG;AACH,wBAAsB,YAAY,CAChC,UAAU,EAAE,aAAa,EACzB,MAAM,EAAE,WAAW,GAAG,SAAS,GAC9B,OAAO,CAAC,UAAU,EAAE,CAAC,CA0CvB;AAOD,UAAU,aAAa;IACrB,GAAG,EAAE,GAAG,CAAC;IACT,SAAS,EAAE,gBAAgB,CAAC;IAC5B,QAAQ,EAAE,MAAM,CAAC;CAClB;AAED,MAAM,WAAW,cAAc;IAC7B,UAAU,EAAE,aAAa,CAAC;IAC1B,OAAO,EAAE,qBAAqB,CAAC;IAC/B,OAAO,EAAE,aAAa,CAAC;IACvB,eAAe,EAAE,UAAU,EAAE,CAAC;IAC9B;;;;;;;;;;;OAWG;IACH,gBAAgB,CAAC,EAAE,MAAM,CAAC;CAC3B;AAED;;;;;;;;GAQG;AACH,wBAAsB,YAAY,CAChC,OAAO,EAAE,qBAAqB,EAC9B,GAAG,EAAE,GAAG,EACR,QAAQ,EAAE,MAAM,GACf,OAAO,CAAC,cAAc,CAAC,CA+CzB;AAED;;;GAGG;AACH,wBAAsB,UAAU,CAAC,IAAI,EAAE,cAAc,GAAG,OAAO,CAAC,cAAc,CAAC,CA4H9E"}
package/esm/handle.js CHANGED
@@ -1,5 +1,7 @@
1
1
  import { buildTools } from './build-tools.js';
2
2
  import { serializeServer } from './cache/serialize.js';
3
+ import { closeQuietly } from './close-quietly.js';
4
+ import { MCPCacheWriteError } from './errors.js';
3
5
  import { connect } from './mcp-connection.js';
4
6
  export function normalizeUrl(url) {
5
7
  return url instanceof URL ? url : new URL(url);
@@ -14,19 +16,48 @@ const MAX_LIST_PAGES = 1000;
14
16
  function nextCursorOrUndefined(value) {
15
17
  return typeof value === 'string' && value.length > 0 ? value : undefined;
16
18
  }
19
+ /**
20
+ * Parse a server-advertised doom-loop identity from a tool definition's
21
+ * `_meta['openrouter/loopKey']`: a field-name array (the declarative subset
22
+ * of arguments identifying a call) or `false` (tool exempt). Data-only by
23
+ * design — functions can't cross the wire; any other shape is ignored.
24
+ */
25
+ function advertisedLoopKey(meta) {
26
+ const advertised = meta?.['openrouter/loopKey'];
27
+ if (advertised === false) {
28
+ return false;
29
+ }
30
+ if (Array.isArray(advertised) && advertised.every((f) => typeof f === 'string')) {
31
+ return advertised;
32
+ }
33
+ return undefined;
34
+ }
17
35
  /**
18
36
  * Read the discovered tools off the live connection into our internal shape.
19
37
  *
20
38
  * `tools/list` is paginated: each response may carry a `nextCursor` that must be
21
39
  * passed back as `{ cursor }` to fetch the next page. We accumulate every page so
22
40
  * servers that paginate their tool list aren't silently truncated.
41
+ *
42
+ * Always `cacheMode: 'refresh'`. SDK v2 keeps a per-client response cache
43
+ * honouring the server's `ttlMs` on `tools/list` (up to 24h), so the default
44
+ * `'use'` would serve a still-fresh cached list without a round trip. That is
45
+ * fine for an opportunistic read but wrong for every caller here: `refresh()`
46
+ * documents a forced re-read, `freshConnect` is establishing the tool set for the
47
+ * first time, and the stale-replay path exists precisely because the age of the
48
+ * cached list is no longer acceptable. `'refresh'` fetches and re-stores, so the
49
+ * cache stays warm for the SDK's own readers.
50
+ *
51
+ * `'refresh'` rather than `'bypass'` deliberately — bypass would skip the write
52
+ * too, leaving a stale entry behind for anything that later reads with `'use'`.
23
53
  */
24
54
  export async function listToolDefs(connection, signal) {
25
- const requestOptions = signal !== undefined
26
- ? {
55
+ const requestOptions = {
56
+ cacheMode: 'refresh',
57
+ ...(signal !== undefined && {
27
58
  signal,
28
- }
29
- : undefined;
59
+ }),
60
+ };
30
61
  const collected = [];
31
62
  let cursor;
32
63
  for (let page = 0; page < MAX_LIST_PAGES; page += 1) {
@@ -37,6 +68,7 @@ export async function listToolDefs(connection, signal) {
37
68
  : undefined;
38
69
  const { tools, nextCursor } = await connection.client.listTools(params, requestOptions);
39
70
  for (const t of tools) {
71
+ const loopKey = advertisedLoopKey(t._meta);
40
72
  collected.push({
41
73
  name: t.name,
42
74
  ...(t.description !== undefined && {
@@ -46,6 +78,9 @@ export async function listToolDefs(connection, signal) {
46
78
  ...(t.outputSchema !== undefined && {
47
79
  outputSchema: t.outputSchema,
48
80
  }),
81
+ ...(loopKey !== undefined && {
82
+ loopKey,
83
+ }),
49
84
  });
50
85
  }
51
86
  const next = nextCursorOrUndefined(nextCursor);
@@ -89,6 +124,15 @@ export async function freshConnect(options, url, cacheKey) {
89
124
  ...(options.onElicitation !== undefined && {
90
125
  onElicitation: options.onElicitation,
91
126
  }),
127
+ ...(options.protocolNegotiation !== undefined && {
128
+ protocolNegotiation: options.protocolNegotiation,
129
+ }),
130
+ ...(options.probeTimeoutMs !== undefined && {
131
+ probeTimeoutMs: options.probeTimeoutMs,
132
+ }),
133
+ ...(options.signal !== undefined && {
134
+ signal: options.signal,
135
+ }),
92
136
  });
93
137
  // Tear the connection down if discovery or the initial cache write throws —
94
138
  // otherwise the open transport (HTTP keep-alive / SSE stream) leaks.
@@ -106,7 +150,7 @@ export async function freshConnect(options, url, cacheKey) {
106
150
  });
107
151
  }
108
152
  catch (err) {
109
- await connection.close().catch(() => { });
153
+ await closeQuietly(connection);
110
154
  throw err;
111
155
  }
112
156
  }
@@ -118,6 +162,8 @@ export async function makeHandle(args) {
118
162
  const { connection, options, context, initialToolDefs } = args;
119
163
  const listeners = new Set();
120
164
  let toolDefs = initialToolDefs;
165
+ // Cleared by refresh(): once we have re-listed, age is measured from now.
166
+ let replayedCachedAt = args.replayedCachedAt;
121
167
  const serverInfo = connection.client.getServerVersion();
122
168
  const rebuild = () => buildTools(buildToolsArgs(connection, toolDefs, options));
123
169
  let tools = rebuild();
@@ -127,16 +173,47 @@ export async function makeHandle(args) {
127
173
  toolDefs,
128
174
  serverInfo,
129
175
  options,
176
+ ...(replayedCachedAt !== undefined && {
177
+ cachedAt: replayedCachedAt,
178
+ }),
130
179
  }));
131
180
  const writeCache = async () => {
132
181
  const store = options.cache?.store;
133
182
  if (store === undefined) {
134
183
  return;
135
184
  }
136
- await store.set(context.cacheKey, await snapshot());
185
+ // Built OUTSIDE the try: `snapshot()` can reject from the caller's own
186
+ // OAuth provider (`provider.tokens()`), and relabelling that as a cache
187
+ // write failure would send it down every "store outages are harmless"
188
+ // path — swallowed by the list_changed handler, ignored by
189
+ // `refreshStaleReplay`, and dismissed by callers following the documented
190
+ // `MCPCacheWriteError` pattern. Only the store op earns the tag.
191
+ const payload = await snapshot();
192
+ try {
193
+ await store.set(context.cacheKey, payload);
194
+ }
195
+ catch (writeErr) {
196
+ // Tagged so callers can tell a store outage from a failed read. The two
197
+ // look identical coming out of `refresh()` but mean opposite things: a
198
+ // failed read leaves the tool set unknown, while a failed write leaves a
199
+ // live connection with current tools and only a stale cache entry.
200
+ throw new MCPCacheWriteError('Failed to write the MCP snapshot to the cache store', {
201
+ cause: writeErr,
202
+ });
203
+ }
137
204
  };
205
+ // INVARIANT internal callers rely on: `refresh()` swaps `tools` to a fresh
206
+ // array (rebuild() always allocates) exactly when the re-list succeeded, and
207
+ // it does so BEFORE attempting to persist. A caller that captured the old
208
+ // reference can therefore tell "the re-list failed" (reference unchanged)
209
+ // from "only something after the re-list failed" (reference changed) without
210
+ // inspecting the error — which no error-class check can do reliably, since
211
+ // persistence can fail outside the store op too (the caller's own OAuth
212
+ // provider rejecting inside `snapshot()`).
138
213
  const refresh = async () => {
139
214
  toolDefs = await listToolDefs(connection, options.signal);
215
+ // Re-listed, so the snapshot is genuinely current from here on.
216
+ replayedCachedAt = undefined;
140
217
  tools = rebuild();
141
218
  await writeCache();
142
219
  return tools;
@@ -144,17 +221,42 @@ export async function makeHandle(args) {
144
221
  if (options.autoRefreshOnListChanged ?? true) {
145
222
  connection.setToolListChangedHandler(() => {
146
223
  // Fire-and-forget, but never let a failed refresh escape as an unhandled
147
- // rejection. On failure listeners keep the last good tool set.
224
+ // rejection. Whether to announce is keyed on ADOPTION (the reference
225
+ // swap above), not on error class: once `refresh()` has swapped `tools`,
226
+ // skipping the announcement would leave subscribers permanently out of
227
+ // sync with `handle.tools`, no matter what broke afterwards — a store
228
+ // outage and an OAuth provider failing during serialize gate it equally
229
+ // little. On a failed re-list the reference is untouched and subscribers
230
+ // correctly keep the last good set.
231
+ const before = tools;
148
232
  void refresh()
149
- .then((next) => {
233
+ .catch(() => undefined)
234
+ .then(() => {
235
+ if (tools === before) {
236
+ return;
237
+ }
150
238
  for (const listener of listeners) {
151
- listener(next);
239
+ listener(tools);
152
240
  }
153
241
  })
154
242
  .catch(() => { });
155
243
  });
156
244
  }
157
- await writeCache();
245
+ // Skipped entirely on a replay: with `replayedCachedAt` carried forward the
246
+ // snapshot we would write is the one just read — same tool defs, same
247
+ // `cachedAt` — so the write is an external store round-trip per rehydrate
248
+ // buying nothing. (It also cannot *refresh* anything: preserving the age is
249
+ // the point.) A cold connect writes; `refresh()` clears `replayedCachedAt`
250
+ // and writes. Skipping here additionally narrows DEV-766's surface — one
251
+ // fewer path that can rewrite a credentialed entry under different options.
252
+ //
253
+ // Best-effort when it does run: the handle is fully usable without its cache
254
+ // entry, so a store outage should not stop a connection that already
255
+ // succeeded. `refresh()` reports write failures as `MCPCacheWriteError` for
256
+ // callers that do care.
257
+ if (args.replayedCachedAt === undefined) {
258
+ await writeCache().catch(() => { });
259
+ }
158
260
  return {
159
261
  get tools() {
160
262
  return tools;
@@ -193,6 +295,9 @@ function buildToolsArgs(connection, toolDefs, options) {
193
295
  ...(options.signal !== undefined && {
194
296
  signal: options.signal,
195
297
  }),
298
+ ...(options.loopKeys !== undefined && {
299
+ loopKeys: options.loopKeys,
300
+ }),
196
301
  ...(options.resources !== undefined && {
197
302
  resources: options.resources,
198
303
  }),
@@ -206,13 +311,10 @@ function serializeArgs(args) {
206
311
  transport: connection.transport,
207
312
  toolDefs,
208
313
  cacheCredentials: options.cacheCredentials ?? false,
209
- cachedAt: Date.now(),
314
+ cachedAt: args.cachedAt ?? Date.now(),
210
315
  ...(serverInfo !== undefined && {
211
316
  serverInfo,
212
317
  }),
213
- ...(connection.sessionId !== undefined && {
214
- sessionId: connection.sessionId,
215
- }),
216
318
  ...(options.auth !== undefined && {
217
319
  auth: options.auth,
218
320
  }),
package/esm/handle.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"handle.js","sourceRoot":"","sources":["../src/handle.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,UAAU,EAAE,MAAM,kBAAkB,CAAC;AAG9C,OAAO,EAAE,eAAe,EAAE,MAAM,sBAAsB,CAAC;AAEvD,OAAO,EAAE,OAAO,EAAE,MAAM,qBAAqB,CAAC;AAK9C,MAAM,UAAU,YAAY,CAAC,GAAiB;IAC5C,OAAO,GAAG,YAAY,GAAG,CAAC,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC,IAAI,GAAG,CAAC,GAAG,CAAC,CAAC;AACjD,CAAC;AAED,4EAA4E;AAC5E,iFAAiF;AACjF,MAAM,cAAc,GAAG,IAAI,CAAC;AAE5B;;;GAGG;AACH,SAAS,qBAAqB,CAAC,KAAyB;IACtD,OAAO,OAAO,KAAK,KAAK,QAAQ,IAAI,KAAK,CAAC,MAAM,GAAG,CAAC,CAAC,CAAC,CAAC,KAAK,CAAC,CAAC,CAAC,SAAS,CAAC;AAC3E,CAAC;AAED;;;;;;GAMG;AACH,MAAM,CAAC,KAAK,UAAU,YAAY,CAChC,UAAyB,EACzB,MAA+B;IAE/B,MAAM,cAAc,GAClB,MAAM,KAAK,SAAS;QAClB,CAAC,CAAC;YACE,MAAM;SACP;QACH,CAAC,CAAC,SAAS,CAAC;IAChB,MAAM,SAAS,GAAiB,EAAE,CAAC;IACnC,IAAI,MAA0B,CAAC;IAC/B,KAAK,IAAI,IAAI,GAAG,CAAC,EAAE,IAAI,GAAG,c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AAO,CAAC,qBAAqB,KAAK,SAAS,IAAI;YACjD,UAAU,EAAE,OAAO,CAAC,qBAAqB;SAC1C,CAAC;QACF,GAAG,CAAC,OAAO,CAAC,MAAM,KAAK,SAAS,IAAI;YAClC,MAAM,EAAE,OAAO,CAAC,MAAM;SACvB,CAAC;QACF,GAAG,CAAC,OAAO,CAAC,QAAQ,KAAK,SAAS,IAAI;YACpC,QAAQ,EAAE,OAAO,CAAC,QAAQ;SAC3B,CAAC;QACF,GAAG,CAAC,OAAO,CAAC,SAAS,KAAK,SAAS,IAAI;YACrC,SAAS,EAAE,OAAO,CAAC,SAAS;SAC7B,CAAC;KACH,CAAC;AACJ,CAAC;AAiBD,qFAAqF;AACrF,SAAS,aAAa,CAAC,IAAwB;IAC7C,MAAM,EAAE,UAAU,EAAE,OAAO,EAAE,QAAQ,EAAE,UAAU,EAAE,OAAO,EAAE,GAAG,IAAI,CAAC;IACpE,OAAO;QACL,GAAG,EAAE,OAAO,CAAC,GAAG,CAAC,IAAI;QACrB,SAAS,EAAE,UAAU,CAAC,SAAS;QAC/B,QAAQ;QACR,gBAAgB,EAAE,OAAO,CAAC,gBAAgB,IAAI,KAAK;QACnD,QAAQ,EAAE,IAAI,CAAC,QAAQ,IAAI,IAAI,CAAC,GAAG,EAAE;QACrC,GAAG,CAAC,UAAU,KAAK,SAAS,IAAI;YAC9B,UAAU;SACX,CAAC;QACF,GAAG,CAAC,OAAO,CAAC,IAAI,KAAK,SAAS,IAAI;YAChC,IAAI,EAAE,OAAO,CAAC,IAAI;SACnB,CAAC;KACH,CAAC;AACJ,CAAC"}
package/esm/index.d.ts CHANGED
@@ -1,13 +1,13 @@
1
- export type { MCPAuth } from './auth/auth-types.js';
1
+ export type { MCPAuth, MCPOAuthClientProvider } from './auth/auth-types.js';
2
2
  export type { MCPCacheStore } from './cache/cache-store.js';
3
3
  export { defaultCacheKey, InMemoryMCPCacheStore } from './cache/cache-store.js';
4
4
  export type { SerializedMCPServer, SerializedMCPToolDef, SerializedTokenSet, } from './cache/cache-types.js';
5
5
  export { isSerializedMCPServer } from './cache/cache-types.js';
6
6
  export { createMCPTools } from './create-mcp-tools.js';
7
- export { MCPCacheError, MCPConnectionError, MCPError, MCPToolCallError, } from './errors.js';
7
+ export { MCPCacheError, MCPCacheWriteError, MCPConnectionError, MCPError, MCPStaleSnapshotError, MCPToolCallError, } from './errors.js';
8
8
  export type { RehydrateMCPToolsOptions } from './rehydrate.js';
9
9
  export { rehydrateMCPTools } from './rehydrate.js';
10
10
  export type { UnconvertibleSchemaMode } from './schema/json-schema-to-zod.js';
11
11
  export { convertMcpInputSchema } from './schema/json-schema-to-zod.js';
12
- export type { CreateMCPToolsOptions, ElicitationHandler, ElicitationResponse, MCPToolsHandle, MCPTransportKind, ResourcesOption, } from './types.js';
12
+ export type { CreateMCPToolsOptions, ElicitationHandler, ElicitationResponse, MCPProtocolNegotiation, MCPProtocolRevision, MCPToolsHandle, MCPTransportKind, ResourcesOption, } from './types.js';
13
13
  //# sourceMappingURL=index.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAGA,YAAY,EAAE,OAAO,EAAE,MAAM,sBAAsB,CAAC;AACpD,YAAY,EAAE,aAAa,EAAE,MAAM,wBAAwB,CAAC;AAE5D,OAAO,EAAE,eAAe,EAAE,qBAAqB,EAAE,MAAM,wBAAwB,CAAC;AAChF,YAAY,EACV,mBAAmB,EACnB,oBAAoB,EACpB,kBAAkB,GACnB,MAAM,wBAAwB,CAAC;AAChC,OAAO,EAAE,qBAAqB,EAAE,MAAM,wBAAwB,CAAC;AAC/D,OAAO,EAAE,cAAc,EAAE,MAAM,uBAAuB,CAAC;AAEvD,OAAO,EACL,aAAa,EACb,kBAAkB,EAClB,QAAQ,EACR,gBAAgB,GACjB,MAAM,aAAa,CAAC;AACrB,YAAY,EAAE,wBAAwB,EAAE,MAAM,gBAAgB,CAAC;AAC/D,OAAO,EAAE,iBAAiB,EAAE,MAAM,gBAAgB,CAAC;AACnD,YAAY,EAAE,uBAAuB,EAAE,MAAM,gCAAgC,CAAC;AAE9E,OAAO,EAAE,qBAAqB,EAAE,MAAM,gCAAgC,CAAC;AAEvE,YAAY,EACV,qBAAqB,EACrB,kBAAkB,EAClB,mBAAmB,EACnB,cAAc,EACd,gBAAgB,EAChB,eAAe,GAChB,MAAM,YAAY,CAAC"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAGA,YAAY,EAAE,OAAO,EAAE,sBAAsB,EAAE,MAAM,sBAAsB,CAAC;AAC5E,YAAY,EAAE,aAAa,EAAE,MAAM,wBAAwB,CAAC;AAE5D,OAAO,EAAE,eAAe,EAAE,qBAAqB,EAAE,MAAM,wBAAwB,CAAC;AAChF,YAAY,EACV,mBAAmB,EACnB,oBAAoB,EACpB,kBAAkB,GACnB,MAAM,wBAAwB,CAAC;AAChC,OAAO,EAAE,qBAAqB,EAAE,MAAM,wBAAwB,CAAC;AAC/D,OAAO,EAAE,cAAc,EAAE,MAAM,uBAAuB,CAAC;AAEvD,OAAO,EACL,aAAa,EACb,kBAAkB,EAClB,kBAAkB,EAClB,QAAQ,EACR,qBAAqB,EACrB,gBAAgB,GACjB,MAAM,aAAa,CAAC;AACrB,YAAY,EAAE,wBAAwB,EAAE,MAAM,gBAAgB,CAAC;AAC/D,OAAO,EAAE,iBAAiB,EAAE,MAAM,gBAAgB,CAAC;AACnD,YAAY,EAAE,uBAAuB,EAAE,MAAM,gCAAgC,CAAC;AAE9E,OAAO,EAAE,qBAAqB,EAAE,MAAM,gCAAgC,CAAC;AAEvE,YAAY,EACV,qBAAqB,EACrB,kBAAkB,EAClB,mBAAmB,EACnB,sBAAsB,EACtB,mBAAmB,EACnB,cAAc,EACd,gBAAgB,EAChB,eAAe,GAChB,MAAM,YAAY,CAAC"}
package/esm/index.js CHANGED
@@ -4,7 +4,7 @@ export { defaultCacheKey, InMemoryMCPCacheStore } from './cache/cache-store.js';
4
4
  export { isSerializedMCPServer } from './cache/cache-types.js';
5
5
  export { createMCPTools } from './create-mcp-tools.js';
6
6
  // Errors
7
- export { MCPCacheError, MCPConnectionError, MCPError, MCPToolCallError, } from './errors.js';
7
+ export { MCPCacheError, MCPCacheWriteError, MCPConnectionError, MCPError, MCPStaleSnapshotError, MCPToolCallError, } from './errors.js';
8
8
  export { rehydrateMCPTools } from './rehydrate.js';
9
9
  // Schema conversion (exported for testing/reuse)
10
10
  export { convertMcpInputSchema } from './schema/json-schema-to-zod.js';
package/esm/index.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,6BAA6B;AAK7B,QAAQ;AACR,OAAO,EAAE,eAAe,EAAE,qBAAqB,EAAE,MAAM,wBAAwB,CAAC;AAMhF,OAAO,EAAE,qBAAqB,EAAE,MAAM,wBAAwB,CAAC;AAC/D,OAAO,EAAE,cAAc,EAAE,MAAM,uBAAuB,CAAC;AACvD,SAAS;AACT,OAAO,EACL,aAAa,EACb,kBAAkB,EAClB,QAAQ,EACR,gBAAgB,GACjB,MAAM,aAAa,CAAC;AAErB,OAAO,EAAE,iBAAiB,EAAE,MAAM,gBAAgB,CAAC;AAEnD,iDAAiD;AACjD,OAAO,EAAE,qBAAqB,EAAE,MAAM,gCAAgC,CAAC"}
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,6BAA6B;AAK7B,QAAQ;AACR,OAAO,EAAE,eAAe,EAAE,qBAAqB,EAAE,MAAM,wBAAwB,CAAC;AAMhF,OAAO,EAAE,qBAAqB,EAAE,MAAM,wBAAwB,CAAC;AAC/D,OAAO,EAAE,cAAc,EAAE,MAAM,uBAAuB,CAAC;AACvD,SAAS;AACT,OAAO,EACL,aAAa,EACb,kBAAkB,EAClB,kBAAkB,EAClB,QAAQ,EACR,qBAAqB,EACrB,gBAAgB,GACjB,MAAM,aAAa,CAAC;AAErB,OAAO,EAAE,iBAAiB,EAAE,MAAM,gBAAgB,CAAC;AAEnD,iDAAiD;AACjD,OAAO,EAAE,qBAAqB,EAAE,MAAM,gCAAgC,CAAC"}
@@ -1,6 +1,7 @@
1
- import { Client } from '@modelcontextprotocol/sdk/client/index.js';
1
+ import { Client } from '@modelcontextprotocol/client';
2
2
  import type { MCPAuth } from './auth/auth-types.js';
3
- import type { ElicitationHandler, MCPTransportKind } from './types.js';
3
+ import type { MCPProtocolNegotiation, MCPTransportKind } from './transport-types.js';
4
+ import type { ElicitationHandler } from './types.js';
4
5
  export interface ConnectOptions {
5
6
  url: URL;
6
7
  transport?: MCPTransportKind;
@@ -10,12 +11,41 @@ export interface ConnectOptions {
10
11
  name: string;
11
12
  version: string;
12
13
  };
14
+ /**
15
+ * Streamable HTTP session id to resume.
16
+ *
17
+ * @deprecated Ignored by 2026-07-28 servers — protocol-level sessions are
18
+ * removed (SEP-2567). Still honoured on 2025-era connections.
19
+ */
13
20
  sessionId?: string;
14
21
  onElicitation?: ElicitationHandler;
22
+ protocolNegotiation?: MCPProtocolNegotiation;
23
+ /**
24
+ * Aborts the connect — every transport attempt, the negotiation probe, and
25
+ * the implicit legacy retry. Without this a caller with its own deadline has
26
+ * no way to bound the ladder, whose worst case is minutes (see
27
+ * {@link DEFAULT_PROBE_TIMEOUT_MS}).
28
+ */
29
+ signal?: AbortSignal;
30
+ /**
31
+ * Ceiling on the `server/discover` probe, in ms. Defaults to
32
+ * `DEFAULT_PROBE_TIMEOUT_MS` (30000).
33
+ *
34
+ * Raise it for a server that is slow to answer its first request; the SDK's own
35
+ * default is the full request timeout, which makes a hanging gateway far slower
36
+ * to fail than it needs to be.
37
+ */
38
+ probeTimeoutMs?: number;
15
39
  }
16
40
  export interface MCPConnection {
17
41
  client: Client;
18
42
  transport: MCPTransportKind;
43
+ /**
44
+ * Session id reported by the transport, when there is one.
45
+ *
46
+ * @deprecated `undefined` on 2026-07-28 connections (SEP-2567) and on SSE,
47
+ * which never had a protocol-level session.
48
+ */
19
49
  sessionId?: string;
20
50
  /**
21
51
  * Register a callback for `tools/list_changed`. Settable after connect so the
@@ -25,11 +55,88 @@ export interface MCPConnection {
25
55
  close(): Promise<void>;
26
56
  }
27
57
  /**
28
- * Connect a `Client` to the MCP server. Defaults to Streamable HTTP and falls
29
- * back to SSE on connection failure (legacy servers), unless a transport is
30
- * pinned explicitly. Auth, the elicitation handler, and the list_changed
31
- * subscription are wired into the single connected client so they apply to
32
- * discovery and every tool call.
58
+ * Build the configured `Client` — negotiation mode, elicitation handler, and the
59
+ * `tools/list_changed` subscription — without connecting it.
60
+ *
61
+ * Exported for tests only, and deliberately not re-exported from the package
62
+ * entrypoint. It is the seam that lets a test drive the real handler wiring over
63
+ * `InMemoryTransport`: the notification method name is compile-checked (it is a
64
+ * `NotificationMethod` literal union, so a typo fails `tsc`), but the *dispatch*
65
+ * — that an inbound notification actually reaches `setToolListChangedHandler`'s
66
+ * callback — is only observable end to end. Without this seam, default-on
67
+ * `autoRefreshOnListChanged` could be silently dead with every test green.
68
+ *
69
+ * @internal
70
+ */
71
+ export declare function makeClientForTest(options: ConnectOptions, onListChanged: () => void): Client;
72
+ /**
73
+ * Did any attempt behind this failure end in the server rejecting our
74
+ * credentials?
75
+ *
76
+ * Searches the whole tree rather than the `cause` spine, because a single
77
+ * `connect()` can fail more than once: `connectWithNegotiation` wraps transport
78
+ * errors in `MCPConnectionError`, and on the two-transport path it records both
79
+ * attempts in `errors`. Checking only `cause` would miss an `UnauthorizedError`
80
+ * from the Streamable HTTP attempt whenever the SSE fallback then failed for an
81
+ * unrelated reason — a URL that isn't an SSE endpoint answering 404 never
82
+ * reaches the auth path — and the retry would re-drive the OAuth flow anyway.
83
+ *
84
+ * Depth-capped: a cyclic `cause` would otherwise hang, and real chains here are
85
+ * two or three links.
86
+ *
87
+ * Exported for `rehydrateMCPTools`, whose fallback-to-`freshConnect` is a third
88
+ * reconnect layer with the same duplicated-OAuth hazard; deliberately not
89
+ * re-exported from the package entrypoint.
90
+ */
91
+ export declare function isAuthFailure(err: unknown, auth: MCPAuth | undefined, depth?: number): boolean;
92
+ /**
93
+ * Connect a `Client` to the MCP server, degrading to the 2025-era handshake if
94
+ * protocol negotiation gets in the way.
95
+ *
96
+ * We default `protocolNegotiation` to `'auto'` where the SDK defaults to
97
+ * `'legacy'`, so every connection's first request is a `server/discover` probe.
98
+ * On its own that would be a connectivity regression: a proxy, WAF, or strict
99
+ * gateway that hangs or 5xx's on an unknown method takes a server from working
100
+ * to failing, and the SSE fallback re-probes and fails identically — so the
101
+ * two-transport fallback, which looks like defense in depth, collapses to a
102
+ * single point of failure against exactly the infrastructure it should rescue.
103
+ *
104
+ * So when the caller left `protocolNegotiation` unset, a failed connect is
105
+ * retried once with `'legacy'`. That makes `'auto'` strictly additive: modern
106
+ * servers get the new revision, everything else lands exactly where it did
107
+ * before this package started probing. The cost is one extra attempt on a path
108
+ * that is already failing, which is where latency matters least.
109
+ *
110
+ * The retry fires on **any** failure rather than only on ones that look like
111
+ * probe timeouts. Inspecting the cause would avoid a wasted attempt against a
112
+ * genuinely dead server, but it would couple this to SDK error codes — and a
113
+ * reshaped error would silently disable the degradation, which is the failure
114
+ * mode this package keeps getting bitten by.
115
+ *
116
+ * Two bounds keep the retry from making a bad situation worse:
117
+ *
118
+ * 1. **It is skipped on an auth failure.** `UnauthorizedError` means the
119
+ * transport reached the server and the credentials were rejected — nothing a
120
+ * different protocol revision changes. Retrying would re-drive the OAuth
121
+ * provider's authorization flow: a second `redirectToAuthorization`, a second
122
+ * saved PKCE verifier overwriting the first. Replaying a failure that had
123
+ * side effects is worse than not retrying.
124
+ * 2. **It re-walks the same transport ladder the first pass did.** An earlier
125
+ * revision pinned Streamable HTTP to cap the attempt count, which broke the
126
+ * case this whole mechanism exists for: a legacy server reachable *only* over
127
+ * SSE, behind infrastructure that chokes on the probe, would fail under
128
+ * `'auto'` and then never be offered SSE again. Correctness wins over the
129
+ * attempt count here — a server that used to connect must still connect.
130
+ *
131
+ * So the worst case for a genuinely unreachable server is four `connect()`
132
+ * calls, two per negotiation mode. That is the honest cost of guaranteeing no
133
+ * working setup regresses, and it is paid only on a path that was already going
134
+ * to fail. The bound that matters is that it is a fixed multiple rather than a
135
+ * retry loop.
136
+ *
137
+ * An **explicit** `protocolNegotiation` is honoured exactly, including
138
+ * `'auto'`: asking for a mode means asking for its failures too, and silently
139
+ * ignoring a caller's `{ pin }` would defeat the point of pinning.
33
140
  */
34
141
  export declare function connect(options: ConnectOptions): Promise<MCPConnection>;
35
142
  //# sourceMappingURL=mcp-connection.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"mcp-connection.d.ts","sourceRoot":"","sources":["../src/mcp-connection.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,MAAM,2CAA2C,CAAC;AAWnE,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,sBAAsB,CAAC;AAGpD,OAAO,KAAK,EAAE,kBAAkB,EAAE,gBAAgB,EAAE,MAAM,YAAY,CAAC;AAOvE,MAAM,WAAW,cAAc;IAC7B,GAAG,EAAE,GAAG,CAAC;IACT,SAAS,CAAC,EAAE,gBAAgB,CAAC;IAC7B,IAAI,CAAC,EAAE,OAAO,CAAC;IACf,KAAK,CAAC,EAAE,OAAO,KAAK,CAAC;IACrB,UAAU,CAAC,EAAE;QACX,IAAI,EAAE,MAAM,CAAC;QACb,OAAO,EAAE,MAAM,CAAC;KACjB,CAAC;IACF,SAAS,CAAC,EAAE,MAAM,CAAC;IACnB,aAAa,CAAC,EAAE,kBAAkB,CAAC;CACpC;AAED,MAAM,WAAW,aAAa;IAC5B,MAAM,EAAE,MAAM,CAAC;IACf,SAAS,EAAE,gBAAgB,CAAC;IAC5B,SAAS,CAAC,EAAE,MAAM,CAAC;IACnB;;;OAGG;IACH,yBAAyB,CAAC,OAAO,EAAE,MAAM,IAAI,GAAG,IAAI,CAAC;IACrD,KAAK,IAAI,OAAO,CAAC,I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