1
+"""Tests for provider-facing tool schema aliases."""
2
+
3
+import asyncio
4
+import copy
5
+import importlib.util
6
+import inspect
7
+import re
8
+import sys
9
+import types
10
+from pathlib import Path
11
+from types import SimpleNamespace
12
+from unittest.mock import Mock
13
+
14
+import pytest
15
+
16
+from composio.exceptions import InvalidSchemaError
17
+from composio.utils.shared import (
18
+ alias_tool_input_schema,
19
+ json_schema_to_model,
20
+ substitute_reserved_python_keywords,
21
+)
22
+
23
+
24
+PYTHON_ROOT = Path(__file__).resolve().parents[1]
25
+ANTHROPIC_PROPERTY_RE = re.compile(r"^[a-zA-Z0-9_.-]{1,64}$")
26
+
27
+
28
+def _load_module(monkeypatch, module_name: str, path: Path):
29
+ spec = importlib.util.spec_from_file_location(module_name, path)
30
+ assert spec is not None
31
+ assert spec.loader is not None
32
+ module = importlib.util.module_from_spec(spec)
33
+ monkeypatch.setitem(sys.modules, module_name, module)
34
+ spec.loader.exec_module(module)
35
+ return module
36
+
37
+
38
+def test_alias_tool_input_schema_restores_nested_aliases_without_mutating_schema():
39
+ schema = {
40
+ "type": "object",
41
+ "properties": {
42
+ "from": {"type": "string"},
43
+ "payload": {
44
+ "type": "object",
45
+ "properties": {"class": {"type": "string"}},
46
+ "required": ["class"],
47
+ },
48
+ "items": {
49
+ "type": "array",
50
+ "items": {
51
+ "type": "object",
52
+ "properties": {"for": {"type": "string"}},
53
+ "required": ["for"],
54
+ },
55
+ },
56
+ },
57
+ "required": ["from", "payload"],
58
+ }
59
+ original = copy.deepcopy(schema)
60
+
61
+ aliases = alias_tool_input_schema(schema)
62
+
63
+ assert schema == original
64
+ assert list(aliases.schema["properties"]) == ["from_rs", "payload", "items"]
65
+ assert aliases.schema["required"] == ["from_rs", "payload"]
66
+ assert aliases.schema["properties"]["payload"]["required"] == ["class_rs"]
67
+ assert aliases.schema["properties"]["items"]["items"]["required"] == ["for_rs"]
68
+
69
+ arguments = {
70
+ "from_rs": "sender@example.com",
71
+ "payload": {"class_rs": "primary"},
72
+ "items": [{"for_rs": "recipient@example.com"}],
73
+ }
74
+ assert aliases.restore_arguments(arguments) == {
75
+ "from": "sender@example.com",
76
+ "payload": {"class": "primary"},
77
+ "items": [{"for": "recipient@example.com"}],
78
+ }
79
+
80
+
81
+def test_alias_tool_input_schema_rejects_duplicate_aliases():
82
+ schema = {
83
+ "type": "object",
84
+ "properties": {
85
+ "from": {"type": "string"},
86
+ "from_rs": {"type": "string"},
87
+ },
88
+ }
89
+
90
+ with pytest.raises(InvalidSchemaError, match="duplicate Python parameter alias"):
91
+ alias_tool_input_schema(schema)
92
+
93
+
94
+def test_legacy_keyword_helpers_use_tool_schema_aliases():
95
+ long_name = "x" * 80
96
+ schema = {
97
+ "title": "ODataParams",
98
+ "type": "object",
99
+ "properties": {
100
+ "$top": {"type": "integer"},
101
+ "@microsoft.graph.conflictBehavior": {"type": "string"},
102
+ long_name: {"type": "string"},
103
+ },
104
+ "required": ["$top", long_name],
105
+ }
106
+
107
+ aliased_schema, aliases = substitute_reserved_python_keywords(schema)
108
+
109
+ aliased_names = list(aliased_schema["properties"])
110
+ assert aliased_names[0] == "param_top"
111
+ assert aliased_names[1] == "param_microsoft_graph_conflictBehavior"
112
+ assert len(aliased_names[2]) == 64
113
+ assert all(ANTHROPIC_PROPERTY_RE.fullmatch(name) for name in aliased_names)
114
+ assert aliased_schema["required"] == ["param_top", aliased_names[2]]
115
+ assert aliases["param_top"] == "$top"
116
+ assert aliases["param_microsoft_graph_conflictBehavior"] == (
117
+ "@microsoft.graph.conflictBehavior"
118
+ )
119
+ assert aliases[aliased_names[2]] == long_name
120
+ model = json_schema_to_model(aliased_schema)
121
+ assert "param_top" in model.model_fields
122
+
123
+
124
+def test_alias_tool_input_schema_dereferences_refs_before_aliasing():
125
+ schema = {
126
+ "$ref": "#/$defs/SearchParams",
127
+ "$defs": {
128
+ "SearchParams": {
129
+ "type": "object",
130
+ "properties": {
131
+ "filter": {"$ref": "#/$defs/ODataFilter"},
132
+ },
133
+ "required": ["filter"],
134
+ },
135
+ "ODataFilter": {
136
+ "type": "object",
137
+ "properties": {"$top": {"type": "integer"}},
138
+ "required": ["$top"],
139
+ },
140
+ },
141
+ }
142
+
143
+ aliases = alias_tool_input_schema(schema)
144
+
145
+ assert "$defs" not in aliases.schema
146
+ assert aliases.schema["required"] == ["filter"]
147
+ filter_schema = aliases.schema["properties"]["filter"]
148
+ assert list(filter_schema["properties"]) == ["param_top"]
149
+ assert filter_schema["required"] == ["param_top"]
150
+ assert aliases.restore_arguments({"filter": {"param_top": 10}}) == {
151
+ "filter": {"$top": 10}
152
+ }
153
+
154
+
155
+def test_gemini_manual_response_restores_provider_visible_aliases(monkeypatch):
156
+ google_module = types.ModuleType("google")
157
+ genai_module = types.ModuleType("google.genai")
158
+ genai_types_module = types.ModuleType("google.genai.types")
159
+
160
+ class FunctionResponse:
161
+ def __init__(self, name, response):
162
+ self.name = name
163
+ self.response = response
164
+
165
+ class Part:
166
+ def __init__(self, function_response):
167
+ self.function_response = function_response
168
+
169
+ genai_types_module.FunctionResponse = FunctionResponse
170
+ genai_types_module.Part = Part
171
+ genai_module.types = genai_types_module
172
+ google_module.genai = genai_module
173
+ monkeypatch.setitem(sys.modules, "google", google_module)
174
+ monkeypatch.setitem(sys.modules, "google.genai", genai_module)
175
+ monkeypatch.setitem(sys.modules, "google.genai.types", genai_types_module)
176
+
177
+ provider_module = _load_module(
178
+ monkeypatch,
179
+ "test_composio_gemini_provider",
180
+ PYTHON_ROOT / "providers/gemini/composio_gemini/provider.py",
181
+ )
182
+ provider = provider_module.GeminiProvider()
183
+ execute_tool = Mock(return_value={"successful": True, "data": {"ok": True}})
184
+ tool = SimpleNamespace(
185
+ slug="TOOL_WITH_RESERVED",
186
+ description="Tool with reserved parameters",
187
+ input_parameters={
188
+ "type": "object",
189
+ "properties": {"for": {"type": "string"}},
190
+ "required": ["for"],
191
+ },
192
+ )
193
+ provider.wrap_tools([tool], execute_tool)
194
+
195
+ response = SimpleNamespace(
196
+ candidates=[
197
+ SimpleNamespace(
198
+ content=SimpleNamespace(
199
+ parts=[
200
+ SimpleNamespace(
201
+ function_call=SimpleNamespace(
202
+ name="TOOL_WITH_RESERVED",
203
+ args={"for_rs": "recipient@example.com"},
204
+ )
205
+ )
206
+ ]
207
+ )
208
+ )
209
+ ]
210
+ )
211
+
212
+ function_responses, executed = provider.handle_response(response)
213
+
214
+ assert executed is True
215
+ assert function_responses[0].function_response.name == "TOOL_WITH_RESERVED"
216
+ execute_tool.assert_called_once_with(
217
+ slug="TOOL_WITH_RESERVED", arguments={"for": "recipient@example.com"}
218
+ )
219
+
220
+
221
+def test_google_adk_wrap_tool_aliases_signature_and_restores_arguments(monkeypatch):
222
+ google_module = types.ModuleType("google")
223
+ adk_module = types.ModuleType("google.adk")
224
+ tools_module = types.ModuleType("google.adk.tools")
225
+
226
+ class FunctionTool:
227
+ def __init__(self, func):
228
+ self.func = func
229
+
230
+ tools_module.FunctionTool = FunctionTool
231
+ adk_module.tools = tools_module
232
+ google_module.adk = adk_module
233
+ monkeypatch.setitem(sys.modules, "google", google_module)
234
+ monkeypatch.setitem(sys.modules, "google.adk", adk_module)
235
+ monkeypatch.setitem(sys.modules, "google.adk.tools", tools_module)
236
+
237
+ provider_module = _load_module(
238
+ monkeypatch,
239
+ "test_composio_google_adk_provider",
240
+ PYTHON_ROOT / "providers/google_adk/composio_google_adk/provider.py",
241
+ )
242
+ provider = provider_module.GoogleAdkProvider()
243
+ execute_tool = Mock(return_value={"successful": True, "data": {"ok": True}})
244
+ tool = SimpleNamespace(
245
+ slug="TOOL_WITH_RESERVED",
246
+ description="Tool with reserved parameters",
247
+ input_parameters={
248
+ "type": "object",
249
+ "properties": {
250
+ "from": {"type": "string", "description": "Sender"},
251
+ "limit": {"type": "integer", "description": "Limit"},
252
+ },
253
+ "required": ["from"],
254
+ },
255
+ )
256
+
257
+ wrapped = provider.wrap_tool(tool, execute_tool)
258
+
259
+ assert list(inspect.signature(wrapped.func).parameters) == ["from_rs", "limit"]
260
+ assert "from_rs: Sender" in (wrapped.func.__doc__ or "")
261
+ wrapped.func(from_rs="sender@example.com", limit=10)
262
+ execute_tool.assert_called_once_with(
263
+ slug="TOOL_WITH_RESERVED",
264
+ arguments={"from": "sender@example.com", "limit": 10},
265
+ )
266
+
267
+
268
+def test_anthropic_wrap_tool_aliases_schema_and_restores_arguments(monkeypatch):
269
+ anthropic_module = types.ModuleType("anthropic")
270
+ types_module = types.ModuleType("anthropic.types")
271
+ beta_module = types.ModuleType("anthropic.types.beta")
272
+ beta_tool_use_module = types.ModuleType("anthropic.types.beta.beta_tool_use_block")
273
+ message_module = types.ModuleType("anthropic.types.message")
274
+ tool_param_module = types.ModuleType("anthropic.types.tool_param")
275
+ tool_use_module = types.ModuleType("anthropic.types.tool_use_block")
276
+
277
+ class BetaToolUseBlock:
278
+ pass
279
+
280
+ class Message:
281
+ def __init__(self, **kwargs):
282
+ self.__dict__.update(kwargs)
283
+
284
+ class ToolUseBlock:
285
+ pass
286
+
287
+ beta_tool_use_module.BetaToolUseBlock = BetaToolUseBlock
288
+ message_module.Message = Message
289
+ tool_param_module.ToolParam = dict
290
+ tool_use_module.ToolUseBlock = ToolUseBlock
291
+ monkeypatch.setitem(sys.modules, "anthropic", anthropic_module)
292
+ monkeypatch.setitem(sys.modules, "anthropic.types", types_module)
293
+ monkeypatch.setitem(sys.modules, "anthropic.types.beta", beta_module)
294
+ monkeypatch.setitem(
295
+ sys.modules,
296
+ "anthropic.types.beta.beta_tool_use_block",
297
+ beta_tool_use_module,
298
+ )
299
+ monkeypatch.setitem(sys.modules, "anthropic.types.message", message_module)
300
+ monkeypatch.setitem(sys.modules, "anthropic.types.tool_param", tool_param_module)
301
+ monkeypatch.setitem(sys.modules, "anthropic.types.tool_use_block", tool_use_module)
302
+
303
+ provider_module = _load_module(
304
+ monkeypatch,
305
+ "test_composio_anthropic_provider",
306
+ PYTHON_ROOT / "providers/anthropic/composio_anthropic/provider.py",
307
+ )
308
+ provider = provider_module.AnthropicProvider()
309
+ provider.execute_tool = Mock(return_value={"successful": True})
310
+ long_name = "x" * 80
311
+ tool = SimpleNamespace(
312
+ slug="TOOL_WITH_ODATA",
313
+ description="Tool with OData parameters",
314
+ input_parameters={
315
+ "type": "object",
316
+ "properties": {
317
+ "$top": {"type": "integer"},
318
+ "@microsoft.graph.conflictBehavior": {"type": "string"},
319
+ long_name: {"type": "string"},
320
+ },
321
+ "required": ["$top", long_name],
322
+ },
323
+ )
324
+
325
+ wrapped = provider.wrap_tool(tool)
326
+
327
+ aliased_names = list(wrapped["input_schema"]["properties"])
328
+ assert aliased_names[0] == "param_top"
329
+ assert aliased_names[1] == "param_microsoft_graph_conflictBehavior"
330
+ assert len(aliased_names[2]) == 64
331
+ assert all(ANTHROPIC_PROPERTY_RE.fullmatch(name) for name in aliased_names)
332
+
333
+ tool_call = SimpleNamespace(
334
+ name="TOOL_WITH_ODATA",
335
+ input={
336
+ "param_top": 10,
337
+ "param_microsoft_graph_conflictBehavior": "rename",
338
+ aliased_names[2]: "value",
339
+ },
340
+ )
341
+ provider.execute_tool_call(user_id="user", tool_call=tool_call)
342
+
343
+ provider.execute_tool.assert_called_once_with(
344
+ slug="TOOL_WITH_ODATA",
345
+ arguments={
346
+ "$top": 10,
347
+ "@microsoft.graph.conflictBehavior": "rename",
348
+ long_name: "value",
349
+ },
350
+ modifiers=None,
351
+ user_id="user",
352
+ )
353
+
354
+
355
+def test_claude_agent_sdk_wrap_tool_aliases_schema_and_restores_arguments(monkeypatch):
356
+ claude_agent_sdk_module = types.ModuleType("claude_agent_sdk")
357
+
358
+ def sdk_tool(name, description, input_schema):
359
+ def decorator(fn):
360
+ fn._tool_name = name
361
+ fn._tool_description = description
362
+ fn._input_schema = input_schema
363
+ return fn
364
+
365
+ return decorator
366
+
367
+ claude_agent_sdk_module.McpSdkServerConfig = dict
368
+ claude_agent_sdk_module.SdkMcpTool = object
369
+ claude_agent_sdk_module.create_sdk_mcp_server = Mock()
370
+ claude_agent_sdk_module.tool = sdk_tool
371
+ monkeypatch.setitem(sys.modules, "claude_agent_sdk", claude_agent_sdk_module)
372
+
373
+ provider_module = _load_module(
374
+ monkeypatch,
375
+ "test_composio_claude_agent_sdk_provider",
376
+ PYTHON_ROOT
377
+ / "providers/claude_agent_sdk/composio_claude_agent_sdk/provider.py",
378
+ )
379
+ provider = provider_module.ClaudeAgentSDKProvider()
380
+ execute_tool = Mock(return_value={"successful": True})
381
+ tool = SimpleNamespace(
382
+ slug="TOOL_WITH_ODATA",
383
+ description="Tool with OData parameters",
384
+ input_parameters={
385
+ "type": "object",
386
+ "properties": {"$top": {"type": "integer"}},
387
+ "required": ["$top"],
388
+ },
389
+ )
390
+
391
+ wrapped = provider.wrap_tool(tool, execute_tool)
392
+
393
+ assert list(wrapped._input_schema["properties"]) == ["param_top"]
394
+ assert wrapped._input_schema["required"] == ["param_top"]
395
+ result = asyncio.run(wrapped({"param_top": 5}))
396
+
397
+ assert result["content"][0]["type"] == "text"
398
+ execute_tool.assert_called_once_with("TOOL_WITH_ODATA", {"$top": 5})