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): declare limit minimum/maximum in tool schemas so clients see the cap (#893) (#894)
bobbyhyamf4b8c7bdbd57
diff --git a/src/cmd/mcp_route_test.go b/src/cmd/mcp_route_test.goindex d0d9c008..5960f6a6 100644--- a/src/cmd/mcp_route_test.go+++ b/src/cmd/mcp_route_test.go@@ -143,3 +143,61 @@ func TestMcpToolCallRejectsInvalidArgumentsViaSchema(t *testing.T) {"expected mcp-go's validator to reject the missing 'message' field before the handler ran, got: %q", text)assert.Contains(t, text, "message", "expected the validator error to name the missing field, got: %q", text)}++// TestMcpChatSchemaDeclaresLimitBounds guards #893: the whatsapp_chat limit+// cap enforced by ValidateListChats/ValidateGetChatMessages (1..100) must be+// published in the tools/list input schema, so clients can see it before+// they call.+func TestMcpChatSchemaDeclaresLimitBounds(t *testing.T) {+ app := newMcpTestApp(false)++ rec, listRes := mcpRPC(t, app, toolsListRPC, false)+ require.Equal(t, 200, rec.Code)+ result, ok := listRes["result"].(map[string]any)+ require.True(t, ok, "tools/list result: %v", listRes)+ tools, ok := result["tools"].([]any)+ require.True(t, ok)++ var chatTool map[string]any+ for _, tl := range tools {+ if m := tl.(map[string]any); m["name"] == "whatsapp_chat" {+ chatTool = m+ }+ }+ require.NotNil(t, chatTool, "whatsapp_chat missing from tools/list")++ schema, ok := chatTool["inputSchema"].(map[string]any)+ require.True(t, ok, "whatsapp_chat inputSchema: %v", chatTool)+ props, ok := schema["properties"].(map[string]any)+ require.True(t, ok)+ limit, ok := props["limit"].(map[string]any)+ require.True(t, ok)+ assert.EqualValues(t, 1, limit["minimum"])+ assert.EqualValues(t, 100, limit["maximum"])+ offset, ok := props["offset"].(map[string]any)+ require.True(t, ok)+ assert.EqualValues(t, 0, offset["minimum"])+}++// TestMcpChatToolCallRejectsLimitOverMaximum guards #893: a limit above the+// cap is rejected by mcp-go's schema validator before the handler runs (see+// TestMcpToolCallRejectsInvalidArgumentsViaSchema for why the validator text+// is what proves that).+func TestMcpChatToolCallRejectsLimitOverMaximum(t *testing.T) {+ app := newMcpTestApp(false)++ const callRPC = `{"jsonrpc":"2.0","id":3,"method":"tools/call","params":{"name":"whatsapp_chat","arguments":{"action":"list_chats","limit":101}}}`+ rec, res := mcpRPC(t, app, callRPC, false)+ require.Equal(t, 200, rec.Code)++ result, ok := res["result"].(map[string]any)+ require.True(t, ok, "tools/call response missing result: %v", res)+ require.Equal(t, true, result["isError"], "expected schema validation failure to surface as isError:true, got: %v", res)++ content, ok := result["content"].([]any)+ require.True(t, ok && len(content) > 0, "tools/call error result missing content: %v", res)+ text, _ := content[0].(map[string]any)["text"].(string)+ assert.Contains(t, text, "input schema validation failed",+ "expected mcp-go's validator to reject limit 101 before the handler ran, got: %q", text)+ assert.Contains(t, text, "limit", "expected the validator error to name the limit field, got: %q", text)+}diff --git a/src/ui/mcp/schemas.go b/src/ui/mcp/schemas.goindex e41ee474..9e4968f6 100644--- a/src/ui/mcp/schemas.go+++ b/src/ui/mcp/schemas.go@@ -33,10 +33,10 @@ const sendSchema = `{"contact_phone": {"type": "string", "description": "type=contact: contact phone number"},"question": {"type": "string", "description": "type=poll: the poll question"},"options": {"type": "array", "items": {"type": "string"}, "minItems": 2, "description": "type=poll: poll options (min 2)"},- "max_answer": {"type": "integer", "description": "type=poll: max selectable options (default 1)"},+ "max_answer": {"type": "integer", "minimum": 1, "description": "type=poll: max selectable options, at most the number of options (default 1)"},"link": {"type": "string", "description": "type=link: the URL to send"},"message_id": {"type": "string", "description": "type=forward: source message ID from chat storage"},- "duration": {"type": "integer", "description": "type=forward: disappearing duration seconds (0, 86400, 604800, 7776000)"},+ "duration": {"type": "integer", "enum": [0, 86400, 604800, 7776000], "description": "type=forward: disappearing duration seconds (0, 86400, 604800, 7776000)"},"force_reupload": {"type": "boolean", "description": "type=forward: re-upload media instead of reusing references (default false)"},"scheduled_at": {"type": "string", "description": "Schedule instead of sending now: RFC3339 time of the first send (must be in the future)"},"timezone": {"type": "string", "description": "IANA timezone (e.g. Asia/Jakarta); required when scheduled_at is set"},@@ -103,8 +103,8 @@ const chatSchema = `{"action": {"type": "string", "enum": ["list_chats","list_contacts","get_messages","archive"], "description": "Chat/contact query or archive toggle"},"device_id": {"type": "string", "description": "Act as this device instead of the connection default"},"chat_jid": {"type": "string", "description": "get_messages/archive: chat JID (e.g. [email protected] or [email protected])"},- "limit": {"type": "integer", "description": "list_chats (default 25) / get_messages (default 50): max rows"},- "offset": {"type": "integer", "description": "list_chats/get_messages: rows to skip (default 0)"},+ "limit": {"type": "integer", "minimum": 1, "maximum": 100, "description": "list_chats (default 25) / get_messages (default 50): max rows, 1 to 100"},+ "offset": {"type": "integer", "minimum": 0, "description": "list_chats/get_messages: rows to skip (default 0)"},"search": {"type": "string", "description": "list_chats: filter by chat name; get_messages: full-text search"},"has_media": {"type": "boolean", "description": "list_chats: only chats containing media"},"start_time": {"type": "string", "description": "get_messages: only messages after this RFC3339 timestamp"},@@ -130,7 +130,7 @@ const groupSchema = `{"participants": {"type": "array", "items": {"type": "string"}, "description": "create (optional) / add_participants / remove_participants / promote / demote / manage_join_requests: phone numbers without suffix"},"invite_link": {"type": "string", "description": "join_with_link: WhatsApp invite link"},"reset": {"type": "boolean", "description": "invite_link: revoke and regenerate the link (default false)"},- "name": {"type": "string", "description": "set_name: new group name"},+ "name": {"type": "string", "maxLength": 25, "description": "set_name: new group name (max 25 characters)"},"topic": {"type": "string", "description": "set_topic: new topic/description"},"announce": {"type": "boolean", "description": "set_settings: true = only admins can send"},"locked": {"type": "boolean", "description": "set_settings: true = only admins can edit group info"},diff --git a/src/ui/mcp/schemas_test.go b/src/ui/mcp/schemas_test.goindex e45f061b..49fb280f 100644--- a/src/ui/mcp/schemas_test.go+++ b/src/ui/mcp/schemas_test.go@@ -63,6 +63,9 @@ func TestSchemas(t *testing.T) {{"send weekly integer weekdays", sendSchema, `{"type":"text","phone":"628","message":"hi","scheduled_at":"2026-09-22T10:00:00Z","timezone":"UTC","recurrence":"weekly","weekdays":[1,3]}`, false},{"send weekly string weekdays", sendSchema, `{"type":"text","phone":"628","message":"hi","scheduled_at":"2026-09-22T10:00:00Z","timezone":"UTC","recurrence":"weekly","weekdays":["1"]}`, true},{"send weekday out of range", sendSchema, `{"type":"text","phone":"628","message":"hi","recurrence":"weekly","weekdays":[7]}`, true},+ {"send poll max_answer zero", sendSchema, `{"type":"poll","phone":"628","question":"q","options":["a","b"],"max_answer":0}`, true},+ {"send forward duration allowed", sendSchema, `{"type":"forward","phone":"628","message_id":"M1","duration":604800}`, false},+ {"send forward duration not allowed", sendSchema, `{"type":"forward","phone":"628","message_id":"M1","duration":3600}`, true},// ---- whatsapp_schedule ----{"schedule list", scheduleSchema, `{"action":"list"}`, false},@@ -94,6 +97,11 @@ func TestSchemas(t *testing.T) {{"chat archive ok", chatSchema, `{"action":"archive","chat_jid":"[email protected]","archived":true}`, false},{"chat archive missing flag", chatSchema, `{"action":"archive","chat_jid":"[email protected]"}`, true},{"chat bad action", chatSchema, `{"action":"nuke"}`, true},+ {"chat list_chats limit at max", chatSchema, `{"action":"list_chats","limit":100}`, false},+ {"chat list_chats limit over max", chatSchema, `{"action":"list_chats","limit":101}`, true},+ {"chat list_chats limit zero", chatSchema, `{"action":"list_chats","limit":0}`, true},+ {"chat list_chats negative offset", chatSchema, `{"action":"list_chats","offset":-1}`, true},+ {"chat get_messages limit over max", chatSchema, `{"action":"get_messages","chat_jid":"[email protected]","limit":500}`, true},// ---- whatsapp_group ----{"group create ok", groupSchema, `{"action":"create","title":"T","participants":["628"]}`, false},@@ -112,6 +120,7 @@ func TestSchemas(t *testing.T) {{"group invite_link ok", groupSchema, `{"action":"invite_link","group_id":"[email protected]","reset":true}`, false},{"group set_name ok", groupSchema, `{"action":"set_name","group_id":"[email protected]","name":"N"}`, false},{"group set_name missing name", groupSchema, `{"action":"set_name","group_id":"[email protected]"}`, true},+ {"group set_name too long", groupSchema, `{"action":"set_name","group_id":"[email protected]","name":"abcdefghijklmnopqrstuvwxyz"}`, true},{"group set_topic ok", groupSchema, `{"action":"set_topic","group_id":"[email protected]","topic":"T"}`, false},{"group set_settings announce", groupSchema, `{"action":"set_settings","group_id":"[email protected]","announce":true}`, false},{"group set_settings locked", groupSchema, `{"action":"set_settings","group_id":"[email protected]","locked":false}`, false},