client_test.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. package client
  2. import (
  3. "context"
  4. "fmt"
  5. "github.com/mark3labs/mcp-go/mcp"
  6. "testing"
  7. )
  8. // 测试 MCP 客户端连接
  9. func TestMcpClientConnection(t *testing.T) {
  10. c, err := NewClient("http://localhost:8888/sse", "test-client", "1.0.0", "gin-vue-admin MCP服务")
  11. defer c.Close()
  12. if err != nil {
  13. t.Fatalf(err.Error())
  14. }
  15. }
  16. func TestTools(t *testing.T) {
  17. t.Run("currentTime", func(t *testing.T) {
  18. c, err := NewClient("http://localhost:8888/sse", "test-client", "1.0.0", "gin-vue-admin MCP服务")
  19. defer c.Close()
  20. if err != nil {
  21. t.Fatalf("Failed to create client: %v", err)
  22. }
  23. ctx := context.Background()
  24. request := mcp.CallToolRequest{}
  25. request.Params.Name = "currentTime"
  26. request.Params.Arguments = map[string]interface{}{
  27. "timezone": "UTC+8",
  28. }
  29. result, err := c.CallTool(ctx, request)
  30. if err != nil {
  31. t.Fatalf("方法调用错误: %v", err)
  32. }
  33. if len(result.Content) != 1 {
  34. t.Errorf("应该有且仅返回1条信息,但是现在有 %d", len(result.Content))
  35. }
  36. if content, ok := result.Content[0].(mcp.TextContent); ok {
  37. t.Logf("成功返回信息%s", content.Text)
  38. } else {
  39. t.Logf("返回为止类型信息%+v", content)
  40. }
  41. })
  42. t.Run("getNickname", func(t *testing.T) {
  43. c, err := NewClient("http://localhost:8888/sse", "test-client", "1.0.0", "gin-vue-admin MCP服务")
  44. defer c.Close()
  45. if err != nil {
  46. t.Fatalf("Failed to create client: %v", err)
  47. }
  48. ctx := context.Background()
  49. // Initialize
  50. initRequest := mcp.InitializeRequest{}
  51. initRequest.Params.ProtocolVersion = mcp.LATEST_PROTOCOL_VERSION
  52. initRequest.Params.ClientInfo = mcp.Implementation{
  53. Name: "test-client",
  54. Version: "1.0.0",
  55. }
  56. _, err = c.Initialize(ctx, initRequest)
  57. if err != nil {
  58. t.Fatalf("初始化失败: %v", err)
  59. }
  60. request := mcp.CallToolRequest{}
  61. request.Params.Name = "getNickname"
  62. request.Params.Arguments = map[string]interface{}{
  63. "username": "admin",
  64. }
  65. result, err := c.CallTool(ctx, request)
  66. if err != nil {
  67. t.Fatalf("方法调用错误: %v", err)
  68. }
  69. if len(result.Content) != 1 {
  70. t.Errorf("应该有且仅返回1条信息,但是现在有 %d", len(result.Content))
  71. }
  72. if content, ok := result.Content[0].(mcp.TextContent); ok {
  73. t.Logf("成功返回信息%s", content.Text)
  74. } else {
  75. t.Logf("返回为止类型信息%+v", content)
  76. }
  77. })
  78. }
  79. func TestGetTools(t *testing.T) {
  80. c, err := NewClient("http://localhost:8888/sse", "test-client", "1.0.0", "gin-vue-admin MCP服务")
  81. defer c.Close()
  82. if err != nil {
  83. t.Fatalf("Failed to create client: %v", err)
  84. }
  85. ctx := context.Background()
  86. toolsRequest := mcp.ListToolsRequest{}
  87. toolListResult, err := c.ListTools(ctx, toolsRequest)
  88. if err != nil {
  89. t.Fatalf("获取工具列表失败: %v", err)
  90. }
  91. for i := range toolListResult.Tools {
  92. tool := toolListResult.Tools[i]
  93. fmt.Printf("工具名称: %s\n", tool.Name)
  94. fmt.Printf("工具描述: %s\n", tool.Description)
  95. // 打印参数信息
  96. if tool.InputSchema.Properties != nil {
  97. fmt.Println("参数列表:")
  98. for paramName, prop := range tool.InputSchema.Properties {
  99. required := "否"
  100. // 检查参数是否在必填列表中
  101. for _, reqField := range tool.InputSchema.Required {
  102. if reqField == paramName {
  103. required = "是"
  104. break
  105. }
  106. }
  107. fmt.Printf(" - %s (类型: %s, 描述: %s, 必填: %s)\n",
  108. paramName, prop.(map[string]any)["type"], prop.(map[string]any)["description"], required)
  109. }
  110. } else {
  111. fmt.Println("该工具没有参数")
  112. }
  113. fmt.Println("-------------------")
  114. }
  115. }