Files
MyGoNavi/internal/mcpserver/server.go
Syngnat 5b843ee25b feat(ai-mcp): 完善外部客户端安装链路并收紧 SQL 安全控制
- 新增 GoNavi MCP stdio server 与 Claude/Codex 用户级安装入口

- 增加安装状态检测、刷新复制能力和浏览器联调 mock

- 外部 execute_sql 对齐 GoNavi safetyLevel 并补充前端/后端验证
2026-06-07 20:27:50 +08:00

60 lines
1.8 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
package mcpserver
import (
"runtime/debug"
"strings"
"github.com/modelcontextprotocol/go-sdk/mcp"
)
func NewServer(backend Backend) *mcp.Server {
server := mcp.NewServer(&mcp.Implementation{
Name: "gonavi-ai",
Version: implementationVersion(),
}, nil)
service := NewService(backend)
mcp.AddTool(server, &mcp.Tool{
Name: "get_connections",
Description: "列出当前 GoNavi 已保存的数据库连接,先调用它获取 connectionId。不会返回明文密码等敏感信息。",
}, service.GetConnections)
mcp.AddTool(server, &mcp.Tool{
Name: "get_databases",
Description: "根据 connectionId 获取数据库/Schema 列表。",
}, service.GetDatabases)
mcp.AddTool(server, &mcp.Tool{
Name: "get_tables",
Description: "根据 connectionId 和可选 dbName 获取表列表。dbName 为空时优先使用保存连接里的默认数据库。",
}, service.GetTables)
mcp.AddTool(server, &mcp.Tool{
Name: "get_columns",
Description: "根据 connectionId、可选 dbName、tableName 获取字段定义。",
}, service.GetColumns)
mcp.AddTool(server, &mcp.Tool{
Name: "get_table_ddl",
Description: "根据 connectionId、可选 dbName、tableName 获取建表或建视图语句。",
}, service.GetTableDDL)
mcp.AddTool(server, &mcp.Tool{
Name: "execute_sql",
Description: "执行 SQL支持多语句结果集。执行范围受 GoNavi AI 设置中的安全控制约束;命中允许范围内的 DML/DDL 等非只读语句时,仍必须显式传 allowMutating=true。",
}, service.ExecuteSQL)
return server
}
func implementationVersion() string {
if info, ok := debug.ReadBuildInfo(); ok {
version := strings.TrimSpace(info.Main.Version)
if version != "" && version != "(devel)" {
return version
}
}
return "dev"
}