Commit d72ca80
authored
feat: Add MCP-backed dynamic tool fetching to Python SDK (#39)
* feat: implement MCP-backed fetch_tools() with dynamic tool discovery
Add MCP (Model Context Protocol) integration to enable dynamic tool fetching
at runtime, matching the Node SDK functionality. This allows users to pull
the latest tool definitions from the StackOne API without SDK updates.
Key Features:
- Dynamic tool discovery via MCP server endpoint
- _StackOneRpcTool class for RPC-backed execution at /actions/rpc
- Account filtering with set_accounts() and account_ids parameter
- Provider filtering (case-insensitive, prefix-based)
- Action filtering with glob pattern support
- Thread-safe async execution wrapper (_run_async)
- Proper authentication with Basic Auth headers
Implementation Details:
- MCP client integration with streamablehttp transport
- Tool catalog fetching with pagination support (cursor-based)
- Schema normalization for proper nullable field handling
- RPC payload construction with body/headers/path/query support
- Account scoping via x-account-id header
- User-Agent tracking for SDK version telemetry
Dependencies:
- Requires optional 'mcp' extra: pip install 'stackone-ai[mcp]'
- Raises ToolsetConfigError if MCP dependencies not available
The implementation maintains API parity with the Node SDK's
stackone.mcp-fetch functionality while following Python idioms.
* test: add comprehensive test suite for MCP fetch_tools functionality
Add 12 new tests covering MCP-backed tool fetching to match Node SDK test
coverage in stackone.mcp-fetch.spec.ts. Tests are organized into logical
groups for account and provider/action filtering scenarios.
Test Coverage:
Account Filtering (TestAccountFiltering):
- fetch_tools_with_single_account_id: Single account filtering
- fetch_tools_with_multiple_account_ids: Multiple account filtering
- fetch_tools_with_set_accounts: Using set_accounts() method
- fetch_tools_account_ids_override_set_accounts: Parameter precedence
- fetch_tools_with_constructor_account_id: Constructor-based account
- fetch_tools_with_empty_account_ids: Empty list handling
Provider/Action Filtering (TestProviderAndActionFiltering):
- fetch_tools_with_provider_filter: Single provider filtering
- fetch_tools_with_multiple_providers: Multiple provider filtering
- fetch_tools_with_action_glob_pattern: Glob pattern matching
- fetch_tools_with_exact_action_match: Exact action name matching
- fetch_tools_with_provider_and_action_filters: Combined filtering
- fetch_tools_with_exclusion_pattern: Negative glob patterns
Removed:
- Deleted old fetch_tools tests that used non-MCP approach
- These tests were testing the old implementation that loaded from
OpenAPI specs instead of the MCP server
The test suite uses comprehensive mocking of MCP client components:
- ClientSession for MCP protocol communication
- streamablehttp_client for HTTP transport
- Tool list responses with pagination support
- Proper async context manager handling
All tests verify:
- Correct tool count after filtering
- Proper tool presence/absence
- Account ID preservation in tool context
- Filter precedence and interaction
* docs: document MCP-backed dynamic tool discovery feature1 parent a1c688b commit d72ca80
File tree
4 files changed
+537
-224
lines changed- stackone_ai
- tests
4 files changed
+537
-224
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
14 | 14 | | |
15 | 15 | | |
16 | 16 | | |
| 17 | + | |
17 | 18 | | |
18 | 19 | | |
19 | 20 | | |
| |||
105 | 106 | | |
106 | 107 | | |
107 | 108 | | |
| 109 | + | |
| 110 | + | |
108 | 111 | | |
109 | 112 | | |
110 | 113 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | 1 | | |
2 | 2 | | |
3 | 3 | | |
| 4 | + | |
| 5 | + | |
4 | 6 | | |
| 7 | + | |
5 | 8 | | |
| 9 | + | |
6 | 10 | | |
7 | | - | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
8 | 15 | | |
9 | 16 | | |
10 | 17 | | |
| 18 | + | |
| 19 | + | |
11 | 20 | | |
| 21 | + | |
12 | 22 | | |
13 | 23 | | |
14 | 24 | | |
15 | 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 | + | |
16 | 50 | | |
17 | 51 | | |
18 | 52 | | |
| |||
32 | 66 | | |
33 | 67 | | |
34 | 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 | + | |
35 | 229 | | |
36 | 230 | | |
37 | 231 | | |
| |||
59 | 253 | | |
60 | 254 | | |
61 | 255 | | |
62 | | - | |
| 256 | + | |
63 | 257 | | |
64 | 258 | | |
65 | 259 | | |
| |||
194 | 388 | | |
195 | 389 | | |
196 | 390 | | |
197 | | - | |
198 | 391 | | |
| 392 | + | |
| 393 | + | |
199 | 394 | | |
200 | | - | |
201 | | - | |
202 | | - | |
203 | 395 | | |
204 | | - | |
205 | | - | |
206 | | - | |
| 396 | + | |
207 | 397 | | |
208 | | - | |
209 | | - | |
| 398 | + | |
| 399 | + | |
| 400 | + | |
| 401 | + | |
| 402 | + | |
| 403 | + | |
| 404 | + | |
| 405 | + | |
| 406 | + | |
| 407 | + | |
210 | 408 | | |
211 | | - | |
212 | 409 | | |
213 | | - | |
| 410 | + | |
214 | 411 | | |
215 | | - | |
216 | 412 | | |
217 | | - | |
| 413 | + | |
218 | 414 | | |
219 | 415 | | |
220 | 416 | | |
221 | | - | |
222 | | - | |
223 | | - | |
224 | | - | |
| 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 | + | |
| 443 | + | |
| 444 | + | |
| 445 | + | |
| 446 | + | |
| 447 | + | |
| 448 | + | |
| 449 | + | |
| 450 | + | |
| 451 | + | |
| 452 | + | |
| 453 | + | |
| 454 | + | |
| 455 | + | |
| 456 | + | |
| 457 | + | |
| 458 | + | |
| 459 | + | |
| 460 | + | |
| 461 | + | |
| 462 | + | |
| 463 | + | |
| 464 | + | |
| 465 | + | |
| 466 | + | |
| 467 | + | |
225 | 468 | | |
226 | 469 | | |
227 | 470 | | |
| |||
0 commit comments