@openrouter/mcp 0.0.1 → 1.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +35 -4
- package/esm/cache.d.ts +7 -0
- package/esm/cache.d.ts.map +1 -0
- package/esm/cache.js +2 -0
- package/esm/cache.js.map +1 -0
- package/esm/create-mcp-tools.d.ts +4 -10
- package/esm/create-mcp-tools.d.ts.map +1 -1
- package/esm/create-mcp-tools.js +3 -72
- package/esm/create-mcp-tools.js.map +1 -1
- package/esm/index.d.ts +8 -12
- package/esm/index.d.ts.map +1 -1
- package/esm/index.js +1 -10
- package/esm/index.js.map +1 -1
- package/esm/schema.d.ts +7 -0
- package/esm/schema.d.ts.map +1 -0
- package/esm/schema.js +2 -0
- package/esm/schema.js.map +1 -0
- package/esm/types.d.ts +3 -101
- package/esm/types.d.ts.map +1 -1
- package/package.json +14 -13
- package/esm/auth/auth-resolver.d.ts +0 -14
- package/esm/auth/auth-resolver.d.ts.map +0 -1
- package/esm/auth/auth-resolver.js +0 -27
- package/esm/auth/auth-resolver.js.map +0 -1
- package/esm/auth/auth-types.d.ts +0 -25
- package/esm/auth/auth-types.d.ts.map +0 -1
- package/esm/auth/auth-types.js +0 -4
- package/esm/auth/auth-types.js.map +0 -1
- package/esm/build-tools.d.ts +0 -27
- package/esm/build-tools.d.ts.map +0 -1
- package/esm/build-tools.js +0 -77
- package/esm/build-tools.js.map +0 -1
- package/esm/cache/cache-store.d.ts +0 -24
- package/esm/cache/cache-store.d.ts.map +0 -1
- package/esm/cache/cache-store.js +0 -20
- package/esm/cache/cache-store.js.map +0 -1
- package/esm/cache/cache-types.d.ts +0 -54
- package/esm/cache/cache-types.d.ts.map +0 -1
- package/esm/cache/cache-types.js +0 -41
- package/esm/cache/cache-types.js.map +0 -1
- package/esm/cache/serialize.d.ts +0 -25
- package/esm/cache/serialize.d.ts.map +0 -1
- package/esm/cache/serialize.js +0 -85
- package/esm/cache/serialize.js.map +0 -1
- package/esm/elicitation.d.ts +0 -11
- package/esm/elicitation.d.ts.map +0 -1
- package/esm/elicitation.js +0 -51
- package/esm/elicitation.js.map +0 -1
- package/esm/errors.d.ts +0 -35
- package/esm/errors.d.ts.map +0 -1
- package/esm/errors.js +0 -39
- package/esm/errors.js.map +0 -1
- package/esm/handle.d.ts +0 -41
- package/esm/handle.d.ts.map +0 -1
- package/esm/handle.js +0 -221
- package/esm/handle.js.map +0 -1
- package/esm/mcp-connection.d.ts +0 -35
- package/esm/mcp-connection.d.ts.map +0 -1
- package/esm/mcp-connection.js +0 -147
- package/esm/mcp-connection.js.map +0 -1
- package/esm/rehydrate.d.ts +0 -41
- package/esm/rehydrate.d.ts.map +0 -1
- package/esm/rehydrate.js +0 -173
- package/esm/rehydrate.js.map +0 -1
- package/esm/resource-tools.d.ts +0 -17
- package/esm/resource-tools.d.ts.map +0 -1
- package/esm/resource-tools.js +0 -133
- package/esm/resource-tools.js.map +0 -1
- package/esm/result-mapper.d.ts +0 -19
- package/esm/result-mapper.d.ts.map +0 -1
- package/esm/result-mapper.js +0 -40
- package/esm/result-mapper.js.map +0 -1
- package/esm/schema/json-schema-guards.d.ts +0 -14
- package/esm/schema/json-schema-guards.d.ts.map +0 -1
- package/esm/schema/json-schema-guards.js +0 -19
- package/esm/schema/json-schema-guards.js.map +0 -1
- package/esm/schema/json-schema-to-zod.d.ts +0 -20
- package/esm/schema/json-schema-to-zod.d.ts.map +0 -1
- package/esm/schema/json-schema-to-zod.js +0 -72
- package/esm/schema/json-schema-to-zod.js.map +0 -1
- package/esm/tool-wrapper.d.ts +0 -26
- package/esm/tool-wrapper.d.ts.map +0 -1
- package/esm/tool-wrapper.js +0 -118
- package/esm/tool-wrapper.js.map +0 -1
- package/esm/transport-types.d.ts +0 -3
- package/esm/transport-types.d.ts.map +0 -1
- package/esm/transport-types.js +0 -2
- package/esm/transport-types.js.map +0 -1
package/esm/resource-tools.d.ts
DELETED
|
@@ -1,17 +0,0 @@
|
|
|
1
|
-
import type { Client } from '@modelcontextprotocol/sdk/client/index.js';
|
|
2
|
-
import type { McpBranded } from '@openrouter/agent/tool-types';
|
|
3
|
-
export interface ResourceToolsOptions {
|
|
4
|
-
client: Client;
|
|
5
|
-
namePrefix?: string;
|
|
6
|
-
signal?: AbortSignal;
|
|
7
|
-
}
|
|
8
|
-
/**
|
|
9
|
-
* Build synthetic tools that let the model browse and read MCP resources
|
|
10
|
-
* through the normal tool loop:
|
|
11
|
-
* - `list_resources`: concrete resources plus resource templates.
|
|
12
|
-
* - `read_resource`: fetch a resource's contents by URI.
|
|
13
|
-
*
|
|
14
|
-
* Only call this when the server advertises the `resources` capability.
|
|
15
|
-
*/
|
|
16
|
-
export declare function buildResourceTools(options: ResourceToolsOptions): McpBranded[];
|
|
17
|
-
//# sourceMappingURL=resource-tools.d.ts.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"resource-tools.d.ts","sourceRoot":"","sources":["../src/resource-tools.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,2CAA2C,CAAC;AAExE,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,8BAA8B,CAAC;AAG/D,MAAM,WAAW,oBAAoB;IACnC,MAAM,EAAE,MAAM,CAAC;IACf,UAAU,CAAC,EAAE,MAAM,CAAC;IACpB,MAAM,CAAC,EAAE,WAAW,CAAC;CACtB;AA0ED;;;;;;;GAOG;AACH,wBAAgB,kBAAkB,CAAC,OAAO,EAAE,oBAAoB,GAAG,UAAU,EAAE,CAiG9E"}
|
package/esm/resource-tools.js
DELETED
|
@@ -1,133 +0,0 @@
|
|
|
1
|
-
import { markMcp, tool } from '@openrouter/agent/tool';
|
|
2
|
-
import * as z from 'zod';
|
|
3
|
-
// Hard cap on pagination pages. A well-behaved server terminates the cursor
|
|
4
|
-
// chain by omitting `nextCursor`; this bounds a misbehaving one that never does.
|
|
5
|
-
const MAX_LIST_PAGES = 1000;
|
|
6
|
-
/**
|
|
7
|
-
* Normalize a paginated `nextCursor` field: treat anything that is not a
|
|
8
|
-
* non-empty string as "no more pages" so a malformed cursor terminates the loop.
|
|
9
|
-
*/
|
|
10
|
-
function nextCursorOrUndefined(value) {
|
|
11
|
-
return typeof value === 'string' && value.length > 0 ? value : undefined;
|
|
12
|
-
}
|
|
13
|
-
/**
|
|
14
|
-
* Walk a paginated MCP list endpoint, following `nextCursor` (passed back as
|
|
15
|
-
* `{ cursor }`) until exhausted, and return the flattened items. Stops when the
|
|
16
|
-
* server omits `nextCursor`, repeats the same cursor, or the page cap is hit.
|
|
17
|
-
*/
|
|
18
|
-
async function collectPages(fetchPage, requestOptions) {
|
|
19
|
-
const collected = [];
|
|
20
|
-
let cursor;
|
|
21
|
-
for (let page = 0; page < MAX_LIST_PAGES; page += 1) {
|
|
22
|
-
const params = cursor !== undefined
|
|
23
|
-
? {
|
|
24
|
-
cursor,
|
|
25
|
-
}
|
|
26
|
-
: undefined;
|
|
27
|
-
const { items, nextCursor } = await fetchPage(params, requestOptions);
|
|
28
|
-
collected.push(...items);
|
|
29
|
-
const next = nextCursorOrUndefined(nextCursor);
|
|
30
|
-
if (next === undefined || next === cursor) {
|
|
31
|
-
break;
|
|
32
|
-
}
|
|
33
|
-
cursor = next;
|
|
34
|
-
}
|
|
35
|
-
return collected;
|
|
36
|
-
}
|
|
37
|
-
/**
|
|
38
|
-
* Build synthetic tools that let the model browse and read MCP resources
|
|
39
|
-
* through the normal tool loop:
|
|
40
|
-
* - `list_resources`: concrete resources plus resource templates.
|
|
41
|
-
* - `read_resource`: fetch a resource's contents by URI.
|
|
42
|
-
*
|
|
43
|
-
* Only call this when the server advertises the `resources` capability.
|
|
44
|
-
*/
|
|
45
|
-
export function buildResourceTools(options) {
|
|
46
|
-
const prefix = options.namePrefix ?? '';
|
|
47
|
-
const requestOptions = options.signal !== undefined
|
|
48
|
-
? {
|
|
49
|
-
signal: options.signal,
|
|
50
|
-
}
|
|
51
|
-
: undefined;
|
|
52
|
-
const listResources = tool({
|
|
53
|
-
name: `${prefix}list_resources`,
|
|
54
|
-
description: 'List the resources and resource templates exposed by the MCP server.',
|
|
55
|
-
inputSchema: z.object({}),
|
|
56
|
-
execute: async () => {
|
|
57
|
-
const [resources, resourceTemplates] = await Promise.all([
|
|
58
|
-
collectPages(async (params, opts) => {
|
|
59
|
-
const page = await options.client.listResources(params, opts);
|
|
60
|
-
return {
|
|
61
|
-
items: page.resources,
|
|
62
|
-
nextCursor: page.nextCursor,
|
|
63
|
-
};
|
|
64
|
-
}, requestOptions),
|
|
65
|
-
// Templates are optional: a server may not support them. The whole
|
|
66
|
-
// paginated fetch degrades gracefully to an empty list on any error.
|
|
67
|
-
collectPages(async (params, opts) => {
|
|
68
|
-
const page = await options.client.listResourceTemplates(params, opts);
|
|
69
|
-
return {
|
|
70
|
-
items: page.resourceTemplates,
|
|
71
|
-
nextCursor: page.nextCursor,
|
|
72
|
-
};
|
|
73
|
-
}, requestOptions).catch(() => []),
|
|
74
|
-
]);
|
|
75
|
-
return {
|
|
76
|
-
resources: resources.map((r) => ({
|
|
77
|
-
uri: r.uri,
|
|
78
|
-
name: r.name,
|
|
79
|
-
...(r.description !== undefined && {
|
|
80
|
-
description: r.description,
|
|
81
|
-
}),
|
|
82
|
-
...(r.mimeType !== undefined && {
|
|
83
|
-
mimeType: r.mimeType,
|
|
84
|
-
}),
|
|
85
|
-
})),
|
|
86
|
-
resourceTemplates: resourceTemplates.map((t) => ({
|
|
87
|
-
uriTemplate: t.uriTemplate,
|
|
88
|
-
name: t.name,
|
|
89
|
-
...(t.description !== undefined && {
|
|
90
|
-
description: t.description,
|
|
91
|
-
}),
|
|
92
|
-
})),
|
|
93
|
-
};
|
|
94
|
-
},
|
|
95
|
-
});
|
|
96
|
-
const readResource = tool({
|
|
97
|
-
name: `${prefix}read_resource`,
|
|
98
|
-
description: 'Read the contents of an MCP resource by its URI.',
|
|
99
|
-
inputSchema: z.object({
|
|
100
|
-
uri: z.string().describe('Resource URI to read'),
|
|
101
|
-
}),
|
|
102
|
-
execute: async (args) => {
|
|
103
|
-
const result = await options.client.readResource({
|
|
104
|
-
uri: args.uri,
|
|
105
|
-
}, requestOptions);
|
|
106
|
-
return {
|
|
107
|
-
contents: result.contents.map((c) => {
|
|
108
|
-
if ('text' in c) {
|
|
109
|
-
return {
|
|
110
|
-
uri: c.uri,
|
|
111
|
-
text: c.text,
|
|
112
|
-
...(c.mimeType !== undefined && {
|
|
113
|
-
mimeType: c.mimeType,
|
|
114
|
-
}),
|
|
115
|
-
};
|
|
116
|
-
}
|
|
117
|
-
return {
|
|
118
|
-
uri: c.uri,
|
|
119
|
-
blob: c.blob,
|
|
120
|
-
...(c.mimeType !== undefined && {
|
|
121
|
-
mimeType: c.mimeType,
|
|
122
|
-
}),
|
|
123
|
-
};
|
|
124
|
-
}),
|
|
125
|
-
};
|
|
126
|
-
},
|
|
127
|
-
});
|
|
128
|
-
return [
|
|
129
|
-
markMcp(listResources),
|
|
130
|
-
markMcp(readResource),
|
|
131
|
-
];
|
|
132
|
-
}
|
|
133
|
-
//# sourceMappingURL=resource-tools.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"resource-tools.js","sourceRoot":"","sources":["../src/resource-tools.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,OAAO,EAAE,IAAI,EAAE,MAAM,wBAAwB,CAAC;AAEvD,OAAO,KAAK,CAAC,MAAM,KAAK,CAAC;AAQzB,4EAA4E;AAC5E,iFAAiF;AACjF,MAAM,cAAc,GAAG,IAAI,CAAC;AAuB5B;;;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;;;;GAIG;AACH,KAAK,UAAU,YAAY,CACzB,SAUE,EACF,cAA8B;IAE9B,MAAM,SAAS,GAA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|
package/esm/result-mapper.d.ts
DELETED
|
@@ -1,19 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* The subset of an MCP `CallToolResult` we read. The SDK types it more richly,
|
|
3
|
-
* but we narrow defensively from `unknown`-ish content rather than trusting the
|
|
4
|
-
* wire shape.
|
|
5
|
-
*/
|
|
6
|
-
export interface RawCallToolResult {
|
|
7
|
-
content?: unknown;
|
|
8
|
-
structuredContent?: unknown;
|
|
9
|
-
isError?: boolean;
|
|
10
|
-
[key: string]: unknown;
|
|
11
|
-
}
|
|
12
|
-
/**
|
|
13
|
-
* Map an MCP tool result into a value `callModel` can hand back to the model.
|
|
14
|
-
* Prefers `structuredContent` (already JSON), otherwise the collapsed text
|
|
15
|
-
* content. Throws {@link MCPToolCallError} when the server flags `isError`, so
|
|
16
|
-
* the agent loop reports the failure instead of treating error text as success.
|
|
17
|
-
*/
|
|
18
|
-
export declare function mapCallToolResult(toolName: string, result: RawCallToolResult): unknown;
|
|
19
|
-
//# sourceMappingURL=result-mapper.d.ts.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"result-mapper.d.ts","sourceRoot":"","sources":["../src/result-mapper.ts"],"names":[],"mappings":"AAGA;;;;GAIG;AACH,MAAM,WAAW,iBAAiB;IAChC,OAAO,CAAC,EAAE,OAAO,CAAC;IAClB,iBAAiB,CAAC,EAAE,OAAO,CAAC;IAC5B,OAAO,CAAC,EAAE,OAAO,CAAC;IAClB,CAAC,GAAG,EAAE,MAAM,GAAG,OAAO,CAAC;CACxB;AA2BD;;;;;GAKG;AACH,wBAAgB,iBAAiB,CAAC,QAAQ,EAAE,MAAM,EAAE,MAAM,EAAE,iBAAiB,GAAG,OAAO,CAStF"}
|
package/esm/result-mapper.js
DELETED
|
@@ -1,40 +0,0 @@
|
|
|
1
|
-
import { MCPToolCallError } from './errors.js';
|
|
2
|
-
import { isJsonSchemaObject } from './schema/json-schema-guards.js';
|
|
3
|
-
function isTextBlock(block) {
|
|
4
|
-
return isJsonSchemaObject(block) && block['type'] === 'text' && typeof block['text'] === 'string';
|
|
5
|
-
}
|
|
6
|
-
/** Collapse the content array into a single string for the model. */
|
|
7
|
-
function contentToText(content) {
|
|
8
|
-
if (!Array.isArray(content)) {
|
|
9
|
-
return '';
|
|
10
|
-
}
|
|
11
|
-
const parts = [];
|
|
12
|
-
for (const block of content) {
|
|
13
|
-
if (isTextBlock(block)) {
|
|
14
|
-
parts.push(block.text);
|
|
15
|
-
}
|
|
16
|
-
else if (isJsonSchemaObject(block) && typeof block['type'] === 'string') {
|
|
17
|
-
// Non-text blocks (image/audio/resource) aren't passed through as media
|
|
18
|
-
// in v1 — surface a typed placeholder so the model knows it exists.
|
|
19
|
-
parts.push(`[${block['type']} content]`);
|
|
20
|
-
}
|
|
21
|
-
}
|
|
22
|
-
return parts.join('\n');
|
|
23
|
-
}
|
|
24
|
-
/**
|
|
25
|
-
* Map an MCP tool result into a value `callModel` can hand back to the model.
|
|
26
|
-
* Prefers `structuredContent` (already JSON), otherwise the collapsed text
|
|
27
|
-
* content. Throws {@link MCPToolCallError} when the server flags `isError`, so
|
|
28
|
-
* the agent loop reports the failure instead of treating error text as success.
|
|
29
|
-
*/
|
|
30
|
-
export function mapCallToolResult(toolName, result) {
|
|
31
|
-
if (result.isError === true) {
|
|
32
|
-
const message = contentToText(result.content) || 'MCP tool returned an error';
|
|
33
|
-
throw new MCPToolCallError(toolName, message);
|
|
34
|
-
}
|
|
35
|
-
if (result.structuredContent !== undefined) {
|
|
36
|
-
return result.structuredContent;
|
|
37
|
-
}
|
|
38
|
-
return contentToText(result.content);
|
|
39
|
-
}
|
|
40
|
-
//# sourceMappingURL=result-mapper.js.map
|
package/esm/result-mapper.js.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"result-mapper.js","sourceRoot":"","sources":["../src/result-mapper.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,gBAAgB,EAAE,MAAM,aAAa,CAAC;AAC/C,OAAO,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AC,iBAAiB,CAAC;IAClC,CAAC;IACD,OAAO,aAAa,CAAC,MAAM,CAAC,OAAO,CAAC,CAAC;AACvC,CAAC"}
|
|
@@ -1,14 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Typeguards over the raw JSON-Schema-shaped values that arrive from an MCP
|
|
3
|
-
* server. MCP tool input/output schemas are untrusted `unknown` data, so we
|
|
4
|
-
* narrow them with runtime checks rather than `as` casts.
|
|
5
|
-
*/
|
|
6
|
-
/** A non-null, non-array object — the shape every JSON Schema node takes. */
|
|
7
|
-
export declare function isJsonSchemaObject(value: unknown): value is Record<string, unknown>;
|
|
8
|
-
/** A JSON Schema whose top-level `type` is `"object"`. */
|
|
9
|
-
export declare function isObjectJsonSchema(value: unknown): value is Record<string, unknown> & {
|
|
10
|
-
type: 'object';
|
|
11
|
-
};
|
|
12
|
-
/** The `properties` map of an object schema, when present and well-formed. */
|
|
13
|
-
export declare function getSchemaProperties(schema: Record<string, unknown>): Record<string, unknown> | undefined;
|
|
14
|
-
//# sourceMappingURL=json-schema-guards.d.ts.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"json-schema-guards.d.ts","sourceRoot":"","sources":["../../src/schema/json-schema-guards.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAEH,6EAA6E;AAC7E,wBAAgB,kBAAkB,CAAC,KAAK,EAAE,OAAO,GAAG,KAAK,IAAI,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAEnF;AAED,0DAA0D;AAC1D,wBAAgB,kBAAkB,CAAC,KAAK,EAAE,OAAO,GAAG,KAAK,IAAI,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,GAAG;IACrF,IAAI,EAAE,QAAQ,CAAC;CAChB,CAEA;AAED,8EAA8E;AAC9E,wBAAgB,mBAAmB,CACjC,MAAM,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,GAC9B,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,GAAG,SAAS,CAGrC"}
|
|
@@ -1,19 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Typeguards over the raw JSON-Schema-shaped values that arrive from an MCP
|
|
3
|
-
* server. MCP tool input/output schemas are untrusted `unknown` data, so we
|
|
4
|
-
* narrow them with runtime checks rather than `as` casts.
|
|
5
|
-
*/
|
|
6
|
-
/** A non-null, non-array object — the shape every JSON Schema node takes. */
|
|
7
|
-
export function isJsonSchemaObject(value) {
|
|
8
|
-
return typeof value === 'object' && value !== null && !Array.isArray(value);
|
|
9
|
-
}
|
|
10
|
-
/** A JSON Schema whose top-level `type` is `"object"`. */
|
|
11
|
-
export function isObjectJsonSchema(value) {
|
|
12
|
-
return isJsonSchemaObject(value) && value['type'] === 'object';
|
|
13
|
-
}
|
|
14
|
-
/** The `properties` map of an object schema, when present and well-formed. */
|
|
15
|
-
export function getSchemaProperties(schema) {
|
|
16
|
-
const { properties } = schema;
|
|
17
|
-
return isJsonSchemaObject(properties) ? properties : undefined;
|
|
18
|
-
}
|
|
19
|
-
//# sourceMappingURL=json-schema-guards.js.map
|
|
@@ -1 +0,0 @@
|
|
|
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"}
|
|
@@ -1,20 +0,0 @@
|
|
|
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
|
|
@@ -1 +0,0 @@
|
|
|
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"}
|
|
@@ -1,72 +0,0 @@
|
|
|
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
|
|
@@ -1 +0,0 @@
|
|
|
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,MA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AG,CAAC,CAAC,OAAO,EAAE,CAAC;QACxB,CAAC;QACD,KAAK,CAAC,GAAG,CAAC,GAAG,QAAQ,CAAC,GAAG,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,OAAO,CAAC,CAAC,CAAC,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"}
|
package/esm/tool-wrapper.d.ts
DELETED
|
@@ -1,26 +0,0 @@
|
|
|
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
|
|
@@ -1 +0,0 @@
|
|
|
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"}
|
package/esm/tool-wrapper.js
DELETED
|
@@ -1,118 +0,0 @@
|
|
|
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
|
package/esm/tool-wrapper.js.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
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,C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AM,GAAG,CAAC,EAAE,CAAC;oBACjC,OAAO,KAAK,CAAC,MAAM,GAAG,CAAC,EAAE,CAAC;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"}
|
package/esm/transport-types.d.ts
DELETED
|
@@ -1 +0,0 @@
|
|
|
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"}
|
package/esm/transport-types.js
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"transport-types.js","sourceRoot":"","sources":["../src/transport-types.ts"],"names":[],"mappings":""}
|