- 补入库:server/internal/db/dump.go、redis 分页/扫描预算测试、opsxdb-mcp main_test、 packages/opsxdb-mcp package-lock、docs 审计与对比文档等 37 个新文件 - 含结构扁平化进行中的 80 处修改与 155 处旧结构删除(server/ src/ src-tauri/ 为当前有效源) - 构建产物(bin/target/dist)与签名私钥仍按规则排除
58 lines
1.6 KiB
Go
58 lines
1.6 KiB
Go
// opsxdb-mcp:stdio MCP Server,按本机策略暴露数据库工具。
|
||
//
|
||
// 配置:
|
||
// ~/.opsxdb/mcp.json — allowed_connection_ids;mode;每次工具调用重新读取
|
||
// ~/.opsxdb/connections.json — 桌面双写的连接列表(密码 ox1. 混淆)
|
||
// ~/.opsxdb/logs/mcp-audit.jsonl
|
||
//
|
||
// Cursor 示例见 docs/mcp-cursor.example.json
|
||
package main
|
||
|
||
import (
|
||
"context"
|
||
"encoding/json"
|
||
"fmt"
|
||
"os"
|
||
|
||
"github.com/mark3labs/mcp-go/mcp"
|
||
"github.com/mark3labs/mcp-go/server"
|
||
|
||
"opsxdb/server/internal/aitools"
|
||
)
|
||
|
||
func main() {
|
||
s := server.NewMCPServer(
|
||
"opsxdb",
|
||
"0.1.0",
|
||
server.WithToolCapabilities(true),
|
||
)
|
||
|
||
for _, spec := range aitools.ToolSpecs() {
|
||
raw, _ := json.Marshal(spec.InputSchema())
|
||
t := mcp.NewToolWithRawSchema(spec.Name, spec.Description, raw)
|
||
toolName := spec.Name
|
||
s.AddTool(t, func(ctx context.Context, req mcp.CallToolRequest) (*mcp.CallToolResult, error) {
|
||
out, err := callTool(ctx, toolName, req.GetArguments(), aitools.DefaultRuntime)
|
||
if err != nil {
|
||
return mcp.NewToolResultError(err.Error()), nil
|
||
}
|
||
return mcp.NewToolResultText(aitools.ResultJSON(out)), nil
|
||
})
|
||
}
|
||
|
||
if err := server.ServeStdio(s); err != nil {
|
||
fmt.Fprintf(os.Stderr, "opsxdb-mcp: %v\n", err)
|
||
os.Exit(1)
|
||
}
|
||
}
|
||
|
||
// 每次调用使用新的策略快照;读取失败直接拒绝,不继续用上一次的权限。
|
||
func callTool(ctx context.Context, tool string, args map[string]any, load func() (*aitools.Runtime, error)) (any, error) {
|
||
rt, err := load()
|
||
if err != nil {
|
||
return nil, fmt.Errorf("load policy: %w", err)
|
||
}
|
||
rt.Source = "mcp"
|
||
return rt.Call(ctx, tool, args)
|
||
}
|