@jsondb-cloud/mcp 1.0.14 → 1.0.17

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/index.js CHANGED
@@ -1,9 +1,33 @@
1
1
  "use strict";
2
+ var __create = Object.create;
3
+ var __defProp = Object.defineProperty;
4
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
5
+ var __getOwnPropNames = Object.getOwnPropertyNames;
6
+ var __getProtoOf = Object.getPrototypeOf;
7
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
8
+ var __copyProps = (to, from, except, desc) => {
9
+ if (from && typeof from === "object" || typeof from === "function") {
10
+ for (let key of __getOwnPropNames(from))
11
+ if (!__hasOwnProp.call(to, key) && key !== except)
12
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
13
+ }
14
+ return to;
15
+ };
16
+ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
17
+ // If the importer is in node compatibility mode or this is not an ESM
18
+ // file that has been converted to a CommonJS file using a Babel-
19
+ // compatible transform (i.e. "__esModule" has not been set), then set
20
+ // "default" to the CommonJS "module.exports" for node compatibility.
21
+ isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
22
+ mod
23
+ ));
2
24
 
3
25
  // src/index.ts
4
26
  var import_mcp2 = require("@modelcontextprotocol/sdk/server/mcp.js");
5
27
  var import_stdio = require("@modelcontextprotocol/sdk/server/stdio.js");
28
+ var import_streamableHttp = require("@modelcontextprotocol/sdk/server/streamableHttp.js");
6
29
  var import_client = require("@jsondb-cloud/client");
30
+ var import_node_http = __toESM(require("http"));
7
31
 
8
32
  // src/tools/documents.ts
9
33
  var import_zod = require("zod");
@@ -1177,6 +1201,171 @@ function registerWebhookTools(server, _db) {
1177
1201
  );
1178
1202
  }
1179
1203
 
1204
+ // src/tools/vectors.ts
1205
+ var import_zod6 = require("zod");
1206
+
1207
+ // src/tools/_helpers.ts
1208
+ function success6(data) {
1209
+ return {
1210
+ content: [{ type: "text", text: JSON.stringify(data, null, 2) }]
1211
+ };
1212
+ }
1213
+ function error6(code, message, suggestion) {
1214
+ return {
1215
+ content: [
1216
+ {
1217
+ type: "text",
1218
+ text: JSON.stringify({ error: { code, message, suggestion } }, null, 2)
1219
+ }
1220
+ ],
1221
+ isError: true
1222
+ };
1223
+ }
1224
+ function resolveEnv3() {
1225
+ return {
1226
+ apiKey: process.env.JSONDB_API_KEY || "",
1227
+ project: process.env.JSONDB_PROJECT || process.env.JSONDB_NAMESPACE || "v1",
1228
+ baseUrl: (process.env.JSONDB_BASE_URL || "https://api.jsondb.cloud").replace(/\/$/, "")
1229
+ };
1230
+ }
1231
+ async function apiFetch(url, apiKey, method = "GET", body) {
1232
+ const opts = {
1233
+ method,
1234
+ headers: {
1235
+ Authorization: `Bearer ${apiKey}`,
1236
+ "Content-Type": "application/json"
1237
+ }
1238
+ };
1239
+ if (body !== void 0 && ["POST", "PUT", "PATCH"].includes(method)) {
1240
+ opts.body = JSON.stringify(body);
1241
+ }
1242
+ const res = await fetch(url, opts);
1243
+ if (!res.ok) {
1244
+ const b = await res.json().catch(() => ({}));
1245
+ throw {
1246
+ status: res.status,
1247
+ message: b.error || res.statusText
1248
+ };
1249
+ }
1250
+ if (res.status === 204) return { ok: true };
1251
+ return res.json();
1252
+ }
1253
+
1254
+ // src/tools/vectors.ts
1255
+ function registerVectorTools(server, _db) {
1256
+ server.tool(
1257
+ "semantic_search",
1258
+ "Search documents using natural language semantic similarity. Returns ranked results with relevance scores. Requires documents to have been stored with embeddings.",
1259
+ {
1260
+ collection: import_zod6.z.string().describe("Collection to search in"),
1261
+ query: import_zod6.z.string().describe("Natural language search query"),
1262
+ limit: import_zod6.z.number().min(1).max(100).default(10).optional().describe("Max results to return"),
1263
+ threshold: import_zod6.z.number().min(0).max(1).default(0.7).optional().describe("Minimum similarity score (0-1)"),
1264
+ filter: import_zod6.z.record(import_zod6.z.unknown()).optional().describe("Additional filter criteria")
1265
+ },
1266
+ async ({ collection, query, limit, threshold, filter }) => {
1267
+ try {
1268
+ const { apiKey, project, baseUrl } = resolveEnv3();
1269
+ const body = { query };
1270
+ if (limit !== void 0) body.limit = limit;
1271
+ if (threshold !== void 0) body.threshold = threshold;
1272
+ if (filter !== void 0) body.filter = filter;
1273
+ const data = await apiFetch(
1274
+ `${baseUrl}/${project}/${collection}/_search`,
1275
+ apiKey,
1276
+ "POST",
1277
+ body
1278
+ );
1279
+ return success6(data);
1280
+ } catch (err) {
1281
+ const e = err;
1282
+ if (e.status === 404) {
1283
+ return error6(
1284
+ "COLLECTION_NOT_FOUND",
1285
+ `Collection '${collection}' not found or has no embeddings.`,
1286
+ `Ensure the collection exists and documents were stored with store_with_embedding. Use list_collections to see available collections.`
1287
+ );
1288
+ }
1289
+ if (e.status === 400) {
1290
+ return error6(
1291
+ "INVALID_SEARCH",
1292
+ e.message || `Invalid search request for collection '${collection}'.`,
1293
+ `Check that the query is a non-empty string and threshold is between 0 and 1.`
1294
+ );
1295
+ }
1296
+ if (e.status === 403) {
1297
+ return error6(
1298
+ "SEARCH_NOT_AVAILABLE",
1299
+ e.message || "Semantic search is not available on your current plan.",
1300
+ `Upgrade your plan at https://jsondb.cloud/dashboard/billing to enable vector search.`
1301
+ );
1302
+ }
1303
+ return error6(
1304
+ "SEARCH_FAILED",
1305
+ e.message || `Failed to search collection '${collection}'.`,
1306
+ `Verify the collection name and that documents have been stored with embeddings.`
1307
+ );
1308
+ }
1309
+ }
1310
+ );
1311
+ server.tool(
1312
+ "store_with_embedding",
1313
+ "Store a document and automatically generate a vector embedding for semantic search. The embed_field specifies which field's text content should be embedded.",
1314
+ {
1315
+ collection: import_zod6.z.string().describe("Collection to store in"),
1316
+ data: import_zod6.z.record(import_zod6.z.unknown()).describe("Document data to store"),
1317
+ embed_field: import_zod6.z.string().describe("Field name whose content will be embedded for search"),
1318
+ id: import_zod6.z.string().optional().describe("Optional custom document ID")
1319
+ },
1320
+ async ({ collection, data, embed_field, id }) => {
1321
+ try {
1322
+ const { apiKey, project, baseUrl } = resolveEnv3();
1323
+ const payload = { ...data, $embed: embed_field };
1324
+ let result;
1325
+ if (id) {
1326
+ result = await apiFetch(
1327
+ `${baseUrl}/${project}/${collection}/${id}`,
1328
+ apiKey,
1329
+ "PUT",
1330
+ payload
1331
+ );
1332
+ } else {
1333
+ result = await apiFetch(`${baseUrl}/${project}/${collection}`, apiKey, "POST", payload);
1334
+ }
1335
+ return success6(result);
1336
+ } catch (err) {
1337
+ const e = err;
1338
+ if (e.status === 400) {
1339
+ return error6(
1340
+ "VALIDATION_ERROR",
1341
+ e.message || `The document failed validation for collection '${collection}'.`,
1342
+ `Ensure the embed_field '${embed_field}' exists in your data and contains text content suitable for embedding.`
1343
+ );
1344
+ }
1345
+ if (e.status === 413) {
1346
+ return error6(
1347
+ "DOCUMENT_TOO_LARGE",
1348
+ `The document exceeds the maximum allowed size.`,
1349
+ `Reduce the document size. Free plans allow up to 16 KB per document, Pro plans allow up to 1 MB.`
1350
+ );
1351
+ }
1352
+ if (e.status === 403) {
1353
+ return error6(
1354
+ "EMBEDDING_NOT_AVAILABLE",
1355
+ e.message || "Embedding generation is not available on your current plan.",
1356
+ `Upgrade your plan at https://jsondb.cloud/dashboard/billing to enable automatic embeddings.`
1357
+ );
1358
+ }
1359
+ return error6(
1360
+ "STORE_EMBEDDING_FAILED",
1361
+ e.message || `Failed to store document with embedding in collection '${collection}'.`,
1362
+ `Check that the collection name is valid, the data is a valid JSON object, and the embed_field references an existing text field.`
1363
+ );
1364
+ }
1365
+ }
1366
+ );
1367
+ }
1368
+
1180
1369
  // src/resources/collections.ts
1181
1370
  var import_mcp = require("@modelcontextprotocol/sdk/server/mcp.js");
1182
1371
  function registerCollectionResources(server, db) {
@@ -1313,9 +1502,52 @@ async function main() {
1313
1502
  registerSchemaTools(server, db);
1314
1503
  registerVersionTools(server, db);
1315
1504
  registerWebhookTools(server, db);
1505
+ registerVectorTools(server, db);
1316
1506
  registerCollectionResources(server, db);
1317
- const transport = new import_stdio.StdioServerTransport();
1318
- await server.connect(transport);
1507
+ const transportType = process.env.JSONDB_MCP_TRANSPORT || "stdio";
1508
+ if (transportType === "http") {
1509
+ const port = parseInt(process.env.JSONDB_MCP_PORT || "3100", 10);
1510
+ const host = process.env.JSONDB_MCP_HOST || "127.0.0.1";
1511
+ const httpServer = import_node_http.default.createServer(async (req, res) => {
1512
+ if (req.method === "GET" && req.url === "/health") {
1513
+ res.writeHead(200, { "Content-Type": "application/json" });
1514
+ res.end(JSON.stringify({ status: "ok" }));
1515
+ return;
1516
+ }
1517
+ if (req.method === "POST" && req.url === "/mcp") {
1518
+ const transport = new import_streamableHttp.StreamableHTTPServerTransport({
1519
+ sessionIdGenerator: void 0
1520
+ });
1521
+ await server.connect(transport);
1522
+ await transport.handleRequest(req, res);
1523
+ return;
1524
+ }
1525
+ if (req.method === "GET" && req.url === "/mcp") {
1526
+ const transport = new import_streamableHttp.StreamableHTTPServerTransport({
1527
+ sessionIdGenerator: void 0
1528
+ });
1529
+ await server.connect(transport);
1530
+ await transport.handleRequest(req, res);
1531
+ return;
1532
+ }
1533
+ if (req.method === "DELETE" && req.url === "/mcp") {
1534
+ const transport = new import_streamableHttp.StreamableHTTPServerTransport({
1535
+ sessionIdGenerator: void 0
1536
+ });
1537
+ await server.connect(transport);
1538
+ await transport.handleRequest(req, res);
1539
+ return;
1540
+ }
1541
+ res.writeHead(404, { "Content-Type": "application/json" });
1542
+ res.end(JSON.stringify({ error: "Not found" }));
1543
+ });
1544
+ httpServer.listen(port, host, () => {
1545
+ console.error(`MCP HTTP server listening on ${host}:${port}`);
1546
+ });
1547
+ } else {
1548
+ const transport = new import_stdio.StdioServerTransport();
1549
+ await server.connect(transport);
1550
+ }
1319
1551
  }
1320
1552
  main().catch((err) => {
1321
1553
  console.error("Fatal error starting MCP server:", err);
package/dist/index.mjs CHANGED
@@ -1,7 +1,9 @@
1
1
  // src/index.ts
2
2
  import { McpServer as McpServer2 } from "@modelcontextprotocol/sdk/server/mcp.js";
3
3
  import { StdioServerTransport } from "@modelcontextprotocol/sdk/server/stdio.js";
4
+ import { StreamableHTTPServerTransport } from "@modelcontextprotocol/sdk/server/streamableHttp.js";
4
5
  import { JsonDB } from "@jsondb-cloud/client";
6
+ import http from "http";
5
7
 
6
8
  // src/tools/documents.ts
7
9
  import { z } from "zod";
@@ -1175,6 +1177,171 @@ function registerWebhookTools(server, _db) {
1175
1177
  );
1176
1178
  }
1177
1179
 
1180
+ // src/tools/vectors.ts
1181
+ import { z as z6 } from "zod";
1182
+
1183
+ // src/tools/_helpers.ts
1184
+ function success6(data) {
1185
+ return {
1186
+ content: [{ type: "text", text: JSON.stringify(data, null, 2) }]
1187
+ };
1188
+ }
1189
+ function error6(code, message, suggestion) {
1190
+ return {
1191
+ content: [
1192
+ {
1193
+ type: "text",
1194
+ text: JSON.stringify({ error: { code, message, suggestion } }, null, 2)
1195
+ }
1196
+ ],
1197
+ isError: true
1198
+ };
1199
+ }
1200
+ function resolveEnv3() {
1201
+ return {
1202
+ apiKey: process.env.JSONDB_API_KEY || "",
1203
+ project: process.env.JSONDB_PROJECT || process.env.JSONDB_NAMESPACE || "v1",
1204
+ baseUrl: (process.env.JSONDB_BASE_URL || "https://api.jsondb.cloud").replace(/\/$/, "")
1205
+ };
1206
+ }
1207
+ async function apiFetch(url, apiKey, method = "GET", body) {
1208
+ const opts = {
1209
+ method,
1210
+ headers: {
1211
+ Authorization: `Bearer ${apiKey}`,
1212
+ "Content-Type": "application/json"
1213
+ }
1214
+ };
1215
+ if (body !== void 0 && ["POST", "PUT", "PATCH"].includes(method)) {
1216
+ opts.body = JSON.stringify(body);
1217
+ }
1218
+ const res = await fetch(url, opts);
1219
+ if (!res.ok) {
1220
+ const b = await res.json().catch(() => ({}));
1221
+ throw {
1222
+ status: res.status,
1223
+ message: b.error || res.statusText
1224
+ };
1225
+ }
1226
+ if (res.status === 204) return { ok: true };
1227
+ return res.json();
1228
+ }
1229
+
1230
+ // src/tools/vectors.ts
1231
+ function registerVectorTools(server, _db) {
1232
+ server.tool(
1233
+ "semantic_search",
1234
+ "Search documents using natural language semantic similarity. Returns ranked results with relevance scores. Requires documents to have been stored with embeddings.",
1235
+ {
1236
+ collection: z6.string().describe("Collection to search in"),
1237
+ query: z6.string().describe("Natural language search query"),
1238
+ limit: z6.number().min(1).max(100).default(10).optional().describe("Max results to return"),
1239
+ threshold: z6.number().min(0).max(1).default(0.7).optional().describe("Minimum similarity score (0-1)"),
1240
+ filter: z6.record(z6.unknown()).optional().describe("Additional filter criteria")
1241
+ },
1242
+ async ({ collection, query, limit, threshold, filter }) => {
1243
+ try {
1244
+ const { apiKey, project, baseUrl } = resolveEnv3();
1245
+ const body = { query };
1246
+ if (limit !== void 0) body.limit = limit;
1247
+ if (threshold !== void 0) body.threshold = threshold;
1248
+ if (filter !== void 0) body.filter = filter;
1249
+ const data = await apiFetch(
1250
+ `${baseUrl}/${project}/${collection}/_search`,
1251
+ apiKey,
1252
+ "POST",
1253
+ body
1254
+ );
1255
+ return success6(data);
1256
+ } catch (err) {
1257
+ const e = err;
1258
+ if (e.status === 404) {
1259
+ return error6(
1260
+ "COLLECTION_NOT_FOUND",
1261
+ `Collection '${collection}' not found or has no embeddings.`,
1262
+ `Ensure the collection exists and documents were stored with store_with_embedding. Use list_collections to see available collections.`
1263
+ );
1264
+ }
1265
+ if (e.status === 400) {
1266
+ return error6(
1267
+ "INVALID_SEARCH",
1268
+ e.message || `Invalid search request for collection '${collection}'.`,
1269
+ `Check that the query is a non-empty string and threshold is between 0 and 1.`
1270
+ );
1271
+ }
1272
+ if (e.status === 403) {
1273
+ return error6(
1274
+ "SEARCH_NOT_AVAILABLE",
1275
+ e.message || "Semantic search is not available on your current plan.",
1276
+ `Upgrade your plan at https://jsondb.cloud/dashboard/billing to enable vector search.`
1277
+ );
1278
+ }
1279
+ return error6(
1280
+ "SEARCH_FAILED",
1281
+ e.message || `Failed to search collection '${collection}'.`,
1282
+ `Verify the collection name and that documents have been stored with embeddings.`
1283
+ );
1284
+ }
1285
+ }
1286
+ );
1287
+ server.tool(
1288
+ "store_with_embedding",
1289
+ "Store a document and automatically generate a vector embedding for semantic search. The embed_field specifies which field's text content should be embedded.",
1290
+ {
1291
+ collection: z6.string().describe("Collection to store in"),
1292
+ data: z6.record(z6.unknown()).describe("Document data to store"),
1293
+ embed_field: z6.string().describe("Field name whose content will be embedded for search"),
1294
+ id: z6.string().optional().describe("Optional custom document ID")
1295
+ },
1296
+ async ({ collection, data, embed_field, id }) => {
1297
+ try {
1298
+ const { apiKey, project, baseUrl } = resolveEnv3();
1299
+ const payload = { ...data, $embed: embed_field };
1300
+ let result;
1301
+ if (id) {
1302
+ result = await apiFetch(
1303
+ `${baseUrl}/${project}/${collection}/${id}`,
1304
+ apiKey,
1305
+ "PUT",
1306
+ payload
1307
+ );
1308
+ } else {
1309
+ result = await apiFetch(`${baseUrl}/${project}/${collection}`, apiKey, "POST", payload);
1310
+ }
1311
+ return success6(result);
1312
+ } catch (err) {
1313
+ const e = err;
1314
+ if (e.status === 400) {
1315
+ return error6(
1316
+ "VALIDATION_ERROR",
1317
+ e.message || `The document failed validation for collection '${collection}'.`,
1318
+ `Ensure the embed_field '${embed_field}' exists in your data and contains text content suitable for embedding.`
1319
+ );
1320
+ }
1321
+ if (e.status === 413) {
1322
+ return error6(
1323
+ "DOCUMENT_TOO_LARGE",
1324
+ `The document exceeds the maximum allowed size.`,
1325
+ `Reduce the document size. Free plans allow up to 16 KB per document, Pro plans allow up to 1 MB.`
1326
+ );
1327
+ }
1328
+ if (e.status === 403) {
1329
+ return error6(
1330
+ "EMBEDDING_NOT_AVAILABLE",
1331
+ e.message || "Embedding generation is not available on your current plan.",
1332
+ `Upgrade your plan at https://jsondb.cloud/dashboard/billing to enable automatic embeddings.`
1333
+ );
1334
+ }
1335
+ return error6(
1336
+ "STORE_EMBEDDING_FAILED",
1337
+ e.message || `Failed to store document with embedding in collection '${collection}'.`,
1338
+ `Check that the collection name is valid, the data is a valid JSON object, and the embed_field references an existing text field.`
1339
+ );
1340
+ }
1341
+ }
1342
+ );
1343
+ }
1344
+
1178
1345
  // src/resources/collections.ts
1179
1346
  import { ResourceTemplate } from "@modelcontextprotocol/sdk/server/mcp.js";
1180
1347
  function registerCollectionResources(server, db) {
@@ -1311,9 +1478,52 @@ async function main() {
1311
1478
  registerSchemaTools(server, db);
1312
1479
  registerVersionTools(server, db);
1313
1480
  registerWebhookTools(server, db);
1481
+ registerVectorTools(server, db);
1314
1482
  registerCollectionResources(server, db);
1315
- const transport = new StdioServerTransport();
1316
- await server.connect(transport);
1483
+ const transportType = process.env.JSONDB_MCP_TRANSPORT || "stdio";
1484
+ if (transportType === "http") {
1485
+ const port = parseInt(process.env.JSONDB_MCP_PORT || "3100", 10);
1486
+ const host = process.env.JSONDB_MCP_HOST || "127.0.0.1";
1487
+ const httpServer = http.createServer(async (req, res) => {
1488
+ if (req.method === "GET" && req.url === "/health") {
1489
+ res.writeHead(200, { "Content-Type": "application/json" });
1490
+ res.end(JSON.stringify({ status: "ok" }));
1491
+ return;
1492
+ }
1493
+ if (req.method === "POST" && req.url === "/mcp") {
1494
+ const transport = new StreamableHTTPServerTransport({
1495
+ sessionIdGenerator: void 0
1496
+ });
1497
+ await server.connect(transport);
1498
+ await transport.handleRequest(req, res);
1499
+ return;
1500
+ }
1501
+ if (req.method === "GET" && req.url === "/mcp") {
1502
+ const transport = new StreamableHTTPServerTransport({
1503
+ sessionIdGenerator: void 0
1504
+ });
1505
+ await server.connect(transport);
1506
+ await transport.handleRequest(req, res);
1507
+ return;
1508
+ }
1509
+ if (req.method === "DELETE" && req.url === "/mcp") {
1510
+ const transport = new StreamableHTTPServerTransport({
1511
+ sessionIdGenerator: void 0
1512
+ });
1513
+ await server.connect(transport);
1514
+ await transport.handleRequest(req, res);
1515
+ return;
1516
+ }
1517
+ res.writeHead(404, { "Content-Type": "application/json" });
1518
+ res.end(JSON.stringify({ error: "Not found" }));
1519
+ });
1520
+ httpServer.listen(port, host, () => {
1521
+ console.error(`MCP HTTP server listening on ${host}:${port}`);
1522
+ });
1523
+ } else {
1524
+ const transport = new StdioServerTransport();
1525
+ await server.connect(transport);
1526
+ }
1317
1527
  }
1318
1528
  main().catch((err) => {
1319
1529
  console.error("Fatal error starting MCP server:", err);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@jsondb-cloud/mcp",
3
- "version": "1.0.14",
3
+ "version": "1.0.17",
4
4
  "description": "MCP (Model Context Protocol) server for jsondb.cloud — lets AI agents interact with your JSON database",
5
5
  "license": "MIT",
6
6
  "main": "dist/index.js",