@mcp-ts/sdk 1.6.2 → 2.0.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 +12 -6
- package/dist/adapters/agui-adapter.d.mts +3 -3
- package/dist/adapters/agui-adapter.d.ts +3 -3
- package/dist/adapters/agui-adapter.js +4 -5
- package/dist/adapters/agui-adapter.js.map +1 -1
- package/dist/adapters/agui-adapter.mjs +4 -5
- package/dist/adapters/agui-adapter.mjs.map +1 -1
- package/dist/adapters/agui-middleware.d.mts +3 -3
- package/dist/adapters/agui-middleware.d.ts +3 -3
- package/dist/adapters/ai-adapter.d.mts +9 -3
- package/dist/adapters/ai-adapter.d.ts +9 -3
- package/dist/adapters/ai-adapter.js +20 -6
- package/dist/adapters/ai-adapter.js.map +1 -1
- package/dist/adapters/ai-adapter.mjs +20 -6
- package/dist/adapters/ai-adapter.mjs.map +1 -1
- package/dist/adapters/langchain-adapter.d.mts +3 -3
- package/dist/adapters/langchain-adapter.d.ts +3 -3
- package/dist/adapters/langchain-adapter.js +9 -6
- package/dist/adapters/langchain-adapter.js.map +1 -1
- package/dist/adapters/langchain-adapter.mjs +9 -6
- package/dist/adapters/langchain-adapter.mjs.map +1 -1
- package/dist/adapters/mastra-adapter.d.mts +1 -1
- package/dist/adapters/mastra-adapter.d.ts +1 -1
- package/dist/adapters/mastra-adapter.js +5 -1
- package/dist/adapters/mastra-adapter.js.map +1 -1
- package/dist/adapters/mastra-adapter.mjs +5 -1
- package/dist/adapters/mastra-adapter.mjs.map +1 -1
- package/dist/bin/mcp-ts.js +7 -1
- package/dist/bin/mcp-ts.js.map +1 -1
- package/dist/bin/mcp-ts.mjs +7 -1
- package/dist/bin/mcp-ts.mjs.map +1 -1
- package/dist/client/index.d.mts +2 -2
- package/dist/client/index.d.ts +2 -2
- package/dist/client/index.js +9 -13
- package/dist/client/index.js.map +1 -1
- package/dist/client/index.mjs +9 -13
- package/dist/client/index.mjs.map +1 -1
- package/dist/client/react.d.mts +7 -7
- package/dist/client/react.d.ts +7 -7
- package/dist/client/react.js +15 -19
- package/dist/client/react.js.map +1 -1
- package/dist/client/react.mjs +15 -19
- package/dist/client/react.mjs.map +1 -1
- package/dist/client/vue.d.mts +7 -7
- package/dist/client/vue.d.ts +7 -7
- package/dist/client/vue.js +14 -18
- package/dist/client/vue.js.map +1 -1
- package/dist/client/vue.mjs +14 -18
- package/dist/client/vue.mjs.map +1 -1
- package/dist/{index-DhA-OEAe.d.ts → index-C9gvpxy5.d.ts} +5 -5
- package/dist/{index-bFL4ZF2N.d.mts → index-eaH14_5u.d.mts} +5 -5
- package/dist/index.d.mts +6 -6
- package/dist/index.d.ts +6 -6
- package/dist/index.js +616 -370
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +615 -370
- package/dist/index.mjs.map +1 -1
- package/dist/{multi-session-client-CHE8QpVE.d.ts → multi-session-client-BYtguGJm.d.ts} +22 -22
- package/dist/{multi-session-client-CQsRbxYI.d.mts → multi-session-client-DYNe6az3.d.mts} +22 -22
- package/dist/server/index.d.mts +31 -34
- package/dist/server/index.d.ts +31 -34
- package/dist/server/index.js +531 -256
- package/dist/server/index.js.map +1 -1
- package/dist/server/index.mjs +530 -256
- package/dist/server/index.mjs.map +1 -1
- package/dist/shared/index.d.mts +5 -5
- package/dist/shared/index.d.ts +5 -5
- package/dist/shared/index.js +76 -101
- package/dist/shared/index.js.map +1 -1
- package/dist/shared/index.mjs +76 -101
- package/dist/shared/index.mjs.map +1 -1
- package/dist/{tool-router-Dh2804tM.d.ts → tool-router-Ddtybmr0.d.ts} +71 -73
- package/dist/{tool-router-BVaV1udm.d.mts → tool-router-Dnd6IOKC.d.mts} +71 -73
- package/dist/{types-rIuN1CQi.d.mts → types-BCAG20P6.d.mts} +4 -4
- package/dist/{types-rIuN1CQi.d.ts → types-BCAG20P6.d.ts} +4 -4
- package/dist/{utils-0qmYrqoa.d.mts → utils-DELRKQPU.d.mts} +1 -1
- package/dist/{utils-0qmYrqoa.d.ts → utils-DELRKQPU.d.ts} +1 -1
- package/migrations/neon/20260513010000_install_mcp_sessions.sql +69 -0
- package/migrations/neon/20260513020000_add_session_cleanup_cron.sql +35 -0
- package/{supabase/migrations → migrations/supabase}/20260330195700_install_mcp_sessions.sql +7 -9
- package/package.json +14 -5
- package/src/adapters/ai-adapter.ts +30 -1
- package/src/adapters/langchain-adapter.ts +6 -2
- package/src/adapters/mastra-adapter.ts +6 -2
- package/src/bin/mcp-ts.ts +8 -1
- package/src/client/core/app-host.ts +1 -1
- package/src/client/core/sse-client.ts +12 -14
- package/src/client/core/types.ts +1 -1
- package/src/client/react/use-mcp-apps.tsx +1 -1
- package/src/client/react/use-mcp.ts +11 -11
- package/src/client/vue/use-mcp.ts +10 -10
- package/src/server/handlers/nextjs-handler.ts +18 -15
- package/src/server/handlers/sse-handler.ts +29 -29
- package/src/server/index.ts +1 -1
- package/src/server/mcp/multi-session-client.ts +17 -17
- package/src/server/mcp/oauth-client.ts +37 -37
- package/src/server/mcp/storage-oauth-provider.ts +17 -17
- package/src/server/storage/file-backend.ts +25 -25
- package/src/server/storage/index.ts +67 -10
- package/src/server/storage/memory-backend.ts +34 -34
- package/src/server/storage/neon-backend.ts +281 -0
- package/src/server/storage/redis-backend.ts +64 -64
- package/src/server/storage/sqlite-backend.ts +33 -33
- package/src/server/storage/supabase-backend.ts +23 -24
- package/src/server/storage/types.ts +18 -21
- package/src/shared/errors.ts +1 -1
- package/src/shared/index.ts +1 -2
- package/src/shared/meta-tools.ts +4 -6
- package/src/shared/schema-compressor.ts +2 -42
- package/src/shared/tool-index.ts +89 -84
- package/src/shared/tool-router.ts +0 -24
- package/src/shared/types.ts +4 -4
- /package/{supabase/migrations → migrations/supabase}/20260421010000_add_session_cleanup_cron.sql +0 -0
package/dist/shared/index.mjs
CHANGED
|
@@ -154,7 +154,7 @@ var ToolExecutionError = class extends McpError {
|
|
|
154
154
|
};
|
|
155
155
|
var RpcErrorCodes = {
|
|
156
156
|
EXECUTION_ERROR: "EXECUTION_ERROR",
|
|
157
|
-
|
|
157
|
+
MISSING_USER_ID: "MISSING_USER_ID",
|
|
158
158
|
UNAUTHORIZED: "UNAUTHORIZED",
|
|
159
159
|
NO_CONNECTION: "NO_CONNECTION",
|
|
160
160
|
UNKNOWN_METHOD: "UNKNOWN_METHOD",
|
|
@@ -211,17 +211,7 @@ function findToolByName(connections, toolName) {
|
|
|
211
211
|
}
|
|
212
212
|
|
|
213
213
|
// src/shared/tool-index.ts
|
|
214
|
-
var
|
|
215
|
-
function classifyChar(ch) {
|
|
216
|
-
const code = ch.charCodeAt(0);
|
|
217
|
-
if (code <= 32 || ch === "{" || ch === "}" || ch === "[" || ch === "]" || ch === ":" || ch === ",") return 1;
|
|
218
|
-
if (code >= 33 && code <= 47) return 1.5;
|
|
219
|
-
if (code >= 48 && code <= 57) return 2;
|
|
220
|
-
if (code >= 65 && code <= 90) return 3.5;
|
|
221
|
-
if (code >= 97 && code <= 122) return 4;
|
|
222
|
-
return 2.5;
|
|
223
|
-
}
|
|
224
|
-
var ToolIndex = class _ToolIndex {
|
|
214
|
+
var ToolIndex = class {
|
|
225
215
|
constructor(options = {}) {
|
|
226
216
|
/** All indexed tools keyed by name (supports duplicates). */
|
|
227
217
|
__publicField(this, "tools", /* @__PURE__ */ new Map());
|
|
@@ -239,8 +229,6 @@ var ToolIndex = class _ToolIndex {
|
|
|
239
229
|
__publicField(this, "docLengths", /* @__PURE__ */ new Map());
|
|
240
230
|
/** BM25: average document length across the entire index. */
|
|
241
231
|
__publicField(this, "avgDocLength", 0);
|
|
242
|
-
/** Cached total estimated token cost across all indexed tools. */
|
|
243
|
-
__publicField(this, "totalTokenCost", 0);
|
|
244
232
|
__publicField(this, "options");
|
|
245
233
|
this.options = {
|
|
246
234
|
embedFn: options.embedFn ?? void 0,
|
|
@@ -263,7 +251,6 @@ var ToolIndex = class _ToolIndex {
|
|
|
263
251
|
this.embeddings.clear();
|
|
264
252
|
this.docLengths.clear();
|
|
265
253
|
this.avgDocLength = 0;
|
|
266
|
-
this.totalTokenCost = 0;
|
|
267
254
|
const allTokenSets = /* @__PURE__ */ new Map();
|
|
268
255
|
let totalLength = 0;
|
|
269
256
|
for (const tool of tools) {
|
|
@@ -272,19 +259,17 @@ var ToolIndex = class _ToolIndex {
|
|
|
272
259
|
this.tools.set(tool.name, []);
|
|
273
260
|
}
|
|
274
261
|
this.tools.get(tool.name).push(tool);
|
|
275
|
-
const estimatedTokens = _ToolIndex.estimateTokens(tool);
|
|
276
262
|
this.toolSummaries.set(docKey, {
|
|
277
263
|
name: tool.name,
|
|
278
264
|
description: tool.description ?? "",
|
|
279
265
|
serverName: tool.serverName,
|
|
280
266
|
serverId: tool.serverId,
|
|
281
|
-
sessionId: tool.sessionId
|
|
282
|
-
estimatedTokens
|
|
267
|
+
sessionId: tool.sessionId
|
|
283
268
|
});
|
|
284
|
-
this.
|
|
285
|
-
const text =
|
|
269
|
+
const rawText = this.buildSearchableText(tool);
|
|
270
|
+
const text = rawText.toLowerCase();
|
|
286
271
|
this.searchTexts.set(docKey, text);
|
|
287
|
-
const tokens = this.tokenize(
|
|
272
|
+
const tokens = this.tokenize(rawText);
|
|
288
273
|
const tf = /* @__PURE__ */ new Map();
|
|
289
274
|
const uniqueTokens = /* @__PURE__ */ new Set();
|
|
290
275
|
for (const tok of tokens) {
|
|
@@ -548,30 +533,6 @@ var ToolIndex = class _ToolIndex {
|
|
|
548
533
|
}
|
|
549
534
|
return count;
|
|
550
535
|
}
|
|
551
|
-
/** Total estimated token cost of all indexed tool schemas. */
|
|
552
|
-
getTotalTokenCost() {
|
|
553
|
-
return this.totalTokenCost;
|
|
554
|
-
}
|
|
555
|
-
// -----------------------------------------------------------------------
|
|
556
|
-
// Static Helpers
|
|
557
|
-
// -----------------------------------------------------------------------
|
|
558
|
-
/**
|
|
559
|
-
* Estimate token count of a tool's full schema (name + description + inputSchema).
|
|
560
|
-
*
|
|
561
|
-
* Uses character-class weighted counting calibrated against cl100k_base.
|
|
562
|
-
* Accuracy is typically within ±10% for JSON Schema payloads.
|
|
563
|
-
*/
|
|
564
|
-
static estimateTokens(tool) {
|
|
565
|
-
const parts = [tool.name];
|
|
566
|
-
if (tool.description) parts.push(tool.description);
|
|
567
|
-
if (tool.inputSchema) parts.push(JSON.stringify(tool.inputSchema));
|
|
568
|
-
const text = parts.join(" ");
|
|
569
|
-
let weightedLen = 0;
|
|
570
|
-
for (let i = 0; i < text.length; i++) {
|
|
571
|
-
weightedLen += 1 / classifyChar(text[i]);
|
|
572
|
-
}
|
|
573
|
-
return Math.ceil(weightedLen / (1 / CALIBRATION_DIVISOR));
|
|
574
|
-
}
|
|
575
536
|
// -----------------------------------------------------------------------
|
|
576
537
|
// Internals
|
|
577
538
|
// -----------------------------------------------------------------------
|
|
@@ -580,18 +541,74 @@ var ToolIndex = class _ToolIndex {
|
|
|
580
541
|
const parts = [tool.name];
|
|
581
542
|
if (tool.description) parts.push(tool.description);
|
|
582
543
|
if (tool.inputSchema && typeof tool.inputSchema === "object") {
|
|
583
|
-
|
|
584
|
-
|
|
585
|
-
|
|
586
|
-
|
|
587
|
-
|
|
588
|
-
|
|
589
|
-
|
|
590
|
-
|
|
544
|
+
this.collectSchemaSearchText(tool.inputSchema, parts);
|
|
545
|
+
}
|
|
546
|
+
return parts.join(" ");
|
|
547
|
+
}
|
|
548
|
+
/** Recursively collect JSON Schema argument names and descriptions. */
|
|
549
|
+
collectSchemaSearchText(schema, parts, seen = /* @__PURE__ */ new WeakSet()) {
|
|
550
|
+
if (!schema || typeof schema !== "object") return;
|
|
551
|
+
if (seen.has(schema)) return;
|
|
552
|
+
seen.add(schema);
|
|
553
|
+
if (Array.isArray(schema)) {
|
|
554
|
+
for (const item of schema) {
|
|
555
|
+
this.collectSchemaSearchText(item, parts, seen);
|
|
556
|
+
}
|
|
557
|
+
return;
|
|
558
|
+
}
|
|
559
|
+
const schemaObject = schema;
|
|
560
|
+
this.pushStringValue(schemaObject.description, parts);
|
|
561
|
+
this.pushStringValue(schemaObject.title, parts);
|
|
562
|
+
const properties = schemaObject.properties;
|
|
563
|
+
if (properties && typeof properties === "object" && !Array.isArray(properties)) {
|
|
564
|
+
for (const [propertyName, propertySchema] of Object.entries(properties)) {
|
|
565
|
+
parts.push(propertyName);
|
|
566
|
+
this.collectSchemaSearchText(propertySchema, parts, seen);
|
|
567
|
+
}
|
|
568
|
+
}
|
|
569
|
+
const patternProperties = schemaObject.patternProperties;
|
|
570
|
+
if (patternProperties && typeof patternProperties === "object" && !Array.isArray(patternProperties)) {
|
|
571
|
+
for (const [propertyPattern, propertySchema] of Object.entries(patternProperties)) {
|
|
572
|
+
parts.push(propertyPattern);
|
|
573
|
+
this.collectSchemaSearchText(propertySchema, parts, seen);
|
|
574
|
+
}
|
|
575
|
+
}
|
|
576
|
+
const dependentSchemas = schemaObject.dependentSchemas;
|
|
577
|
+
if (dependentSchemas && typeof dependentSchemas === "object" && !Array.isArray(dependentSchemas)) {
|
|
578
|
+
for (const [propertyName, dependentSchema] of Object.entries(dependentSchemas)) {
|
|
579
|
+
parts.push(propertyName);
|
|
580
|
+
this.collectSchemaSearchText(dependentSchema, parts, seen);
|
|
581
|
+
}
|
|
582
|
+
}
|
|
583
|
+
for (const key of [
|
|
584
|
+
"items",
|
|
585
|
+
"additionalProperties",
|
|
586
|
+
"contains",
|
|
587
|
+
"propertyNames",
|
|
588
|
+
"if",
|
|
589
|
+
"then",
|
|
590
|
+
"else",
|
|
591
|
+
"not"
|
|
592
|
+
]) {
|
|
593
|
+
this.collectSchemaSearchText(schemaObject[key], parts, seen);
|
|
594
|
+
}
|
|
595
|
+
for (const key of ["allOf", "anyOf", "oneOf", "prefixItems"]) {
|
|
596
|
+
this.collectSchemaSearchText(schemaObject[key], parts, seen);
|
|
597
|
+
}
|
|
598
|
+
for (const key of ["$defs", "definitions"]) {
|
|
599
|
+
const definitions = schemaObject[key];
|
|
600
|
+
if (definitions && typeof definitions === "object" && !Array.isArray(definitions)) {
|
|
601
|
+
for (const [definitionName, definitionSchema] of Object.entries(definitions)) {
|
|
602
|
+
parts.push(definitionName);
|
|
603
|
+
this.collectSchemaSearchText(definitionSchema, parts, seen);
|
|
591
604
|
}
|
|
592
605
|
}
|
|
593
606
|
}
|
|
594
|
-
|
|
607
|
+
}
|
|
608
|
+
pushStringValue(value, parts) {
|
|
609
|
+
if (typeof value === "string" && value.trim()) {
|
|
610
|
+
parts.push(value);
|
|
611
|
+
}
|
|
595
612
|
}
|
|
596
613
|
getDocumentKey(tool) {
|
|
597
614
|
return `${tool.sessionId}::${tool.serverId}::${tool.name}`;
|
|
@@ -612,7 +629,7 @@ var ToolIndex = class _ToolIndex {
|
|
|
612
629
|
}
|
|
613
630
|
/** Simple whitespace + camelCase + snake_case tokenizer. */
|
|
614
631
|
tokenize(text) {
|
|
615
|
-
return text.replace(/([a-z])([A-Z])/g, "$1 $2").replace(/[_-]/g, " ").replace(/[^a-z0-9\s]/g, "").split(/\s+/).filter((t) => t.length > 1);
|
|
632
|
+
return text.replace(/([a-z])([A-Z])/g, "$1 $2").replace(/[_-]/g, " ").toLowerCase().replace(/[^a-z0-9\s]/g, "").split(/\s+/).filter((t) => t.length > 1);
|
|
616
633
|
}
|
|
617
634
|
/** Cosine similarity between two vectors. */
|
|
618
635
|
cosineSimilarity(a, b) {
|
|
@@ -667,27 +684,6 @@ var SchemaCompressor = class _SchemaCompressor {
|
|
|
667
684
|
const limited = options?.maxTools ? tools.slice(0, options.maxTools) : tools;
|
|
668
685
|
return limited.map((t) => _SchemaCompressor.toCompact(t));
|
|
669
686
|
}
|
|
670
|
-
/**
|
|
671
|
-
* Estimate token savings from using compact vs full tool schemas.
|
|
672
|
-
*/
|
|
673
|
-
static estimateSavings(tools) {
|
|
674
|
-
let fullTokens = 0;
|
|
675
|
-
let compactTokens = 0;
|
|
676
|
-
for (const tool of tools) {
|
|
677
|
-
fullTokens += ToolIndex.estimateTokens(tool);
|
|
678
|
-
const compact = _SchemaCompressor.toCompact(tool);
|
|
679
|
-
const text = [compact.name, compact.description ?? "", compact.parameterHint ?? ""].join(" ");
|
|
680
|
-
compactTokens += Math.ceil(text.length / 4);
|
|
681
|
-
}
|
|
682
|
-
const saved = fullTokens - compactTokens;
|
|
683
|
-
const pct = fullTokens > 0 ? (saved / fullTokens * 100).toFixed(1) : "0.0";
|
|
684
|
-
return {
|
|
685
|
-
fullTokens,
|
|
686
|
-
compactTokens,
|
|
687
|
-
savedTokens: saved,
|
|
688
|
-
savingsPercent: `${pct}%`
|
|
689
|
-
};
|
|
690
|
-
}
|
|
691
687
|
};
|
|
692
688
|
|
|
693
689
|
// src/shared/meta-tools.ts
|
|
@@ -883,7 +879,7 @@ async function executeMetaTool(toolName, args, router, callToolFn) {
|
|
|
883
879
|
const lines = [];
|
|
884
880
|
if (found.length > 0) {
|
|
885
881
|
lines.push(...found.map(
|
|
886
|
-
(t, i) => `${i + 1}. **${t.name}** (
|
|
882
|
+
(t, i) => `${i + 1}. **${t.name}** (serverName: ${t.serverName}, serverId: ${t.serverId})
|
|
887
883
|
${t.description}`
|
|
888
884
|
));
|
|
889
885
|
}
|
|
@@ -911,7 +907,7 @@ async function executeMetaTool(toolName, args, router, callToolFn) {
|
|
|
911
907
|
serverName: query || void 0
|
|
912
908
|
});
|
|
913
909
|
const text = servers.length === 0 ? "No connected servers found." : servers.map(
|
|
914
|
-
(server, i) => `${i + 1}. **${server.serverName}** (serverId: ${server.serverId}
|
|
910
|
+
(server, i) => `${i + 1}. **${server.serverName}** (serverId: ${server.serverId})
|
|
915
911
|
Tool count: ${server.toolCount}`
|
|
916
912
|
).join("\n");
|
|
917
913
|
return {
|
|
@@ -1018,9 +1014,8 @@ async function executeMetaTool(toolName, args, router, callToolFn) {
|
|
|
1018
1014
|
}
|
|
1019
1015
|
function formatToolSummaries(tools) {
|
|
1020
1016
|
return tools.map(
|
|
1021
|
-
(t, i) => `${i + 1}. **${t.name}** (
|
|
1022
|
-
${t.description}
|
|
1023
|
-
Estimated tokens: ${t.estimatedTokens}`
|
|
1017
|
+
(t, i) => `${i + 1}. **${t.name}** (serverName: ${t.serverName}, serverId: ${t.serverId})
|
|
1018
|
+
${t.description}`
|
|
1024
1019
|
);
|
|
1025
1020
|
}
|
|
1026
1021
|
function isMetaTool(toolName) {
|
|
@@ -1163,26 +1158,6 @@ var ToolRouter = class {
|
|
|
1163
1158
|
getActiveGroups() {
|
|
1164
1159
|
return [...this.activeGroups];
|
|
1165
1160
|
}
|
|
1166
|
-
// -----------------------------------------------------------------------
|
|
1167
|
-
// Stats & Introspection
|
|
1168
|
-
// -----------------------------------------------------------------------
|
|
1169
|
-
/** Total token cost of all tools if loaded without filtering. */
|
|
1170
|
-
getTotalTokenCost() {
|
|
1171
|
-
return this.index.getTotalTokenCost();
|
|
1172
|
-
}
|
|
1173
|
-
/** Estimate token cost of the currently filtered tool set. */
|
|
1174
|
-
async getFilteredTokenCost() {
|
|
1175
|
-
const tools = await this.getFilteredTools();
|
|
1176
|
-
let total = 0;
|
|
1177
|
-
for (const tool of tools) {
|
|
1178
|
-
total += ToolIndex.estimateTokens(tool);
|
|
1179
|
-
}
|
|
1180
|
-
return total;
|
|
1181
|
-
}
|
|
1182
|
-
/** Get compression stats showing savings from current strategy. */
|
|
1183
|
-
getCompressionStats() {
|
|
1184
|
-
return SchemaCompressor.estimateSavings(this.allTools);
|
|
1185
|
-
}
|
|
1186
1161
|
/** Number of total indexed tools. */
|
|
1187
1162
|
get totalToolCount() {
|
|
1188
1163
|
return this.allTools.length;
|