-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_fetch_tools.py
More file actions
442 lines (361 loc) · 18.7 KB
/
test_fetch_tools.py
File metadata and controls
442 lines (361 loc) · 18.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
"""Tests for StackOneToolSet MCP functionality using real MCP mock server."""
from __future__ import annotations
from contextlib import asynccontextmanager
from unittest.mock import AsyncMock, MagicMock, patch
import pytest
from stackone_ai.toolset import StackOneToolSet, _fetch_mcp_tools, _McpToolDefinition
class TestAccountFiltering:
"""Test account filtering functionality with real MCP server."""
def test_set_accounts_chaining(self, mcp_mock_server: str):
"""Test that setAccounts() returns self for chaining"""
toolset = StackOneToolSet(api_key="test-key", base_url=mcp_mock_server)
result = toolset.set_accounts(["acc1", "acc2"])
assert result is toolset
def test_fetch_tools_without_account_filtering(self, mcp_mock_server: str):
"""Test fetching tools without account filtering"""
toolset = StackOneToolSet(api_key="test-key", base_url=mcp_mock_server)
tools = toolset.fetch_tools()
assert len(tools) == 2
tool_names = [t.name for t in tools.to_list()]
assert "default_tool_1" in tool_names
assert "default_tool_2" in tool_names
def test_fetch_tools_with_account_ids(self, mcp_mock_server: str):
"""Test fetching tools with specific account IDs"""
toolset = StackOneToolSet(api_key="test-key", base_url=mcp_mock_server)
tools = toolset.fetch_tools(account_ids=["acc1"])
assert len(tools) == 2
tool_names = [t.name for t in tools.to_list()]
assert "acc1_tool_1" in tool_names
assert "acc1_tool_2" in tool_names
def test_fetch_tools_uses_set_accounts(self, mcp_mock_server: str):
"""Test that fetch_tools uses set_accounts when no accountIds provided"""
toolset = StackOneToolSet(api_key="test-key", base_url=mcp_mock_server)
toolset.set_accounts(["acc1", "acc2"])
tools = toolset.fetch_tools()
# acc1 has 2 tools, acc2 has 2 tools, total should be 4
assert len(tools) == 4
tool_names = [t.name for t in tools.to_list()]
assert "acc1_tool_1" in tool_names
assert "acc1_tool_2" in tool_names
assert "acc2_tool_1" in tool_names
assert "acc2_tool_2" in tool_names
def test_fetch_tools_overrides_set_accounts(self, mcp_mock_server: str):
"""Test that accountIds parameter overrides set_accounts"""
toolset = StackOneToolSet(api_key="test-key", base_url=mcp_mock_server)
toolset.set_accounts(["acc1", "acc2"])
tools = toolset.fetch_tools(account_ids=["acc3"])
# Should fetch tools only for acc3 (ignoring acc1, acc2)
assert len(tools) == 1
tool_names = [t.name for t in tools.to_list()]
assert "acc3_tool_1" in tool_names
# Verify set_accounts state is preserved
assert toolset._account_ids == ["acc1", "acc2"]
def test_fetch_tools_multiple_account_ids(self, mcp_mock_server: str):
"""Test fetching tools for multiple account IDs"""
toolset = StackOneToolSet(api_key="test-key", base_url=mcp_mock_server)
tools = toolset.fetch_tools(account_ids=["acc1", "acc2", "acc3"])
# acc1: 2 tools, acc2: 2 tools, acc3: 1 tool = 5 total
assert len(tools) == 5
def test_fetch_tools_preserves_account_context(self, mcp_mock_server: str):
"""Test that tools preserve their account context"""
toolset = StackOneToolSet(api_key="test-key", base_url=mcp_mock_server)
tools = toolset.fetch_tools(account_ids=["acc1"])
tool = tools.get_tool("acc1_tool_1")
assert tool is not None
assert tool.get_account_id() == "acc1"
class TestProviderAndActionFiltering:
"""Test provider and action filtering functionality with real MCP server."""
def test_filter_by_providers(self, mcp_mock_server: str):
"""Test filtering tools by providers"""
toolset = StackOneToolSet(api_key="test-key", base_url=mcp_mock_server)
tools = toolset.fetch_tools(account_ids=["mixed"], providers=["hibob", "bamboohr"])
assert len(tools) == 4
tool_names = [t.name for t in tools.to_list()]
assert "hibob_list_employees" in tool_names
assert "hibob_create_employees" in tool_names
assert "bamboohr_list_employees" in tool_names
assert "bamboohr_get_employee" in tool_names
assert "workday_list_employees" not in tool_names
def test_filter_by_actions_exact_match(self, mcp_mock_server: str):
"""Test filtering tools by exact action names"""
toolset = StackOneToolSet(api_key="test-key", base_url=mcp_mock_server)
tools = toolset.fetch_tools(
account_ids=["mixed"], actions=["hibob_list_employees", "hibob_create_employees"]
)
assert len(tools) == 2
tool_names = [t.name for t in tools.to_list()]
assert "hibob_list_employees" in tool_names
assert "hibob_create_employees" in tool_names
def test_filter_by_actions_glob_pattern(self, mcp_mock_server: str):
"""Test filtering tools by glob patterns"""
toolset = StackOneToolSet(api_key="test-key", base_url=mcp_mock_server)
tools = toolset.fetch_tools(account_ids=["mixed"], actions=["*_list_employees"])
assert len(tools) == 3
tool_names = [t.name for t in tools.to_list()]
assert "hibob_list_employees" in tool_names
assert "bamboohr_list_employees" in tool_names
assert "workday_list_employees" in tool_names
assert "hibob_create_employees" not in tool_names
assert "bamboohr_get_employee" not in tool_names
def test_combine_account_and_action_filters(self, mcp_mock_server: str):
"""Test combining account and action filters"""
toolset = StackOneToolSet(api_key="test-key", base_url=mcp_mock_server)
# acc1 has acc1_tool_1, acc1_tool_2
# acc2 has acc2_tool_1, acc2_tool_2
tools = toolset.fetch_tools(account_ids=["acc1", "acc2"], actions=["*_tool_1"])
assert len(tools) == 2
tool_names = [t.name for t in tools.to_list()]
assert "acc1_tool_1" in tool_names
assert "acc2_tool_1" in tool_names
assert "acc1_tool_2" not in tool_names
assert "acc2_tool_2" not in tool_names
def test_combine_provider_and_action_filters(self, mcp_mock_server: str):
"""Test combining providers and actions filters"""
toolset = StackOneToolSet(api_key="test-key", base_url=mcp_mock_server)
tools = toolset.fetch_tools(account_ids=["mixed"], providers=["hibob"], actions=["*_list_*"])
# Should only return hibob_list_employees (matches both filters)
assert len(tools) == 1
tool_names = [t.name for t in tools.to_list()]
assert "hibob_list_employees" in tool_names
class TestMcpHeaders:
"""Test that MCP headers are built correctly."""
def test_authorization_header_is_set(self, mcp_mock_server: str):
"""Test that authorization header is properly set (server validates basic auth)"""
toolset = StackOneToolSet(api_key="test-key", base_url=mcp_mock_server)
# If auth fails, this would raise an error
tools = toolset.fetch_tools()
assert len(tools) > 0
def test_account_id_header_is_sent(self, mcp_mock_server: str):
"""Test that x-account-id header is sent when account_id is provided"""
toolset = StackOneToolSet(api_key="test-key", base_url=mcp_mock_server)
# When we fetch with acc1, we should get acc1's tools, proving header was sent
tools = toolset.fetch_tools(account_ids=["acc1"])
tool_names = [t.name for t in tools.to_list()]
assert all("acc1" in name for name in tool_names)
class TestToolCreation:
"""Test that tools are created correctly from MCP responses."""
def test_tool_has_name_and_description(self, mcp_mock_server: str):
"""Test that tools have proper name and description"""
toolset = StackOneToolSet(api_key="test-key", base_url=mcp_mock_server)
tools = toolset.fetch_tools()
tool = tools.get_tool("default_tool_1")
assert tool is not None
assert tool.name == "default_tool_1"
assert tool.description == "Default Tool 1"
def test_tool_has_parameters_type(self, mcp_mock_server: str):
"""Test that tools have proper parameters type from input schema"""
toolset = StackOneToolSet(api_key="test-key", base_url=mcp_mock_server)
tools = toolset.fetch_tools()
tool = tools.get_tool("default_tool_1")
assert tool is not None
assert tool.parameters is not None
assert tool.parameters.type == "object"
class TestSchemaPropertyNormalization:
"""Test schema property normalization with monkeypatch (for precise schema control)."""
def test_tool_properties_are_normalized(self, monkeypatch):
"""Test that tool properties are correctly extracted from input schema"""
from stackone_ai.toolset import _McpToolDefinition
def fake_fetch(_: str, headers: dict[str, str]) -> list[_McpToolDefinition]:
return [
_McpToolDefinition(
name="test_tool",
description="Test tool",
input_schema={
"type": "object",
"properties": {
"name": {"type": "string", "description": "The name"},
"age": {"type": "integer"},
},
"required": ["name"],
},
)
]
monkeypatch.setattr("stackone_ai.toolset._fetch_mcp_tools", fake_fetch)
toolset = StackOneToolSet(api_key="test-key")
tools = toolset.fetch_tools()
tool = tools.get_tool("test_tool")
assert tool is not None
assert "name" in tool.parameters.properties
assert "age" in tool.parameters.properties
def test_required_fields_marked_not_nullable(self, monkeypatch):
"""Test that required fields are marked as not nullable"""
from stackone_ai.toolset import _McpToolDefinition
def fake_fetch(_: str, headers: dict[str, str]) -> list[_McpToolDefinition]:
return [
_McpToolDefinition(
name="test_tool",
description="Test tool",
input_schema={
"type": "object",
"properties": {"id": {"type": "string"}},
"required": ["id"],
},
)
]
monkeypatch.setattr("stackone_ai.toolset._fetch_mcp_tools", fake_fetch)
toolset = StackOneToolSet(api_key="test-key")
tools = toolset.fetch_tools()
tool = tools.get_tool("test_tool")
assert tool is not None
assert tool.parameters.properties["id"].get("nullable") is False
def test_optional_fields_marked_nullable(self, monkeypatch):
"""Test that optional fields are marked as nullable"""
from stackone_ai.toolset import _McpToolDefinition
def fake_fetch(_: str, headers: dict[str, str]) -> list[_McpToolDefinition]:
return [
_McpToolDefinition(
name="test_tool",
description="Test tool",
input_schema={
"type": "object",
"properties": {"optional_field": {"type": "string"}},
},
)
]
monkeypatch.setattr("stackone_ai.toolset._fetch_mcp_tools", fake_fetch)
toolset = StackOneToolSet(api_key="test-key")
tools = toolset.fetch_tools()
tool = tools.get_tool("test_tool")
assert tool is not None
assert tool.parameters.properties["optional_field"].get("nullable") is True
class TestRpcToolExecution:
"""Test RPC tool execution through the MCP server."""
def test_execute_tool_returns_response(self, mcp_mock_server: str):
"""Test executing a tool via RPC returns response"""
toolset = StackOneToolSet(api_key="test-key", base_url=mcp_mock_server)
tools = toolset.fetch_tools(account_ids=["your-bamboohr-account-id"])
tool = tools.get_tool("bamboohr_list_employees")
assert tool is not None
result = tool.execute()
assert result is not None
assert "data" in result
def test_execute_tool_with_arguments(self, mcp_mock_server: str):
"""Test executing a tool with arguments"""
toolset = StackOneToolSet(api_key="test-key", base_url=mcp_mock_server)
tools = toolset.fetch_tools(account_ids=["your-bamboohr-account-id"])
tool = tools.get_tool("bamboohr_get_employee")
assert tool is not None
result = tool.execute({"id": "emp-123"})
assert result is not None
assert result.get("data", {}).get("id") == "emp-123"
def test_execute_tool_sends_account_id_header(self, mcp_mock_server: str):
"""Test that tool execution sends x-account-id header"""
toolset = StackOneToolSet(api_key="test-key", base_url=mcp_mock_server)
tools = toolset.fetch_tools(account_ids=["test-account"])
tool = tools.get_tool("dummy_action")
assert tool is not None
assert tool.get_account_id() == "test-account"
# Execute and verify account context is preserved
result = tool.execute({"foo": "bar"})
assert result is not None
class TestAccountIdFallback:
"""Test account ID fallback to instance account_id."""
def test_uses_instance_account_id_when_no_other_provided(self, monkeypatch):
"""Test that fetch_tools uses instance account_id when no account_ids provided."""
sample_tool = _McpToolDefinition(
name="test_tool",
description="Test tool",
input_schema={"type": "object", "properties": {}},
)
captured_accounts: list[str | None] = []
def fake_fetch(_: str, headers: dict[str, str]) -> list[_McpToolDefinition]:
captured_accounts.append(headers.get("x-account-id"))
return [sample_tool]
monkeypatch.setattr("stackone_ai.toolset._fetch_mcp_tools", fake_fetch)
# Create toolset with account_id in constructor
toolset = StackOneToolSet(api_key="test_key", account_id="instance_account")
tools = toolset.fetch_tools() # No account_ids, no set_accounts
# Should use the instance account_id
assert captured_accounts == ["instance_account"]
assert len(tools) == 1
tool = tools.get_tool("test_tool")
assert tool is not None
assert tool.get_account_id() == "instance_account"
class TestToolsetErrorHandling:
"""Test error handling in fetch_tools."""
def test_reraises_toolset_error(self, monkeypatch):
"""Test that ToolsetError is re-raised without wrapping."""
from stackone_ai.toolset import ToolsetConfigError
def fake_fetch(_: str, headers: dict[str, str]) -> list[_McpToolDefinition]:
raise ToolsetConfigError("Original config error")
monkeypatch.setattr("stackone_ai.toolset._fetch_mcp_tools", fake_fetch)
toolset = StackOneToolSet(api_key="test_key")
with pytest.raises(ToolsetConfigError, match="Original config error"):
toolset.fetch_tools()
class TestFetchMcpToolsInternal:
"""Test _fetch_mcp_tools internal implementation."""
def test_fetch_mcp_tools_single_page(self):
"""Test fetching tools with single page response."""
# Create mock tool response
mock_tool = MagicMock()
mock_tool.name = "test_tool"
mock_tool.description = "Test description"
mock_tool.inputSchema = {"type": "object", "properties": {"id": {"type": "string"}}}
mock_result = MagicMock()
mock_result.tools = [mock_tool]
mock_result.nextCursor = None
# Create mock session
mock_session = AsyncMock()
mock_session.initialize = AsyncMock()
mock_session.list_tools = AsyncMock(return_value=mock_result)
mock_session.__aenter__ = AsyncMock(return_value=mock_session)
mock_session.__aexit__ = AsyncMock(return_value=None)
# Create mock streamable client
@asynccontextmanager
async def mock_streamable_client(endpoint, headers):
yield (MagicMock(), MagicMock(), MagicMock())
# Patch at the module where imports happen
with (
patch(
"mcp.client.streamable_http.streamablehttp_client",
side_effect=mock_streamable_client,
),
patch("mcp.client.session.ClientSession", return_value=mock_session),
patch("mcp.types.Implementation", MagicMock()),
):
result = _fetch_mcp_tools("https://api.example.com/mcp", {"Authorization": "Basic test"})
assert len(result) == 1
assert result[0].name == "test_tool"
assert result[0].description == "Test description"
assert result[0].input_schema == {"type": "object", "properties": {"id": {"type": "string"}}}
def test_fetch_mcp_tools_with_pagination(self):
"""Test fetching tools with multiple pages."""
# First page
mock_tool1 = MagicMock()
mock_tool1.name = "tool_1"
mock_tool1.description = "Tool 1"
mock_tool1.inputSchema = {}
mock_result1 = MagicMock()
mock_result1.tools = [mock_tool1]
mock_result1.nextCursor = "cursor_page_2"
# Second page
mock_tool2 = MagicMock()
mock_tool2.name = "tool_2"
mock_tool2.description = "Tool 2"
mock_tool2.inputSchema = None # Test None inputSchema
mock_result2 = MagicMock()
mock_result2.tools = [mock_tool2]
mock_result2.nextCursor = None
# Create mock session with pagination
mock_session = AsyncMock()
mock_session.initialize = AsyncMock()
mock_session.list_tools = AsyncMock(side_effect=[mock_result1, mock_result2])
mock_session.__aenter__ = AsyncMock(return_value=mock_session)
mock_session.__aexit__ = AsyncMock(return_value=None)
@asynccontextmanager
async def mock_streamable_client(endpoint, headers):
yield (MagicMock(), MagicMock(), MagicMock())
with (
patch(
"mcp.client.streamable_http.streamablehttp_client",
side_effect=mock_streamable_client,
),
patch("mcp.client.session.ClientSession", return_value=mock_session),
patch("mcp.types.Implementation", MagicMock()),
):
result = _fetch_mcp_tools("https://api.example.com/mcp", {})
assert len(result) == 2
assert result[0].name == "tool_1"
assert result[1].name == "tool_2"
assert result[1].input_schema == {} # None should become empty dict
assert mock_session.list_tools.call_count == 2