@momeidejiyi/dsh-general-mcp 0.2.1 → 0.3.1

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/lib/index.js CHANGED
@@ -1,4 +1,4 @@
1
- // @momeidejiyi/dsh-general-mcp@0.2.1 (generated)
1
+ // @momeidejiyi/dsh-general-mcp@0.3.1 (generated)
2
2
 
3
3
  // packages/general-mcp/src/index.ts
4
4
  import { appendFileSync } from "node:fs";
@@ -96,7 +96,7 @@ function folderFromMeta(tool) {
96
96
  }
97
97
  function folderFromName(rawName) {
98
98
  const parts = rawName.split(/[./]/).filter(Boolean);
99
- return parts.length > 1 ? parts.slice(0, -1) : ["uncategorized"];
99
+ return parts.length > 1 ? parts.slice(0, -1) : [];
100
100
  }
101
101
  function shortHash(value) {
102
102
  let hash = 2166136261;
@@ -825,7 +825,7 @@ function apply(ctx, config) {
825
825
  }
826
826
  }
827
827
  const enabled = item.enabled !== false;
828
- const serverTimeout = typeof item.timeoutMs === "number" && Number.isFinite(item.timeoutMs) ? Math.min(Math.max(Math.round(item.timeoutMs), 1e3), 6e5) : 0;
828
+ const serverTimeout = typeof item.timeoutMs === "number" && Number.isFinite(item.timeoutMs) ? item.timeoutMs === 0 ? 0 : Math.min(Math.max(Math.round(item.timeoutMs), 1e3), 6e5) : 0;
829
829
  const changed = entry.endpoint !== (item.endpoint ?? "") || JSON.stringify(entry.headers) !== JSON.stringify(headers) || entry.enabled !== enabled || entry.timeoutMs !== serverTimeout;
830
830
  entry.endpoint = item.endpoint ?? "";
831
831
  entry.headers = headers;
@@ -1053,27 +1053,18 @@ function installRoutes(ctx) {
1053
1053
  return { editor, entry };
1054
1054
  };
1055
1055
  try {
1056
- webServer.register({
1057
- kind: "exact",
1058
- path: "/api/general-mcp/catalog",
1059
- resource: async (request) => {
1060
- const hdr = {
1061
- authorization: request.headers?.get?.("authorization") ?? "",
1062
- origin: request.headers?.get?.("origin") ?? "",
1063
- host: request.headers?.get?.("host") ?? ""
1064
- };
1065
- const allowed = cors(hdr);
1066
- if (request.method === "OPTIONS") {
1067
- return new Response(null, { status: 204, headers: { ...allowed, "cache-control": "no-store" } });
1068
- }
1069
- if (!trusted(hdr)) {
1070
- return new Response("unauthorized", { status: 401, headers: { ...allowed, "cache-control": "no-store" } });
1071
- }
1072
- const url = new URL(request.url);
1073
- const raw = url.searchParams.get("since");
1074
- const since = raw !== null && /^\d+$/.test(raw) ? Number(raw) : -1;
1075
- if (since === live.version) return new Response(null, { status: 304, headers: { ...allowed, "cache-control": "no-store" } });
1076
- return new Response(JSON.stringify({
1056
+ const catalogPayload = (method, rawUrl, headers) => {
1057
+ const allowed = cors({ headers });
1058
+ if (method === "OPTIONS") return { status: 204, headers: { ...allowed }, body: void 0 };
1059
+ if (!trusted({ headers })) return { status: 401, headers: { ...allowed }, body: "unauthorized" };
1060
+ const url = new URL(rawUrl, "http://localhost");
1061
+ const raw = url.searchParams.get("since");
1062
+ const since = raw !== null && /^\d+$/.test(raw) ? Number(raw) : -1;
1063
+ if (since === live.version) return { status: 304, headers: { ...allowed }, body: void 0 };
1064
+ return {
1065
+ status: 200,
1066
+ headers: { ...allowed, "content-type": "application/json; charset=utf-8" },
1067
+ body: JSON.stringify({
1077
1068
  version: live.version,
1078
1069
  timeoutMs: live.timeoutMs,
1079
1070
  retryIntervalMs: live.retryIntervalMs,
@@ -1093,7 +1084,37 @@ function installRoutes(ctx) {
1093
1084
  tools: entry.catalog,
1094
1085
  settings: entry.settings
1095
1086
  }))
1096
- }), { status: 200, headers: { ...allowed, "content-type": "application/json; charset=utf-8", "cache-control": "no-store" } });
1087
+ })
1088
+ };
1089
+ };
1090
+ webServer.register({
1091
+ kind: "exact",
1092
+ path: "/api/general-mcp/catalog",
1093
+ handler: (req, res) => {
1094
+ const out = catalogPayload(
1095
+ req.method ?? "GET",
1096
+ req.url ?? "/",
1097
+ {
1098
+ authorization: String(req.headers?.authorization ?? ""),
1099
+ origin: String(req.headers?.origin ?? ""),
1100
+ host: String(req.headers?.host ?? "")
1101
+ }
1102
+ );
1103
+ res.writeHead(out.status, { "cache-control": "no-store", ...out.headers });
1104
+ if (out.body === void 0) res.end();
1105
+ else res.end(out.body);
1106
+ },
1107
+ resource: async (request) => {
1108
+ const out = catalogPayload(
1109
+ request.method,
1110
+ request.url,
1111
+ {
1112
+ authorization: request.headers?.get?.("authorization") ?? "",
1113
+ origin: request.headers?.get?.("origin") ?? "",
1114
+ host: request.headers?.get?.("host") ?? ""
1115
+ }
1116
+ );
1117
+ return new Response(out.body ?? null, { status: out.status, headers: { "cache-control": "no-store", ...out.headers } });
1097
1118
  }
1098
1119
  });
1099
1120
  webServer.register({
@@ -1179,12 +1200,18 @@ function installRoutes(ctx) {
1179
1200
  for (const item of incoming) {
1180
1201
  const name2 = String(item.name ?? "general");
1181
1202
  if (removed.has(name2)) continue;
1203
+ if (body.createOnly === true && byName.get(name2) !== void 0) {
1204
+ throw new Error("\u7F51\u5173 " + name2 + " \u5DF2\u5B58\u5728");
1205
+ }
1182
1206
  const previous = byName.get(name2) ?? {};
1183
1207
  const hasEndpoint = typeof item.endpoint === "string";
1208
+ if (!hasEndpoint && byName.get(name2) === void 0) {
1209
+ throw new Error("\u7F51\u5173 " + name2 + " \u4E0D\u5B58\u5728\uFF0C\u6539\u52A8\u672A\u4FDD\u5B58");
1210
+ }
1184
1211
  const endpoint2 = hasEndpoint ? item.endpoint.trim() : previous.endpoint ?? "";
1185
1212
  const sameEndpoint = (previous.endpoint ?? "") === endpoint2;
1186
1213
  const next2 = { ...previous, ...item, name: name2, endpoint: endpoint2 };
1187
- if (!sameEndpoint) delete next2.tools;
1214
+ void sameEndpoint;
1188
1215
  if (item.headersPatch !== null && typeof item.headersPatch === "object" && !Array.isArray(item.headersPatch)) {
1189
1216
  const headers2 = { ...previous.headers ?? {} };
1190
1217
  for (const [key, value] of Object.entries(item.headersPatch)) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@momeidejiyi/dsh-general-mcp",
3
- "version": "0.2.1",
3
+ "version": "0.3.1",
4
4
  "description": "Single-endpoint MCP bridge for DSH: connects one gateway and registers its tools.",
5
5
  "type": "module",
6
6
  "main": "lib/index.js",
package/src/catalog.ts CHANGED
@@ -146,9 +146,14 @@ function folderFromMeta(tool: McpTool): string[] | undefined {
146
146
  return undefined
147
147
  }
148
148
 
149
+ /**
150
+ * A name like "github.create_issue" implies a folder; a flat name implies none.
151
+ * Returning [] keeps those tools directly under their gateway card instead of
152
+ * dumping every server's tools into one "uncategorized" bucket.
153
+ */
149
154
  function folderFromName(rawName: string): string[] {
150
155
  const parts = rawName.split(/[./]/).filter(Boolean)
151
- return parts.length > 1 ? parts.slice(0, -1) : ['uncategorized']
156
+ return parts.length > 1 ? parts.slice(0, -1) : []
152
157
  }
153
158
 
154
159
  function shortHash(value: string): string {
package/src/index.ts CHANGED
@@ -211,7 +211,7 @@ export function apply(ctx: any, config?: Config): void {
211
211
  }
212
212
  const enabled = item.enabled !== false
213
213
  const serverTimeout = typeof item.timeoutMs === 'number' && Number.isFinite(item.timeoutMs)
214
- ? Math.min(Math.max(Math.round(item.timeoutMs), 1_000), 600_000)
214
+ ? (item.timeoutMs === 0 ? 0 : Math.min(Math.max(Math.round(item.timeoutMs), 1_000), 600_000))
215
215
  : 0
216
216
  const changed = entry.endpoint !== (item.endpoint ?? '') ||
217
217
  JSON.stringify(entry.headers) !== JSON.stringify(headers) ||
@@ -456,27 +456,20 @@ function installRoutes(ctx: any): void {
456
456
  }
457
457
 
458
458
  try {
459
- webServer.register({
460
- kind: 'exact',
461
- path: '/api/general-mcp/catalog',
462
- resource: async (request: any) => {
463
- const hdr = {
464
- authorization: request.headers?.get?.('authorization') ?? '',
465
- origin: request.headers?.get?.('origin') ?? '',
466
- host: request.headers?.get?.('host') ?? '',
467
- }
468
- const allowed = cors(hdr)
469
- if (request.method === 'OPTIONS') {
470
- return new Response(null, { status: 204, headers: { ...allowed, 'cache-control': 'no-store' } })
471
- }
472
- if (!trusted(hdr)) {
473
- return new Response('unauthorized', { status: 401, headers: { ...allowed, 'cache-control': 'no-store' } })
474
- }
475
- const url = new URL(request.url)
476
- const raw = url.searchParams.get('since')
477
- const since = raw !== null && /^\d+$/.test(raw) ? Number(raw) : -1
478
- if (since === live.version) return new Response(null, { status: 304, headers: { ...allowed, 'cache-control': 'no-store' } })
479
- return new Response(JSON.stringify({
459
+ // Older DSH builds only dispatch "handler" routes; newer ones prefer the
460
+ // Fetch-style "resource". Register both so the catalog works either way.
461
+ const catalogPayload = (method: string, rawUrl: string, headers: Record<string, string>) => {
462
+ const allowed = cors({ headers })
463
+ if (method === 'OPTIONS') return { status: 204, headers: { ...allowed }, body: undefined as string | undefined }
464
+ if (!trusted({ headers })) return { status: 401, headers: { ...allowed }, body: 'unauthorized' }
465
+ const url = new URL(rawUrl, 'http://localhost')
466
+ const raw = url.searchParams.get('since')
467
+ const since = raw !== null && /^\d+$/.test(raw) ? Number(raw) : -1
468
+ if (since === live.version) return { status: 304, headers: { ...allowed }, body: undefined }
469
+ return {
470
+ status: 200,
471
+ headers: { ...allowed, 'content-type': 'application/json; charset=utf-8' },
472
+ body: JSON.stringify({
480
473
  version: live.version,
481
474
  timeoutMs: live.timeoutMs,
482
475
  retryIntervalMs: live.retryIntervalMs,
@@ -496,7 +489,38 @@ function installRoutes(ctx: any): void {
496
489
  tools: entry.catalog,
497
490
  settings: entry.settings,
498
491
  })),
499
- }), { status: 200, headers: { ...allowed, 'content-type': 'application/json; charset=utf-8', 'cache-control': 'no-store' } })
492
+ }),
493
+ }
494
+ }
495
+
496
+ webServer.register({
497
+ kind: 'exact',
498
+ path: '/api/general-mcp/catalog',
499
+ handler: (req: any, res: any) => {
500
+ const out = catalogPayload(
501
+ req.method ?? 'GET',
502
+ req.url ?? '/',
503
+ {
504
+ authorization: String(req.headers?.authorization ?? ''),
505
+ origin: String(req.headers?.origin ?? ''),
506
+ host: String(req.headers?.host ?? ''),
507
+ },
508
+ )
509
+ res.writeHead(out.status, { 'cache-control': 'no-store', ...out.headers })
510
+ if (out.body === undefined) res.end()
511
+ else res.end(out.body)
512
+ },
513
+ resource: async (request: any) => {
514
+ const out = catalogPayload(
515
+ request.method,
516
+ request.url,
517
+ {
518
+ authorization: request.headers?.get?.('authorization') ?? '',
519
+ origin: request.headers?.get?.('origin') ?? '',
520
+ host: request.headers?.get?.('host') ?? '',
521
+ },
522
+ )
523
+ return new Response(out.body ?? null, { status: out.status, headers: { 'cache-control': 'no-store', ...out.headers } })
500
524
  },
501
525
  })
502
526
 
@@ -577,14 +601,23 @@ function installRoutes(ctx: any): void {
577
601
  for (const item of incoming as any[]) {
578
602
  const name = String(item.name ?? 'general')
579
603
  if (removed.has(name)) continue
604
+ if (body.createOnly === true && byName.get(name) !== undefined) {
605
+ throw new Error('网关 ' + name + ' 已存在')
606
+ }
580
607
  const previous = byName.get(name) ?? {}
581
608
  // A partial payload (only enabled/timeout/headers) must keep the
582
609
  // stored endpoint and tool settings.
583
610
  const hasEndpoint = typeof item.endpoint === 'string'
611
+ if (!hasEndpoint && byName.get(name) === undefined) {
612
+ throw new Error('网关 ' + name + ' 不存在,改动未保存')
613
+ }
584
614
  const endpoint = hasEndpoint ? item.endpoint.trim() : (previous.endpoint ?? '')
585
615
  const sameEndpoint = (previous.endpoint ?? '') === endpoint
616
+ // Keep tool settings when only the endpoint changes: silently
617
+ // dropping hidden/disabled/overrides was data loss. Entries for
618
+ // tools the new server does not expose are pruned on the next sync.
586
619
  const next: any = { ...previous, ...item, name, endpoint }
587
- if (!sameEndpoint) delete next.tools
620
+ void sameEndpoint
588
621
  if (item.headersPatch !== null && typeof item.headersPatch === 'object' && !Array.isArray(item.headersPatch)) {
589
622
  const headers: Record<string, string> = { ...(previous.headers ?? {}) }
590
623
  for (const [key, value] of Object.entries(item.headersPatch as Record<string, unknown>)) {