@mebular/mcp 0.1.1 → 0.2.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/src/serve.mjs CHANGED
@@ -19,6 +19,10 @@ import { attachFleetEvents, resolveFleetNamespace } from './fleet-events.mjs';
19
19
  import { READ_ROUTES, buildHandoffPlan } from './admin.mjs';
20
20
  import { configPath } from './config.mjs';
21
21
  import { createServiceFacade } from './service-facade.mjs';
22
+ import { APP_VERSION } from './version.mjs';
23
+
24
+ // 兼容既有导入方:APP_VERSION 的真源是 ./version.mjs。
25
+ export { APP_VERSION } from './version.mjs';
22
26
 
23
27
  // ---------- 控制台配置写入(schema 驱动:editable 可写 / status-only 只读 / internal 默认不可写) ----------
24
28
  // 单一真源:packages/mcp/src/config-schema.mjs。写入策略(原子写 + .bak 备份;全部需重启生效):
@@ -220,8 +224,7 @@ function settleWithin(promise, ms) {
220
224
  }
221
225
 
222
226
  // /healthz、/app/embed/info 与 <home>/runtime.json 共用的对外版本号与 embed 协议版本。
223
- // 单一真源:不要在 buildEmbedInfo / runtime 写入处再写字面量。
224
- export const APP_VERSION = '0.1.1';
227
+ // APP_VERSION 的唯一真源是 ./version.mjs;不要在 buildEmbedInfo / runtime 写入处再写字面量。
225
228
  export const EMBED_PROTOCOL = 1;
226
229
 
227
230
  // ---------- 控制台静态托管(D1) ----------
@@ -1786,12 +1789,18 @@ export async function startHttpServer({
1786
1789
  }
1787
1790
  let plan;
1788
1791
  try {
1789
- const { restartPlanFor } = await import('../../service/dist/index.js');
1790
- plan = restartPlanFor({ kind, args: [], heartbeatDir: home, env: { MEBULAR_HOME: home } }, { home });
1792
+ const { planRuntimeServiceRestart, runtimeProviderFromEnv } = await import('./runtime-service.mjs');
1793
+ const { resolveServiceHome } = await import('./service-control.mjs');
1794
+ plan = await planRuntimeServiceRestart({
1795
+ root: cloud.available ? cloud.root : null,
1796
+ home,
1797
+ serviceHome: resolveServiceHome(),
1798
+ fallbackProvider: runtimeProviderFromEnv({ cliPath: process.argv[1] }),
1799
+ });
1791
1800
  } catch (error) {
1792
1801
  return { registered: false, manual: `服务管理不可用(${String(error?.message ?? error)}):请手动重启` };
1793
1802
  }
1794
- const command = plan.commands.map((entry) => entry.join(' ')).join(' && ') || null;
1803
+ const command = plan.commands?.map((entry) => entry.join(' ')).join(' && ') || null;
1795
1804
  if (!plan.registered) return { registered: false, command, manual: plan.manual };
1796
1805
  const dryRun = process.env.MEBULAR_RESTART_DRY_RUN === '1';
1797
1806
  return {
@@ -2750,21 +2759,63 @@ export async function startHttpServer({
2750
2759
  return sendJson(res, result.ok ? 200 : 409, result);
2751
2760
  }
2752
2761
 
2753
- // H-1:常驻运行(服务托管,动作式;服务命令带**守护自身 home**)。CI 用 MEBULAR_SERVICE_DRY_RUN=1 替身断言命令序列。
2762
+ // H-1:常驻运行(服务托管,动作式;统一走共享 runtime/service 层)。CI 用 MEBULAR_SERVICE_DRY_RUN=1 替身断言命令序列。
2754
2763
  const svcAction = path.match(/^\/admin\/api\/service\/(install|uninstall)$/);
2755
2764
  if (svcAction) {
2756
- const { installServiceForHome, uninstallServiceForHome, serviceStatusForHome, resolveServiceHome } = await import('./service-control.mjs');
2765
+ const {
2766
+ installRuntimeService,
2767
+ runtimeProviderFromEnv,
2768
+ runtimeServiceStatus,
2769
+ uninstallRuntimeService,
2770
+ } = await import('./runtime-service.mjs');
2771
+ const { resolveServiceHome } = await import('./service-control.mjs');
2757
2772
  const dryRun = process.env.MEBULAR_SERVICE_DRY_RUN === '1';
2758
2773
  const commands = [];
2759
2774
  const run = dryRun ? (cmd, args) => { commands.push([cmd, ...(args ?? [])]); return { status: 0, stdout: '', stderr: '' }; } : undefined;
2760
2775
  // 两套 home:home = 实例身份(descriptor),serviceHome = unit/manifest 落盘(用户 OS home)。
2761
2776
  const serviceHome = resolveServiceHome();
2777
+ const root = cloud.available ? cloud.root : null;
2778
+ const fallbackProvider = runtimeProviderFromEnv({ cliPath: process.argv[1] });
2779
+ if (root === null) {
2780
+ // 自定义 home / 受限 env:保留 App 既有 service-control 行为。
2781
+ const { installServiceForHome, uninstallServiceForHome, serviceStatusForHome } = await import('./service-control.mjs');
2782
+ try {
2783
+ const action = svcAction[1];
2784
+ const result = action === 'install'
2785
+ ? installServiceForHome({ home, serviceHome, ...(run ? { run } : {}) })
2786
+ : uninstallServiceForHome({ home, serviceHome, ...(run ? { run } : {}) });
2787
+ const status = serviceStatusForHome({ home, serviceHome, ...(run ? { run } : {}) });
2788
+ return sendJson(res, 200, { ok: true, action, dryRun, commands, result, status });
2789
+ } catch (error) {
2790
+ return sendJson(res, 500, { ok: false, action: svcAction[1], error: 'service_action_failed', message: String(error?.message ?? error) });
2791
+ }
2792
+ }
2762
2793
  try {
2763
2794
  const action = svcAction[1];
2764
2795
  const result = action === 'install'
2765
- ? installServiceForHome({ home, serviceHome, ...(run ? { run } : {}) })
2766
- : uninstallServiceForHome({ home, serviceHome, ...(run ? { run } : {}) });
2767
- const status = serviceStatusForHome({ home, serviceHome, ...(run ? { run } : {}) });
2796
+ ? await installRuntimeService({ root, home, serviceHome, http: { port }, fallbackProvider, ...(run ? { run } : {}) })
2797
+ : await uninstallRuntimeService({ root, home, serviceHome, ...(run ? { run } : {}) });
2798
+ const status = await runtimeServiceStatus({
2799
+ root,
2800
+ home,
2801
+ serviceHome,
2802
+ fallbackProvider,
2803
+ ...(run ? { run } : {}),
2804
+ });
2805
+ if (result?.ok !== true) {
2806
+ const httpStatus = ['runtime_required', 'service_conflict', 'service_foreign'].includes(result?.reason) ? 409 : 500;
2807
+ return sendJson(res, httpStatus, {
2808
+ ok: false,
2809
+ action,
2810
+ dryRun,
2811
+ commands,
2812
+ reason: result?.reason ?? 'service_action_failed',
2813
+ message: result?.message ?? 'service action failed',
2814
+ current: result?.current ?? null,
2815
+ result,
2816
+ status,
2817
+ });
2818
+ }
2768
2819
  return sendJson(res, 200, { ok: true, action, dryRun, commands, result, status });
2769
2820
  } catch (error) {
2770
2821
  return sendJson(res, 500, { ok: false, action: svcAction[1], error: 'service_action_failed', message: String(error?.message ?? error) });
package/src/server.mjs CHANGED
@@ -10,6 +10,7 @@ import { MemoryService } from '@mebular/core';
10
10
  import { createMebular, homeDir, loadConfigFile } from './config.mjs';
11
11
  import { registerTools } from './tools.mjs';
12
12
  import { startHttpServer, acquireLock, pidAlive } from './serve.mjs';
13
+ import { APP_VERSION } from './version.mjs';
13
14
  import { createJoinServer } from './jointoken.mjs';
14
15
  import { clearApplyPending, readApplyPending, writeApplyResult, writeServeReady } from './config-apply.mjs';
15
16
  import { buildSettings } from './admin.mjs';
@@ -26,7 +27,7 @@ const MEMORY_POLICY = `# Mebular 记忆使用规约(memory_policy)
26
27
  6. 无结果别编:召回为空就如实说明,不要臆造记忆。`;
27
28
 
28
29
  export function buildServer(service, options = {}) {
29
- const server = new McpServer({ name: 'mebular', version: '0.1.1' });
30
+ const server = new McpServer({ name: 'mebular', version: APP_VERSION });
30
31
  registerTools(server, service, options);
31
32
  server.registerPrompt(
32
33
  'memory_policy',
@@ -39,14 +39,32 @@ export function cliBinPath() {
39
39
  }
40
40
 
41
41
  /** 守护服务描述符:**必须**携带守护自身 home(env + workingDir + heartbeatDir)。 */
42
- export function serviceDescriptor({ home, execPath = process.execPath, extraArgs = [] } = {}) {
42
+ export function serviceDescriptor({ home, execPath = process.execPath, extraArgs = [], cloudRoot = null } = {}) {
43
+ const cliPath = cliBinPath();
43
44
  return {
44
45
  kind: SERVICE_KIND,
45
- args: [cliBinPath(), 'serve', ...extraArgs],
46
+ args: [cliPath, 'serve', ...extraArgs],
46
47
  heartbeatDir: home,
47
48
  workingDir: home,
48
49
  execPath,
49
- env: { MEBULAR_HOME: home },
50
+ env: {
51
+ MEBULAR_HOME: home,
52
+ ...(typeof cloudRoot === 'string' && cloudRoot.length > 0 ? { MEBULAR_CLOUD_ROOT: cloudRoot } : {}),
53
+ MEBULAR_RUNTIME_PROVIDER: 'cli',
54
+ MEBULAR_RUNTIME_OWNER: 'cli',
55
+ MEBULAR_RUNTIME_PATH: cliPath,
56
+ },
57
+ runtime: {
58
+ version: null,
59
+ runtimePath: null,
60
+ cliPath,
61
+ provider: 'cli',
62
+ owner: 'cli',
63
+ execPath,
64
+ electron: false,
65
+ cloudRoot: typeof cloudRoot === 'string' && cloudRoot.length > 0 ? cloudRoot : null,
66
+ home,
67
+ },
50
68
  };
51
69
  }
52
70
 
@@ -0,0 +1,84 @@
1
+ // 共享 service 加载器:bare `@mebular/service` 优先,安装布局下回退到 core tarball 内的
2
+ // `packages/service/dist/index.js`。
3
+ //
4
+ // 背景(Phase 4a blocker):
5
+ // `@mebular/core` 的 package.json 不依赖 `@mebular/service`,但 core tarball 实际包含
6
+ // `packages/service/dist`。共享 runtime 安装到 `<cloudRoot>/runtime/<version>/node_modules/
7
+ // @mebular/core/...` 后,bare import 解析不到独立包;若不回退,`runtime resolve` 的
8
+ // service 状态会变成 `service_unknown`,plugin-only 首次启动会被阻断。
9
+ //
10
+ // 决策:不新增 npm 依赖、不改发布依赖;由本 loader 统一 bare → bundled dist。
11
+ // App 仍可注入自己的 service adapter(cloud-switch 的 `deps` / supervisor 的注入点),
12
+ // 本 loader 只服务“默认共享 core/CLI”路径。
13
+ //
14
+ // 约束:
15
+ // - 只接受 bare 包名或本文件相对定位的 bundled dist,不加载任意 URL/路径;
16
+ // - 只缓存成功结果;失败不缓存,便于用户修复安装后重试;
17
+ // - 失败抛 `SERVICE_MODULE_UNAVAILABLE`,保留 bare/bundled 两个 cause 供诊断(不含密钥)。
18
+
19
+ const BUNDLED_SERVICE_URL = new URL('../../service/dist/index.js', import.meta.url);
20
+
21
+ const importBareService = () => import('@mebular/service');
22
+ const importBundledService = () => import(BUNDLED_SERVICE_URL.href);
23
+
24
+ let cachedModule = null;
25
+ let cachedPromise = null;
26
+
27
+ /** 本 loader 在安装布局中定位到的 service dist(测试/诊断用;不返回文件内容)。 */
28
+ export function bundledServiceModuleUrl() {
29
+ return BUNDLED_SERVICE_URL;
30
+ }
31
+
32
+ /** 清空缓存(测试或修复安装后显式重试)。 */
33
+ export function resetServiceModuleCache() {
34
+ cachedModule = null;
35
+ cachedPromise = null;
36
+ }
37
+
38
+ /**
39
+ * 加载 service 模块。
40
+ *
41
+ * `importBare` / `importBundled` 可注入,便于离线测试安装布局;生产调用无需传参。
42
+ * 返回 ES module namespace,与 `import('@mebular/service')` 形状一致。
43
+ */
44
+ export async function loadServiceModule({
45
+ importBare = importBareService,
46
+ importBundled = importBundledService,
47
+ cache = true,
48
+ } = {}) {
49
+ if (cache && cachedModule !== null) return cachedModule;
50
+ if (cache && cachedPromise !== null) return cachedPromise;
51
+
52
+ const load = async () => {
53
+ try {
54
+ return await importBare();
55
+ } catch (bareError) {
56
+ try {
57
+ return await importBundled();
58
+ } catch (bundledError) {
59
+ const error = new Error(
60
+ '无法加载 @mebular/service:bare 包与 core 内置 packages/service/dist 均不可用',
61
+ );
62
+ error.code = 'SERVICE_MODULE_UNAVAILABLE';
63
+ error.cause = bareError;
64
+ error.bareError = bareError;
65
+ error.bundledError = bundledError;
66
+ throw error;
67
+ }
68
+ }
69
+ };
70
+
71
+ const pending = load();
72
+ if (cache) cachedPromise = pending;
73
+ try {
74
+ const module = await pending;
75
+ if (cache) {
76
+ cachedModule = module;
77
+ cachedPromise = null;
78
+ }
79
+ return module;
80
+ } catch (error) {
81
+ if (cache) cachedPromise = null;
82
+ throw error;
83
+ }
84
+ }
@@ -0,0 +1,8 @@
1
+ // MCP 包对外报告的版本号唯一真源。
2
+ //
3
+ // serve.mjs 的 /healthz、/app/embed/info、runtime.json,server.mjs 的 MCP server
4
+ // 元数据,client.mjs 的 clientInfo 都从这里读取。发布脚本
5
+ // scripts/bump-version.mjs 只改这一处,scripts/verify-release-version.mjs
6
+ // 校验它等于发布版本;不要在其它文件再写版本字面量。
7
+
8
+ export const APP_VERSION = '0.2.0';