LeoYeAI/agent-master-skills / skills/agent-comm-hub
agent-comm-hub
Flagged多智能体协同通信基础设施——基于 MCP+SSE 的实时消息、任务调度、记忆共享与进化引擎。支持 WorkBuddy、Hermes、QClaw 及任意 MCP 兼容 Agent 接入。53 个 MCP 工具、4 级权限、零外部依赖 Python SDK。触发词:agent通信、智能体通信、hub通信、多智能体、跨agent通信、任务调度、assign_task、send_message、hermes通信、workbuddy通信、agent hub、通信hub、mcp通信、记忆共享、进化引擎、策略共享、经验分享、共享记忆,共同进化
src/tools/security.ts
View on GitHub/**
* security.ts — 安全与维护工具
* Tools: set_agent_role, recalculate_trust_scores, get_db_stats, archive_data
*/
import { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js";
import { z } from "zod";
import { db, getEnhancedDbStats, archiveOldMessages, archiveOldAuditLogs, vacuumDatabase, getDbSize } from "../db.js";
import { auditLog, recalculateTrustScore, recalculateAllTrustScores, type AuthContext } from "../security.js";
import { setAgentRole as setAgentRoleFromIdentity } from "../identity.js";
import { logError } from "../logger.js";
import { withRetry, requireAuth, mcpError, mcpFail } from "../utils.js";
/**
* 注册安全与维护工具
*/
export function registerSecurityTools(server: McpServer, authContext?: AuthContext): void {
// ────────────────────────────────────────────────────
// Phase 5a: set_agent_role — admin only
// 任命/撤销 group_admin,或调整角色
// ────────────────────────────────────────────────────
server.tool(
"set_agent_role",
"设置 Agent 角色(admin/member/group_admin)。group_admin 需指定 managed_group_id,仅能管理该 parallel_group 内成员的任务。仅 admin 可调用。",
{
agent_id: z.string().describe("目标 Agent ID"),
role: z.enum(["admin", "member", "group_admin"]).describe("新角色"),
managed_group_id: z.string().optional().describe("管理组 ID(仅 group_admin 角色需要)"),
},
async ({ agent_id, role, managed_group_id }) => {
const ctx = requireAuth(authContext, "set_agent_role");
const result = setAgentRoleFromIdentity(agent_id, role, ctx.agentId, managed_group_id);
if (!result.ok) {
return mcpFail(result.error, "set_agent_role");
}
return {
content: [{
type: "text",
text: JSON.stringify({
success: true,
agent_id,
old_role: result.old_role,
new_role: result.new_role,
managed_group_id: result.managed_group_id,
note: result.new_role === "group_admin"
? "group_admin 可管理指定 parallel_group 内成员的任务"
: result.new_role === "member"
? "已降级为普通成员"
: "管理员权限(完全控制)",
}, null, 2),
}],
};
}
);
// ────────────────────────────────────────────────────
// Phase 5a: recalculate_trust_scores — admin only
// 手动触发信任分重算(admin 覆盖自动值后可用此工具重置)
// ────────────────────────────────────────────────────
server.tool(
"recalculate_trust_scores",
"手动触发信任评分重算。基于多因子自动计算:verified capabilities (+3)、approved strategies (+2)、positive feedback (+1)、negative feedback (-2)、rejected applications (-3)、revoked tokens (-10)。不传 agent_id 则重算全部。仅 admin 可调用。",
{
agent_id: z.string().optional().describe("目标 Agent ID(不传则重算全部 Agent)"),
},
async ({ agent_id }) => {
const ctx = requireAuth(authContext, "recalculate_trust_scores");
try {
if (agent_id) {
const score = recalculateTrustScore(agent_id);
return {
content: [{
type: "text",
text: JSON.stringify({
success: true,
agent_id,
new_score: score,
note: "信任评分已重新计算并写入 agents.trust_score",
}, null, 2),
}],
};
} else {
const results = recalculateAllTrustScores();
return {
content: [{
type: "text",
text: JSON.stringify({
success: true,
total_agents: results.length,
scores: results,
note: `已重算 ${results.length} 个 Agent 的信任评分`,
}, null, 2),
}],
};
}
} catch (err: unknown) {
return mcpError(err, "recalculate_trust_scores");
}
}
);
// ────────────────────────────────────────────────────
// v2.3 Phase 3.2: 数据库维护工具(admin only)
// ────────────────────────────────────────────────────
// Tool DB1: get_db_stats — admin only
server.tool(
"get_db_stats",
"获取数据库统计信息。包括各表行数、数据库文件大小、WAL 大小、最后归档时间等。仅 admin 可调用。",
{},
async () => {
requireAuth(authContext, "get_db_stats");
try {
const stats = getEnhancedDbStats();
return {
content: [{
type: "text",
text: JSON.stringify({
success: true,
...stats,
note: stats.database_size_mb > 100
? "⚠️ 数据库超过 100MB,建议执行 VACUUM 或归档旧数据"
: "数据库状态正常",
}, null, 2),
}],
};
} catch (err: unknown) {
logError("get_db_stats_error", err);
return mcpError(err, "get_db_stats");
}
}
);
// Tool DB2: archive_data — admin only
server.tool(
"archive_data",
"手动触发数据归档。将指定天数之前的记录从主表移入归档表,以减少主表体积。可归档 messages(默认 30 天前)或 audit_log(默认 90 天前)。仅 admin 可调用。",
{
type: z.enum(["messages", "audit_log"]).describe("要归档的数据类型"),
days: z.number().int().min(1).max(365).optional()
.describe("归档多少天前的数据(messages 默认 30 天,audit_log 默认 90 天)"),
vacuum: z.boolean().optional().default(false)
.describe("归档后是否执行 VACUUM 压缩数据库文件"),
},
async ({ type, days, vacuum }) => {
requireAuth(authContext, "archive_data");
try {
const daysForType = days ?? (type === "messages" ? 30 : 90);
let archivedCount = 0;
if (type === "messages") {
archivedCount = archiveOldMessages(daysForType);
} else {
archivedCount = archiveOldAuditLogs(daysForType);
}
// VACUUM(可选,低峰期调用)
if (vacuum) {
vacuumDatabase();
}
const dbSize = getDbSize();
auditLog("tool_archive_data", authContext!.agentId,
`type=${type}, days=${daysForType}, archived=${archivedCount}, vacuum=${vacuum}`);
return {
content: [{
type: "text",
text: JSON.stringify({
success: true,
type,
days: daysForType,
archived_count: archivedCount,
vacuum_executed: vacuum ?? false,
database_size_bytes: dbSize,
database_size_mb: Math.round((dbSize / 1024 / 1024) * 100) / 100,
note: archivedCount > 0
? `已归档 ${archivedCount} 条 ${type} 记录`
: `没有需要归档的 ${type} 记录`,
}, null, 2),
}],
};
} catch (err: unknown) {
logError("archive_data_error", err);
return mcpError(err, "archive_data");
}
}
);
}
Related skills
| Skill | Stars | Updated | Works with | Status | Install |
|---|---|---|---|---|---|
| reverse-engineer-anythingmorluto/reaReverse engineer native, managed, Electron/JavaScript, packaged, firmware, and browser targets with REA, and build agent environments (AgentEnv bundles) of web applications from a live instance or a capture, and of Linux desktop apps with a task from a request. Use shipped-artifact or requested runtime evidence to explain features, compare versions, decompile code, or guide a reconstruction. Skip REA for ordinary source-repository architecture analysis. | Stars: 472k | Updated | Claude CodeCodex | Verified | Install |
| diagnosing-superpowersobra/superpowersUse when a superpowers session went wrong and your human partner wants to know why — repeated work, ignored plans, stumbles, poor results, a skill that didn't fire, "it took too long", "why is it so expensive", "what is it doing" — or wants to build a bug report for the superpowers maintainers, for the current session or a past one identified by id or path, on any harness. | Stars: 322k | Updated | Claude CodeCodex | Verified | Install |
| dispatching-parallel-agentsobra/superpowersUse when facing 2+ independent tasks that can be worked on without shared state or sequential dependencies | Stars: 322k | Updated | Claude CodeCodex | Verified | Install |
| executing-plansobra/superpowersUse when executing an implementation plan in the current session as the implementer yourself — your human partner chose inline execution, or no subagent tool is available | Stars: 322k | Updated | Claude CodeCodex | Verified | Install |