Files
MyGoNavi/internal/mcpserver/server.go
Syngnat eff2f7f63a feat(ai-mcp): 补全结构探针并优化客户端接入体验
- 新增 get_indexes、get_foreign_keys、get_triggers 内置工具与 MCP Server 对应实现
- 拆分 AI 设置中的 MCP 接入面板和服务卡片,补充参数提示与客户端状态展示
- 补齐前后端测试与真实页面验证,降低 AI 设置区域的臃肿度
2026-06-07 22:06:24 +08:00

75 lines
2.3 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_indexes",
Description: "根据 connectionId、可选 dbName、tableName 获取索引定义。",
}, service.GetIndexes)
mcp.AddTool(server, &mcp.Tool{
Name: "get_foreign_keys",
Description: "根据 connectionId、可选 dbName、tableName 获取外键关系。",
}, service.GetForeignKeys)
mcp.AddTool(server, &mcp.Tool{
Name: "get_triggers",
Description: "根据 connectionId、可选 dbName、tableName 获取触发器定义。",
}, service.GetTriggers)
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"
}