@openrouter/mcp 0.0.1

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 (82) hide show
  1. package/README.md +127 -0
  2. package/esm/auth/auth-resolver.d.ts +14 -0
  3. package/esm/auth/auth-resolver.d.ts.map +1 -0
  4. package/esm/auth/auth-resolver.js +27 -0
  5. package/esm/auth/auth-resolver.js.map +1 -0
  6. package/esm/auth/auth-types.d.ts +25 -0
  7. package/esm/auth/auth-types.d.ts.map +1 -0
  8. package/esm/auth/auth-types.js +4 -0
  9. package/esm/auth/auth-types.js.map +1 -0
  10. package/esm/build-tools.d.ts +27 -0
  11. package/esm/build-tools.d.ts.map +1 -0
  12. package/esm/build-tools.js +77 -0
  13. package/esm/build-tools.js.map +1 -0
  14. package/esm/cache/cache-store.d.ts +24 -0
  15. package/esm/cache/cache-store.d.ts.map +1 -0
  16. package/esm/cache/cache-store.js +20 -0
  17. package/esm/cache/cache-store.js.map +1 -0
  18. package/esm/cache/cache-types.d.ts +54 -0
  19. package/esm/cache/cache-types.d.ts.map +1 -0
  20. package/esm/cache/cache-types.js +41 -0
  21. package/esm/cache/cache-types.js.map +1 -0
  22. package/esm/cache/serialize.d.ts +25 -0
  23. package/esm/cache/serialize.d.ts.map +1 -0
  24. package/esm/cache/serialize.js +85 -0
  25. package/esm/cache/serialize.js.map +1 -0
  26. package/esm/create-mcp-tools.d.ts +13 -0
  27. package/esm/create-mcp-tools.d.ts.map +1 -0
  28. package/esm/create-mcp-tools.js +75 -0
  29. package/esm/create-mcp-tools.js.map +1 -0
  30. package/esm/elicitation.d.ts +11 -0
  31. package/esm/elicitation.d.ts.map +1 -0
  32. package/esm/elicitation.js +51 -0
  33. package/esm/elicitation.js.map +1 -0
  34. package/esm/errors.d.ts +35 -0
  35. package/esm/errors.d.ts.map +1 -0
  36. package/esm/errors.js +39 -0
  37. package/esm/errors.js.map +1 -0
  38. package/esm/handle.d.ts +41 -0
  39. package/esm/handle.d.ts.map +1 -0
  40. package/esm/handle.js +221 -0
  41. package/esm/handle.js.map +1 -0
  42. package/esm/index.d.ts +13 -0
  43. package/esm/index.d.ts.map +1 -0
  44. package/esm/index.js +11 -0
  45. package/esm/index.js.map +1 -0
  46. package/esm/mcp-connection.d.ts +35 -0
  47. package/esm/mcp-connection.d.ts.map +1 -0
  48. package/esm/mcp-connection.js +147 -0
  49. package/esm/mcp-connection.js.map +1 -0
  50. package/esm/rehydrate.d.ts +41 -0
  51. package/esm/rehydrate.d.ts.map +1 -0
  52. package/esm/rehydrate.js +173 -0
  53. package/esm/rehydrate.js.map +1 -0
  54. package/esm/resource-tools.d.ts +17 -0
  55. package/esm/resource-tools.d.ts.map +1 -0
  56. package/esm/resource-tools.js +133 -0
  57. package/esm/resource-tools.js.map +1 -0
  58. package/esm/result-mapper.d.ts +19 -0
  59. package/esm/result-mapper.d.ts.map +1 -0
  60. package/esm/result-mapper.js +40 -0
  61. package/esm/result-mapper.js.map +1 -0
  62. package/esm/schema/json-schema-guards.d.ts +14 -0
  63. package/esm/schema/json-schema-guards.d.ts.map +1 -0
  64. package/esm/schema/json-schema-guards.js +19 -0
  65. package/esm/schema/json-schema-guards.js.map +1 -0
  66. package/esm/schema/json-schema-to-zod.d.ts +20 -0
  67. package/esm/schema/json-schema-to-zod.d.ts.map +1 -0
  68. package/esm/schema/json-schema-to-zod.js +72 -0
  69. package/esm/schema/json-schema-to-zod.js.map +1 -0
  70. package/esm/tool-wrapper.d.ts +26 -0
  71. package/esm/tool-wrapper.d.ts.map +1 -0
  72. package/esm/tool-wrapper.js +118 -0
  73. package/esm/tool-wrapper.js.map +1 -0
  74. package/esm/transport-types.d.ts +3 -0
  75. package/esm/transport-types.d.ts.map +1 -0
  76. package/esm/transport-types.js +2 -0
  77. package/esm/transport-types.js.map +1 -0
  78. package/esm/types.d.ts +104 -0
  79. package/esm/types.d.ts.map +1 -0
  80. package/esm/types.js +2 -0
  81. package/esm/types.js.map +1 -0
  82. package/package.json +72 -0
@@ -0,0 +1,75 @@
1
+ import { defaultCacheKey } from './cache/cache-store.js';
2
+ import { isSerializedMCPServer } from './cache/cache-types.js';
3
+ import { freshConnect, normalizeUrl } from './handle.js';
4
+ import { rehydrateMCPTools } from './rehydrate.js';
5
+ /**
6
+ * Connect to a remote MCP server, discover its tools, and return a handle whose
7
+ * `.tools` can be passed straight into `callModel({ tools })`. Auth is supplied
8
+ * once and reused for discovery and every subsequent tool call.
9
+ *
10
+ * When `cache` is provided, a valid non-stale snapshot is rehydrated instead of
11
+ * re-listing; otherwise the fresh result is written back to the cache.
12
+ */
13
+ export async function createMCPTools(options) {
14
+ const url = normalizeUrl(options.url);
15
+ const cacheKey = options.cache?.key ?? defaultCacheKey(url.href);
16
+ if (options.cache !== undefined) {
17
+ const hit = await tryCacheHit(options, options.cache.store, cacheKey);
18
+ if (hit !== undefined) {
19
+ return hit;
20
+ }
21
+ }
22
+ return freshConnect(options, url, cacheKey);
23
+ }
24
+ // Option keys forwarded verbatim from a cache-hit `createMCPTools` call into
25
+ // `rehydrateMCPTools`, so a warm handle applies the same auth, filters, prefix,
26
+ // and credential-caching behavior as a cold one.
27
+ const FORWARDED_REHYDRATE_KEYS = [
28
+ 'auth',
29
+ 'fetch',
30
+ 'clientInfo',
31
+ 'onUnconvertibleSchema',
32
+ 'onElicitation',
33
+ 'signal',
34
+ 'toolNamePrefix',
35
+ 'includeTools',
36
+ 'excludeTools',
37
+ 'resources',
38
+ 'emitProgress',
39
+ 'autoRefreshOnListChanged',
40
+ 'cacheCredentials',
41
+ ];
42
+ /** Copy the defined forwarded options from `createMCPTools` into a rehydrate base. */
43
+ function forwardedRehydrateOptions(options) {
44
+ const out = {};
45
+ for (const key of FORWARDED_REHYDRATE_KEYS) {
46
+ const value = options[key];
47
+ if (value !== undefined) {
48
+ Object.assign(out, {
49
+ [key]: value,
50
+ });
51
+ }
52
+ }
53
+ return out;
54
+ }
55
+ async function tryCacheHit(options, store, cacheKey) {
56
+ const snapshot = await store.get(cacheKey);
57
+ if (snapshot === null || snapshot === undefined || !isSerializedMCPServer(snapshot)) {
58
+ return undefined;
59
+ }
60
+ const maxAge = options.staleness?.maxAgeMs;
61
+ if (maxAge !== undefined && Date.now() - snapshot.cachedAt > maxAge) {
62
+ return undefined;
63
+ }
64
+ // Defer to rehydrate, which reconnects and falls back to a fresh connect on
65
+ // expiry.
66
+ return rehydrateMCPTools({
67
+ snapshot,
68
+ ...forwardedRehydrateOptions(options),
69
+ cache: {
70
+ store,
71
+ key: cacheKey,
72
+ },
73
+ });
74
+ }
75
+ //# sourceMappingURL=create-mcp-tools.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"create-mcp-tools.js","sourceRoot":"","sources":["../src/create-mcp-tools.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,eAAe,EAAE,MAAM,wBAAwB,CAAC;AAEzD,OAAO,EAAE,qBAAqB,EAAE,MAAM,wBAAwB,CAAC;AAC/D,OAAO,EAAE,YAAY,EAAE,YAAY,EAAE,MAAM,aAAa,CAAC;AAEzD,OAAO,EAAE,iBAAiB,EAAE,MAAM,gBAAgB,CAAC;AAGnD;;;;;;;GAOG;AACH,MAAM,CAAC,KAAK,UAAU,cAAc,CAAC,OAA8B;IACjE,MAAM,GAAG,GAAG,YAAY,CAAC,OAAO,CAAC,GAAG,CAAC,CAAC;IACtC,MAAM,QAAQ,GAAG,OAAO,CAAC,KAAK,EAAE,GAAG,IAAI,eAAe,CAAC,GAAG,CAAC,IAAI,CAAC,CAAC;IAEjE,IAAI,OAAO,CAAC,KAAK,KAAK,SAAS,EAAE,CAAC;QAChC,MAAM,GAAG,GAAG,MAAM,WAAW,CAAC,OAAO,EAAE,OAAO,CAAC,KAAK,CAAC,KAAK,EAAE,QAAQ,CAAC,CAAC;QACtE,IAAI,GAAG,KAAK,SAAS,EAAE,CAAC;YACtB,OAAO,GAAG,CAAC;QACb,CAAC;IACH,CAAC;IAED,OAAO,YAAY,CAAC,OAAO,EAAE,GAAG,EAAE,Q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yBAAyB,CAAC,OAAO,CAAC;QACrC,KAAK,EAAE;YACL,KAAK;YACL,GAAG,EAAE,QAAQ;SACd;KACF,CAAC,CAAC;AACL,CAAC"}
@@ -0,0 +1,11 @@
1
+ import type { ElicitRequest, ElicitResult } from '@modelcontextprotocol/sdk/types.js';
2
+ import type { ElicitationHandler } from './types.js';
3
+ /**
4
+ * Build the `elicitation/create` request handler registered on the MCP client.
5
+ * Delegates to the caller's {@link ElicitationHandler}; when none is supplied,
6
+ * auto-declines so a tool call awaiting input fails gracefully instead of
7
+ * hanging. The MCP spec forbids eliciting sensitive/PII data — that contract is
8
+ * the server's responsibility; callers should treat requested fields as such.
9
+ */
10
+ export declare function makeElicitationRequestHandler(handler: ElicitationHandler | undefined): (request: ElicitRequest) => Promise<ElicitResult>;
11
+ //# sourceMappingURL=elicitation.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"elicitation.d.ts","sourceRoot":"","sources":["../src/elicitation.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,aAAa,EAAE,YAAY,EAAE,MAAM,oCAAoC,CAAC;AAEtF,OAAO,KAAK,EAAE,kBAAkB,EAAE,MAAM,YAAY,CAAC;AA4BrD;;;;;;GAMG;AACH,wBAAgB,6BAA6B,CAC3C,OAAO,EAAE,kBAAkB,GAAG,SAAS,GACtC,CAAC,OAAO,EAAE,aAAa,KAAK,OAAO,CAAC,YAAY,CAAC,CA2BnD"}
@@ -0,0 +1,51 @@
1
+ import { isJsonSchemaObject } from './schema/json-schema-guards.js';
2
+ /** A form-mode elicitation request carries a `requestedSchema`. */
3
+ function hasRequestedSchema(params) {
4
+ return 'requestedSchema' in params;
5
+ }
6
+ /** Narrow handler-returned content to the primitive record the spec allows. */
7
+ function toElicitContent(content) {
8
+ const out = {};
9
+ for (const [key, value] of Object.entries(content)) {
10
+ if (typeof value === 'string' ||
11
+ typeof value === 'number' ||
12
+ typeof value === 'boolean' ||
13
+ (Array.isArray(value) && value.every((v) => typeof v === 'string'))) {
14
+ out[key] = value;
15
+ }
16
+ }
17
+ return out;
18
+ }
19
+ /**
20
+ * Build the `elicitation/create` request handler registered on the MCP client.
21
+ * Delegates to the caller's {@link ElicitationHandler}; when none is supplied,
22
+ * auto-declines so a tool call awaiting input fails gracefully instead of
23
+ * hanging. The MCP spec forbids eliciting sensitive/PII data — that contract is
24
+ * the server's responsibility; callers should treat requested fields as such.
25
+ */
26
+ export function makeElicitationRequestHandler(handler) {
27
+ return async (request) => {
28
+ if (handler === undefined) {
29
+ return {
30
+ action: 'decline',
31
+ };
32
+ }
33
+ const { params } = request;
34
+ const requestedSchema = hasRequestedSchema(params) ? params.requestedSchema : undefined;
35
+ const schema = isJsonSchemaObject(requestedSchema) ? requestedSchema : {};
36
+ const response = await handler({
37
+ message: params.message,
38
+ requestedSchema: schema,
39
+ });
40
+ if (response.action === 'accept') {
41
+ return {
42
+ action: 'accept',
43
+ content: toElicitContent(response.content),
44
+ };
45
+ }
46
+ return {
47
+ action: response.action,
48
+ };
49
+ };
50
+ }
51
+ //# sourceMappingURL=elicitation.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"elicitation.js","sourceRoot":"","sources":["../src/elicitation.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,kBAAkB,EAAE,MAAM,gCAAgC,CAAC;AAMpE,mEAAmE;AACnE,SAAS,kBAAkB,CAAC,MAA+B;IAGzD,OAAO,iBAAiB,IAAI,MAAM,CAAC;AACrC,CAAC;AAED,+EAA+E;AAC/E,SAAS,eAAe,CAAC,OAAgC;IACvD,MAAM,GAAG,GAAuC,EAAE,CAAC;IACnD,KAAK,MAAM,CAAC,GAAG,EAAE,KAAK,CAAC,IAAI,MAAM,CAAC,OAAO,CAAC,OAAO,CAAC,EAAE,CAAC;QACnD,IACE,OAAO,KAAK,KAAK,QAAQ;YACzB,OAAO,KAAK,KAAK,QAAQ;YACzB,OAAO,KAAK,KAAK,SAAS;YAC1B,CAAC,KAAK,CAAC,OAAO,CAAC,KAAK,CAAC,IAAI,KAAK,CAAC,KAAK,CAAC,CAAC,CAAC,EAAe,EAAE,CAAC,OAAO,CAAC,KAAK,QAAQ,CAAC,CAAC,EAChF,CAAC;YACD,GAAG,CAAC,GAAG,CAAC,GAAG,KAAK,CAAC;QACnB,CAAC;IACH,CAAC;IACD,OAAO,GAAG,CAAC;AACb,CAAC;AAED;;;;;;GAMG;AACH,MAAM,UAAU,6BAA6B,CAC3C,OAAuC;IAEvC,OAAO,KAAK,EAAE,OAAsB,EAAyB,EAAE;QAC7D,IAAI,OAAO,KAAK,SAAS,EAAE,CAAC;YAC1B,OAAO;gBACL,MAAM,EAAE,SAAS;aAClB,CAAC;QACJ,CAAC;QAED,MAAM,EAAE,MAAM,EAAE,GAAG,OAAO,CAAC;QAC3B,MAAM,eAAe,GAAG,kBAAkB,CAAC,MAAM,CAAC,CAAC,CAAC,CAAC,MAAM,CAAC,eAAe,CAAC,CAAC,CAAC,SAAS,CAAC;QACxF,MAAM,MAAM,GAAG,kBAAkB,CAAC,eAAe,CAAC,CAAC,CAAC,CAAC,eAAe,CAAC,CAAC,CAAC,EAAE,CAAC;QAE1E,MAAM,QAAQ,GAAG,MAAM,OAAO,CAAC;YAC7B,OAAO,EAAE,MAAM,CAAC,OAAO;YACvB,eAAe,EAAE,MAAM;SACxB,CAAC,CAAC;QAEH,IAAI,QAAQ,CAAC,MAAM,KAAK,QAAQ,EAAE,CAAC;YACjC,OAAO;gBACL,MAAM,EAAE,QAAQ;gBAChB,OAAO,EAAE,eAAe,CAAC,QAAQ,CAAC,OAAO,CAAC;aAC3C,CAAC;QACJ,CAAC;QACD,OAAO;YACL,MAAM,EAAE,QAAQ,CAAC,MAAM;SACxB,CAAC;IACJ,CAAC,CAAC;AACJ,CAAC"}
@@ -0,0 +1,35 @@
1
+ /**
2
+ * Base error for all @openrouter/mcp failures.
3
+ */
4
+ export declare class MCPError extends Error {
5
+ constructor(message: string, options?: {
6
+ cause?: unknown;
7
+ });
8
+ }
9
+ /**
10
+ * Raised when an MCP tool call returns `isError: true` or when the result
11
+ * cannot be mapped to a usable model output.
12
+ */
13
+ export declare class MCPToolCallError extends MCPError {
14
+ readonly toolName: string;
15
+ constructor(toolName: string, message: string, options?: {
16
+ cause?: unknown;
17
+ });
18
+ }
19
+ /**
20
+ * Raised when a cached snapshot cannot be validated or rehydrated.
21
+ */
22
+ export declare class MCPCacheError extends MCPError {
23
+ constructor(message: string, options?: {
24
+ cause?: unknown;
25
+ });
26
+ }
27
+ /**
28
+ * Raised when connecting to the MCP server fails across all attempted transports.
29
+ */
30
+ export declare class MCPConnectionError extends MCPError {
31
+ constructor(message: string, options?: {
32
+ cause?: unknown;
33
+ });
34
+ }
35
+ //# sourceMappingURL=errors.d.ts.map
@@ -0,0 +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,OAAO,CAAC;KACjB;CAMJ;AAED;;GAEG;AACH,qBAAa,aAAc,SAAQ,QAAQ;gBAEvC,OAAO,EAAE,MAAM,EACf,OAAO,CAAC,EAAE;QACR,KAAK,CAAC,EAAE,OAAO,CAAC;KACjB;CAKJ;AAED;;GAEG;AACH,qBAAa,kBAAmB,SAAQ,QAAQ;gBAE5C,OAAO,EAAE,MAAM,EACf,OAAO,CAAC,EAAE;QACR,KAAK,CAAC,EAAE,OAAO,CAAC;KACjB;CAKJ"}
package/esm/errors.js ADDED
@@ -0,0 +1,39 @@
1
+ /**
2
+ * Base error for all @openrouter/mcp failures.
3
+ */
4
+ export class MCPError extends Error {
5
+ constructor(message, options) {
6
+ super(message, options);
7
+ this.name = 'MCPError';
8
+ }
9
+ }
10
+ /**
11
+ * Raised when an MCP tool call returns `isError: true` or when the result
12
+ * cannot be mapped to a usable model output.
13
+ */
14
+ export class MCPToolCallError extends MCPError {
15
+ constructor(toolName, message, options) {
16
+ super(message, options);
17
+ this.name = 'MCPToolCallError';
18
+ this.toolName = toolName;
19
+ }
20
+ }
21
+ /**
22
+ * Raised when a cached snapshot cannot be validated or rehydrated.
23
+ */
24
+ export class MCPCacheError extends MCPError {
25
+ constructor(message, options) {
26
+ super(message, options);
27
+ this.name = 'MCPCacheError';
28
+ }
29
+ }
30
+ /**
31
+ * Raised when connecting to the MCP server fails across all attempted transports.
32
+ */
33
+ export class MCPConnectionError extends MCPError {
34
+ constructor(message, options) {
35
+ super(message, options);
36
+ this.name = 'MCPConnectionError';
37
+ }
38
+ }
39
+ //# sourceMappingURL=errors.js.map
@@ -0,0 +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"}
@@ -0,0 +1,41 @@
1
+ import type { MCPConnection } from './mcp-connection.js';
2
+ import type { McpToolDef } from './tool-wrapper.js';
3
+ import type { MCPTransportKind } from './transport-types.js';
4
+ import type { CreateMCPToolsOptions, MCPToolsHandle } from './types.js';
5
+ export declare function normalizeUrl(url: string | URL): URL;
6
+ /**
7
+ * Read the discovered tools off the live connection into our internal shape.
8
+ *
9
+ * `tools/list` is paginated: each response may carry a `nextCursor` that must be
10
+ * passed back as `{ cursor }` to fetch the next page. We accumulate every page so
11
+ * servers that paginate their tool list aren't silently truncated.
12
+ */
13
+ export declare function listToolDefs(connection: MCPConnection, signal: AbortSignal | undefined): Promise<McpToolDef[]>;
14
+ interface HandleContext {
15
+ url: URL;
16
+ transport: MCPTransportKind;
17
+ cacheKey: string;
18
+ }
19
+ export interface MakeHandleArgs {
20
+ connection: MCPConnection;
21
+ options: CreateMCPToolsOptions;
22
+ context: HandleContext;
23
+ initialToolDefs: McpToolDef[];
24
+ }
25
+ /**
26
+ * Connect, discover tools via `listTools()`, and build a handle WITHOUT
27
+ * consulting the cache for a hit. The cache (when present on `options`) is still
28
+ * written through `makeHandle`, so refreshes persist. This is the cold cache-miss
29
+ * path, and is also called directly by `rehydrateMCPTools`'s fallback: routing
30
+ * the fallback here instead of back through `createMCPTools` is what prevents the
31
+ * cache-fallback loop — re-reading the same snapshot would re-enter rehydrate and
32
+ * recurse without bound.
33
+ */
34
+ export declare function freshConnect(options: CreateMCPToolsOptions, url: URL, cacheKey: string): Promise<MCPToolsHandle>;
35
+ /**
36
+ * Construct an {@link MCPToolsHandle} around a live connection, wiring refresh,
37
+ * serialize, list_changed listeners, and cache writes.
38
+ */
39
+ export declare function makeHandle(args: MakeHandleArgs): Promise<MCPToolsHandle>;
40
+ export {};
41
+ //# sourceMappingURL=handle.d.ts.map
@@ -0,0 +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"}
package/esm/handle.js ADDED
@@ -0,0 +1,221 @@
1
+ import { buildTools } from './build-tools.js';
2
+ import { serializeServer } from './cache/serialize.js';
3
+ import { connect } from './mcp-connection.js';
4
+ export function normalizeUrl(url) {
5
+ return url instanceof URL ? url : new URL(url);
6
+ }
7
+ // Hard cap on pagination pages. A well-behaved server terminates the cursor
8
+ // chain by omitting `nextCursor`; this bounds a misbehaving one that never does.
9
+ const MAX_LIST_PAGES = 1000;
10
+ /**
11
+ * Normalize a paginated `nextCursor` field: treat anything that is not a
12
+ * non-empty string as "no more pages" so a malformed cursor terminates the loop.
13
+ */
14
+ function nextCursorOrUndefined(value) {
15
+ return typeof value === 'string' && value.length > 0 ? value : undefined;
16
+ }
17
+ /**
18
+ * Read the discovered tools off the live connection into our internal shape.
19
+ *
20
+ * `tools/list` is paginated: each response may carry a `nextCursor` that must be
21
+ * passed back as `{ cursor }` to fetch the next page. We accumulate every page so
22
+ * servers that paginate their tool list aren't silently truncated.
23
+ */
24
+ export async function listToolDefs(connection, signal) {
25
+ const requestOptions = signal !== undefined
26
+ ? {
27
+ signal,
28
+ }
29
+ : undefined;
30
+ const collected = [];
31
+ let cursor;
32
+ for (let page = 0; page < MAX_LIST_PAGES; page += 1) {
33
+ const params = cursor !== undefined
34
+ ? {
35
+ cursor,
36
+ }
37
+ : undefined;
38
+ const { tools, nextCursor } = await connection.client.listTools(params, requestOptions);
39
+ for (const t of tools) {
40
+ collected.push({
41
+ name: t.name,
42
+ ...(t.description !== undefined && {
43
+ description: t.description,
44
+ }),
45
+ inputSchema: t.inputSchema,
46
+ ...(t.outputSchema !== undefined && {
47
+ outputSchema: t.outputSchema,
48
+ }),
49
+ });
50
+ }
51
+ const next = nextCursorOrUndefined(nextCursor);
52
+ // Stop at the end of the chain, or if the server echoes the same cursor
53
+ // (which would otherwise spin forever).
54
+ if (next === undefined || next === cursor) {
55
+ break;
56
+ }
57
+ cursor = next;
58
+ }
59
+ return collected;
60
+ }
61
+ function serverHasResources(connection) {
62
+ const caps = connection.client.getServerCapabilities();
63
+ return caps?.resources !== undefined;
64
+ }
65
+ /**
66
+ * Connect, discover tools via `listTools()`, and build a handle WITHOUT
67
+ * consulting the cache for a hit. The cache (when present on `options`) is still
68
+ * written through `makeHandle`, so refreshes persist. This is the cold cache-miss
69
+ * path, and is also called directly by `rehydrateMCPTools`'s fallback: routing
70
+ * the fallback here instead of back through `createMCPTools` is what prevents the
71
+ * cache-fallback loop — re-reading the same snapshot would re-enter rehydrate and
72
+ * recurse without bound.
73
+ */
74
+ export async function freshConnect(options, url, cacheKey) {
75
+ const connection = await connect({
76
+ url,
77
+ ...(options.transport !== undefined && {
78
+ transport: options.transport,
79
+ }),
80
+ ...(options.auth !== undefined && {
81
+ auth: options.auth,
82
+ }),
83
+ ...(options.fetch !== undefined && {
84
+ fetch: options.fetch,
85
+ }),
86
+ ...(options.clientInfo !== undefined && {
87
+ clientInfo: options.clientInfo,
88
+ }),
89
+ ...(options.onElicitation !== undefined && {
90
+ onElicitation: options.onElicitation,
91
+ }),
92
+ });
93
+ // Tear the connection down if discovery or the initial cache write throws —
94
+ // otherwise the open transport (HTTP keep-alive / SSE stream) leaks.
95
+ try {
96
+ const initialToolDefs = await listToolDefs(connection, options.signal);
97
+ return await makeHandle({
98
+ connection,
99
+ options,
100
+ context: {
101
+ url,
102
+ transport: connection.transport,
103
+ cacheKey,
104
+ },
105
+ initialToolDefs,
106
+ });
107
+ }
108
+ catch (err) {
109
+ await connection.close().catch(() => { });
110
+ throw err;
111
+ }
112
+ }
113
+ /**
114
+ * Construct an {@link MCPToolsHandle} around a live connection, wiring refresh,
115
+ * serialize, list_changed listeners, and cache writes.
116
+ */
117
+ export async function makeHandle(args) {
118
+ const { connection, options, context, initialToolDefs } = args;
119
+ const listeners = new Set();
120
+ let toolDefs = initialToolDefs;
121
+ const serverInfo = connection.client.getServerVersion();
122
+ const rebuild = () => buildTools(buildToolsArgs(connection, toolDefs, options));
123
+ let tools = rebuild();
124
+ const snapshot = () => serializeServer(serializeArgs({
125
+ connection,
126
+ context,
127
+ toolDefs,
128
+ serverInfo,
129
+ options,
130
+ }));
131
+ const writeCache = async () => {
132
+ const store = options.cache?.store;
133
+ if (store === undefined) {
134
+ return;
135
+ }
136
+ await store.set(context.cacheKey, await snapshot());
137
+ };
138
+ const refresh = async () => {
139
+ toolDefs = await listToolDefs(connection, options.signal);
140
+ tools = rebuild();
141
+ await writeCache();
142
+ return tools;
143
+ };
144
+ if (options.autoRefreshOnListChanged ?? true) {
145
+ connection.setToolListChangedHandler(() => {
146
+ // Fire-and-forget, but never let a failed refresh escape as an unhandled
147
+ // rejection. On failure listeners keep the last good tool set.
148
+ void refresh()
149
+ .then((next) => {
150
+ for (const listener of listeners) {
151
+ listener(next);
152
+ }
153
+ })
154
+ .catch(() => { });
155
+ });
156
+ }
157
+ await writeCache();
158
+ return {
159
+ get tools() {
160
+ return tools;
161
+ },
162
+ ...(serverInfo !== undefined && {
163
+ serverInfo,
164
+ }),
165
+ serialize: snapshot,
166
+ refresh,
167
+ onToolsChanged: (listener) => {
168
+ listeners.add(listener);
169
+ return () => listeners.delete(listener);
170
+ },
171
+ close: () => connection.close(),
172
+ };
173
+ }
174
+ /** Assemble the {@link buildTools} arguments, threading only the defined options. */
175
+ function buildToolsArgs(connection, toolDefs, options) {
176
+ return {
177
+ client: connection.client,
178
+ toolDefs,
179
+ emitProgress: options.emitProgress ?? true,
180
+ serverHasResources: serverHasResources(connection),
181
+ ...(options.toolNamePrefix !== undefined && {
182
+ namePrefix: options.toolNamePrefix,
183
+ }),
184
+ ...(options.includeTools !== undefined && {
185
+ includeTools: options.includeTools,
186
+ }),
187
+ ...(options.excludeTools !== undefined && {
188
+ excludeTools: options.excludeTools,
189
+ }),
190
+ ...(options.onUnconvertibleSchema !== undefined && {
191
+ schemaMode: options.onUnconvertibleSchema,
192
+ }),
193
+ ...(options.signal !== undefined && {
194
+ signal: options.signal,
195
+ }),
196
+ ...(options.resources !== undefined && {
197
+ resources: options.resources,
198
+ }),
199
+ };
200
+ }
201
+ /** Assemble the {@link serializeServer} input, threading only the defined fields. */
202
+ function serializeArgs(args) {
203
+ const { connection, context, toolDefs, serverInfo, options } = args;
204
+ return {
205
+ url: context.url.href,
206
+ transport: connection.transport,
207
+ toolDefs,
208
+ cacheCredentials: options.cacheCredentials ?? false,
209
+ cachedAt: Date.now(),
210
+ ...(serverInfo !== undefined && {
211
+ serverInfo,
212
+ }),
213
+ ...(connection.sessionId !== undefined && {
214
+ sessionId: connection.sessionId,
215
+ }),
216
+ ...(options.auth !== undefined && {
217
+ auth: options.auth,
218
+ }),
219
+ };
220
+ }
221
+ //# sourceMappingURL=handle.js.map
@@ -0,0 +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;SAC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AAK,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,SAAS,KAAK,SAAS,IAAI;YACrC,SAAS,EAAE,OAAO,CAAC,SAAS;SAC7B,CAAC;KACH,CAAC;AACJ,CAAC;AAeD,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,GAAG,EAAE;QACpB,GAAG,CAAC,UAAU,KAAK,SAAS,IAAI;YAC9B,UAAU;SACX,CAAC;QACF,GAAG,CAAC,UAAU,CAAC,SAAS,KAAK,SAAS,IAAI;YACxC,SAAS,EAAE,UAAU,CAAC,SAAS;SAChC,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 ADDED
@@ -0,0 +1,13 @@
1
+ export type { MCPAuth } from './auth/auth-types.js';
2
+ export type { MCPCacheStore } from './cache/cache-store.js';
3
+ export { defaultCacheKey, InMemoryMCPCacheStore } from './cache/cache-store.js';
4
+ export type { SerializedMCPServer, SerializedMCPToolDef, SerializedTokenSet, } from './cache/cache-types.js';
5
+ export { isSerializedMCPServer } from './cache/cache-types.js';
6
+ export { createMCPTools } from './create-mcp-tools.js';
7
+ export { MCPCacheError, MCPConnectionError, MCPError, MCPToolCallError, } from './errors.js';
8
+ export type { RehydrateMCPToolsOptions } from './rehydrate.js';
9
+ export { rehydrateMCPTools } from './rehydrate.js';
10
+ export type { UnconvertibleSchemaMode } from './schema/json-schema-to-zod.js';
11
+ export { convertMcpInputSchema } from './schema/json-schema-to-zod.js';
12
+ export type { CreateMCPToolsOptions, ElicitationHandler, ElicitationResponse, MCPToolsHandle, MCPTransportKind, ResourcesOption, } from './types.js';
13
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +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"}
package/esm/index.js ADDED
@@ -0,0 +1,11 @@
1
+ // Main factory + rehydration
2
+ // Cache
3
+ export { defaultCacheKey, InMemoryMCPCacheStore } from './cache/cache-store.js';
4
+ export { isSerializedMCPServer } from './cache/cache-types.js';
5
+ export { createMCPTools } from './create-mcp-tools.js';
6
+ // Errors
7
+ export { MCPCacheError, MCPConnectionError, MCPError, MCPToolCallError, } from './errors.js';
8
+ export { rehydrateMCPTools } from './rehydrate.js';
9
+ // Schema conversion (exported for testing/reuse)
10
+ export { convertMcpInputSchema } from './schema/json-schema-to-zod.js';
11
+ //# sourceMappingURL=index.js.map
@@ -0,0 +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"}
@@ -0,0 +1,35 @@
1
+ import { Client } from '@modelcontextprotocol/sdk/client/index.js';
2
+ import type { MCPAuth } from './auth/auth-types.js';
3
+ import type { ElicitationHandler, MCPTransportKind } from './types.js';
4
+ export interface ConnectOptions {
5
+ url: URL;
6
+ transport?: MCPTransportKind;
7
+ auth?: MCPAuth;
8
+ fetch?: typeof fetch;
9
+ clientInfo?: {
10
+ name: string;
11
+ version: string;
12
+ };
13
+ sessionId?: string;
14
+ onElicitation?: ElicitationHandler;
15
+ }
16
+ export interface MCPConnection {
17
+ client: Client;
18
+ transport: MCPTransportKind;
19
+ sessionId?: string;
20
+ /**
21
+ * Register a callback for `tools/list_changed`. Settable after connect so the
22
+ * handle can wire it to its own `refresh()`. Replaces any prior handler.
23
+ */
24
+ setToolListChangedHandler(handler: () => void): void;
25
+ close(): Promise<void>;
26
+ }
27
+ /**
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.
33
+ */
34
+ export declare function connect(options: ConnectOptions): Promise<MCPConnection>;
35
+ //# sourceMappingURL=mcp-connection.d.ts.map
@@ -0,0 +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,IAAI,CAAC,CAAC;CACxB;AAmFD;;;;;;GAMG;AACH,wBAAsB,OAAO,CAAC,OAAO,EAAE,cAAc,GAAG,OAAO,CAAC,aAAa,CAAC,CAkD7E"}