Composio powers 1000+ toolkits, tool search, context management, authentication, and a sandboxed workbench to help you build AI agents that turn intent into action.
Mirrored from https://github.com/Camo67/composio.git
Synced Nov 22, 19:30 UTC
Add integration tests (#2021)
Co-authored-by: Prateek <prateek@composio.dev>
prateek committed
Oct 9, 2025 at 11:32 UTC
ea67e5e0ab6aa18746dbb6a37dc522e43bd427c7
8 files changed
+971
-1
.github/workflows/py.test.yml
+54
@@ -170,3 +170,57 @@ jobs:
170
cd python/
171
source .venv/bin/activate
172
ruff format --check --config config/ruff.toml composio/ tests/
173
+
174
+ integration-tests:
175
+ name: Integration Tests
176
+ runs-on: ubuntu-22.04 # Use Ubuntu 22.04 for stability
177
+ permissions: read-all
178
+ env:
179
+ COMPOSIO_API_KEY: ${{ secrets.COMPOSIO_API_KEY_PROD }}
180
+
181
+ steps:
182
+ - name: Checkout repository
183
+ uses: actions/checkout@v4
184
+
185
+ - name: Install UV
186
+ uses: astral-sh/setup-uv@v6
187
+ with:
188
+ uv-version: '0.8.19' # Pin to a recent version that supports --group syntax
189
+
190
+ - name: Cache UV dependencies
191
+ uses: actions/cache@v4
192
+ with:
193
+ path: ~/.cache/uv
194
+ key: uv-0.8.19-py3.12-${{ hashFiles('python/pyproject.toml') }}
195
+ restore-keys: |
196
+ uv-0.8.19-py3.12-
197
+
198
+ - name: Install Python Dependencies
199
+ working-directory: ./python
200
+ run: |
201
+ # Let UV manage Python version to satisfy workspace requirements
202
+ uv sync --group dev --python 3.12
203
+
204
+ - name: Verify API Key
205
+ run: |
206
+ if [ -z "$COMPOSIO_API_KEY" ]; then
207
+ echo "::error::COMPOSIO_API_KEY is not set"
208
+ exit 1
209
+ fi
210
+ echo "✅ COMPOSIO_API_KEY is configured"
211
+
212
+ - name: Run Integration Tests
213
+ working-directory: ./python
214
+ run: |
215
+ echo "🧪 Running Integration Tests..."
216
+ uv run pytest composio/integration_test/ -v --tb=short --maxfail=3
217
+ timeout-minutes: 15
218
+
219
+ - name: Upload Test Results
220
+ if: always()
221
+ uses: actions/upload-artifact@v4
222
+ with:
223
+ name: integration-test-results
224
+ path: |
225
+ python/composio/integration_test/
226
+ retention-days: 7
python/composio/integration_test/README.md
new
+36
@@ -0,0 +1,36 @@
1
+# Integration Tests
2
+
3
+Integration tests for Composio SDK functionality.
4
+
5
+## Setup
6
+
7
+```bash
8
+export COMPOSIO_API_KEY="your_api_key_here"
9
+```
10
+
11
+## Running Tests
12
+
13
+```bash
14
+# From project root
15
+cd /path/to/composio
16
+python -m pytest python/composio/integration_test/ -v
17
+
18
+# From python directory
19
+cd /path/to/composio/python
20
+pytest composio/integration_test/ -v
21
+
22
+# Using uv
23
+cd /path/to/composio/python
24
+uv run pytest composio/integration_test/ -v
25
+```
26
+
27
+## Test Files
28
+
29
+- **`test_mcp.py`** - MCP (Model Context Protocol) functionality tests
30
+- **`test_tool_router.py`** - ToolRouter experimental feature tests
31
+
32
+## Requirements
33
+
34
+- Python 3.12+
35
+- pytest
36
+- Valid Composio API key
\ No newline at end of file
python/composio/integration_test/__init__.py
new
+8
@@ -0,0 +1,8 @@
1
+"""
2
+Integration test package for Composio SDK.
3
+
4
+This package contains comprehensive integration tests for various Composio features,
5
+including experimental MCP functionality.
6
+"""
7
+
8
+__all__ = []
python/composio/integration_test/conftest.py
new
+93
@@ -0,0 +1,93 @@
1
+"""
2
+Pytest configuration and shared fixtures for Composio integration tests.
3
+"""
4
+
5
+import os
6
+import sys
7
+from pathlib import Path
8
+
9
+import pytest
10
+
11
+# Add the python directory to the path for imports
12
+python_dir = Path(__file__).parent.parent.parent
13
+sys.path.insert(0, str(python_dir))
14
+
15
+# Test configuration
16
+API_KEY = os.getenv("COMPOSIO_API_KEY")
17
+
18
+if not API_KEY:
19
+ pytest.skip(
20
+ "COMPOSIO_API_KEY environment variable not set", allow_module_level=True
21
+ )
22
+
23
+
24
+@pytest.fixture(scope="session", autouse=True)
25
+def setup_environment():
26
+ """Set up the test environment for all tests."""
27
+ os.environ["COMPOSIO_API_KEY"] = API_KEY
28
+ yield
29
+
30
+
31
+@pytest.fixture(scope="session")
32
+def composio_client():
33
+ """Provide a Composio client instance for all tests."""
34
+ from composio import Composio
35
+
36
+ return Composio()
37
+
38
+
39
+@pytest.fixture(scope="session")
40
+def auth_configs(composio_client):
41
+ """Get available auth configurations for testing."""
42
+ try:
43
+ configs_response = composio_client.auth_configs.list()
44
+
45
+ # Debug what we get
46
+ print(f"Raw response type: {type(configs_response)}")
47
+
48
+ # Handle different response formats
49
+ if hasattr(configs_response, "items"):
50
+ # Direct access to items
51
+ items = configs_response.items
52
+ print(f"Found items directly: {len(items) if items else 0}")
53
+ return list(items) if items else []
54
+ elif hasattr(configs_response, "__iter__"):
55
+ # If it's being converted to an iterable of tuples, find the items tuple
56
+ items_tuple = None
57
+ for item in configs_response:
58
+ if isinstance(item, tuple) and len(item) == 2 and item[0] == "items":
59
+ items_tuple = item[1]
60
+ break
61
+
62
+ if items_tuple:
63
+ print(f"Found items in tuple: {len(items_tuple)}")
64
+ return list(items_tuple)
65
+
66
+ return []
67
+
68
+ except Exception as e:
69
+ print(f"Exception in auth_configs fixture: {e}")
70
+ import traceback
71
+
72
+ traceback.print_exc()
73
+ return []
74
+
75
+
76
+@pytest.fixture
77
+def sample_mcp_config_data():
78
+ """Provide sample data for MCP configuration testing."""
79
+ import time
80
+
81
+ return {
82
+ "name": f"pytest_test_{int(time.time())}",
83
+ "server_config": [
84
+ {"auth_config_id": "test_auth_id", "allowed_tools": ["GMAIL_FETCH_EMAILS"]}
85
+ ],
86
+ "options": {"is_chat_auth": True},
87
+ }
88
+
89
+
90
+@pytest.fixture
91
+def test_user_id():
92
+ """Provide a consistent test user ID."""
93
+ return "pytest_integration_user_123"
python/composio/integration_test/pytest.ini
new
+9
@@ -0,0 +1,9 @@
1
+[tool:pytest]
2
+# Simple pytest configuration for Composio integration tests
3
+
4
+testpaths = .
5
+python_files = test_*.py
6
+python_classes = Test*
7
+python_functions = test_*
8
+
9
+addopts = -v --tb=short
python/composio/integration_test/test_mcp.py
new
+643
@@ -0,0 +1,643 @@
1
+"""
2
+Comprehensive integration tests for Composio MCP functionality.
3
+
4
+This module provides comprehensive pytest-based tests for the MCP (Model Context Protocol) functionality,
5
+combining both structured pytest tests and direct execution tests for non-auth toolkits.
6
+
7
+Usage:
8
+ export COMPOSIO_API_KEY="your_api_key_here"
9
+ pytest python/composio/integration_test/test_mcp.py -v
10
+"""
11
+
12
+import os
13
+import uuid
14
+
15
+import pytest
16
+import requests
17
+
18
+from composio import Composio
19
+from composio.exceptions import ValidationError
20
+
21
+# Test configuration
22
+API_KEY = os.getenv("COMPOSIO_API_KEY")
23
+TEST_CONFIG_PREFIX = "pytest_integration_test"
24
+
25
+if not API_KEY:
26
+ pytest.fail("COMPOSIO_API_KEY environment variable not set", pytrace=False)
27
+
28
+
29
+def generate_unique_name(prefix: str = "pytest") -> str:
30
+ """Generate a unique test name using UUID to avoid collisions."""
31
+ # Use first 8 characters of UUID for readability while maintaining uniqueness
32
+ unique_id = str(uuid.uuid4())[:8]
33
+ return f"{prefix}-{unique_id}"
34
+
35
+
36
+@pytest.fixture
37
+def composio_client():
38
+ """Fixture providing Composio client instance."""
39
+ return Composio(api_key=API_KEY)
40
+
41
+
42
+@pytest.fixture
43
+def test_mcp_config_data():
44
+ """Fixture providing test data for MCP config creation."""
45
+ return {
46
+ "name": generate_unique_name("pytest-data"),
47
+ "toolkits": ["composio_search", "text_to_pdf"],
48
+ "allowed_tools": [
49
+ "COMPOSIO_SEARCH_DUCK_DUCK_GO_SEARCH",
50
+ "TEXT_TO_PDF_CONVERT_TEXT_TO_PDF",
51
+ ],
52
+ "manually_manage_connections": False,
53
+ }
54
+
55
+
56
+class TestMCPStructure:
57
+ """Test the basic structure and availability of MCP features."""
58
+
59
+ def test_mcp_namespace_exists(self, composio_client):
60
+ """Test that mcp namespace exists at top level."""
61
+ assert hasattr(composio_client, "mcp"), "Missing mcp namespace"
62
+
63
+ @pytest.mark.parametrize(
64
+ "method_name", ["create", "list", "get", "update", "delete", "generate"]
65
+ )
66
+ def test_mcp_methods_available(self, composio_client, method_name):
67
+ """Test that all required MCP methods are available."""
68
+ assert hasattr(composio_client.mcp, method_name), (
69
+ f"Missing method: {method_name}"
70
+ )
71
+
72
+
73
+class TestMCPOperations:
74
+ """Test MCP CRUD operations."""
75
+
76
+ def test_list_mcp_configs(self, composio_client):
77
+ """Test listing MCP configurations."""
78
+ try:
79
+ configs = composio_client.mcp.list()
80
+ assert isinstance(configs, dict), "list() should return a dictionary"
81
+ assert "items" in configs, "Response should contain 'items' key"
82
+ assert "current_page" in configs, (
83
+ "Response should contain 'current_page' key"
84
+ )
85
+ assert "total_pages" in configs, "Response should contain 'total_pages' key"
86
+ assert isinstance(configs["items"], list), "items should be a list"
87
+ except Exception as e:
88
+ # List might fail if no configs exist or API issues, but method should exist
89
+ assert "Failed to list MCP servers" in str(e) or "list" in str(e).lower()
90
+
91
+ def test_list_with_pagination(self, composio_client):
92
+ """Test listing with pagination parameters."""
93
+ try:
94
+ configs = composio_client.mcp.list(page_no=1, limit=5)
95
+ assert isinstance(configs, dict), (
96
+ "Paginated list should return a dictionary"
97
+ )
98
+ assert "items" in configs, "Response should contain 'items'"
99
+ except Exception as e:
100
+ # Expected to fail with current API implementation
101
+ assert "Failed to list MCP servers" in str(e)
102
+
103
+ def test_list_with_filters(self, composio_client):
104
+ """Test listing with filter parameters."""
105
+ try:
106
+ # Test toolkit filter with non-auth toolkits
107
+ configs_search = composio_client.mcp.list(toolkits="composio_search")
108
+ assert configs_search["items"] is None or isinstance(
109
+ configs_search["items"], list
110
+ )
111
+
112
+ # Test name filter
113
+ configs_name = composio_client.mcp.list(name="test")
114
+ assert configs_name["items"] is None or isinstance(
115
+ configs_name["items"], list
116
+ )
117
+ except Exception as e:
118
+ # Expected to fail with current API implementation
119
+ assert "Failed to list MCP servers" in str(e)
120
+
121
+ def test_create_mcp_config(self, composio_client):
122
+ """Test creating MCP configuration with new API using non-auth toolkits."""
123
+ # Server name must be ≤30 chars and only contain letters, numbers, spaces, hyphens
124
+ test_name = generate_unique_name("pytest-create")
125
+
126
+ mcp_config = composio_client.mcp.create(
127
+ test_name,
128
+ toolkits=["composio_search", "text_to_pdf"],
129
+ allowed_tools=[
130
+ "COMPOSIO_SEARCH_DUCK_DUCK_GO_SEARCH",
131
+ "TEXT_TO_PDF_CONVERT_TEXT_TO_PDF",
132
+ ],
133
+ manually_manage_connections=False,
134
+ )
135
+
136
+ # Basic response validation
137
+ assert mcp_config.id
138
+ assert mcp_config.name == test_name
139
+ assert callable(mcp_config.generate)
140
+ assert mcp_config.allowed_tools == [
141
+ "COMPOSIO_SEARCH_DUCK_DUCK_GO_SEARCH",
142
+ "TEXT_TO_PDF_CONVERT_TEXT_TO_PDF",
143
+ ]
144
+ assert mcp_config.commands.claude
145
+ assert mcp_config.commands.cursor
146
+ assert mcp_config.commands.windsurf
147
+
148
+ # Test the generate method
149
+ try:
150
+ server_instance = mcp_config.generate("test_user_123")
151
+ assert isinstance(server_instance, dict), (
152
+ "generate should return a dictionary"
153
+ )
154
+ assert "id" in server_instance, "Server instance should have id"
155
+ assert "url" in server_instance, "Server instance should have url"
156
+ assert "type" in server_instance, "Server instance should have type"
157
+ assert server_instance["type"] == "streamable_http", (
158
+ "Server type should be streamable_http"
159
+ )
160
+ assert isinstance(server_instance["url"], str), "URL should be string"
161
+ assert len(server_instance["url"]) > 0, "URL should not be empty"
162
+ except Exception as e:
163
+ print(f"Generate method failed (may be expected): {e}")
164
+
165
+ def test_get_nonexistent_config(self, composio_client):
166
+ """Test getting a non-existent configuration."""
167
+ with pytest.raises(ValidationError):
168
+ composio_client.mcp.get("nonexistent_config_id")
169
+
170
+ def test_create_with_empty_toolkits(self, composio_client):
171
+ """Test creating with empty toolkit configuration."""
172
+ with pytest.raises(ValidationError):
173
+ composio_client.mcp.create(
174
+ "test_empty",
175
+ toolkits=[], # Empty toolkits
176
+ )
177
+
178
+ def test_generate_method_directly(self, composio_client):
179
+ """Test the generate method directly on MCP class using non-auth toolkits."""
180
+ # First create a config
181
+ test_name = generate_unique_name("pytest-generate")
182
+
183
+ mcp_config = composio_client.mcp.create(
184
+ test_name,
185
+ toolkits=["composio_search", "text_to_pdf"],
186
+ allowed_tools=[
187
+ "COMPOSIO_SEARCH_DUCK_DUCK_GO_SEARCH",
188
+ "TEXT_TO_PDF_CONVERT_TEXT_TO_PDF",
189
+ ],
190
+ manually_manage_connections=False,
191
+ )
192
+
193
+ # Test generate method directly
194
+ try:
195
+ server_instance = composio_client.mcp.generate(
196
+ "test_user_direct_123",
197
+ mcp_config.id,
198
+ {"manually_manage_connections": False},
199
+ )
200
+
201
+ assert isinstance(server_instance, dict), (
202
+ "generate should return a dictionary"
203
+ )
204
+ assert server_instance["id"] == mcp_config.id, (
205
+ "Instance ID should match config ID"
206
+ )
207
+ assert server_instance["user_id"] == "test_user_direct_123", (
208
+ "User ID should match"
209
+ )
210
+ assert server_instance["type"] == "streamable_http", (
211
+ "Type should be streamable_http"
212
+ )
213
+ assert "url" in server_instance, "Should have URL"
214
+ except Exception as e:
215
+ print(f"Direct generate failed (may be expected): {e}")
216
+
217
+ def test_create_with_string_toolkits(self, composio_client):
218
+ """Test creating MCP configuration using simple string toolkit names."""
219
+ test_name = generate_unique_name("pytest-strings")
220
+
221
+ # Test with simple string toolkit names
222
+ mcp_config = composio_client.mcp.create(
223
+ test_name,
224
+ toolkits=["composio_search", "text_to_pdf"],
225
+ manually_manage_connections=False,
226
+ )
227
+
228
+ # Basic validation
229
+ assert mcp_config.id
230
+ assert mcp_config.name == test_name
231
+ assert callable(mcp_config.generate)
232
+
233
+ def test_create_with_mixed_toolkits(self, composio_client):
234
+ """Test creating MCP configuration with mixed string and object formats."""
235
+ test_name = generate_unique_name("pytest-mixed")
236
+
237
+ # Test with mixed formats (string and object with auth_config_id)
238
+ mcp_config = composio_client.mcp.create(
239
+ test_name,
240
+ toolkits=[
241
+ "composio_search", # String format
242
+ {
243
+ "toolkit": "text_to_pdf",
244
+ # No auth_config_id needed for non-auth toolkit
245
+ }, # Object format
246
+ ],
247
+ allowed_tools=[
248
+ "COMPOSIO_SEARCH_DUCK_DUCK_GO_SEARCH",
249
+ "TEXT_TO_PDF_CONVERT_TEXT_TO_PDF",
250
+ ],
251
+ manually_manage_connections=False,
252
+ )
253
+
254
+ # Basic validation
255
+ assert mcp_config.id
256
+ assert mcp_config.name == test_name
257
+ assert callable(mcp_config.generate)
258
+
259
+ def test_create_response_structure(self, composio_client):
260
+ """Test that create response has all required fields."""
261
+ test_name = generate_unique_name("pytest-structure")
262
+
263
+ mcp_config = composio_client.mcp.create(
264
+ test_name,
265
+ toolkits=["composio_search", "text_to_pdf"],
266
+ allowed_tools=[
267
+ "COMPOSIO_SEARCH_DUCK_DUCK_GO_SEARCH",
268
+ "TEXT_TO_PDF_CONVERT_TEXT_TO_PDF",
269
+ ],
270
+ manually_manage_connections=False,
271
+ )
272
+
273
+ # Basic structure validation
274
+ assert mcp_config.id
275
+ assert mcp_config.name == test_name
276
+ assert mcp_config.allowed_tools == [
277
+ "COMPOSIO_SEARCH_DUCK_DUCK_GO_SEARCH",
278
+ "TEXT_TO_PDF_CONVERT_TEXT_TO_PDF",
279
+ ]
280
+ assert mcp_config.auth_config_ids == []
281
+ assert mcp_config.mcp_url
282
+ assert mcp_config.commands.claude
283
+ assert mcp_config.commands.cursor
284
+ assert mcp_config.commands.windsurf
285
+ assert callable(mcp_config.generate)
286
+
287
+
288
+class TestMCPErrorHandling:
289
+ """Test error handling and edge cases."""
290
+
291
+ @pytest.mark.parametrize(
292
+ "invalid_config_id", ["", "invalid_id", "mcp_000000", "nonexistent"]
293
+ )
294
+ def test_invalid_config_ids(self, composio_client, invalid_config_id):
295
+ """Test various invalid configuration IDs."""
296
+ with pytest.raises(ValidationError):
297
+ composio_client.mcp.get(invalid_config_id)
298
+
299
+ def test_generate_with_invalid_params(self, composio_client):
300
+ """Test generate method with invalid parameters."""
301
+ with pytest.raises(ValidationError):
302
+ composio_client.mcp.generate("", "invalid_config_id")
303
+
304
+ def test_create_with_invalid_toolkit_config(self, composio_client):
305
+ """Test create with invalid toolkit configuration."""
306
+ # Test with empty toolkit config - should fail during API call
307
+ try:
308
+ result = composio_client.mcp.create(
309
+ "test",
310
+ toolkits=[
311
+ {
312
+ # Empty toolkit config - will fail at API level
313
+ }
314
+ ],
315
+ )
316
+ # If it somehow succeeds, that's unexpected but not necessarily wrong
317
+ print(f"Create succeeded with empty config: {result.id}")
318
+ except ValidationError as e:
319
+ # Expected - should fail with validation error
320
+ assert "Failed to create MCP server" in str(e)
321
+ except Exception as e:
322
+ # Also acceptable - might fail for other API reasons
323
+ print(f"Create failed with: {type(e).__name__}: {e}")
324
+
325
+
326
+class TestMCPNoAuthToolkits:
327
+ """Test MCP functionality with non-authentication toolkits."""
328
+
329
+ def test_mcp_with_no_auth_toolkits(self, composio_client):
330
+ """Test MCP with toolkits that don't require authentication."""
331
+ print("🔧 Testing MCP with Non-Auth Toolkits")
332
+ print("=" * 50)
333
+
334
+ # Create MCP server with non-auth toolkits
335
+ server_name = generate_unique_name("no-auth-test")
336
+ print(f"🚀 Creating MCP server: {server_name}")
337
+
338
+ mcp_server = composio_client.mcp.create(
339
+ server_name,
340
+ toolkits=["composio_search", "text_to_pdf"],
341
+ allowed_tools=[
342
+ "COMPOSIO_SEARCH_DUCK_DUCK_GO_SEARCH",
343
+ "TEXT_TO_PDF_CONVERT_TEXT_TO_PDF",
344
+ ],
345
+ manually_manage_connections=False,
346
+ )
347
+
348
+ print("✅ MCP server created successfully!")
349
+ print(f" Server ID: {mcp_server.id}")
350
+ print(f" Server Name: {mcp_server.name}")
351
+ print(f" Toolkits: {getattr(mcp_server, 'toolkits', 'N/A')}")
352
+
353
+ # Generate server instance for a test user
354
+ test_user_id = "test_user_no_auth_123"
355
+ print(f"\n🔗 Generating server instance for user: {test_user_id}")
356
+
357
+ server_instance = mcp_server.generate(test_user_id)
358
+
359
+ print("✅ Server instance generated successfully!")
360
+ print(f" Instance ID: {server_instance['id']}")
361
+ print(f" Instance Type: {server_instance['type']}")
362
+ print(f" Instance URL: {server_instance['url']}")
363
+ print(f" User ID: {server_instance['user_id']}")
364
+ print(f" Allowed Tools: {server_instance['allowed_tools']}")
365
+ print(f" Auth Configs: {server_instance['auth_configs']}")
366
+
367
+ # Test direct generate method as well
368
+ print("\n🔄 Testing direct generate method...")
369
+
370
+ direct_instance = composio_client.mcp.generate(
371
+ test_user_id + "_direct",
372
+ mcp_server.id,
373
+ {"manually_manage_connections": False},
374
+ )
375
+
376
+ print("✅ Direct generate method successful!")
377
+ print(f" Direct Instance URL: {direct_instance['url']}")
378
+ print(f" Direct Instance User ID: {direct_instance['user_id']}")
379
+
380
+ # Test URL connectivity (basic check)
381
+ print("\n🌐 Testing MCP URL connectivity...")
382
+
383
+ mcp_url = server_instance["url"]
384
+
385
+ try:
386
+ # Test with SSE-appropriate headers
387
+ headers = {
388
+ "Accept": "text/event-stream, application/json, */*",
389
+ "Cache-Control": "no-cache",
390
+ "User-Agent": "Composio-Python-MCP-Test",
391
+ }
392
+
393
+ response = requests.get(mcp_url, headers=headers, timeout=5, stream=True)
394
+
395
+ print("✅ MCP URL is accessible!")
396
+ print(f" Status Code: {response.status_code}")
397
+ print(f" Content-Type: {response.headers.get('Content-Type', 'N/A')}")
398
+
399
+ # Try to read first event
400
+ try:
401
+ chunk = next(
402
+ response.iter_content(chunk_size=1024, decode_unicode=True)
403
+ )
404
+ print(f" First Event: {chunk.strip()[:100]}...")
405
+ except StopIteration:
406
+ print(" No initial content received")
407
+
408
+ except requests.exceptions.Timeout:
409
+ print("⚠️ MCP URL timeout (normal for SSE endpoints)")
410
+ except Exception as e:
411
+ print(f"⚠️ MCP URL test failed: {e}")
412
+
413
+ print("\n📊 Test Summary:")
414
+ print(f" ✅ MCP Server Created: {mcp_server.id}")
415
+ print(f" ✅ Server Instance Generated: {server_instance['type']}")
416
+ print(" ✅ Direct Generate Method: Working")
417
+ print(f" ✅ Available Tools: {len(server_instance['allowed_tools'])} tools")
418
+ print(
419
+ f" ✅ No Auth Required: {len(server_instance['auth_configs'])} auth configs"
420
+ )
421
+
422
+ # Assertions for pytest
423
+ assert mcp_server.id is not None
424
+ assert mcp_server.name == server_name
425
+ assert server_instance["type"] == "streamable_http"
426
+ assert server_instance["user_id"] == test_user_id
427
+ assert len(server_instance["allowed_tools"]) > 0
428
+ assert len(server_instance["auth_configs"]) == 0 # No auth required
429
+
430
+ def test_mcp_with_string_toolkits(self, composio_client):
431
+ """Test MCP server creation using simple string toolkit names."""
432
+ print("🧪 Testing MCP with string toolkit names...")
433
+
434
+ # Create MCP server with string toolkit names (simplified API)
435
+ server_name = generate_unique_name("string-test")
436
+ print(f"🚀 Creating MCP server with strings: {server_name}")
437
+
438
+ mcp_server = composio_client.mcp.create(
439
+ server_name,
440
+ toolkits=["composio_search", "text_to_pdf"], # Simple strings
441
+ manually_manage_connections=False,
442
+ )
443
+
444
+ print("✅ MCP server created successfully with string toolkits!")
445
+ print(f" Server ID: {mcp_server.id}")
446
+ print(f" Server Name: {mcp_server.name}")
447
+
448
+ # Test generate method
449
+ user_id = f"string_user_{str(uuid.uuid4())[:8]}"
450
+ server_instance = mcp_server.generate(user_id)
451
+
452
+ print("✅ Server instance generated successfully!")
453
+ print(f" Instance URL: {server_instance['url']}")
454
+ print(f" User ID: {server_instance['user_id']}")
455
+ print(f" Server Type: {server_instance['type']}")
456
+
457
+ # Basic validation
458
+ assert server_instance["user_id"] == user_id
459
+ assert server_instance["type"] == "streamable_http"
460
+ assert "url" in server_instance
461
+ assert len(server_instance["url"]) > 0
462
+
463
+ print("🎉 String toolkit test completed successfully!")
464
+
465
+
466
+class TestMCPRealWorldScenarios:
467
+ """Test real-world usage scenarios."""
468
+
469
+ def test_full_workflow_with_no_auth_toolkits(self, composio_client):
470
+ """Test complete workflow: create -> generate -> use with non-auth toolkits."""
471
+ # Server name must be ≤30 chars and only contain letters, numbers, spaces, hyphens
472
+ test_name = generate_unique_name("pytest-work")
473
+
474
+ # Step 1: Create MCP config
475
+ mcp_config = composio_client.mcp.create(
476
+ test_name,
477
+ toolkits=["composio_search", "text_to_pdf"],
478
+ allowed_tools=[
479
+ "COMPOSIO_SEARCH_DUCK_DUCK_GO_SEARCH",
480
+ "TEXT_TO_PDF_CONVERT_TEXT_TO_PDF",
481
+ ],
482
+ manually_manage_connections=False,
483
+ )
484
+
485
+ assert mcp_config.id is not None
486
+ assert mcp_config.name == test_name
487
+
488
+ # Step 2: Generate server instance
489
+ try:
490
+ server_instance = mcp_config.generate("workflow_user_123")
491
+
492
+ # Step 3: Verify server instance
493
+ assert server_instance["id"] == mcp_config.id
494
+ assert server_instance["user_id"] == "workflow_user_123"
495
+ assert server_instance["type"] == "streamable_http"
496
+ assert isinstance(server_instance["url"], str)
497
+ assert len(server_instance["url"]) > 0
498
+
499
+ print("✅ Full workflow successful!")
500
+ print(f" Config ID: {mcp_config.id}")
501
+ print(f" Server URL: {server_instance['url'][:50]}...")
502
+
503
+ except Exception as e:
504
+ print(f"Workflow generate step failed (may be expected): {e}")
505
+
506
+ def test_api_compatibility_with_typescript(self, composio_client):
507
+ """Test that Python API matches TypeScript patterns."""
508
+ # Check method availability (matching TypeScript)
509
+ mcp = composio_client.mcp
510
+
511
+ # CRUD operations
512
+ assert hasattr(mcp, "create"), "Missing create method"
513
+ assert hasattr(mcp, "list"), "Missing list method"
514
+ assert hasattr(mcp, "get"), "Missing get method"
515
+ assert hasattr(mcp, "update"), "Missing update method"
516
+ assert hasattr(mcp, "delete"), "Missing delete method"
517
+
518
+ # Instance generation
519
+ assert hasattr(mcp, "generate"), "Missing generate method"
520
+
521
+ print("✅ API compatibility verified with TypeScript patterns")
522
+
523
+ @pytest.mark.skip(
524
+ reason="MCP update bug with 'custom_tools' argument - TypeError in McpResource.update()"
525
+ )
526
+ def test_full_crud_cycle(self, composio_client):
527
+ """Test complete CRUD cycle: create -> get -> update -> get with assertions at each step."""
528
+ test_name = generate_unique_name("pytest-crud")
529
+
530
+ # Step 1: CREATE MCP server
531
+ print(f"🏗️ Step 1: Creating MCP server '{test_name}'")
532
+ mcp_server = composio_client.mcp.create(
533
+ test_name,
534
+ toolkits=["composio_search"],
535
+ allowed_tools=["COMPOSIO_SEARCH_DUCK_DUCK_GO_SEARCH"],
536
+ manually_manage_connections=False,
537
+ )
538
+
539
+ # Assertions after CREATE
540
+ assert mcp_server.id is not None, "Created server should have ID"
541
+ assert mcp_server.name == test_name, f"Server name should be {test_name}"
542
+ assert mcp_server.allowed_tools == ["COMPOSIO_SEARCH_DUCK_DUCK_GO_SEARCH"], (
543
+ "Should have correct allowed tools"
544
+ )
545
+ assert len(mcp_server.auth_config_ids) == 0, (
546
+ "Should have no auth configs for non-auth toolkits"
547
+ )
548
+ assert callable(mcp_server.generate), "Should have generate method"
549
+ print(f"✅ CREATE: Server created with ID {mcp_server.id}")
550
+
551
+ # Step 2: GET MCP server
552
+ print(f"📖 Step 2: Getting MCP server '{mcp_server.id}'")
553
+ retrieved_server = composio_client.mcp.get(mcp_server.id)
554
+
555
+ # Assertions after GET
556
+ assert retrieved_server.id == mcp_server.id, (
557
+ "Retrieved ID should match created ID"
558
+ )
559
+ assert retrieved_server.name == test_name, "Retrieved name should match"
560
+ print("✅ GET: Successfully retrieved server")
561
+
562
+ # Step 3: UPDATE MCP server
563
+ updated_name = f"{test_name}-updated"
564
+ print(
565
+ f"🔄 Step 3: Updating MCP server to '{updated_name}' with additional toolkit"
566
+ )
567
+ composio_client.mcp.update(
568
+ mcp_server.id,
569
+ name=updated_name,
570
+ toolkits=["composio_search", "text_to_pdf"],
571
+ allowed_tools=[
572
+ "COMPOSIO_SEARCH_DUCK_DUCK_GO_SEARCH",
573
+ "TEXT_TO_PDF_CONVERT_TEXT_TO_PDF",
574
+ ],
575
+ )
576
+ print("✅ UPDATE: Server updated successfully")
577
+
578
+ # Step 4: GET updated MCP server
579
+ print("📖 Step 4: Getting updated MCP server")
580
+ final_server = composio_client.mcp.get(mcp_server.id)
581
+
582
+ # Assertions after UPDATE and final GET
583
+ assert final_server.id == mcp_server.id, "ID should remain the same"
584
+
585
+ # Verify that updates took effect
586
+ assert final_server.name == updated_name, (
587
+ f"Name should be updated to {updated_name}"
588
+ )
589
+
590
+ # Check that toolkits were updated (should now include both composio_search and text_to_pdf)
591
+ expected_toolkits = ["composio_search", "text_to_pdf"]
592
+ actual_toolkits = getattr(final_server, "toolkits", [])
593
+ for toolkit in expected_toolkits:
594
+ assert toolkit in actual_toolkits, (
595
+ f"Updated toolkits should include {toolkit}"
596
+ )
597
+
598
+ # Check that allowed_tools were updated
599
+ expected_tools = [
600
+ "COMPOSIO_SEARCH_DUCK_DUCK_GO_SEARCH",
601
+ "TEXT_TO_PDF_CONVERT_TEXT_TO_PDF",
602
+ ]
603
+ actual_tools = getattr(final_server, "allowed_tools", [])
604
+ for tool in expected_tools:
605
+ assert tool in actual_tools, f"Updated allowed_tools should include {tool}"
606
+
607
+ print("✅ FINAL GET: Retrieved updated server with correct changes")
608
+ print(f" Updated name: {final_server.name}")
609
+ print(f" Updated toolkits: {actual_toolkits}")
610
+ print(f" Updated allowed_tools: {actual_tools}")
611
+
612
+ print(f"🎉 CRUD cycle test completed for server {mcp_server.id}")
613
+
614
+
615
+# Direct execution support for backward compatibility
616
+def main():
617
+ """Main test function for direct execution."""
618
+ print("🎯 MCP Integration Tests")
619
+ print("Testing MCP functionality with non-auth toolkits")
620
+ print()
621
+
622
+ # Initialize Composio client
623
+ composio = Composio()
624
+
625
+ # Run the no-auth toolkit test directly
626
+ try:
627
+ # Create test instance
628
+ test_instance = TestMCPNoAuthToolkits()
629
+ test_instance.test_mcp_with_no_auth_toolkits(composio)
630
+
631
+ print("\n🎉 All tests passed! MCP is working with non-auth toolkits.")
632
+ return True
633
+ except Exception as e:
634
+ print(f"\n💥 Tests failed: {e}")
635
+ import traceback
636
+
637
+ traceback.print_exc()
638
+ return False
639
+
640
+
641
+if __name__ == "__main__":
642
+ success = main()
643
+ exit(0 if success else 1)
python/composio/integration_test/test_tool_router.py
new
+127
@@ -0,0 +1,127 @@
1
+"""
2
+Integration tests for ToolRouter functionality.
3
+
4
+This module tests the experimental ToolRouter feature with clean Python API design.
5
+"""
6
+
7
+import os
8
+import time
9
+
10
+import pytest
11
+
12
+from composio import Composio
13
+from composio.exceptions import ValidationError
14
+
15
+# Test configuration
16
+API_KEY = os.getenv("COMPOSIO_API_KEY")
17
+
18
+if not API_KEY:
19
+ pytest.fail("COMPOSIO_API_KEY environment variable not set", pytrace=False)
20
+
21
+
22
+@pytest.fixture
23
+def composio_client():
24
+ """Fixture providing Composio client instance."""
25
+ return Composio(api_key=API_KEY)
26
+
27
+
28
+class TestToolRouterStructure:
29
+ """Test the basic structure and availability of ToolRouter features."""
30
+
31
+ def test_experimental_tool_router_exists(self, composio_client):
32
+ """Test that experimental.tool_router exists."""
33
+ assert hasattr(composio_client.experimental, "tool_router"), (
34
+ "Missing experimental.tool_router"
35
+ )
36
+
37
+ def test_tool_router_methods_available(self, composio_client):
38
+ """Test that required ToolRouter methods are available."""
39
+ assert hasattr(composio_client.experimental.tool_router, "create_session"), (
40
+ "Missing create_session method"
41
+ )
42
+
43
+
44
+class TestToolRouterOperations:
45
+ """Test ToolRouter operations."""
46
+
47
+ def test_create_session_with_string_toolkits(self, composio_client):
48
+ """Test creating tool router session with simple string toolkit names."""
49
+ user_id = f"pytest-user-{int(time.time()) % 1000000}"
50
+
51
+ session = composio_client.experimental.tool_router.create_session(
52
+ user_id, toolkits=["composio_search", "text_to_pdf"]
53
+ )
54
+
55
+ # Basic validation
56
+ assert session["session_id"]
57
+ assert session["url"]
58
+ assert isinstance(session["session_id"], str)
59
+ assert isinstance(session["url"], str)
60
+ assert len(session["url"]) > 0
61
+
62
+ def test_create_session_with_mixed_toolkits(self, composio_client):
63
+ """Test creating session with mixed string and object formats."""
64
+ user_id = f"pytest-mixed-{int(time.time()) % 1000000}"
65
+
66
+ session = composio_client.experimental.tool_router.create_session(
67
+ user_id,
68
+ toolkits=[
69
+ "composio_search", # String format
70
+ {
71
+ "toolkit": "text_to_pdf",
72
+ # No auth_config_id needed for non-auth toolkit
73
+ }, # Object format
74
+ ],
75
+ manually_manage_connections=False,
76
+ )
77
+
78
+ # Basic validation
79
+ assert session["session_id"]
80
+ assert session["url"]
81
+
82
+ def test_create_session_minimal(self, composio_client):
83
+ """Test creating session with minimal parameters."""
84
+ user_id = f"pytest-minimal-{int(time.time()) % 1000000}"
85
+
86
+ session = composio_client.experimental.tool_router.create_session(user_id)
87
+
88
+ # Should work even without toolkits specified
89
+ assert session["session_id"]
90
+ assert session["url"]
91
+
92
+
93
+class TestToolRouterErrorHandling:
94
+ """Test error handling and edge cases."""
95
+
96
+ def test_create_session_with_empty_user_id(self, composio_client):
97
+ """Test creating session with empty user ID."""
98
+ with pytest.raises(ValidationError):
99
+ composio_client.experimental.tool_router.create_session("")
100
+
101
+
102
+class TestToolRouterRealWorldScenarios:
103
+ """Test real-world usage scenarios."""
104
+
105
+ def test_complete_session_workflow(self, composio_client):
106
+ """Test complete workflow: create session -> use session details."""
107
+ user_id = f"pytest-workflow-{int(time.time()) % 1000000}"
108
+
109
+ print(f"🏗️ Creating tool router session for user: {user_id}")
110
+
111
+ # Create session with non-auth toolkits
112
+ session = composio_client.experimental.tool_router.create_session(
113
+ user_id,
114
+ toolkits=["composio_search", "text_to_pdf"],
115
+ manually_manage_connections=False,
116
+ )
117
+
118
+ print("✅ Session created successfully!")
119
+ print(f" Session ID: {session['session_id']}")
120
+ print(f" Session URL: {session['url']}")
121
+
122
+ # Validate session structure
123
+ assert session["session_id"]
124
+ assert session["url"]
125
+ assert "composio" in session["url"] # Should be a Composio URL
126
+
127
+ print("🎉 ToolRouter workflow completed successfully!")
python/pyproject.toml
+1
-1
@@ -29,5 +29,5 @@ dev = [
29
"twine>=6.1.0",
30
"click>=8.2.1",
31
"semver>=3.0.4",
32
- "tomli>=2.2.1"
32
+ "tomli>=2.2.1",
33
]