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/file.ts
View on GitHub/**
* file.ts — 文件传输工具
* Tools: upload_file, download_file, list_attachments
*/
import { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js";
import { z } from "zod";
import { randomUUID } from "crypto";
import { type Attachment, attachStmt } from "../db.js";
import { messageRepo } from "../repo/sqlite-impl.js";
import { pushToAgent } from "../sse.js";
import { type AuthContext } from "../security.js";
import { logError } from "../logger.js";
import { incrementMcpCall } from "../metrics.js";
import { existsSync, mkdirSync, writeFileSync, readFileSync } from "fs";
import { join as pathJoin } from "path";
import { withRetry, requireAuth, mcpError } from "../utils.js";
/**
* 注册文件传输工具
*/
export function registerFileTools(server: McpServer, authContext?: AuthContext): void {
// ═══════════════════════════════════════════════════════════════
// v2.3 Phase 1.1: 文件传输工具(3 个)
// ═══════════════════════════════════════════════════════════════
// --- Tool: upload_file ---
// 上传文件附件并关联到消息 — member 及以上
server.tool(
"upload_file",
"上传文件附件并关联到消息。文件以 Base64 编码传入,服务端解码后存储到本地磁盘。",
{
message_id: z.string().describe("关联的消息 ID"),
filename: z.string().describe("文件名(含扩展名)"),
content_base64: z.string().describe("文件内容的 Base64 编码"),
mime_type: z.string().default("application/octet-stream").describe("MIME 类型"),
},
async ({ message_id, filename, content_base64, mime_type }) => {
const ctx = requireAuth(authContext, "upload_file");
try {
// 验证消息存在
const msg = messageRepo.getById(message_id);
if (!msg) {
return { content: [{ type: "text", text: JSON.stringify({ error: `Message ${message_id} not found` }) }] };
}
// Base64 解码
const buffer = Buffer.from(content_base64, "base64");
const fileSize = buffer.length;
// 检查文件大小(10MB 限制)
if (fileSize > 10 * 1024 * 1024) {
return { content: [{ type: "text", text: JSON.stringify({ error: "File too large, max 10MB" }) }] };
}
// 生成存储路径
const id = randomUUID();
const uploadDir = process.env.UPLOAD_DIR || pathJoin(process.cwd(), "uploads");
if (!existsSync(uploadDir)) mkdirSync(uploadDir, { recursive: true });
const storagePath = pathJoin(uploadDir, id);
// 写入文件
writeFileSync(storagePath, buffer);
// 存入数据库
attachStmt.insert.run({
id,
message_id,
filename,
mime_type,
file_size: fileSize,
storage_path: storagePath,
uploaded_by: ctx.agentId,
created_at: Date.now(),
});
// SSE 通知接收方
pushToAgent(msg.to_agent, {
type: "file_attached",
attachment_id: id,
message_id,
filename,
mime_type,
file_size: fileSize,
uploaded_by: ctx.agentId,
created_at: Date.now(),
});
incrementMcpCall("upload_file", "success", ctx.role);
return {
content: [{
type: "text",
text: JSON.stringify({
success: true,
attachment_id: id,
filename,
file_size: fileSize,
message_id,
}, null, 2),
}],
};
} catch (err: unknown) {
logError("upload_file_error", err);
return mcpError(err, "upload_file");
}
}
);
// --- Tool: download_file ---
// 下载附件,返回 Base64 编码 — member 及以上
server.tool(
"download_file",
"下载附件,返回 Base64 编码的文件内容。",
{
attachment_id: z.string().describe("附件 ID"),
},
async ({ attachment_id }) => {
const ctx = requireAuth(authContext, "download_file");
try {
const attach = attachStmt.getById.get(attachment_id) as Attachment | undefined;
if (!attach) {
return { content: [{ type: "text", text: JSON.stringify({ error: `Attachment ${attachment_id} not found` }) }] };
}
if (!existsSync(attach.storage_path)) {
return { content: [{ type: "text", text: JSON.stringify({ error: "File not found on disk" }) }] };
}
const buffer = readFileSync(attach.storage_path);
incrementMcpCall("download_file", "success", ctx.role);
return {
content: [{
type: "text",
text: JSON.stringify({
success: true,
attachment_id: attach.id,
filename: attach.filename,
mime_type: attach.mime_type,
file_size: attach.file_size,
content_base64: buffer.toString("base64"),
message_id: attach.message_id,
}, null, 2),
}],
};
} catch (err: unknown) {
logError("download_file_error", err);
return mcpError(err, "download_file");
}
}
);
// --- Tool: list_attachments ---
// 列出消息的附件列表 — member 及以上
server.tool(
"list_attachments",
"列出消息的所有附件列表。",
{
message_id: z.string().describe("消息 ID"),
},
async ({ message_id }) => {
const ctx = requireAuth(authContext, "list_attachments");
try {
const attachments = attachStmt.listByMessage.all(message_id) as Attachment[];
incrementMcpCall("list_attachments", "success", ctx.role);
return {
content: [{
type: "text",
text: JSON.stringify({
message_id,
attachments: attachments.map(a => ({
id: a.id,
filename: a.filename,
mime_type: a.mime_type,
file_size: a.file_size,
uploaded_by: a.uploaded_by,
created_at: a.created_at,
})),
count: attachments.length,
}, null, 2),
}],
};
} catch (err: unknown) {
logError("list_attachments_error", err);
return mcpError(err, "list_attachments");
}
}
);
}
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 |