ec9431bfa4
多机/容器化第一时间断裂的隐患(ARCHITECTURE_REVIEW §7 #1):此前报告 源(reportStore)与 .docx 产物(reportExport/reportRender)都写 mcp-go 本地 SUNDYNIX_REPORTS_DIR,gateway 再按同一路径 c.File 回流——mcp-go 与 gateway 不共享磁盘即断。 - blob 包从 gateway/internal 提到 sundynix-shared/blob,gateway 与 mcp-go 共用同一 MinIO;新增 PutBytes/GetBytes 走二进制(.docx)。 - mcp-go 注入 blob:report 源/产物优先 Put 到对象存储(键 reports/<id>.{json,docx}), 导出结果返回 minio://<key>;MinIO 未就绪回退本地盘(单机降级,getSource 兼容旧本地报告)。 - gateway ExportReport 识别 minio:// → GetBytes 流式下载,否则 c.File 本地降级。 - 测试:mcp 本地回落往返单测 + shared/blob 真 MinIO 二进制往返(BLOB_TEST_ENDPOINT 门控)。 四模块 build 绿,受影响测试全过。 - 附带:go mod tidy 引入 genproto 拆包冲突,四模块统一钉单体 genproto 新版解决 (注:勿 go work sync,会剪掉 indirect require 回落旧版重现冲突)。 Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
129 lines
5.4 KiB
Go
129 lines
5.4 KiB
Go
// Command server 启动 sundynix-mcp-go —— 第 5 层 Go I/O 型 MCP 工具微服务。
|
||
package main
|
||
|
||
import (
|
||
"context"
|
||
"log"
|
||
"os"
|
||
"os/signal"
|
||
"syscall"
|
||
|
||
"github.com/sundynix/sundynix-shared/blob"
|
||
sharedbus "github.com/sundynix/sundynix-shared/bus"
|
||
"github.com/sundynix/sundynix-shared/contract"
|
||
|
||
"github.com/sundynix/sundynix-mcp-go/internal/history"
|
||
"github.com/sundynix/sundynix-mcp-go/internal/mcp"
|
||
"github.com/sundynix/sundynix-mcp-go/internal/memory"
|
||
"github.com/sundynix/sundynix-mcp-go/internal/rag"
|
||
|
||
"github.com/sundynix/sundynix-shared/otelx"
|
||
"github.com/sundynix/sundynix-shared/prompts"
|
||
"github.com/sundynix/sundynix-shared/secrets"
|
||
)
|
||
|
||
func main() {
|
||
secrets.MustHaveKeyInProd() // 生产须配 SUNDYNIX_SECRET_KEY 以解密下发的 api_key 密文
|
||
otelx.SetupSlog("sundynix-mcp-go") // 结构化日志 + 链路感知(工具日志带 trace_id)
|
||
|
||
// 链路追踪:工具服务端 span 续上 dispatcher 的 trace(tool.call → tool.serve)。
|
||
shutdownTrace, _ := otelx.Init(context.Background(), "sundynix-mcp-go")
|
||
defer func() { _ = shutdownTrace(context.Background()) }()
|
||
|
||
// 受管 prompt:登记内置默认 + 加载 PROMPTS_FILE 覆盖(不重编译即可改图谱抽取词)。
|
||
prompts.SetDefault(prompts.GraphExtract,
|
||
"你是知识图谱抽取器。从用户文本中抽取知识三元组,输出 JSON 数组,每项形如 {\"s\":\"主体\",\"p\":\"关系\",\"o\":\"客体\"}。实体用简洁名词,关系用简短动词短语。只输出 JSON,不要任何解释或代码块标记。")
|
||
prompts.LoadFile()
|
||
|
||
natsURL := envOr("NATS_URL", "nats://localhost:4222")
|
||
// DSN 用 127.0.0.1 而非 localhost:免 pgx 每连接 DNS 解析,避免高并发连接池扩容时 DNS 雪崩
|
||
// (见 LOAD_TEST_REPORT §4.3)。
|
||
pgDSN := envOr("POSTGRES_DSN", "postgres://sundynix:sundynix@127.0.0.1:5432/sundynix?sslmode=disable")
|
||
redisAddr := envOr("REDIS_ADDR", "localhost:6379")
|
||
milvusAddr := envOr("MILVUS_ADDR", "localhost:19530")
|
||
embBase := envOr("EMBED_BASE_URL", "") // OpenAI 兼容 embeddings 端点(空=向量检索降级)
|
||
embKey := envOr("EMBED_API_KEY", "")
|
||
embModel := envOr("EMBED_MODEL", "")
|
||
rerankBase := envOr("RERANK_BASE_URL", "") // DashScope 文本重排端点(空=不启用 rerank)
|
||
rerankKey := envOr("RERANK_API_KEY", "")
|
||
rerankModel := envOr("RERANK_MODEL", "")
|
||
neo4jURI := envOr("NEO4J_URI", "neo4j://localhost:7687") // GraphRAG 图谱(连不上则降级)
|
||
neo4jUser := envOr("NEO4J_USER", "neo4j")
|
||
neo4jPass := envOr("NEO4J_PASS", "sundynix")
|
||
|
||
b, err := sharedbus.Connect(natsURL)
|
||
if err != nil {
|
||
log.Fatalf("[mcp_go] nats connect: %v", err)
|
||
}
|
||
defer b.Close()
|
||
log.Printf("[mcp_go] connected %s", natsURL)
|
||
|
||
// 报告源/产物落对象存储(与 gateway 同一 MinIO/bucket),跨进程/多机共享,
|
||
// 替代此前 SUNDYNIX_REPORTS_DIR 本地盘耦合;连不上则回退本地盘(单机降级)。
|
||
blobStore := blob.Open(
|
||
envOr("MINIO_ENDPOINT", "localhost:9000"),
|
||
envOr("MINIO_ACCESS_KEY", "minioadmin"),
|
||
envOr("MINIO_SECRET_KEY", "minioadmin"),
|
||
envOr("MINIO_BUCKET", "sundynix-docs"),
|
||
)
|
||
|
||
mem := memory.Open(pgDSN) // 偏好记忆:sundynix_user_profile(连不上则降级)
|
||
defer mem.Close()
|
||
hist := history.Open(redisAddr) // 会话短期历史:Redis(连不上则降级)
|
||
defer hist.Close()
|
||
|
||
ctx, stop := signal.NotifyContext(context.Background(), syscall.SIGINT, syscall.SIGTERM)
|
||
defer stop()
|
||
|
||
// RAG 核心链:embedding + Milvus(向量) + Bleve(全文) + Neo4j(图谱) + 可选 rerank
|
||
ragEngine := rag.Open(ctx, rag.Config{
|
||
MilvusAddr: milvusAddr,
|
||
EmbedBase: embBase, EmbedKey: embKey, EmbedModel: embModel,
|
||
RerankBase: rerankBase, RerankKey: rerankKey, RerankModel: rerankModel,
|
||
Neo4jURI: neo4jURI, Neo4jUser: neo4jUser, Neo4jPass: neo4jPass,
|
||
})
|
||
defer ragEngine.Close()
|
||
|
||
// 配置控制面:取激活 embedding(向量) + chat(图谱抽取) 配置并订阅热更新。
|
||
applyEmbed := func(cfg *contract.ModelConfig) {
|
||
if cfg != nil {
|
||
ragEngine.SetEmbedding(cfg.BaseURL, cfg.APIKey, cfg.Model)
|
||
}
|
||
}
|
||
applyChat := func(cfg *contract.ModelConfig) {
|
||
if cfg != nil {
|
||
ragEngine.SetChat(cfg.BaseURL, cfg.APIKey, cfg.Model)
|
||
}
|
||
}
|
||
// 先订阅热更新(控制台改配置即生效)。
|
||
if _, err := b.SubscribeConfigUpdated(contract.ConfigKindChat, applyChat); err != nil {
|
||
log.Printf("[mcp_go] subscribe chat config: %v", err)
|
||
}
|
||
if _, err := b.SubscribeConfigUpdated(contract.ConfigKindEmbedding, applyEmbed); err != nil {
|
||
log.Printf("[mcp_go] subscribe embedding config: %v", err)
|
||
}
|
||
// 后台重试拉初始配置:容忍 gateway 晚于本服务启动(避免一次性扑空致 RAG 长期降级)。
|
||
go b.RequestConfigWithRetry(ctx, contract.ConfigKindEmbedding, applyEmbed)
|
||
go b.RequestConfigWithRetry(ctx, contract.ConfigKindChat, applyChat)
|
||
|
||
// Prompt 控制面:拉取激活集覆盖内置默认 + 订阅热更新(管理端激活某版即生效,不重启)。
|
||
go b.RequestPromptsWithRetry(ctx, prompts.ApplyOverrides)
|
||
if _, err := b.SubscribePromptsUpdated(prompts.ApplyOverrides); err != nil {
|
||
log.Printf("[mcp_go] subscribe prompts: %v", err)
|
||
}
|
||
|
||
gw := mcp.NewGateway(b, mem, hist, ragEngine, blobStore, pgDSN)
|
||
|
||
log.Println("[mcp_go] serving MCP over sundynix.tools.go.* (Ctrl-C to quit)")
|
||
if err := gw.Serve(ctx); err != nil && err != context.Canceled {
|
||
log.Fatalf("[mcp_go] exit: %v", err)
|
||
}
|
||
}
|
||
|
||
func envOr(key, def string) string {
|
||
if v := os.Getenv(key); v != "" {
|
||
return v
|
||
}
|
||
return def
|
||
}
|