Composio powers 1000+ toolkits, tool search, context management, authentication, and a sandboxed workbench to help you build AI agents that turn intent into action.
| 1 | import { describe, it, expect, vi, beforeEach } from 'vitest'; |
| 2 | import { MCP } from '../../src/models/MCP'; |
| 3 | import ComposioClient from '@composio/client'; |
| 4 | import { telemetry } from '../../src/telemetry/Telemetry'; |
| 5 | import { ValidationError } from '../../src/errors/ValidationErrors'; |
| 6 | |
| 7 | // Mock dependencies |
| 8 | vi.mock('../../src/telemetry/Telemetry', () => ({ |
| 9 | telemetry: { |
| 10 | instrument: vi.fn(), |
| 11 | }, |
| 12 | })); |
| 13 | |
| 14 | // Create mock client with MCP methods |
| 15 | const createMockClient = () => ({ |
| 16 | baseURL: 'https://api.composio.dev', |
| 17 | apiKey: 'test-api-key', |
| 18 | mcp: { |
| 19 | custom: { |
| 20 | create: vi.fn(), |
| 21 | }, |
| 22 | list: vi.fn(), |
| 23 | retrieve: vi.fn(), |
| 24 | delete: vi.fn(), |
| 25 | update: vi.fn(), |
| 26 | generate: { |
| 27 | url: vi.fn(), |
| 28 | }, |
| 29 | }, |
| 30 | withOptions() { |
| 31 | return this; |
| 32 | }, |
| 33 | }); |
| 34 | |
| 35 | describe('MCP', () => { |
| 36 | let mcp: MCP; |
| 37 | let mockClient: ReturnType<typeof createMockClient>; |
| 38 | |
| 39 | beforeEach(() => { |
| 40 | mockClient = createMockClient(); |
| 41 | mcp = new MCP(mockClient as unknown as ComposioClient); |
| 42 | }); |
| 43 | |
| 44 | describe('constructor', () => { |
| 45 | it('should create an instance successfully', () => { |
| 46 | expect(mcp).toBeInstanceOf(MCP); |
| 47 | expect(mcp.client).toBe(mockClient); |
| 48 | }); |
| 49 | |
| 50 | it('should instrument the class with telemetry', () => { |
| 51 | expect(telemetry.instrument).toHaveBeenCalledWith(mcp, 'MCP'); |
| 52 | }); |
| 53 | }); |
| 54 | |
| 55 | describe('create', () => { |
| 56 | it('should create an MCP server successfully', async () => { |
| 57 | const mockResponse = { |
| 58 | id: 'mcp_123', |
| 59 | name: 'test-server', |
| 60 | allowed_tools: ['GMAIL_SEND_EMAIL'], |
| 61 | auth_config_ids: ['auth_456'], |
| 62 | commands: { |
| 63 | claude: 'claude-command', |
| 64 | cursor: 'cursor-command', |
| 65 | windsurf: 'windsurf-command', |
| 66 | }, |
| 67 | mcp_url: 'https://mcp.example.com/server', |
| 68 | }; |
| 69 | |
| 70 | mockClient.mcp.custom.create.mockResolvedValueOnce(mockResponse); |
| 71 | |
| 72 | const result = await mcp.create('test-server', { |
| 73 | toolkits: ['gmail'], |
| 74 | manuallyManageConnections: false, |
| 75 | }); |
| 76 | |
| 77 | expect(result).toHaveProperty('id', 'mcp_123'); |
| 78 | expect(result).toHaveProperty('name', 'test-server'); |
| 79 | expect(result).toHaveProperty('generate'); |
| 80 | expect(typeof result.generate).toBe('function'); |
| 81 | expect(mockClient.mcp.custom.create).toHaveBeenCalledWith( |
| 82 | { |
| 83 | name: 'test-server', |
| 84 | toolkits: ['gmail'], |
| 85 | auth_config_ids: [], |
| 86 | allowed_tools: [], |
| 87 | managed_auth_via_composio: true, |
| 88 | }, |
| 89 | undefined |
| 90 | ); |
| 91 | }); |
| 92 | |
| 93 | it('should handle toolkits with auth config IDs', async () => { |
| 94 | const mockResponse = { |
| 95 | id: 'mcp_123', |
| 96 | name: 'test-server', |
| 97 | allowed_tools: [], |
| 98 | auth_config_ids: ['auth_456'], |
| 99 | commands: { |
| 100 | claude: 'cmd', |
| 101 | cursor: 'cmd', |
| 102 | windsurf: 'cmd', |
| 103 | }, |
| 104 | mcp_url: 'https://mcp.example.com', |
| 105 | }; |
| 106 | |
| 107 | mockClient.mcp.custom.create.mockResolvedValueOnce(mockResponse); |
| 108 | |
| 109 | await mcp.create('test-server', { |
| 110 | toolkits: [{ toolkit: 'gmail', authConfigId: 'auth_456' }], |
| 111 | }); |
| 112 | |
| 113 | expect(mockClient.mcp.custom.create).toHaveBeenCalledWith( |
| 114 | { |
| 115 | name: 'test-server', |
| 116 | toolkits: ['gmail'], |
| 117 | auth_config_ids: ['auth_456'], |
| 118 | allowed_tools: [], |
| 119 | managed_auth_via_composio: true, |
| 120 | }, |
| 121 | undefined |
| 122 | ); |
| 123 | }); |
| 124 | |
| 125 | it('should validate configuration', async () => { |
| 126 | await expect(mcp.create('test', { toolkits: null as unknown })).rejects.toThrow( |
| 127 | ValidationError |
| 128 | ); |
| 129 | }); |
| 130 | }); |
| 131 | |
| 132 | describe('list', () => { |
| 133 | it('should list MCP servers with default options', async () => { |
| 134 | const mockResponse = { |
| 135 | items: [ |
| 136 | { |
| 137 | id: 'mcp_1', |
| 138 | name: 'server-1', |
| 139 | allowed_tools: ['TOOL_1'], |
| 140 | auth_config_ids: ['auth_1'], |
| 141 | commands: { |
| 142 | claude: 'cmd', |
| 143 | cursor: 'cmd', |
| 144 | windsurf: 'cmd', |
| 145 | }, |
| 146 | mcp_url: 'https://mcp.example.com', |
| 147 | toolkit_icons: {}, |
| 148 | server_instance_count: 0, |
| 149 | toolkits: ['gmail'], |
| 150 | }, |
| 151 | ], |
| 152 | current_page: 1, |
| 153 | total_pages: 1, |
| 154 | }; |
| 155 | |
| 156 | mockClient.mcp.list.mockResolvedValueOnce(mockResponse); |
| 157 | |
| 158 | const result = await mcp.list({}); |
| 159 | |
| 160 | expect(result).toHaveProperty('items'); |
| 161 | expect(result).toHaveProperty('currentPage', 1); |
| 162 | expect(result).toHaveProperty('totalPages', 1); |
| 163 | expect(result.items).toHaveLength(1); |
| 164 | expect(mockClient.mcp.list).toHaveBeenCalledWith( |
| 165 | { |
| 166 | page_no: 1, |
| 167 | limit: 10, |
| 168 | toolkits: undefined, |
| 169 | auth_config_ids: undefined, |
| 170 | name: undefined, |
| 171 | }, |
| 172 | undefined |
| 173 | ); |
| 174 | }); |
| 175 | |
| 176 | it('should list MCP servers with filters', async () => { |
| 177 | const mockResponse = { |
| 178 | items: [], |
| 179 | current_page: 1, |
| 180 | total_pages: 1, |
| 181 | }; |
| 182 | |
| 183 | mockClient.mcp.list.mockResolvedValueOnce(mockResponse); |
| 184 | |
| 185 | await mcp.list({ |
| 186 | page: 2, |
| 187 | limit: 20, |
| 188 | toolkits: ['gmail', 'slack'], |
| 189 | authConfigs: ['auth_1'], |
| 190 | name: 'test', |
| 191 | }); |
| 192 | |
| 193 | expect(mockClient.mcp.list).toHaveBeenCalledWith( |
| 194 | { |
| 195 | page_no: 2, |
| 196 | limit: 20, |
| 197 | toolkits: 'gmail,slack', |
| 198 | auth_config_ids: 'auth_1', |
| 199 | name: 'test', |
| 200 | }, |
| 201 | undefined |
| 202 | ); |
| 203 | }); |
| 204 | |
| 205 | it('should handle validation errors', async () => { |
| 206 | await expect(mcp.list({ page: 'invalid' as unknown })).rejects.toThrow(ValidationError); |
| 207 | }); |
| 208 | }); |
| 209 | |
| 210 | describe('get', () => { |
| 211 | it('should retrieve an MCP server by ID', async () => { |
| 212 | const mockResponse = { |
| 213 | id: 'mcp_123', |
| 214 | name: 'test-server', |
| 215 | allowed_tools: ['GMAIL_SEND_EMAIL'], |
| 216 | auth_config_ids: ['auth_456'], |
| 217 | commands: { |
| 218 | claude: 'cmd', |
| 219 | cursor: 'cmd', |
| 220 | windsurf: 'cmd', |
| 221 | }, |
| 222 | mcp_url: 'https://mcp.example.com', |
| 223 | toolkit_icons: {}, |
| 224 | server_instance_count: 5, |
| 225 | toolkits: ['gmail'], |
| 226 | }; |
| 227 | |
| 228 | mockClient.mcp.retrieve.mockResolvedValueOnce(mockResponse); |
| 229 | |
| 230 | const result = await mcp.get('mcp_123'); |
| 231 | |
| 232 | expect(result).toHaveProperty('id', 'mcp_123'); |
| 233 | expect(result).toHaveProperty('name', 'test-server'); |
| 234 | expect(result).toHaveProperty('serverInstanceCount', 5); |
| 235 | expect(mockClient.mcp.retrieve).toHaveBeenCalledWith('mcp_123', undefined); |
| 236 | }); |
| 237 | }); |
| 238 | |
| 239 | describe('delete', () => { |
| 240 | it('should delete an MCP server', async () => { |
| 241 | const mockResponse = { |
| 242 | id: 'mcp_123', |
| 243 | deleted: true, |
| 244 | }; |
| 245 | |
| 246 | mockClient.mcp.delete.mockResolvedValueOnce(mockResponse); |
| 247 | |
| 248 | const result = await mcp.delete('mcp_123'); |
| 249 | |
| 250 | expect(result).toEqual(mockResponse); |
| 251 | expect(mockClient.mcp.delete).toHaveBeenCalledWith('mcp_123', undefined); |
| 252 | }); |
| 253 | }); |
| 254 | |
| 255 | describe('update', () => { |
| 256 | it('should update an MCP server', async () => { |
| 257 | const mockResponse = { |
| 258 | id: 'mcp_123', |
| 259 | name: 'updated-server', |
| 260 | allowed_tools: ['NEW_TOOL'], |
| 261 | auth_config_ids: ['auth_789'], |
| 262 | commands: { |
| 263 | claude: 'cmd', |
| 264 | cursor: 'cmd', |
| 265 | windsurf: 'cmd', |
| 266 | }, |
| 267 | mcp_url: 'https://mcp.example.com', |
| 268 | toolkit_icons: {}, |
| 269 | server_instance_count: 3, |
| 270 | toolkits: ['slack'], |
| 271 | }; |
| 272 | |
| 273 | mockClient.mcp.update.mockResolvedValueOnce(mockResponse); |
| 274 | |
| 275 | const result = await mcp.update('mcp_123', { |
| 276 | name: 'updated-server', |
| 277 | toolkits: ['slack'], |
| 278 | }); |
| 279 | |
| 280 | expect(result).toHaveProperty('id', 'mcp_123'); |
| 281 | expect(result).toHaveProperty('name', 'updated-server'); |
| 282 | expect(mockClient.mcp.update).toHaveBeenCalledWith( |
| 283 | 'mcp_123', |
| 284 | { |
| 285 | name: 'updated-server', |
| 286 | toolkits: ['slack'], |
| 287 | auth_config_ids: [], |
| 288 | }, |
| 289 | undefined |
| 290 | ); |
| 291 | }); |
| 292 | |
| 293 | it('should send tool-only updates through allowed_tools without toolkits', async () => { |
| 294 | const mockResponse = { |
| 295 | id: 'mcp_123', |
| 296 | name: 'test-server', |
| 297 | allowed_tools: ['GITHUB_CREATE_ISSUE'], |
| 298 | auth_config_ids: ['auth_456'], |
| 299 | commands: { |
| 300 | claude: 'cmd', |
| 301 | cursor: 'cmd', |
| 302 | windsurf: 'cmd', |
| 303 | }, |
| 304 | mcp_url: 'https://mcp.example.com', |
| 305 | toolkit_icons: {}, |
| 306 | server_instance_count: 1, |
| 307 | toolkits: ['github'], |
| 308 | }; |
| 309 | |
| 310 | mockClient.mcp.update.mockResolvedValueOnce(mockResponse); |
| 311 | |
| 312 | await mcp.update('mcp_123', { allowedTools: ['GITHUB_CREATE_ISSUE'] }); |
| 313 | |
| 314 | expect(mockClient.mcp.update).toHaveBeenCalledWith( |
| 315 | 'mcp_123', |
| 316 | { allowed_tools: ['GITHUB_CREATE_ISSUE'] }, |
| 317 | undefined |
| 318 | ); |
| 319 | }); |
| 320 | |
| 321 | it('should send allowed_tools alongside toolkits when both are provided', async () => { |
| 322 | const mockResponse = { |
| 323 | id: 'mcp_123', |
| 324 | name: 'test-server', |
| 325 | allowed_tools: ['GITHUB_CREATE_ISSUE'], |
| 326 | auth_config_ids: ['auth_456'], |
| 327 | commands: { |
| 328 | claude: 'cmd', |
| 329 | cursor: 'cmd', |
| 330 | windsurf: 'cmd', |
| 331 | }, |
| 332 | mcp_url: 'https://mcp.example.com', |
| 333 | toolkit_icons: {}, |
| 334 | server_instance_count: 1, |
| 335 | toolkits: ['github'], |
| 336 | }; |
| 337 | |
| 338 | mockClient.mcp.update.mockResolvedValueOnce(mockResponse); |
| 339 | |
| 340 | await mcp.update('mcp_123', { |
| 341 | toolkits: [{ toolkit: 'github', authConfigId: 'auth_456' }], |
| 342 | allowedTools: ['GITHUB_CREATE_ISSUE'], |
| 343 | }); |
| 344 | |
| 345 | expect(mockClient.mcp.update).toHaveBeenCalledWith( |
| 346 | 'mcp_123', |
| 347 | { |
| 348 | toolkits: ['github'], |
| 349 | auth_config_ids: ['auth_456'], |
| 350 | allowed_tools: ['GITHUB_CREATE_ISSUE'], |
| 351 | }, |
| 352 | undefined |
| 353 | ); |
| 354 | }); |
| 355 | |
| 356 | it('should invert manuallyManageConnections into managed_auth_via_composio', async () => { |
| 357 | const mockResponse = { |
| 358 | id: 'mcp_123', |
| 359 | name: 'test-server', |
| 360 | allowed_tools: [], |
| 361 | auth_config_ids: [], |
| 362 | commands: { |
| 363 | claude: 'cmd', |
| 364 | cursor: 'cmd', |
| 365 | windsurf: 'cmd', |
| 366 | }, |
| 367 | mcp_url: 'https://mcp.example.com', |
| 368 | toolkit_icons: {}, |
| 369 | server_instance_count: 1, |
| 370 | toolkits: [], |
| 371 | }; |
| 372 | |
| 373 | mockClient.mcp.update.mockResolvedValue(mockResponse); |
| 374 | |
| 375 | // manuallyManageConnections: true means Composio does NOT manage auth |
| 376 | await mcp.update('mcp_123', { manuallyManageConnections: true }); |
| 377 | expect(mockClient.mcp.update).toHaveBeenLastCalledWith( |
| 378 | 'mcp_123', |
| 379 | { managed_auth_via_composio: false }, |
| 380 | undefined |
| 381 | ); |
| 382 | |
| 383 | // manuallyManageConnections: false means Composio DOES manage auth |
| 384 | await mcp.update('mcp_123', { manuallyManageConnections: false }); |
| 385 | expect(mockClient.mcp.update).toHaveBeenLastCalledWith( |
| 386 | 'mcp_123', |
| 387 | { managed_auth_via_composio: true }, |
| 388 | undefined |
| 389 | ); |
| 390 | |
| 391 | // Omitting the flag must not send the field at all (sparse PATCH) |
| 392 | await mcp.update('mcp_123', { name: 'same-name' }); |
| 393 | expect(mockClient.mcp.update).toHaveBeenLastCalledWith( |
| 394 | 'mcp_123', |
| 395 | { name: 'same-name' }, |
| 396 | undefined |
| 397 | ); |
| 398 | }); |
| 399 | |
| 400 | it('should validate update parameters', async () => { |
| 401 | await expect(mcp.update('mcp_123', { toolkits: 'invalid' as unknown })).rejects.toThrow( |
| 402 | ValidationError |
| 403 | ); |
| 404 | }); |
| 405 | }); |
| 406 | |
| 407 | describe('generate', () => { |
| 408 | it('should generate server instance with valid parameters', async () => { |
| 409 | const mockRetrieveResponse = { |
| 410 | id: 'mcp_123', |
| 411 | name: 'test-server', |
| 412 | allowed_tools: ['GMAIL_SEND_EMAIL'], |
| 413 | auth_config_ids: ['auth_456'], |
| 414 | }; |
| 415 | |
| 416 | const mockUrlResponse = { |
| 417 | user_ids_url: ['https://mcp.example.com/instance/user123'], |
| 418 | connected_account_urls: [], |
| 419 | mcp_url: 'https://mcp.example.com', |
| 420 | }; |
| 421 | |
| 422 | mockClient.mcp.retrieve.mockResolvedValueOnce(mockRetrieveResponse); |
| 423 | mockClient.mcp.generate.url.mockResolvedValueOnce(mockUrlResponse); |
| 424 | |
| 425 | const result = await mcp.generate('user123', 'mcp_123'); |
| 426 | |
| 427 | expect(result).toHaveProperty('id', 'mcp_123'); |
| 428 | expect(result).toHaveProperty('userId', 'user123'); |
| 429 | expect(result).toHaveProperty('type', 'streamable_http'); |
| 430 | expect(result).toHaveProperty('url', 'https://mcp.example.com/instance/user123'); |
| 431 | expect(mockClient.mcp.generate.url).toHaveBeenCalledWith( |
| 432 | { |
| 433 | mcp_server_id: 'mcp_123', |
| 434 | user_ids: ['user123'], |
| 435 | managed_auth_by_composio: true, |
| 436 | }, |
| 437 | undefined |
| 438 | ); |
| 439 | }); |
| 440 | |
| 441 | it('should handle manual connection management', async () => { |
| 442 | const mockRetrieveResponse = { |
| 443 | id: 'mcp_123', |
| 444 | name: 'test-server', |
| 445 | allowed_tools: [], |
| 446 | auth_config_ids: [], |
| 447 | }; |
| 448 | |
| 449 | const mockUrlResponse = { |
| 450 | user_ids_url: ['https://mcp.example.com/instance/user123'], |
| 451 | connected_account_urls: [], |
| 452 | mcp_url: 'https://mcp.example.com', |
| 453 | }; |
| 454 | |
| 455 | mockClient.mcp.retrieve.mockResolvedValueOnce(mockRetrieveResponse); |
| 456 | mockClient.mcp.generate.url.mockResolvedValueOnce(mockUrlResponse); |
| 457 | |
| 458 | await mcp.generate('user123', 'mcp_123', { |
| 459 | manuallyManageConnections: true, |
| 460 | }); |
| 461 | |
| 462 | expect(mockClient.mcp.generate.url).toHaveBeenCalledWith( |
| 463 | { |
| 464 | mcp_server_id: 'mcp_123', |
| 465 | user_ids: ['user123'], |
| 466 | managed_auth_by_composio: false, |
| 467 | }, |
| 468 | undefined |
| 469 | ); |
| 470 | }); |
| 471 | |
| 472 | it('should validate parameters', async () => { |
| 473 | const mockRetrieveResponse = { |
| 474 | id: 'mcp_123', |
| 475 | name: 'test-server', |
| 476 | allowed_tools: [], |
| 477 | auth_config_ids: [], |
| 478 | }; |
| 479 | |
| 480 | mockClient.mcp.retrieve.mockResolvedValueOnce(mockRetrieveResponse); |
| 481 | |
| 482 | await expect( |
| 483 | mcp.generate('user123', 'mcp_123', { manuallyManageConnections: 'invalid' as unknown }) |
| 484 | ).rejects.toThrow(ValidationError); |
| 485 | }); |
| 486 | }); |
| 487 | |
| 488 | describe('create response with generate method', () => { |
| 489 | it('should create server and use the generate method', async () => { |
| 490 | const mockCreateResponse = { |
| 491 | id: 'mcp_123', |
| 492 | name: 'test-server', |
| 493 | allowed_tools: ['GMAIL_SEND_EMAIL'], |
| 494 | auth_config_ids: ['auth_456'], |
| 495 | commands: { |
| 496 | claude: 'cmd', |
| 497 | cursor: 'cmd', |
| 498 | windsurf: 'cmd', |
| 499 | }, |
| 500 | mcp_url: 'https://mcp.example.com', |
| 501 | }; |
| 502 | |
| 503 | const mockRetrieveResponse = { |
| 504 | id: 'mcp_123', |
| 505 | name: 'test-server', |
| 506 | allowed_tools: ['GMAIL_SEND_EMAIL'], |
| 507 | auth_config_ids: ['auth_456'], |
| 508 | }; |
| 509 | |
| 510 | const mockUrlResponse = { |
| 511 | user_ids_url: ['https://mcp.example.com/instance/user123'], |
| 512 | connected_account_urls: [], |
| 513 | mcp_url: 'https://mcp.example.com', |
| 514 | }; |
| 515 | |
| 516 | mockClient.mcp.custom.create.mockResolvedValueOnce(mockCreateResponse); |
| 517 | mockClient.mcp.retrieve.mockResolvedValueOnce(mockRetrieveResponse); |
| 518 | mockClient.mcp.generate.url.mockResolvedValueOnce(mockUrlResponse); |
| 519 | |
| 520 | const server = await mcp.create('test-server', { |
| 521 | toolkits: ['gmail'], |
| 522 | }); |
| 523 | |
| 524 | expect(server.generate).toBeDefined(); |
| 525 | |
| 526 | const instance = await server.generate('user123'); |
| 527 | |
| 528 | expect(instance).toHaveProperty('userId', 'user123'); |
| 529 | expect(instance).toHaveProperty('url', 'https://mcp.example.com/instance/user123'); |
| 530 | }); |
| 531 | }); |
| 532 | }); |