mcp-use 2.0.0-beta.35 → 2.0.0-beta.36

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.
@@ -1,281 +0,0 @@
1
- // src/mcp-proxy.ts
2
- var PROXY_CLIENT_INSTALL_HINT = [
3
- "[mcp-use] server.proxy() requires the optional @mcp-use/client package.",
4
- "Install it in your project:",
5
- "",
6
- " npm install @mcp-use/client"
7
- ].join("\n");
8
- function passthroughJsonSchema(schema) {
9
- return {
10
- "~standard": {
11
- version: 1,
12
- vendor: "mcp-use-proxy",
13
- validate(value) {
14
- return { value };
15
- },
16
- jsonSchema: {
17
- input: () => schema,
18
- output: () => schema
19
- }
20
- }
21
- };
22
- }
23
- function promptArgsToJsonSchema(args) {
24
- const properties = /* @__PURE__ */ Object.create(null);
25
- const required = [];
26
- for (const arg of args ?? []) {
27
- properties[arg.name] = {
28
- type: "string",
29
- ...arg.description !== void 0 && { description: arg.description }
30
- };
31
- if (arg.required === true) required.push(arg.name);
32
- }
33
- return {
34
- type: "object",
35
- properties,
36
- ...required.length > 0 && { required }
37
- };
38
- }
39
- function prefixedName(namespace, name) {
40
- return `${namespace}_${name}`;
41
- }
42
- function proxiedResourceUri(namespace, uri) {
43
- return `mcp-use-proxy:///${encodeURIComponent(namespace)}/${encodeURIComponent(uri)}`;
44
- }
45
- function isConnection(value) {
46
- if (value === null || typeof value !== "object") return false;
47
- const candidate = value;
48
- return typeof candidate.listTools === "function" && typeof candidate.callTool === "function" && typeof candidate.readResource === "function" && typeof candidate.listPrompts === "function" && typeof candidate.getPrompt === "function";
49
- }
50
- function isClientPackageMissing(error) {
51
- if (!(error instanceof Error)) return false;
52
- const code = error.code;
53
- return (code === "ERR_MODULE_NOT_FOUND" || code === "MODULE_NOT_FOUND") && error.message.includes("@mcp-use/client");
54
- }
55
- function proxyDiagnostic(message, error) {
56
- if (error === void 0) {
57
- console.error(`[mcp-use] ${message}`);
58
- return;
59
- }
60
- const detail = error instanceof Error ? error.message : String(error);
61
- console.error(`[mcp-use] ${message}: ${detail}`);
62
- }
63
- function proxyClientInstallError(error) {
64
- return isClientPackageMissing(error) ? new Error(PROXY_CLIENT_INSTALL_HINT, { cause: error }) : void 0;
65
- }
66
- async function loadProxyClient(importer = () => import("@mcp-use/client")) {
67
- try {
68
- return await importer();
69
- } catch (error) {
70
- const installError = proxyClientInstallError(error);
71
- if (installError !== void 0) throw installError;
72
- throw error;
73
- }
74
- }
75
- function supports(connection, capability) {
76
- return connection.supports?.(capability) !== false;
77
- }
78
- async function introspect(namespace, connection) {
79
- let tools = [];
80
- if (supports(connection, "tools")) {
81
- try {
82
- tools = await connection.listTools();
83
- } catch (error) {
84
- proxyDiagnostic(
85
- `Failed to introspect tools from upstream MCP server "${namespace}"`,
86
- error
87
- );
88
- }
89
- }
90
- let resources = [];
91
- if (supports(connection, "resources")) {
92
- try {
93
- if (connection.listAllResources !== void 0) {
94
- resources = (await connection.listAllResources()).resources;
95
- } else if (connection.listResources !== void 0) {
96
- resources = (await connection.listResources()).resources;
97
- }
98
- } catch (error) {
99
- proxyDiagnostic(
100
- `Failed to introspect resources from upstream MCP server "${namespace}"`,
101
- error
102
- );
103
- }
104
- }
105
- let prompts = [];
106
- if (supports(connection, "prompts")) {
107
- try {
108
- prompts = (await connection.listPrompts()).prompts;
109
- } catch (error) {
110
- proxyDiagnostic(
111
- `Failed to introspect prompts from upstream MCP server "${namespace}"`,
112
- error
113
- );
114
- }
115
- }
116
- return { namespace, connection, tools, resources, prompts };
117
- }
118
- function mountPlan(host, plan) {
119
- for (const tool of plan.tools) {
120
- const name = prefixedName(plan.namespace, tool.name);
121
- if (host.hasTool(name)) {
122
- proxyDiagnostic(
123
- `Skipping proxied tool "${name}" from upstream "${plan.namespace}" because that name is already registered`
124
- );
125
- continue;
126
- }
127
- const definition = {
128
- name,
129
- ...tool.title !== void 0 && { title: tool.title },
130
- ...tool.description !== void 0 && { description: tool.description },
131
- ...tool.annotations !== void 0 && { annotations: tool.annotations },
132
- ...tool.inputSchema !== void 0 && {
133
- inputSchema: passthroughJsonSchema(tool.inputSchema)
134
- },
135
- ...tool.outputSchema !== void 0 && {
136
- outputSchema: passthroughJsonSchema(tool.outputSchema)
137
- }
138
- };
139
- const upstreamName = tool.name;
140
- const callback = async (params, ctx) => {
141
- let progressForwarding = Promise.resolve();
142
- try {
143
- return await plan.connection.callTool(upstreamName, params, {
144
- signal: ctx.signal,
145
- onprogress: (progress) => {
146
- progressForwarding = progressForwarding.then(async () => {
147
- try {
148
- await ctx.reportProgress(
149
- progress.progress,
150
- progress.total,
151
- progress.message
152
- );
153
- } catch (error) {
154
- proxyDiagnostic(
155
- `Failed to forward progress for proxied tool "${definition.name}"`,
156
- error
157
- );
158
- }
159
- });
160
- }
161
- });
162
- } finally {
163
- await progressForwarding;
164
- }
165
- };
166
- host.registerTool(definition, callback);
167
- }
168
- for (const resource of plan.resources) {
169
- const name = prefixedName(plan.namespace, resource.name);
170
- if (host.hasResource(name)) {
171
- proxyDiagnostic(
172
- `Skipping proxied resource "${name}" from upstream "${plan.namespace}" because that name is already registered`
173
- );
174
- continue;
175
- }
176
- const upstreamUri = resource.uri;
177
- host.registerResource(
178
- {
179
- name,
180
- uri: proxiedResourceUri(plan.namespace, upstreamUri),
181
- ...resource.title !== void 0 && { title: resource.title },
182
- ...resource.description !== void 0 && {
183
- description: resource.description
184
- },
185
- ...resource.mimeType !== void 0 && {
186
- mimeType: resource.mimeType
187
- }
188
- },
189
- async (_uri, ctx) => plan.connection.readResource(upstreamUri, { signal: ctx.signal })
190
- );
191
- }
192
- for (const prompt of plan.prompts) {
193
- const name = prefixedName(plan.namespace, prompt.name);
194
- if (host.hasPrompt(name)) {
195
- proxyDiagnostic(
196
- `Skipping proxied prompt "${name}" from upstream "${plan.namespace}" because that name is already registered`
197
- );
198
- continue;
199
- }
200
- const upstreamName = prompt.name;
201
- host.registerPrompt(
202
- {
203
- name,
204
- ...prompt.title !== void 0 && { title: prompt.title },
205
- ...prompt.description !== void 0 && {
206
- description: prompt.description
207
- },
208
- schema: passthroughJsonSchema(promptArgsToJsonSchema(prompt.arguments))
209
- },
210
- async (params) => plan.connection.getPrompt(upstreamName, params)
211
- );
212
- }
213
- }
214
- async function mountProxyConnection(host, connection) {
215
- if (host.isStarted()) {
216
- throw new Error(
217
- "Cannot call proxy() after the server has started: register upstream servers before listen()/server.fetch."
218
- );
219
- }
220
- const namespace = connection.info.server.name;
221
- const plan = await introspect(namespace, connection);
222
- mountPlan(host, plan);
223
- }
224
- function toClientConfig(config) {
225
- return {
226
- url: config.url,
227
- ...config.headers !== void 0 && { headers: config.headers },
228
- ...config.authToken !== void 0 && { authToken: config.authToken },
229
- ...config.timeout !== void 0 && { timeout: config.timeout },
230
- ...config.fetch !== void 0 && { fetch: config.fetch },
231
- ...config.protocolNegotiation !== void 0 && {
232
- protocolNegotiation: config.protocolNegotiation
233
- },
234
- oauth: false
235
- };
236
- }
237
- async function mountProxyServers(host, servers) {
238
- if (host.isStarted()) {
239
- throw new Error(
240
- "Cannot call proxy() after the server has started: register upstream servers before listen()/server.fetch."
241
- );
242
- }
243
- const { MCPClient } = await loadProxyClient();
244
- const clientServers = Object.fromEntries(
245
- Object.entries(servers).map(([name, config]) => [
246
- name,
247
- toClientConfig(config)
248
- ])
249
- );
250
- const owner = new MCPClient({ mcpServers: clientServers });
251
- try {
252
- const plans = [];
253
- for (const namespace of Object.keys(clientServers)) {
254
- let connection;
255
- try {
256
- connection = await owner.connect(namespace);
257
- } catch (error) {
258
- proxyDiagnostic(
259
- `Failed to connect to upstream MCP server "${namespace}"`,
260
- error
261
- );
262
- continue;
263
- }
264
- plans.push(await introspect(namespace, connection));
265
- }
266
- for (const plan of plans) mountPlan(host, plan);
267
- host.trackOwner(owner);
268
- } catch (error) {
269
- await owner.close().catch(() => void 0);
270
- throw error;
271
- }
272
- }
273
- function isProxyConnection(value) {
274
- return isConnection(value);
275
- }
276
- export {
277
- isProxyConnection,
278
- mountProxyConnection,
279
- mountProxyServers,
280
- proxyClientInstallError
281
- };
@@ -1,8 +0,0 @@
1
- import {
2
- resolvePublicFilePath,
3
- servePublicFile
4
- } from "./chunk-QCIREWRS.js";
5
- export {
6
- resolvePublicFilePath,
7
- servePublicFile
8
- };