1
+"""Unit tests for the MCP model's request construction.
2
+
3
+Regression coverage for the hackathon Area 5 (MCP lifecycle) findings:
4
+
5
+- Tool-only updates must reach the API through ``allowed_tools`` — the update
6
+ endpoint never reads the create-time ``custom_tools`` alias, and tools must
7
+ not be gated behind ``toolkits``.
8
+- ``manually_manage_connections`` must invert into
9
+ ``managed_auth_via_composio`` on update, matching create/generate.
10
+- ``create`` uses the non-deprecated ``allowed_tools`` field.
11
+"""
12
+
13
+from unittest.mock import Mock
14
+
15
+import pytest
16
+
17
+from composio.core.models.mcp import MCP
18
+
19
+
20
+def _mock_update_response():
21
+ response = Mock()
22
+ response.id = "mcp_123"
23
+ response.name = "test-server"
24
+ response.allowed_tools = ["GITHUB_CREATE_ISSUE"]
25
+ response.auth_config_ids = ["ac_456"]
26
+ return response
27
+
28
+
29
+@pytest.fixture
30
+def mcp():
31
+ client = Mock()
32
+ client.mcp.update = Mock(return_value=_mock_update_response())
33
+ client.mcp.custom.create = Mock(return_value=_mock_update_response())
34
+ return MCP(client)
35
+
36
+
37
+def test_update_sends_tool_only_updates_via_allowed_tools(mcp):
38
+ """A tool-only update (no toolkits) must send `allowed_tools`."""
39
+ mcp.update("mcp_123", allowed_tools=["GITHUB_CREATE_ISSUE"])
40
+
41
+ mcp._client.mcp.update.assert_called_once_with(
42
+ "mcp_123",
43
+ allowed_tools=["GITHUB_CREATE_ISSUE"],
44
+ )
45
+
46
+
47
+def test_update_does_not_send_custom_tools_alias(mcp):
48
+ """`custom_tools` is not part of the update contract and is dropped
49
+ server-side; the SDK must never send it."""
50
+ mcp.update(
51
+ "mcp_123",
52
+ toolkits=["github"],
53
+ allowed_tools=["GITHUB_CREATE_ISSUE"],
54
+ )
55
+
56
+ call_kwargs = mcp._client.mcp.update.call_args.kwargs
57
+ assert "custom_tools" not in call_kwargs
58
+ assert call_kwargs["allowed_tools"] == ["GITHUB_CREATE_ISSUE"]
59
+ assert call_kwargs["toolkits"] == ["github"]
60
+ assert call_kwargs["auth_config_ids"] == []
61
+
62
+
63
+def test_update_inverts_manually_manage_connections(mcp):
64
+ """`manually_manage_connections=True` must store
65
+ `managed_auth_via_composio=False`, consistent with create/generate."""
66
+ mcp.update("mcp_123", manually_manage_connections=True)
67
+ assert mcp._client.mcp.update.call_args.kwargs["managed_auth_via_composio"] is False
68
+
69
+ mcp.update("mcp_123", manually_manage_connections=False)
70
+ assert mcp._client.mcp.update.call_args.kwargs["managed_auth_via_composio"] is True
71
+
72
+
73
+def test_update_omits_unset_fields(mcp):
74
+ """A sparse update must only send the fields the caller provided."""
75
+ mcp.update("mcp_123", name="renamed")
76
+
77
+ mcp._client.mcp.update.assert_called_once_with("mcp_123", name="renamed")
78
+
79
+
80
+def test_update_extracts_toolkit_and_auth_config_from_combo_entry(mcp):
81
+ """A `{toolkit, auth_config_id}` entry contributes to both lists,
82
+ matching the documented behavior."""
83
+ mcp.update(
84
+ "mcp_123",
85
+ toolkits=[{"toolkit": "github", "auth_config_id": "ac_456"}],
86
+ )
87
+
88
+ call_kwargs = mcp._client.mcp.update.call_args.kwargs
89
+ assert call_kwargs["toolkits"] == ["github"]
90
+ assert call_kwargs["auth_config_ids"] == ["ac_456"]
91
+
92
+
93
+def test_create_sends_allowed_tools_not_custom_tools(mcp):
94
+ """Create should use the non-deprecated `allowed_tools` field."""
95
+ mcp.create("test-server", toolkits=["github"], allowed_tools=["TOOL_A"])
96
+
97
+ call_kwargs = mcp._client.mcp.custom.create.call_args.kwargs
98
+ assert "custom_tools" not in call_kwargs
99
+ assert call_kwargs["allowed_tools"] == ["TOOL_A"]
100
+
101
+
102
+def test_create_inverts_manually_manage_connections(mcp):
103
+ mcp.create("test-server", toolkits=["github"], manually_manage_connections=True)
104
+ assert (
105
+ mcp._client.mcp.custom.create.call_args.kwargs["managed_auth_via_composio"]
106
+ is False
107
+ )
108
+
109
+ mcp.create("test-server", toolkits=["github"], manually_manage_connections=False)
110
+ assert (
111
+ mcp._client.mcp.custom.create.call_args.kwargs["managed_auth_via_composio"]
112
+ is True
113
+ )