
* refactor(server): 重构服务器启动和重载逻辑 将服务器启动和重载逻辑进行重构,提取初始化系统为单独函数,优化代码结构。删除冗余的服务器初始化文件,统一使用新的 `server_run.go` 实现优雅关闭和重载功能。同时,将“重启服务”改为“重载服务”以更准确地描述功能。 * refactor: 重构系统事件处理、JWT和Casbin相关逻辑 - 将系统重载逻辑提取到独立的`system_events.go`文件中,并引入全局事件管理器 - 将JWT相关操作从`service`层移动到`utils`层,减少服务层依赖 - 将Casbin实例管理逻辑提取到`utils`层,统一管理Casbin实例的初始化和获取 - 删除冗余的`CreateSysOperationRecord`方法,优化操作记录中间件逻辑 * refactor(server): 重构服务初始化和关闭逻辑 将 `RunServer` 函数重命名为 `initServer`,并调整其调用方式以简化代码。同时,在系统初始化时添加 `SetupHandlers` 函数以注册全局处理函数,提升代码可维护性。 * fix: 修复自动化代码enum查询条件的bug * fix: 修复组合模式下,顶部菜单重复bug * refactor: 修改名称 RunWindowsServer ==> RunServer * 新增mcp * feat: 支持mcp服务 * feat:调整mcp结构,增加客户端和测试用例 * feat:更换mcp基础包和结构 * feat:提交客户端工具测试用例 * feat: 增加自动创建 mcp Tool模板 功能 * fix: 增加默认值属性 * feat: 调整初始化menu的逻辑 * feat: 调整初始config.yaml * feat: 增加全局GVA_MCP_SERVER属性,方便灵活化开发。 * feat: 优化自动化mcp逻辑和成功展示 * feat: 优化mcp tool nickname工具 * feat: 发布2.8.2 Beta版本 --------- Co-authored-by: piexlMax(奇淼 <qimiaojiangjizhao@gmail.com> Co-authored-by: Gor-c <creup@outlook.com> Co-authored-by: QIN xiansheng <sjjlnaps@163.com>
133 lines
3.5 KiB
Go
133 lines
3.5 KiB
Go
package client
|
||
|
||
import (
|
||
"context"
|
||
"fmt"
|
||
"github.com/mark3labs/mcp-go/mcp"
|
||
"testing"
|
||
)
|
||
|
||
// 测试 MCP 客户端连接
|
||
func TestMcpClientConnection(t *testing.T) {
|
||
c, err := NewClient("http://localhost:8888/sse", "test-client", "1.0.0", "gin-vue-admin MCP服务")
|
||
defer c.Close()
|
||
if err != nil {
|
||
t.Fatalf(err.Error())
|
||
}
|
||
}
|
||
|
||
func TestTools(t *testing.T) {
|
||
t.Run("currentTime", func(t *testing.T) {
|
||
c, err := NewClient("http://localhost:8888/sse", "test-client", "1.0.0", "gin-vue-admin MCP服务")
|
||
defer c.Close()
|
||
if err != nil {
|
||
t.Fatalf("Failed to create client: %v", err)
|
||
}
|
||
ctx := context.Background()
|
||
|
||
request := mcp.CallToolRequest{}
|
||
request.Params.Name = "currentTime"
|
||
request.Params.Arguments = map[string]interface{}{
|
||
"timezone": "UTC+8",
|
||
}
|
||
|
||
result, err := c.CallTool(ctx, request)
|
||
if err != nil {
|
||
t.Fatalf("方法调用错误: %v", err)
|
||
}
|
||
|
||
if len(result.Content) != 1 {
|
||
t.Errorf("应该有且仅返回1条信息,但是现在有 %d", len(result.Content))
|
||
}
|
||
if content, ok := result.Content[0].(mcp.TextContent); ok {
|
||
t.Logf("成功返回信息%s", content.Text)
|
||
} else {
|
||
t.Logf("返回为止类型信息%+v", content)
|
||
}
|
||
})
|
||
|
||
t.Run("getNickname", func(t *testing.T) {
|
||
|
||
c, err := NewClient("http://localhost:8888/sse", "test-client", "1.0.0", "gin-vue-admin MCP服务")
|
||
defer c.Close()
|
||
if err != nil {
|
||
t.Fatalf("Failed to create client: %v", err)
|
||
}
|
||
ctx := context.Background()
|
||
|
||
// Initialize
|
||
initRequest := mcp.InitializeRequest{}
|
||
initRequest.Params.ProtocolVersion = mcp.LATEST_PROTOCOL_VERSION
|
||
initRequest.Params.ClientInfo = mcp.Implementation{
|
||
Name: "test-client",
|
||
Version: "1.0.0",
|
||
}
|
||
|
||
_, err = c.Initialize(ctx, initRequest)
|
||
if err != nil {
|
||
t.Fatalf("初始化失败: %v", err)
|
||
}
|
||
|
||
request := mcp.CallToolRequest{}
|
||
request.Params.Name = "getNickname"
|
||
request.Params.Arguments = map[string]interface{}{
|
||
"username": "admin",
|
||
}
|
||
|
||
result, err := c.CallTool(ctx, request)
|
||
if err != nil {
|
||
t.Fatalf("方法调用错误: %v", err)
|
||
}
|
||
|
||
if len(result.Content) != 1 {
|
||
t.Errorf("应该有且仅返回1条信息,但是现在有 %d", len(result.Content))
|
||
}
|
||
if content, ok := result.Content[0].(mcp.TextContent); ok {
|
||
t.Logf("成功返回信息%s", content.Text)
|
||
} else {
|
||
t.Logf("返回为止类型信息%+v", content)
|
||
}
|
||
})
|
||
}
|
||
|
||
func TestGetTools(t *testing.T) {
|
||
c, err := NewClient("http://localhost:8888/sse", "test-client", "1.0.0", "gin-vue-admin MCP服务")
|
||
defer c.Close()
|
||
if err != nil {
|
||
t.Fatalf("Failed to create client: %v", err)
|
||
}
|
||
ctx := context.Background()
|
||
|
||
toolsRequest := mcp.ListToolsRequest{}
|
||
|
||
toolListResult, err := c.ListTools(ctx, toolsRequest)
|
||
if err != nil {
|
||
t.Fatalf("获取工具列表失败: %v", err)
|
||
}
|
||
for i := range toolListResult.Tools {
|
||
tool := toolListResult.Tools[i]
|
||
fmt.Printf("工具名称: %s\n", tool.Name)
|
||
fmt.Printf("工具描述: %s\n", tool.Description)
|
||
|
||
// 打印参数信息
|
||
if tool.InputSchema.Properties != nil {
|
||
fmt.Println("参数列表:")
|
||
for paramName, prop := range tool.InputSchema.Properties {
|
||
required := "否"
|
||
// 检查参数是否在必填列表中
|
||
for _, reqField := range tool.InputSchema.Required {
|
||
if reqField == paramName {
|
||
required = "是"
|
||
break
|
||
}
|
||
}
|
||
fmt.Printf(" - %s (类型: %s, 描述: %s, 必填: %s)\n",
|
||
paramName, prop.(map[string]any)["type"], prop.(map[string]any)["description"], required)
|
||
}
|
||
} else {
|
||
fmt.Println("该工具没有参数")
|
||
}
|
||
fmt.Println("-------------------")
|
||
}
|
||
}
|