@mebular/mcp 0.2.3 → 0.3.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/server.mjs CHANGED
@@ -1,4 +1,4 @@
1
- // MCP server 装配(G6.2 + R1):stdio 传输 + **27 工具**(记忆 11 + 任务 16)+ memory_policy prompt。
1
+ // MCP server 装配(G6.2 + R1):stdio 传输 + **29 工具**(记忆 13 + 任务 16)+ memory_policy prompt。
2
2
  // 全部委托 MemoryService(D33 单一实现)。
3
3
 
4
4
  import { McpServer } from '@modelcontextprotocol/server';
@@ -26,8 +26,15 @@ const MEMORY_POLICY = `# Mebular 记忆使用规约(memory_policy)
26
26
  5. 隐私:不要写入密钥、口令、完整身份证件等高敏感信息。
27
27
  6. 无结果别编:召回为空就如实说明,不要臆造记忆。`;
28
28
 
29
+ /** MCP initialize 返回的全局行为说明(不替代 memory_policy prompt)。 */
30
+ export const MCP_INSTRUCTIONS = `Mebular 是本地优先的图式记忆与任务协作层。
31
+ 1. 先查后写:写入记忆前先用 memory_query/memory_search 查重;只写可复用的稳定事实/偏好,不要把一次会话的过程产物当跨设备记忆。
32
+ 2. 任务派发前先 task_targets 确认可派对象,再 task_submit;非幂等/失败重试的语义以工具描述为准。
33
+ 3. 工具描述首行的「何时用」是首选路由信号;优先按它选择工具。`;
34
+
29
35
  export function buildServer(service, options = {}) {
30
- const server = new McpServer({ name: 'mebular', version: APP_VERSION });
36
+ // McpServer(serverInfo, options):instructions 必须放在第二参数,initialize/discover 才会下发。
37
+ const server = new McpServer({ name: 'mebular', version: APP_VERSION }, { instructions: MCP_INSTRUCTIONS });
31
38
  registerTools(server, service, options);
32
39
  server.registerPrompt(
33
40
  'memory_policy',
@@ -369,6 +376,7 @@ export async function startServeServer(options = {}) {
369
376
  runtime: current?.runtime ?? null,
370
377
  // D2:写端点默认开启(仍需 memory.admin scope + CSRF 双提交);`MEBULAR_CONSOLE_WRITES=0` 可降级为 D1 只读
371
378
  writesEnabled: options.writesEnabled ?? process.env.MEBULAR_CONSOLE_WRITES !== '0',
379
+ manageMemoryRuntime: true,
372
380
  provision: provisionStart,
373
381
  initializeRuntime: async ({ action, result, server }) => {
374
382
  const next = await startRuntime({ action, result, serverContext: server, hot: true });
@@ -388,6 +396,33 @@ export async function startServeServer(options = {}) {
388
396
  current.runtime.mcp.auth = httpServer.auth;
389
397
  // G:装配完成后写「已监听」标记——supervisor 用它判定新实例健康(join 失败则不写)
390
398
  await finalizeRuntime(current);
399
+ // Skill 收敛(启动/升级):受管旧版更新、用户修改保留;失败只记录,绝不阻断 serve。
400
+ void import('./clients.mjs')
401
+ .then(({ autoEnsureSkills, assistantUserHome }) => autoEnsureSkills({ home, userHome: assistantUserHome(), cwd: process.cwd() }))
402
+ .then((result) => {
403
+ if (result?.summary?.failed > 0 || (result?.ok === false && result?.skipped !== true)) {
404
+ console.error(`SKILL_ENSURE_PENDING_REPAIR ${result?.reason ?? `${result?.summary?.failed ?? 1} client(s)`}`);
405
+ }
406
+ })
407
+ .catch((error) => console.error(`SKILL_ENSURE_FAILED ${error?.code ?? 'ERROR'}`));
408
+ // 自动自愈(主路径):实际端口已确定 → 只把已存在的本机 loopback Mebular HTTP 条目
409
+ // 幂等更新到当前 /mcp;不 force Skill、不创建新客户端配置、不改远端自定义 URL。
410
+ void import('./clients.mjs')
411
+ .then(({ reconcileClientEndpoints, assistantUserHome, daemonMcpUrl }) => {
412
+ const reconcileHost = httpServer.host === '0.0.0.0' || httpServer.host === '::' ? '127.0.0.1' : httpServer.host;
413
+ return reconcileClientEndpoints({
414
+ home,
415
+ userHome: assistantUserHome(),
416
+ cwd: process.cwd(),
417
+ mcpUrl: daemonMcpUrl({ host: `${reconcileHost}:${httpServer.port}`, encrypted: serverTls }),
418
+ });
419
+ })
420
+ .then((result) => {
421
+ if (result?.summary?.failed > 0) {
422
+ console.error(`CLIENT_ENDPOINT_RECONCILE_PARTIAL ${result.summary.failed} client(s)`);
423
+ }
424
+ })
425
+ .catch((error) => console.error(`CLIENT_ENDPOINT_RECONCILE_FAILED ${error?.code ?? 'ERROR'}`));
391
426
  }
392
427
 
393
428
  const shutdown = async () => {
@@ -17,11 +17,17 @@ export const TASK_TOOL_SCOPES = Object.fromEntries(
17
17
  TASK_TOOLS.map((tool) => [tool.name, tool.scope]),
18
18
  );
19
19
 
20
+ /** 任务工具在 MCP 无稳定 Agent 身份时的显式默认 actor(与 fleet CLI 缺省一致)。 */
21
+ export const DEFAULT_TASK_AGENT = 'board';
22
+
20
23
  /**
21
24
  * 任务工具规格:与记忆面同形(name/title/description/inputSchema/handler(service,args,context))。
22
25
  *
23
26
  * `context.home`:工具上下文目录(默认 homeDir())。
24
27
  * `context.mebular`:运行中 daemon 的 live app(缺省 = 离线新建)。
28
+ * `context.agent`:可选的稳定 Agent 身份(宿主注入时使用);MCP 协议本身没有
29
+ * Agent 身份,因此缺省 **显式** 为 `board`,不允许隐式分叉。fleet CLI 的
30
+ * `--agent` 是同一 handler 的显式操作者覆盖,缺省同样是 `board`。
25
31
  */
26
32
  export const TASK_TOOL_SPECS = TASK_TOOLS.map((tool) => ({
27
33
  name: tool.name,
@@ -31,9 +37,12 @@ export const TASK_TOOL_SPECS = TASK_TOOLS.map((tool) => ({
31
37
  surface: 'task',
32
38
  handler: async (_service, args, context = {}) => {
33
39
  try {
40
+ const agent = typeof context.agent === 'string' && context.agent.trim() !== ''
41
+ ? context.agent
42
+ : DEFAULT_TASK_AGENT;
34
43
  const result = await tool.handler(args ?? {}, {
35
44
  dir: context.home ?? homeDir(),
36
- agent: 'board',
45
+ agent,
37
46
  ...(context.mebular ? { mebular: context.mebular } : {}),
38
47
  });
39
48
  return json(result);
package/src/tools.mjs CHANGED
@@ -1,4 +1,4 @@
1
- // MCP 工具面(G6.2 / D35 + R1 统一入口):**27 个** agent 中立工具 = 记忆 11(薄壳委托 MemoryService)
1
+ // MCP 工具面(G6.2 / D35 + R1 统一入口):**29 个** agent 中立工具 = 记忆 13(薄壳委托 MemoryService)
2
2
  // + 任务 16(复用 @mebular/fleet 的 TASK_TOOLS handler;同 handler 不复制)。
3
3
  //
4
4
  // 钳制:limit ≤ 50(默认 10)、depth ≤ 5、batch ≤ 100。
@@ -90,12 +90,15 @@ const namespaceFilter = z.union([z.string(), z.array(z.string())]).optional();
90
90
 
91
91
  const internalTypes = z.array(z.enum(['fact', 'episode', 'skill', 'preference', 'observation']));
92
92
 
93
+ /** 工具描述首行 = 首要路由信号「何时用」;原始语义/约束保留在第二行。 */
94
+ const when = (whenToUse, description) => `何时用:${whenToUse}\n${description}`;
95
+
93
96
  /** 工具注册表:handler(service, args) → MCP 结果对象(CLI 直接取 `structuredContent`)。 */
94
97
  export const TOOL_SPECS = [
95
98
  {
96
99
  name: 'memory_write',
97
100
  title: '写入记忆',
98
- description: '写入单条记忆(fact/episode/skill/preference/observation)',
101
+ description: when('需要把用户明确、可复用的稳定事实/偏好写入长期记忆时;先查后写,用 memory_query/memory_search 查重。', '写入单条记忆(fact/episode/skill/preference/observation)'),
99
102
  inputSchema: z.object({ items: z.array(memoryInputSchema).min(1) }),
100
103
  handler: async (service, { items }) => {
101
104
  try {
@@ -108,7 +111,7 @@ export const TOOL_SPECS = [
108
111
  {
109
112
  name: 'memory_write_batch',
110
113
  title: '批量写入记忆',
111
- description: `批量写入记忆(最多 ${MAX_BATCH} 条)`,
114
+ description: when('一次会话结束有 2 条以上稳定事实/偏好要落库,且已查重时。', `批量写入记忆(最多 ${MAX_BATCH} 条)`),
112
115
  inputSchema: z.object({ items: z.array(memoryInputSchema).min(1) }),
113
116
  handler: async (service, { items }) => {
114
117
  if (items.length > MAX_BATCH) return fail(`批量写入超过上限:${items.length} > ${MAX_BATCH}`);
@@ -122,7 +125,7 @@ export const TOOL_SPECS = [
122
125
  {
123
126
  name: 'memory_query',
124
127
  title: '查询记忆',
125
- description: '语义/关键词召回(配置向量索引时走语义)',
128
+ description: when('回答前需要按语义召回已有记忆,或不确定某事实是否已记录时。', '语义/关键词召回(配置向量索引时走语义)'),
126
129
  inputSchema: z.object({
127
130
  query: z.string().optional(),
128
131
  types: internalTypes.optional(),
@@ -161,7 +164,7 @@ export const TOOL_SPECS = [
161
164
  {
162
165
  name: 'memory_search',
163
166
  title: '关键词搜索',
164
- description: '关键词检索基线,可选一跳关系',
167
+ description: when('已知关键词/专有名词,需要精确检索或查重时(比 memory_query 更可控)。', '关键词检索基线,可选一跳关系'),
165
168
  inputSchema: z.object({
166
169
  query: z.string(),
167
170
  types: z.array(z.string()).optional(),
@@ -196,7 +199,7 @@ export const TOOL_SPECS = [
196
199
  {
197
200
  name: 'memory_profile',
198
201
  title: '用户画像',
199
- description: '返回用户偏好与属性(默认不含已归档)',
202
+ description: when('需要用户偏好/属性做个性化回答,或判断写入是否符合用户约束时。', '返回用户偏好与属性(默认不含已归档)'),
200
203
  inputSchema: z.object({ includeArchived: z.boolean().optional() }),
201
204
  handler: async (service, args) => {
202
205
  try {
@@ -209,7 +212,7 @@ export const TOOL_SPECS = [
209
212
  {
210
213
  name: 'memory_skills',
211
214
  title: '技能列表',
212
- description: '按分类/关键词/标签筛选技能',
215
+ description: when('需要复用已有技能步骤,或查找某类可复用流程时。', '按分类/关键词/标签筛选技能'),
213
216
  inputSchema: z.object({
214
217
  category: z.string().optional(),
215
218
  search: z.string().optional(),
@@ -233,7 +236,7 @@ export const TOOL_SPECS = [
233
236
  {
234
237
  name: 'memory_history',
235
238
  title: '会话历史',
236
- description: '按会话/时间/主题筛选会话情节',
239
+ description: when('需要回看某次会话/任务过程,而不是稳定事实时。', '按会话/时间/主题筛选会话情节'),
237
240
  inputSchema: z.object({
238
241
  sessionIds: z.array(z.string()).optional(),
239
242
  startTime: z.number().optional(),
@@ -262,7 +265,7 @@ export const TOOL_SPECS = [
262
265
  {
263
266
  name: 'memory_graph',
264
267
  title: '图遍历',
265
- description: `从起点遍历(深度 ≤ ${MAX_DEPTH})`,
268
+ description: when('需要沿关系边查上下文(相关实体/事实/技能)时。', `从起点遍历(深度 ≤ ${MAX_DEPTH})`),
266
269
  inputSchema: z.object({
267
270
  startId: z.string(),
268
271
  direction: z.enum(['outgoing', 'incoming', 'both']).optional(),
@@ -288,7 +291,7 @@ export const TOOL_SPECS = [
288
291
  {
289
292
  name: 'memory_import',
290
293
  title: '导入记忆',
291
- description: '经适配器导入异构来源(kv/markdown/…)',
294
+ description: when('从外部 kv/markdown 等来源一次性导入历史记忆时。', '经适配器导入异构来源(kv/markdown/…)'),
292
295
  inputSchema: z.object({ kind: z.string().optional(), data: z.unknown(), origin: z.string().optional() }),
293
296
  handler: async (service, args) => {
294
297
  try {
@@ -305,7 +308,7 @@ export const TOOL_SPECS = [
305
308
  {
306
309
  name: 'memory_status',
307
310
  title: '记忆状态',
308
- description: '返回设备/网络/计数/状态哈希/开关',
311
+ description: when('接入、排障或需要确认设备/网络/计数/状态哈希/开关时。', '返回设备/网络/计数/状态哈希/开关'),
309
312
  inputSchema: z.object({}),
310
313
  handler: async (service) => {
311
314
  try {
@@ -318,7 +321,7 @@ export const TOOL_SPECS = [
318
321
  {
319
322
  name: 'memory_sync',
320
323
  title: '同步对端',
321
- description: '连接对端并等待一次双向同步(network.enabled 需为 true)',
324
+ description: when('需要立即拉取/推送某个对端,而不是等待自动同步时(network.enabled 需为 true)。', '连接对端并等待一次双向同步(network.enabled 需为 true)'),
322
325
  inputSchema: z.object({ peerId: z.string(), address: z.string().optional() }),
323
326
  handler: async (service, args) => {
324
327
  try {
@@ -332,7 +335,7 @@ export const TOOL_SPECS = [
332
335
  {
333
336
  name: 'memory_delete',
334
337
  title: '删除记忆',
335
- description: '删除记忆:写墓碑(deletedAt)并随图事件同步到各端;删除后默认不再出现在任何召回。不可逆动作(对端已拷贝的旧快照无法强制回收)。需 memory.admin。',
338
+ description: when('确认内容要永久墓碑删除、且接受不可逆时;不确定时先用 memory_archive。', '删除记忆:写墓碑(deletedAt)并随图事件同步到各端;删除后默认不再出现在任何召回。不可逆动作(对端已拷贝的旧快照无法强制回收)。需 memory.admin。'),
336
339
  inputSchema: z.object({ ids: z.array(z.string()).min(1) }),
337
340
  handler: async (service, args) => {
338
341
  try {
@@ -345,7 +348,7 @@ export const TOOL_SPECS = [
345
348
  {
346
349
  name: 'memory_archive',
347
350
  title: '归档 / 解除归档记忆',
348
- description: '归档记忆:打 metadata.archivedAt 标记(随图同步、无损、可逆),归档后默认不进入召回;archived=false 解除归档。删除(墓碑)节点会被跳过。',
351
+ description: when('内容暂时不该参与召回但将来可能有用(可逆);要永久删除请用 memory_delete。', '归档记忆:打 metadata.archivedAt 标记(随图同步、无损、可逆),归档后默认不进入召回;archived=false 解除归档。删除(墓碑)节点会被跳过。'),
349
352
  inputSchema: z.object({ ids: z.array(z.string()).min(1), archived: z.boolean().optional() }),
350
353
  handler: async (service, args) => {
351
354
  try {
@@ -357,13 +360,13 @@ export const TOOL_SPECS = [
357
360
  },
358
361
  ];
359
362
 
360
- /** 统一工具注册表(记忆 11 + 任务 16 = 27):`mebular mcp`(stdio)与 HTTP `/mcp` 都注册这一份。 */
363
+ /** 统一工具注册表(记忆 13 + 任务 16 = 29):`mebular mcp`(stdio)与 HTTP `/mcp` 都注册这一份。 */
361
364
  export const ALL_TOOL_SPECS = [...TOOL_SPECS, ...TASK_TOOL_SPECS];
362
365
 
363
- /** 统一工具名(27)——控制台「能力清单」与 CLI `mebular tools` 同源。 */
366
+ /** 统一工具名(29)——控制台「能力清单」与 CLI `mebular tools` 同源。 */
364
367
  export const TOOL_NAMES = ALL_TOOL_SPECS.map((spec) => spec.name);
365
368
 
366
- /** 统一 scope 表(记忆 + 任务,27)。 */
369
+ /** 统一 scope 表(记忆 + 任务,29)。 */
367
370
  export const TOOL_ALL_SCOPES = { ...TOOL_SCOPES, ...TASK_TOOL_SCOPES };
368
371
 
369
372
  /** 工具名 → spec(记忆 + 任务;CLI 与 MCP 同 handler)。 */
package/src/version.mjs CHANGED
@@ -5,4 +5,4 @@
5
5
  // scripts/bump-version.mjs 只改这一处,scripts/verify-release-version.mjs
6
6
  // 校验它等于发布版本;不要在其它文件再写版本字面量。
7
7
 
8
- export const APP_VERSION = '0.2.3';
8
+ export const APP_VERSION = '0.3.0';