GOWA - WhatsApp REST API with support for UI, Multi Account, Webhooks, and MCP, and Chatwoot. Built with Golang for efficient memory use.
Go botgogolanggolang-whatsappgolang-whatsapp-apirestrest-apiwhatsappwhatsapp-apiwhatsapp-api-gowhatsapp-multi-devicewhatsapp-web-multi-device
fix(mcp): serialize query results into text content for backwards compatibility (#848) (#849)
Edo Sulaiman543977e5826f
diff --git a/src/ui/mcp/chat.go b/src/ui/mcp/chat.goindex fe61ccbd..926113c9 100644--- a/src/ui/mcp/chat.go+++ b/src/ui/mcp/chat.go@@ -60,13 +60,13 @@ func (h *ChatHandler) handleChat(ctx context.Context, request mcpg.CallToolRequeif err != nil {return mcpg.NewToolResultError(err.Error()), nil}- return mcpg.NewToolResultStructured(resp, fmt.Sprintf("Retrieved %d chats (offset %d, limit %d)", len(resp.Data), req.Offset, req.Limit)), nil+ return mcpg.NewToolResultStructuredOnly(resp), nilcase "list_contacts":resp, err := h.userService.MyListContacts(ctx)if err != nil {return mcpg.NewToolResultError(err.Error()), nil}- return mcpg.NewToolResultStructured(resp, fmt.Sprintf("Found %d contacts", len(resp.Data))), nil+ return mcpg.NewToolResultStructuredOnly(resp), nilcase "get_messages":chatJID := request.GetString("chat_jid", "")var startTimePtr, endTimePtr *string@@ -97,7 +97,7 @@ func (h *ChatHandler) handleChat(ctx context.Context, request mcpg.CallToolRequeif err != nil {return mcpg.NewToolResultError(err.Error()), nil}- return mcpg.NewToolResultStructured(resp, fmt.Sprintf("Retrieved %d messages from %s", len(resp.Data), chatJID)), nil+ return mcpg.NewToolResultStructuredOnly(resp), nilcase "archive":req := domainChat.ArchiveChatRequest{ChatJID: request.GetString("chat_jid", ""),diff --git a/src/ui/mcp/chat_test.go b/src/ui/mcp/chat_test.goindex e26ddb0f..4a8acf14 100644--- a/src/ui/mcp/chat_test.go+++ b/src/ui/mcp/chat_test.go@@ -6,8 +6,10 @@ import (domainChat "github.com/aldinokemal/go-whatsapp-web-multidevice/domains/chat"domainUser "github.com/aldinokemal/go-whatsapp-web-multidevice/domains/user"+ mcpg "github.com/mark3labs/mcp-go/mcp""github.com/stretchr/testify/assert""github.com/stretchr/testify/require"+ "go.mau.fi/whatsmeow/types")type stubChatService struct {@@ -15,15 +17,17 @@ type stubChatService struct {listed *domainChat.ListChatsRequestfetched *domainChat.GetChatMessagesRequestarchived *domainChat.ArchiveChatRequest+ listResp domainChat.ListChatsResponse+ msgResp domainChat.GetChatMessagesResponse}func (s *stubChatService) ListChats(_ context.Context, r domainChat.ListChatsRequest) (domainChat.ListChatsResponse, error) {s.listed = &r- return domainChat.ListChatsResponse{}, nil+ return s.listResp, nil}func (s *stubChatService) GetChatMessages(_ context.Context, r domainChat.GetChatMessagesRequest) (domainChat.GetChatMessagesResponse, error) {s.fetched = &r- return domainChat.GetChatMessagesResponse{}, nil+ return s.msgResp, nil}func (s *stubChatService) ArchiveChat(_ context.Context, r domainChat.ArchiveChatRequest) (domainChat.ArchiveChatResponse, error) {s.archived = &r@@ -33,18 +37,25 @@ func (s *stubChatService) ArchiveChat(_ context.Context, r domainChat.ArchiveChatype stubUserService struct {domainUser.IUserUsecasecontactsCalled bool+ contactsResp domainUser.MyListContactsResponse}func (s *stubUserService) MyListContacts(_ context.Context) (domainUser.MyListContactsResponse, error) {s.contactsCalled = true- return domainUser.MyListContactsResponse{}, nil+ return s.contactsResp, nil}func TestHandleChatDispatch(t *testing.T) {t.Run("list_chats with filters", func(t *testing.T) {- cs, us := &stubChatService{}, &stubUserService{}+ cs, us := &stubChatService{+ listResp: domainChat.ListChatsResponse{+ Data: []domainChat.ChatInfo{+ {JID: "[email protected]", Name: "Alice"},+ },+ },+ }, &stubUserService{}h := InitMcpChat(cs, us, &stubResolver{})- _, err := h.handleChat(deviceCtx(), callReq(map[string]any{+ res, err := h.handleChat(deviceCtx(), callReq(map[string]any{"action": "list_chats", "limit": 10, "search": "bob", "has_media": true,}))require.NoError(t, err)@@ -52,20 +63,48 @@ func TestHandleChatDispatch(t *testing.T) {assert.Equal(t, 10, cs.listed.Limit)assert.Equal(t, "bob", cs.listed.Search)assert.True(t, cs.listed.HasMedia)+ require.NotNil(t, res)+ require.NotEmpty(t, res.Content)+ text, ok := mcpg.AsTextContent(res.Content[0])+ require.True(t, ok)+ assert.Contains(t, text.Text, "[email protected]")+ assert.Contains(t, text.Text, "Alice")+ assert.NotNil(t, res.StructuredContent)})t.Run("list_contacts", func(t *testing.T) {- cs, us := &stubChatService{}, &stubUserService{}+ jid, err := types.ParseJID("[email protected]")+ require.NoError(t, err)+ cs, us := &stubChatService{}, &stubUserService{+ contactsResp: domainUser.MyListContactsResponse{+ Data: []domainUser.MyListContactsResponseData{+ {JID: jid, Name: "Bob"},+ },+ },+ }h := InitMcpChat(cs, us, &stubResolver{})- _, err := h.handleChat(deviceCtx(), callReq(map[string]any{"action": "list_contacts"}))+ res, err := h.handleChat(deviceCtx(), callReq(map[string]any{"action": "list_contacts"}))require.NoError(t, err)assert.True(t, us.contactsCalled)+ require.NotNil(t, res)+ require.NotEmpty(t, res.Content)+ text, ok := mcpg.AsTextContent(res.Content[0])+ require.True(t, ok)+ assert.Contains(t, text.Text, "[email protected]")+ assert.Contains(t, text.Text, "Bob")+ assert.NotNil(t, res.StructuredContent)})t.Run("get_messages with time filters", func(t *testing.T) {- cs, us := &stubChatService{}, &stubUserService{}+ cs, us := &stubChatService{+ msgResp: domainChat.GetChatMessagesResponse{+ Data: []domainChat.MessageInfo{+ {ID: "msg-123", Content: "hello world"},+ },+ },+ }, &stubUserService{}h := InitMcpChat(cs, us, &stubResolver{})- _, err := h.handleChat(deviceCtx(), callReq(map[string]any{+ res, err := h.handleChat(deviceCtx(), callReq(map[string]any{"action": "get_messages", "chat_jid": "[email protected]","start_time": "2026-01-01T00:00:00Z", "is_from_me": true,}))@@ -76,6 +115,13 @@ func TestHandleChatDispatch(t *testing.T) {assert.Equal(t, "2026-01-01T00:00:00Z", *cs.fetched.StartTime)require.NotNil(t, cs.fetched.IsFromMe)assert.True(t, *cs.fetched.IsFromMe)+ require.NotNil(t, res)+ require.NotEmpty(t, res.Content)+ text, ok := mcpg.AsTextContent(res.Content[0])+ require.True(t, ok)+ assert.Contains(t, text.Text, "msg-123")+ assert.Contains(t, text.Text, "hello world")+ assert.NotNil(t, res.StructuredContent)})t.Run("archive", func(t *testing.T) {diff --git a/src/ui/mcp/group.go b/src/ui/mcp/group.goindex 2ca6c86a..cb7c9801 100644--- a/src/ui/mcp/group.go+++ b/src/ui/mcp/group.go@@ -81,13 +81,13 @@ func (h *GroupHandler) handleGroup(ctx context.Context, request mcpg.CallToolReqif err != nil {return mcpg.NewToolResultError(err.Error()), nil}- return mcpg.NewToolResultStructured(resp, fmt.Sprintf("Fetched group info for %s", groupID)), nil+ return mcpg.NewToolResultStructuredOnly(resp), nilcase "participants":resp, err := h.groupService.GetGroupParticipants(ctx, domainGroup.GetGroupParticipantsRequest{GroupID: groupID})if err != nil {return mcpg.NewToolResultError(err.Error()), nil}- return mcpg.NewToolResultStructured(resp, fmt.Sprintf("Group %s has %d participants", resp.GroupID, len(resp.Participants))), nil+ return mcpg.NewToolResultStructuredOnly(resp), nilcase "add_participants", "remove_participants", "promote", "demote":change := map[string]whatsmeow.ParticipantChange{"add_participants": whatsmeow.ParticipantChangeAdd,@@ -150,7 +150,7 @@ func (h *GroupHandler) handleGroup(ctx context.Context, request mcpg.CallToolReqif err != nil {return mcpg.NewToolResultError(err.Error()), nil}- return mcpg.NewToolResultStructured(resp, fmt.Sprintf("Group %s has %d pending requests", groupID, len(resp))), nil+ return mcpg.NewToolResultStructuredOnly(resp), nilcase "manage_join_requests":change, err := parseParticipantRequestChange(request.GetString("request_action", ""))if err != nil {diff --git a/src/ui/mcp/group_test.go b/src/ui/mcp/group_test.goindex 4147b864..bc7cdb0f 100644--- a/src/ui/mcp/group_test.go+++ b/src/ui/mcp/group_test.go@@ -5,6 +5,7 @@ import ("testing"domainGroup "github.com/aldinokemal/go-whatsapp-web-multidevice/domains/group"+ mcpg "github.com/mark3labs/mcp-go/mcp""github.com/stretchr/testify/assert""github.com/stretchr/testify/require""go.mau.fi/whatsmeow"@@ -25,6 +26,9 @@ type stubGroupService struct {lockedReq *domainGroup.SetGroupLockedRequestjoinReqsReq *domainGroup.GetGroupRequestParticipantsRequestmanagedJoins *domainGroup.GroupRequestParticipantsRequest+ infoResp domainGroup.GroupInfoResponse+ partsResp domainGroup.GetGroupParticipantsResponse+ joinReqsResp []domainGroup.GetGroupRequestParticipantsResponse}func (s *stubGroupService) CreateGroup(_ context.Context, r domainGroup.CreateGroupRequest) (string, error) {@@ -41,11 +45,11 @@ func (s *stubGroupService) LeaveGroup(_ context.Context, r domainGroup.LeaveGrou}func (s *stubGroupService) GroupInfo(_ context.Context, r domainGroup.GroupInfoRequest) (domainGroup.GroupInfoResponse, error) {s.infoReq = &r- return domainGroup.GroupInfoResponse{}, nil+ return s.infoResp, nil}func (s *stubGroupService) GetGroupParticipants(_ context.Context, r domainGroup.GetGroupParticipantsRequest) (domainGroup.GetGroupParticipantsResponse, error) {s.partsReq = &r- return domainGroup.GetGroupParticipantsResponse{}, nil+ return s.partsResp, nil}func (s *stubGroupService) ManageParticipant(_ context.Context, r domainGroup.ParticipantRequest) ([]domainGroup.ParticipantStatus, error) {s.managed = &r@@ -73,7 +77,7 @@ func (s *stubGroupService) SetGroupLocked(_ context.Context, r domainGroup.SetGr}func (s *stubGroupService) GetGroupRequestParticipants(_ context.Context, r domainGroup.GetGroupRequestParticipantsRequest) ([]domainGroup.GetGroupRequestParticipantsResponse, error) {s.joinReqsReq = &r- return nil, nil+ return s.joinReqsResp, nil}func (s *stubGroupService) ManageGroupRequestParticipants(_ context.Context, r domainGroup.GroupRequestParticipantsRequest) ([]domainGroup.ParticipantStatus, error) {s.managedJoins = &r@@ -115,16 +119,38 @@ func TestHandleGroupDispatch(t *testing.T) {t.Run("info", func(t *testing.T) {svc, h := newHandler()- _, err := h.handleGroup(deviceCtx(), callReq(map[string]any{"action": "info", "group_id": "[email protected]"}))+ svc.infoResp = domainGroup.GroupInfoResponse{+ Data: map[string]any{+ "Name": "Secret Group",+ },+ }+ res, err := h.handleGroup(deviceCtx(), callReq(map[string]any{"action": "info", "group_id": "[email protected]"}))require.NoError(t, err)require.NotNil(t, svc.infoReq)+ require.NotNil(t, res)+ require.NotEmpty(t, res.Content)+ text, ok := mcpg.AsTextContent(res.Content[0])+ require.True(t, ok)+ assert.Contains(t, text.Text, "Secret Group")+ assert.NotNil(t, res.StructuredContent)})t.Run("participants", func(t *testing.T) {svc, h := newHandler()- _, err := h.handleGroup(deviceCtx(), callReq(map[string]any{"action": "participants", "group_id": "[email protected]"}))+ svc.partsResp = domainGroup.GetGroupParticipantsResponse{+ Participants: []domainGroup.GroupParticipant{+ {JID: "[email protected]", PhoneNumber: "628111"},+ },+ }+ res, err := h.handleGroup(deviceCtx(), callReq(map[string]any{"action": "participants", "group_id": "[email protected]"}))require.NoError(t, err)require.NotNil(t, svc.partsReq)+ require.NotNil(t, res)+ require.NotEmpty(t, res.Content)+ text, ok := mcpg.AsTextContent(res.Content[0])+ require.True(t, ok)+ assert.Contains(t, text.Text, "[email protected]")+ assert.NotNil(t, res.StructuredContent)})t.Run("participant changes map to whatsmeow actions", func(t *testing.T) {@@ -197,9 +223,18 @@ func TestHandleGroupDispatch(t *testing.T) {t.Run("join_requests", func(t *testing.T) {svc, h := newHandler()- _, err := h.handleGroup(deviceCtx(), callReq(map[string]any{"action": "join_requests", "group_id": "[email protected]"}))+ svc.joinReqsResp = []domainGroup.GetGroupRequestParticipantsResponse{+ {JID: "[email protected]"},+ }+ res, err := h.handleGroup(deviceCtx(), callReq(map[string]any{"action": "join_requests", "group_id": "[email protected]"}))require.NoError(t, err)require.NotNil(t, svc.joinReqsReq)+ require.NotNil(t, res)+ require.NotEmpty(t, res.Content)+ text, ok := mcpg.AsTextContent(res.Content[0])+ require.True(t, ok)+ assert.Contains(t, text.Text, "[email protected]")+ assert.NotNil(t, res.StructuredContent)})t.Run("manage_join_requests", func(t *testing.T) {