modality-mcp-kit 1.6.1 → 1.6.2
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/dist/util_mcp_proxy.js +40 -6
- package/package.json +1 -1
package/dist/util_mcp_proxy.js
CHANGED
|
@@ -266,17 +266,47 @@ async function prefetchAndCacheTools(mcpName, serverUrl, cache, storedToken) {
|
|
|
266
266
|
if (storedToken) {
|
|
267
267
|
headers.authorization = `Bearer ${storedToken}`;
|
|
268
268
|
}
|
|
269
|
+
// Step 1: initialize session (required by MCP protocol before tools/list)
|
|
270
|
+
const initResponse = await fetch(serverUrl, {
|
|
271
|
+
method: "POST",
|
|
272
|
+
headers,
|
|
273
|
+
body: JSON.stringify({
|
|
274
|
+
jsonrpc: "2.0",
|
|
275
|
+
id: 1,
|
|
276
|
+
method: "initialize",
|
|
277
|
+
params: {
|
|
278
|
+
protocolVersion: "2024-11-05",
|
|
279
|
+
capabilities: {},
|
|
280
|
+
clientInfo: { name: "mcp-proxy", version: "1.0" },
|
|
281
|
+
},
|
|
282
|
+
}),
|
|
283
|
+
});
|
|
284
|
+
if (!initResponse.ok) {
|
|
285
|
+
console.warn(`[MCP-PROXY] Initialize failed for ${mcpName} — status: ${initResponse.status}`);
|
|
286
|
+
return { tools: null, fromCache: false };
|
|
287
|
+
}
|
|
288
|
+
const sessionId = initResponse.headers.get("mcp-session-id");
|
|
289
|
+
if (sessionId) {
|
|
290
|
+
headers["mcp-session-id"] = sessionId;
|
|
291
|
+
}
|
|
292
|
+
// Step 2: fetch tools/list
|
|
269
293
|
const response = await fetch(serverUrl, {
|
|
270
294
|
method: "POST",
|
|
271
295
|
headers,
|
|
272
|
-
body: JSON.stringify({ jsonrpc: "2.0", id:
|
|
296
|
+
body: JSON.stringify({ jsonrpc: "2.0", id: 2, method: "tools/list", params: {} }),
|
|
273
297
|
});
|
|
274
298
|
const body = await response.text();
|
|
275
|
-
const
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
299
|
+
const tools = parseToolsFromBody(body);
|
|
300
|
+
if (response.ok && tools !== null) {
|
|
301
|
+
const ttl = METHOD_TTL_MS["tools/list"];
|
|
302
|
+
const cacheValue = body.includes("event:") ? body : `event: message\ndata: ${body}\n\n`;
|
|
303
|
+
cache.set(cacheKey, cacheValue, ttl);
|
|
304
|
+
console.log(`[MCP-PROXY] Prefetched and cached tools/list for ${mcpName}`);
|
|
305
|
+
}
|
|
306
|
+
else {
|
|
307
|
+
console.warn(`[MCP-PROXY] Skipping cache for ${mcpName} tools/list — status: ${response.status}, tools parsed: ${tools !== null}`);
|
|
308
|
+
}
|
|
309
|
+
return { tools, fromCache: false };
|
|
280
310
|
}
|
|
281
311
|
catch (e) {
|
|
282
312
|
console.error(`[MCP-PROXY] Failed to prefetch tools for ${mcpName}:`, e);
|
|
@@ -337,6 +367,10 @@ export const mcpProxyHandler = (MCP_SERVERS, oauthAllowAccess) => async (c) => {
|
|
|
337
367
|
const cache = getServerCache(mcpName);
|
|
338
368
|
const storedToken = getStoredOAuthToken(serverConfig.url);
|
|
339
369
|
const { tools, fromCache } = await prefetchAndCacheTools(mcpName, serverConfig.url, cache, storedToken);
|
|
370
|
+
const namesOnly = c.req.query("names") !== undefined;
|
|
371
|
+
if (namesOnly) {
|
|
372
|
+
return c.json(tools?.map((t) => t.name) ?? []);
|
|
373
|
+
}
|
|
340
374
|
return c.json({
|
|
341
375
|
mcpName,
|
|
342
376
|
fromCache,
|
package/package.json
CHANGED