MCPcopy Create free account
hub / github.com/OpenListTeam/OpenList / TestToolsListSuccess

Function TestToolsListSuccess

server/mcp/call_test.go:50–103  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

48}
49
50func TestToolsListSuccess(t *testing.T) {
51 gin.SetMode(gin.TestMode)
52 srv := newTestServer(map[string]*session{
53 "s2": {id: "s2", userID: 1, initialized: true},
54 })
55
56 r := gin.New()
57 r.POST("/mcp", func(c *gin.Context) {
58 common.GinAppendValues(c, conf.UserKey, &model.User{ID: 1, Role: model.ADMIN})
59 srv.handlePost(c)
60 })
61
62 req := httptest.NewRequest(http.MethodPost, "http://example.com/mcp", strings.NewReader(`{
63 "jsonrpc":"2.0",
64 "id":2,
65 "method":"tools/list"
66 }`))
67 req.Header.Set("Accept", "application/json, text/event-stream")
68 req.Header.Set("Origin", "http://example.com")
69 req.Header.Set(ProtocolVersionHeader, ProtocolVersion)
70 req.Header.Set(SessionHeader, "s2")
71
72 w := httptest.NewRecorder()
73 r.ServeHTTP(w, req)
74
75 if w.Code != http.StatusOK {
76 t.Fatalf("unexpected status: got %d want %d", w.Code, http.StatusOK)
77 }
78 resp := decodeResponse(t, w)
79 if resp.Error != nil {
80 t.Fatalf("unexpected error response: %+v", resp.Error)
81 }
82
83 result, ok := resp.Result.(map[string]any)
84 if !ok {
85 t.Fatalf("unexpected result type: %T", resp.Result)
86 }
87 tools, ok := result["tools"].([]any)
88 if !ok || len(tools) != 3 {
89 t.Fatalf("unexpected tools payload: %#v", result["tools"])
90 }
91 names := map[string]bool{}
92 for _, rawTool := range tools {
93 currentTool, ok := rawTool.(map[string]any)
94 if !ok {
95 t.Fatalf("unexpected tool payload: %#v", rawTool)
96 }
97 name, _ := currentTool["name"].(string)
98 names[name] = true
99 }
100 if !names["openlist.fs.list"] || !names["openlist.fs.get"] || !names["openlist.fs.link"] {
101 t.Fatalf("unexpected tool names: %#v", names)
102 }
103}
104
105func TestToolsCallUnknownTool(t *testing.T) {
106 gin.SetMode(gin.TestMode)

Callers

nothing calls this directly

Calls 6

GinAppendValuesFunction · 0.92
newTestServerFunction · 0.85
decodeResponseFunction · 0.85
handlePostMethod · 0.80
ServeHTTPMethod · 0.80
SetMethod · 0.45

Tested by

no test coverage detected