package toolkit import ( "context" "testing" "github.com/modelcontextprotocol/go-sdk/mcp" ) func TestTextAndError(t *testing.T) { txt := Text("hello") if txt.IsError { t.Error("Text must not be an error") } if len(txt.Content) != 1 { t.Fatalf("content = %v", txt.Content) } errRes := Error("boom") if !errRes.IsError { t.Error("Error must set IsError") } } func TestIsProbe(t *testing.T) { probe := &mcp.CallToolRequest{Params: &mcp.CallToolParamsRaw{ Meta: mcp.Meta{metaProbeKey: true}, }} if !IsProbe(probe) { t.Error("IsProbe = false, want true") } if IsProbe(nil) { t.Error("nil request must not be probe") } if IsProbe(&mcp.CallToolRequest{Params: &mcp.CallToolParamsRaw{}}) { t.Error("no meta must not be probe") } } func TestWrapProbeInterceptsAndDelegates(t *testing.T) { handlerCalled := false h := func(context.Context, *mcp.CallToolRequest) (*mcp.CallToolResult, error) { handlerCalled = true return Text("real"), nil } fn := func(args map[string]any) ([]string, []string) { return []string{"run:cat *"}, []string{"cat *"} } wrapped := WrapProbe(h, fn) // Probe: действие не выполняется, возвращаются паттерны в _meta. probe := &mcp.CallToolRequest{Params: &mcp.CallToolParamsRaw{ Meta: mcp.Meta{metaProbeKey: true}, Arguments: []byte(`{"command":"cat /etc/hosts"}`), }} res, err := wrapped(context.Background(), probe) if err != nil { t.Fatalf("probe: %v", err) } if handlerCalled { t.Error("handler must not run on probe") } meta := res.GetMeta() if meta[metaPatternsKey] == nil || meta[metaAlwaysKey] == nil { t.Errorf("probe meta = %v", meta) } // Обычный вызов: делегируется handler. normal := &mcp.CallToolRequest{Params: &mcp.CallToolParamsRaw{}} if _, err := wrapped(context.Background(), normal); err != nil { t.Fatalf("normal: %v", err) } if !handlerCalled { t.Error("handler must run on normal call") } }