@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 @@
1
+ {"version":3,"file":"json-schema-guards.js","sourceRoot":"","sources":["../../src/schema/json-schema-guards.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAEH,6EAA6E;AAC7E,MAAM,UAAU,kBAAkB,CAAC,KAAc;IAC/C,OAAO,OAAO,KAAK,KAAK,QAAQ,IAAI,KAAK,KAAK,IAAI,IAAI,CAAC,KAAK,CAAC,OAAO,CAAC,KAAK,CAAC,CAAC;AAC9E,CAAC;AAED,0DAA0D;AAC1D,MAAM,UAAU,kBAAkB,CAAC,KAAc;IAG/C,OAAO,kBAAkB,CAAC,KAAK,CAAC,IAAI,KAAK,CAAC,MAAM,CAAC,KAAK,QAAQ,CAAC;AACjE,CAAC;AAED,8EAA8E;AAC9E,MAAM,UAAU,mBAAmB,CACjC,MAA+B;IAE/B,MAAM,EAAE,UAAU,EAAE,GAAG,MAAM,CAAC;IAC9B,OAAO,kBAAkB,CAAC,UAAU,CAAC,CAAC,CAAC,CAAC,UAAU,CAAC,CAAC,CAAC,SAAS,CAAC;AACjE,CAAC"}
@@ -0,0 +1,20 @@
1
+ import type { $ZodObject } from 'zod/v4/core';
2
+ /**
3
+ * How to handle a JSON Schema (sub)schema that Zod cannot represent.
4
+ * - `looseLeaf` (default): substitute `z.unknown()` for only the offending
5
+ * property so the rest of the tool's parameters stay faithful.
6
+ * - `throw`: surface the conversion failure to the caller.
7
+ */
8
+ export type UnconvertibleSchemaMode = 'looseLeaf' | 'throw';
9
+ /**
10
+ * Convert an MCP tool's JSON Schema (input or output) into a Zod v4 object
11
+ * schema. `callModel` derives the model-facing parameters from this Zod schema,
12
+ * so the conversion must be faithful — a permissive passthrough would make the
13
+ * model call the tool blind.
14
+ *
15
+ * Strategy: attempt a holistic `z.fromJSONSchema` conversion first (handles
16
+ * `$ref`/`$defs`, `anyOf`, formats, nesting); on failure, fall back per the
17
+ * chosen `mode`.
18
+ */
19
+ export declare function convertMcpInputSchema(jsonSchema: Record<string, unknown>, mode?: UnconvertibleSchemaMode): $ZodObject;
20
+ //# sourceMappingURL=json-schema-to-zod.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"json-schema-to-zod.d.ts","sourceRoot":"","sources":["../../src/schema/json-schema-to-zod.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,UAAU,EAAY,MAAM,aAAa,CAAC;AAIxD;;;;;GAKG;AACH,MAAM,MAAM,uBAAuB,GAAG,WAAW,GAAG,OAAO,CAAC;AA+C5D;;;;;;;;;GASG;AACH,wBAAgB,qBAAqB,CACnC,UAAU,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,EACnC,IAAI,GAAE,uBAAqC,GAC1C,UAAU,CAkBZ"}
@@ -0,0 +1,72 @@
1
+ import * as z from 'zod';
2
+ import { MCPError } from '../errors.js';
3
+ import { getSchemaProperties, isJsonSchemaObject } from './json-schema-guards.js';
4
+ /** Narrow a Zod schema to an object schema via its runtime type tag (no `as`). */
5
+ function isZodObject(schema) {
6
+ return schema._zod.def.type === 'object';
7
+ }
8
+ /** The `required` array of an object schema, filtered to string keys. */
9
+ function requiredKeys(schema) {
10
+ const { required } = schema;
11
+ if (!Array.isArray(required)) {
12
+ return new Set();
13
+ }
14
+ return new Set(required.filter((key) => typeof key === 'string'));
15
+ }
16
+ /**
17
+ * Build a Zod object by converting each property independently, substituting
18
+ * `z.unknown()` for any property Zod cannot represent. Always yields a
19
+ * `$ZodObject`, which is what `callModel`'s `tool()` factory requires.
20
+ */
21
+ function buildObjectFromProperties(jsonSchema, mode) {
22
+ const properties = getSchemaProperties(jsonSchema) ?? {};
23
+ const required = requiredKeys(jsonSchema);
24
+ const shape = {};
25
+ for (const [key, propSchema] of Object.entries(properties)) {
26
+ let zodProp;
27
+ try {
28
+ zodProp = isJsonSchemaObject(propSchema) ? z.fromJSONSchema(propSchema) : z.unknown();
29
+ }
30
+ catch (err) {
31
+ if (mode === 'throw') {
32
+ throw new MCPError(`Cannot convert JSON Schema for property "${key}" to Zod`, {
33
+ cause: err,
34
+ });
35
+ }
36
+ zodProp = z.unknown();
37
+ }
38
+ shape[key] = required.has(key) ? zodProp : z.optional(zodProp);
39
+ }
40
+ return z.object(shape);
41
+ }
42
+ /**
43
+ * Convert an MCP tool's JSON Schema (input or output) into a Zod v4 object
44
+ * schema. `callModel` derives the model-facing parameters from this Zod schema,
45
+ * so the conversion must be faithful — a permissive passthrough would make the
46
+ * model call the tool blind.
47
+ *
48
+ * Strategy: attempt a holistic `z.fromJSONSchema` conversion first (handles
49
+ * `$ref`/`$defs`, `anyOf`, formats, nesting); on failure, fall back per the
50
+ * chosen `mode`.
51
+ */
52
+ export function convertMcpInputSchema(jsonSchema, mode = 'looseLeaf') {
53
+ try {
54
+ const converted = z.fromJSONSchema(jsonSchema);
55
+ if (isZodObject(converted)) {
56
+ return converted;
57
+ }
58
+ // MCP guarantees object-typed tool schemas; if a server sends something
59
+ // else, degrade to a per-property object rather than handing callModel a
60
+ // non-object schema it cannot use.
61
+ return buildObjectFromProperties(jsonSchema, mode);
62
+ }
63
+ catch (err) {
64
+ if (mode === 'throw') {
65
+ throw new MCPError('Cannot convert MCP JSON Schema to Zod', {
66
+ cause: err,
67
+ });
68
+ }
69
+ return buildObjectFromProperties(jsonSchema, mode);
70
+ }
71
+ }
72
+ //# sourceMappingURL=json-schema-to-zod.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"json-schema-to-zod.js","sourceRoot":"","sources":["../../src/schema/json-schema-to-zod.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,CAAC,MAAM,KAAK,CAAC;AAEzB,OAAO,EAAE,QAAQ,EAAE,MAAM,cAAc,CAAC;AACxC,OAAO,EAAE,mBAAmB,EAAE,kBAAkB,EAAE,MAAM,yBAAyB,CAAC;AAUlF,kFAAkF;AAClF,SAAS,WAAW,CAAC,MAAgB;IACnC,OAAO,MAAM,CAAC,IAAI,CAAC,GAAG,CAAC,IAAI,KAAK,QAAQ,CAAC;AAC3C,CAAC;AAED,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AC,CAAC,CAAC,QAAQ,CAAC,OAAO,CAAC,CAAC;IACjE,CAAC;IAED,OAAO,CAAC,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC;AACzB,CAAC;AAED;;;;;;;;;GASG;AACH,MAAM,UAAU,qBAAqB,CACnC,UAAmC,EACnC,OAAgC,WAAW;IAE3C,IAAI,CAAC;QACH,MAAM,SAAS,GAAG,CAAC,CAAC,cAAc,CAAC,UAAU,CAAC,CAAC;QAC/C,IAAI,WAAW,CAAC,SAAS,CAAC,EAAE,CAAC;YAC3B,OAAO,SAAS,CAAC;QACnB,CAAC;QACD,wEAAwE;QACxE,yEAAyE;QACzE,mCAAmC;QACnC,OAAO,yBAAyB,CAAC,UAAU,EAAE,IAAI,CAAC,CAAC;IACrD,CAAC;IAAC,OAAO,GAAG,EAAE,CAAC;QACb,IAAI,IAAI,KAAK,OAAO,EAAE,CAAC;YACrB,MAAM,IAAI,QAAQ,CAAC,uCAAuC,EAAE;gBAC1D,KAAK,EAAE,GAAG;aACX,CAAC,CAAC;QACL,CAAC;QACD,OAAO,yBAAyB,CAAC,UAAU,EAAE,IAAI,CAAC,CAAC;IACrD,CAAC;AACH,CAAC"}
@@ -0,0 +1,26 @@
1
+ import type { Client } from '@modelcontextprotocol/sdk/client/index.js';
2
+ import type { McpBranded } from '@openrouter/agent/tool-types';
3
+ import type { UnconvertibleSchemaMode } from './schema/json-schema-to-zod.js';
4
+ /** A discovered MCP tool definition (the fields we consume). */
5
+ export interface McpToolDef {
6
+ name: string;
7
+ description?: string;
8
+ inputSchema: Record<string, unknown>;
9
+ outputSchema?: Record<string, unknown>;
10
+ }
11
+ export interface WrapToolOptions {
12
+ client: Client;
13
+ namePrefix?: string;
14
+ schemaMode?: UnconvertibleSchemaMode;
15
+ emitProgress?: boolean;
16
+ signal?: AbortSignal;
17
+ }
18
+ /**
19
+ * Wrap one discovered MCP tool as an `@openrouter/agent` tool. Emits a
20
+ * generator tool (streaming progress events) when `emitProgress` is on,
21
+ * otherwise a regular tool. The input schema (and output schema, when declared)
22
+ * are converted to Zod so the model sees faithful parameters. The abort signal,
23
+ * if supplied, is threaded into the underlying `callTool`.
24
+ */
25
+ export declare function wrapMcpTool(def: McpToolDef, options: WrapToolOptions): McpBranded;
26
+ //# sourceMappingURL=tool-wrapper.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"tool-wrapper.d.ts","sourceRoot":"","sources":["../src/tool-wrapper.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,2CAA2C,CAAC;AAGxE,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,8BAA8B,CAAC;AAK/D,OAAO,KAAK,EAAE,uBAAuB,EAAE,MAAM,gCAAgC,CAAC;AAG9E,gEAAgE;AAChE,MAAM,WAAW,UAAU;IACzB,IAAI,EAAE,MAAM,CAAC;IACb,WAAW,CAAC,EAAE,MAAM,CAAC;IACrB,WAAW,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC;IACrC,YAAY,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC;CACxC;AAED,MAAM,WAAW,eAAe;IAC9B,MAAM,EAAE,MAAM,CAAC;IACf,UAAU,CAAC,EAAE,MAAM,CAAC;IACpB,UAAU,CAAC,EAAE,uBAAuB,CAAC;IACrC,YAAY,CAAC,EAAE,OAAO,CAAC;IACvB,MAAM,CAAC,EAAE,WAAW,CAAC;CACtB;AA4CD;;;;;;GAMG;AACH,wBAAgB,WAAW,CAAC,GAAG,EAAE,UAAU,EAAE,OAAO,EAAE,eAAe,GAAG,UAAU,CAuFjF"}
@@ -0,0 +1,118 @@
1
+ import { markMcp, tool } from '@openrouter/agent/tool';
2
+ import * as z from 'zod';
3
+ import { mapCallToolResult } from './result-mapper.js';
4
+ import { isJsonSchemaObject } from './schema/json-schema-guards.js';
5
+ import { convertMcpInputSchema } from './schema/json-schema-to-zod.js';
6
+ /** Event yielded by a progress-emitting generator tool. */
7
+ const progressEventSchema = z.object({
8
+ type: z.literal('progress'),
9
+ progress: z.number(),
10
+ total: z.optional(z.number()),
11
+ message: z.optional(z.string()),
12
+ });
13
+ function callToolResultIsRaw(value) {
14
+ return isJsonSchemaObject(value);
15
+ }
16
+ async function invokeTool(invokeArgs) {
17
+ const { options, mcpName, args, onprogress } = invokeArgs;
18
+ const result = await options.client.callTool({
19
+ name: mcpName,
20
+ arguments: args,
21
+ }, undefined, {
22
+ ...(options.signal !== undefined && {
23
+ signal: options.signal,
24
+ }),
25
+ ...(onprogress !== undefined && {
26
+ onprogress,
27
+ }),
28
+ });
29
+ if (!callToolResultIsRaw(result)) {
30
+ return '';
31
+ }
32
+ return mapCallToolResult(mcpName, result);
33
+ }
34
+ /**
35
+ * Wrap one discovered MCP tool as an `@openrouter/agent` tool. Emits a
36
+ * generator tool (streaming progress events) when `emitProgress` is on,
37
+ * otherwise a regular tool. The input schema (and output schema, when declared)
38
+ * are converted to Zod so the model sees faithful parameters. The abort signal,
39
+ * if supplied, is threaded into the underlying `callTool`.
40
+ */
41
+ export function wrapMcpTool(def, options) {
42
+ const name = `${options.namePrefix ?? ''}${def.name}`;
43
+ const inputSchema = convertMcpInputSchema(def.inputSchema, options.schemaMode);
44
+ const outputSchema = def.outputSchema !== undefined
45
+ ? convertMcpInputSchema(def.outputSchema, options.schemaMode)
46
+ : undefined;
47
+ if (options.emitProgress === true) {
48
+ return markMcp(tool({
49
+ name,
50
+ ...(def.description !== undefined && {
51
+ description: def.description,
52
+ }),
53
+ inputSchema,
54
+ eventSchema: progressEventSchema,
55
+ outputSchema: outputSchema ?? z.unknown(),
56
+ execute: async function* (args) {
57
+ const queue = [];
58
+ let notify;
59
+ const onprogress = (p) => {
60
+ queue.push({
61
+ type: 'progress',
62
+ progress: p.progress,
63
+ ...(p.total !== undefined && {
64
+ total: p.total,
65
+ }),
66
+ ...(typeof p.message === 'string' && {
67
+ message: p.message,
68
+ }),
69
+ });
70
+ notify?.();
71
+ };
72
+ const resultPromise = invokeTool({
73
+ options,
74
+ mcpName: def.name,
75
+ args,
76
+ onprogress,
77
+ });
78
+ let done = false;
79
+ const finalize = resultPromise.finally(() => {
80
+ done = true;
81
+ notify?.();
82
+ });
83
+ while (!done || queue.length > 0) {
84
+ while (queue.length > 0) {
85
+ const event = queue.shift();
86
+ if (event !== undefined) {
87
+ yield event;
88
+ }
89
+ }
90
+ if (done) {
91
+ break;
92
+ }
93
+ await new Promise((resolve) => {
94
+ notify = resolve;
95
+ });
96
+ notify = undefined;
97
+ }
98
+ return await finalize;
99
+ },
100
+ }));
101
+ }
102
+ return markMcp(tool({
103
+ name,
104
+ ...(def.description !== undefined && {
105
+ description: def.description,
106
+ }),
107
+ inputSchema,
108
+ ...(outputSchema !== undefined && {
109
+ outputSchema,
110
+ }),
111
+ execute: (args) => invokeTool({
112
+ options,
113
+ mcpName: def.name,
114
+ args,
115
+ }),
116
+ }));
117
+ }
118
+ //# sourceMappingURL=tool-wrapper.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"tool-wrapper.js","sourceRoot":"","sources":["../src/tool-wrapper.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,OAAO,EAAE,IAAI,EAAE,MAAM,wBAAwB,CAAC;AAEvD,OAAO,KAAK,CAAC,MAAM,KAAK,CAAC;AAEzB,OAAO,EAAE,iBAAiB,EAAE,MAAM,oBAAoB,CAAC;AACvD,OAAO,EAAE,kBAAkB,EAAE,MAAM,gCAAgC,CAAC;AAEpE,OAAO,EAAE,qBAAqB,EAAE,MAAM,gCAAgC,CAAC;AAkBvE,2DAA2D;AAC3D,MAAM,mBAAmB,GAAG,CAAC,CAAC,MAAM,CAAC;IACnC,IAAI,EAAE,CAAC,CAAC,OAAO,CAAC,UAAU,CAAC;IAC3B,QAAQ,EAAE,CAAC,CAAC,MAAM,EAAE;IACpB,KAAK,EAAE,CAAC,CAAC,QAAQ,CAAC,CAAC,CAAC,MAAM,EAAE,CAAC;IAC7B,OAAO,EAAE,CAAC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AAC;wBACxB,MAAM,KAAK,GAAG,KAAK,CAAC,KAAK,EAAE,CAAC;wBAC5B,IAAI,KAAK,KAAK,SAAS,EAAE,CAAC;4BACxB,MAAM,KAAK,CAAC;wBACd,CAAC;oBACH,CAAC;oBACD,IAAI,IAAI,EAAE,CAAC;wBACT,MAAM;oBACR,CAAC;oBACD,MAAM,IAAI,OAAO,CAAO,CAAC,OAAO,EAAE,EAAE;wBAClC,MAAM,GAAG,OAAO,CAAC;oBACnB,CAAC,CAAC,CAAC;oBACH,MAAM,GAAG,SAAS,CAAC;gBACrB,CAAC;gBAED,OAAO,MAAM,QAAQ,CAAC;YACxB,CAAC;SACF,CAAC,CACH,CAAC;IACJ,CAAC;IAED,OAAO,OAAO,CACZ,IAAI,CAAC;QACH,IAAI;QACJ,GAAG,CAAC,GAAG,CAAC,WAAW,KAAK,SAAS,IAAI;YACnC,WAAW,EAAE,GAAG,CAAC,WAAW;SAC7B,CAAC;QACF,WAAW;QACX,GAAG,CAAC,YAAY,KAAK,SAAS,IAAI;YAChC,YAAY;SACb,CAAC;QACF,OAAO,EAAE,CAAC,IAA6B,EAAE,EAAE,CACzC,UAAU,CAAC;YACT,OAAO;YACP,OAAO,EAAE,GAAG,CAAC,IAAI;YACjB,IAAI;SACL,CAAC;KACL,CAAC,CACH,CAAC;AACJ,CAAC"}
@@ -0,0 +1,3 @@
1
+ /** Non-stdio MCP transports this package supports. */
2
+ export type MCPTransportKind = 'streamableHttp' | 'sse';
3
+ //# sourceMappingURL=transport-types.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"transport-types.d.ts","sourceRoot":"","sources":["../src/transport-types.ts"],"names":[],"mappings":"AAAA,sDAAsD;AACtD,MAAM,MAAM,gBAAgB,GAAG,gBAAgB,GAAG,KAAK,CAAC"}
@@ -0,0 +1,2 @@
1
+ export {};
2
+ //# sourceMappingURL=transport-types.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"transport-types.js","sourceRoot":"","sources":["../src/transport-types.ts"],"names":[],"mappings":""}
package/esm/types.d.ts ADDED
@@ -0,0 +1,104 @@
1
+ import type { Tool } from '@openrouter/agent/tool-types';
2
+ import type { MCPAuth } from './auth/auth-types.js';
3
+ import type { MCPCacheStore } from './cache/cache-store.js';
4
+ import type { UnconvertibleSchemaMode } from './schema/json-schema-to-zod.js';
5
+ import type { MCPTransportKind } from './transport-types.js';
6
+ export type { MCPTransportKind };
7
+ /**
8
+ * Response to a server-initiated elicitation request. `accept` must carry
9
+ * `content` matching the server's `requestedSchema`.
10
+ */
11
+ export type ElicitationResponse = {
12
+ action: 'accept';
13
+ content: Record<string, unknown>;
14
+ } | {
15
+ action: 'decline';
16
+ } | {
17
+ action: 'cancel';
18
+ };
19
+ /**
20
+ * Handler for server-initiated `elicitation/create` requests during a tool
21
+ * call. If omitted from options, requests are auto-declined so a tool call
22
+ * needing input fails gracefully rather than hanging.
23
+ */
24
+ export type ElicitationHandler = (request: {
25
+ message: string;
26
+ requestedSchema: Record<string, unknown>;
27
+ }) => Promise<ElicitationResponse> | ElicitationResponse;
28
+ /** How MCP resources are exposed to the model. */
29
+ export type ResourcesOption = boolean | {
30
+ mode?: 'synthetic-tools';
31
+ };
32
+ export interface CreateMCPToolsOptions {
33
+ /** Remote MCP server endpoint. */
34
+ url: string | URL;
35
+ /** Transport to use; defaults to `streamableHttp` with SSE fallback. */
36
+ transport?: MCPTransportKind;
37
+ /** Authentication, supplied once and reused for discovery + every call. */
38
+ auth?: MCPAuth;
39
+ /** Custom fetch implementation for all network requests. */
40
+ fetch?: typeof fetch;
41
+ /** Client identity sent during `initialize`. */
42
+ clientInfo?: {
43
+ name: string;
44
+ version: string;
45
+ };
46
+ /** Prefix applied to every wrapped tool name (e.g. `"github_"`). */
47
+ toolNamePrefix?: string;
48
+ /**
49
+ * Allow-list of MCP tool names to expose. Applies to discovered MCP tools
50
+ * only; synthetic `list_resources`/`read_resource` tools are controlled
51
+ * exclusively by `resources`.
52
+ */
53
+ includeTools?: readonly string[];
54
+ /**
55
+ * Deny-list of MCP tool names to skip. Applies to discovered MCP tools only;
56
+ * synthetic `list_resources`/`read_resource` tools are controlled
57
+ * exclusively by `resources`.
58
+ */
59
+ excludeTools?: readonly string[];
60
+ /** Behavior when a tool's JSON Schema can't be fully represented in Zod. */
61
+ onUnconvertibleSchema?: UnconvertibleSchemaMode;
62
+ /** Cache store + key for automatic rehydrate-on-hit / write-on-miss. */
63
+ cache?: {
64
+ store: MCPCacheStore;
65
+ key?: string;
66
+ };
67
+ /** Persist resolved tokens/session into the snapshot. Off by default. */
68
+ cacheCredentials?: boolean;
69
+ /** Re-list tools when a cached snapshot is older than this. */
70
+ staleness?: {
71
+ maxAgeMs?: number;
72
+ };
73
+ /** Expose resources as synthetic `list_resources`/`read_resource` tools. */
74
+ resources?: ResourcesOption;
75
+ /** Map MCP progress notifications to generator-tool events. Default true. */
76
+ emitProgress?: boolean;
77
+ /** Auto-refresh tools on `tools/list_changed`. Default true when connected. */
78
+ autoRefreshOnListChanged?: boolean;
79
+ /** Handler for server-initiated elicitation; auto-declines when omitted. */
80
+ onElicitation?: ElicitationHandler;
81
+ /** Abort signal threaded into every underlying `callTool`. */
82
+ signal?: AbortSignal;
83
+ }
84
+ /**
85
+ * Handle returned by {@link createMCPTools}/`rehydrateMCPTools`. Holds a live
86
+ * connection (unless rehydrated offline) and the wrapped tools.
87
+ */
88
+ export interface MCPToolsHandle {
89
+ /** Tools ready to pass into `callModel({ tools })`. */
90
+ readonly tools: readonly Tool[];
91
+ readonly serverInfo?: {
92
+ name?: string;
93
+ version?: string;
94
+ };
95
+ /** Snapshot for persistence; omits credentials unless `cacheCredentials`. */
96
+ serialize(): Promise<import('./cache/cache-types.js').SerializedMCPServer>;
97
+ /** Force a fresh `listTools()` and rebuild the tool set. */
98
+ refresh(): Promise<readonly Tool[]>;
99
+ /** Subscribe to auto-refreshes triggered by `tools/list_changed`. */
100
+ onToolsChanged(listener: (tools: readonly Tool[]) => void): () => void;
101
+ /** Close the transport and underlying client. */
102
+ close(): Promise<void>;
103
+ }
104
+ //# sourceMappingURL=types.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,IAAI,EAAE,MAAM,8BAA8B,CAAC;AACzD,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,sBAAsB,CAAC;AACpD,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,wBAAwB,CAAC;AAC5D,OAAO,KAAK,EAAE,uBAAuB,EAAE,MAAM,gCAAgC,CAAC;AAC9E,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,sBAAsB,CAAC;AAE7D,YAAY,EAAE,gBAAgB,EAAE,CAAC;AAEjC;;;GAGG;AACH,MAAM,MAAM,mBAAmB,GAC3B;IACE,MAAM,EAAE,QAAQ,CAAC;IACjB,OAAO,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC;CAClC,GACD;IACE,MAAM,EAAE,SAAS,CAAC;CACnB,GACD;IACE,MAAM,EAAE,QAAQ,CAAC;CAClB,CAAC;AAEN;;;;GAIG;AACH,MAAM,MAAM,kBAAkB,GAAG,CAAC,OAAO,EAAE;IACzC,OAAO,EAAE,MAAM,CAAC;IAChB,eAAe,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC;CAC1C,KAAK,OAAO,CAAC,mBAAmB,CAAC,GAAG,mBAAmB,CAAC;AAEzD,kDAAkD;AAClD,MAAM,MAAM,eAAe,GACvB,OAAO,GACP;IACE,IAAI,CAAC,EAAE,iBAAiB,CAAC;CAC1B,CAAC;AAEN,MAAM,WAAW,qBAAqB;IACpC,kCAAkC;IAClC,GAAG,EAAE,MAAM,GAAG,GAAG,CAAC;IAClB,wEAAwE;IACxE,SAAS,CAAC,EAAE,gBAAgB,CAAC;IAC7B,2EAA2E;IAC3E,IAAI,CAAC,EAAE,OAAO,CAAC;IACf,4DAA4D;IA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;IAC5D,OAAO,IAAI,OAAO,CAAC,SAAS,IAAI,EAAE,CAAC,CAAC;IACpC,qEAAqE;IACrE,cAAc,CAAC,QAAQ,EAAE,CAAC,KAAK,EAAE,SAAS,IAAI,EAAE,KAAK,IAAI,GAAG,MAAM,IAAI,CAAC;IACvE,iDAAiD;IACjD,KAAK,IAAI,OAAO,CAAC,IAAI,CAAC,CAAC;CACxB"}
package/esm/types.js ADDED
@@ -0,0 +1,2 @@
1
+ export {};
2
+ //# sourceMappingURL=types.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"types.js","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":""}
package/package.json ADDED
@@ -0,0 +1,72 @@
1
+ {
2
+ "name": "@openrouter/mcp",
3
+ "version": "0.0.1",
4
+ "author": "OpenRouter",
5
+ "description": "Expose remote MCP server tools (Streamable HTTP / SSE) as tools for @openrouter/agent's callModel, with serializable caching and pluggable auth.",
6
+ "keywords": [
7
+ "openrouter",
8
+ "mcp",
9
+ "model-context-protocol",
10
+ "agent",
11
+ "typescript",
12
+ "ai",
13
+ "tools",
14
+ "llm"
15
+ ],
16
+ "license": "Apache-2.0",
17
+ "type": "module",
18
+ "main": "./esm/index.js",
19
+ "exports": {
20
+ ".": {
21
+ "types": "./esm/index.d.ts",
22
+ "default": "./esm/index.js"
23
+ },
24
+ "./create-mcp-tools": {
25
+ "types": "./esm/create-mcp-tools.d.ts",
26
+ "default": "./esm/create-mcp-tools.js"
27
+ },
28
+ "./types": {
29
+ "types": "./esm/types.d.ts",
30
+ "default": "./esm/types.js"
31
+ },
32
+ "./schema": {
33
+ "types": "./esm/schema/json-schema-to-zod.d.ts",
34
+ "default": "./esm/schema/json-schema-to-zod.js"
35
+ },
36
+ "./cache": {
37
+ "types": "./esm/cache/cache-store.d.ts",
38
+ "default": "./esm/cache/cache-store.js"
39
+ },
40
+ "./package.json": "./package.json"
41
+ },
42
+ "sideEffects": false,
43
+ "repository": {
44
+ "type": "git",
45
+ "url": "https://github.com/OpenRouterTeam/typescript-agent.git",
46
+ "directory": "packages/mcp"
47
+ },
48
+ "publishConfig": {
49
+ "access": "public",
50
+ "provenance": true
51
+ },
52
+ "files": [
53
+ "esm",
54
+ "package.json",
55
+ "README.md"
56
+ ],
57
+ "scripts": {
58
+ "lint": "biome check src tests",
59
+ "lint:fix": "biome check --write src tests",
60
+ "build": "tsc",
61
+ "test": "vitest --run --project unit",
62
+ "test:e2e": "vitest --run --project e2e",
63
+ "test:watch": "vitest --watch --project unit",
64
+ "typecheck": "tsc --noEmit",
65
+ "compile": "tsc"
66
+ },
67
+ "dependencies": {
68
+ "@modelcontextprotocol/sdk": "^1.29.0",
69
+ "@openrouter/agent": "workspace:*",
70
+ "zod": "^4.0.0"
71
+ }
72
+ }