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, type Mock } from 'vitest'; |
| 2 | import { MastraProvider } from '../src'; |
| 3 | import { Tool } from '@composio/core'; |
| 4 | import { createTool } from '@mastra/core/tools'; |
| 5 | |
| 6 | // Define an interface for our mocked Mastra tool |
| 7 | interface MockedMastraTool { |
| 8 | id: string; |
| 9 | description: string; |
| 10 | inputSchema?: unknown; |
| 11 | outputSchema?: unknown; |
| 12 | execute: Function; |
| 13 | _isMockedMastraTool: boolean; |
| 14 | } |
| 15 | |
| 16 | // Minimal shape of the config object the wrapped tool's `execute` closure is |
| 17 | // pulled off of, i.e. the argument the mocked `createTool` factory (below) was |
| 18 | // called with. Only `execute` is dereferenced at the `.mock.calls[...][0]` |
| 19 | // call sites in this file, so that's all this type declares. |
| 20 | type CreateToolMockConfig = { execute: (...args: unknown[]) => unknown }; |
| 21 | type CreateToolMock = Mock<(config: CreateToolMockConfig) => unknown>; |
| 22 | const getCreatedToolExecute = () => |
| 23 | (createTool as unknown as CreateToolMock).mock.calls[0][0].execute; |
| 24 | |
| 25 | // Mock the @mastra/core/tools module |
| 26 | vi.mock('@mastra/core/tools', () => { |
| 27 | return { |
| 28 | createTool: vi.fn().mockImplementation(toolConfig => { |
| 29 | return { |
| 30 | id: toolConfig.id, |
| 31 | description: toolConfig.description, |
| 32 | inputSchema: toolConfig.inputSchema, |
| 33 | outputSchema: toolConfig.outputSchema, |
| 34 | execute: toolConfig.execute, |
| 35 | _isMockedMastraTool: true, |
| 36 | } as MockedMastraTool; |
| 37 | }), |
| 38 | }; |
| 39 | }); |
| 40 | |
| 41 | // Mock the applyCompatLayer function from @mastra/schema-compat |
| 42 | vi.mock('@mastra/schema-compat', async () => { |
| 43 | const actual = await vi.importActual('@mastra/schema-compat'); |
| 44 | return { |
| 45 | ...(actual as object), |
| 46 | applyCompatLayer: vi.fn().mockImplementation(({ schema }) => { |
| 47 | return { type: 'mock-zod-schema', originalSchema: schema }; |
| 48 | }), |
| 49 | }; |
| 50 | }); |
| 51 | |
| 52 | // Expected `originalSchema` for the standard mockTool output after the |
| 53 | // leniency relaxation applied in wrapTool (issue #3047): typed nodes become |
| 54 | // nullable and the object allows extra keys. All tools below spread `mockTool`, |
| 55 | // so they share this output shape. |
| 56 | const RELAXED_MOCK_OUTPUT_SCHEMA = { |
| 57 | type: 'mock-zod-schema', |
| 58 | originalSchema: { |
| 59 | type: ['object', 'null'], |
| 60 | additionalProperties: true, |
| 61 | properties: { |
| 62 | result: { |
| 63 | type: ['string', 'null'], |
| 64 | description: 'Test result', |
| 65 | }, |
| 66 | }, |
| 67 | }, |
| 68 | }; |
| 69 | |
| 70 | describe('MastraProvider', () => { |
| 71 | let provider: MastraProvider; |
| 72 | let mockTool: Tool; |
| 73 | let mockExecuteToolFn: Mock; |
| 74 | |
| 75 | beforeEach(() => { |
| 76 | provider = new MastraProvider(); |
| 77 | |
| 78 | // Mock the global execute tool function |
| 79 | mockExecuteToolFn = vi.fn().mockResolvedValue({ |
| 80 | data: { result: 'success' }, |
| 81 | error: null, |
| 82 | successful: true, |
| 83 | }); |
| 84 | provider._setExecuteToolFn(mockExecuteToolFn); |
| 85 | |
| 86 | // Create a mock Composio tool |
| 87 | mockTool = { |
| 88 | slug: 'test-tool', |
| 89 | name: 'Test Tool', |
| 90 | description: 'A tool for testing', |
| 91 | inputParameters: { |
| 92 | type: 'object', |
| 93 | properties: { |
| 94 | input: { |
| 95 | type: 'string', |
| 96 | description: 'Test input', |
| 97 | }, |
| 98 | }, |
| 99 | required: ['input'], |
| 100 | }, |
| 101 | outputParameters: { |
| 102 | type: 'object', |
| 103 | properties: { |
| 104 | result: { |
| 105 | type: 'string', |
| 106 | description: 'Test result', |
| 107 | }, |
| 108 | }, |
| 109 | }, |
| 110 | toolkit: { |
| 111 | slug: 'test-toolkit', |
| 112 | name: 'Test Toolkit', |
| 113 | }, |
| 114 | version: '20250909_00', |
| 115 | availableVersions: ['20250909_00', '20250901_00'], |
| 116 | tags: [], |
| 117 | }; |
| 118 | |
| 119 | // Reset mocks before each test |
| 120 | vi.clearAllMocks(); |
| 121 | }); |
| 122 | |
| 123 | describe('name property', () => { |
| 124 | it('should have the correct name', () => { |
| 125 | expect(provider.name).toBe('mastra'); |
| 126 | }); |
| 127 | }); |
| 128 | |
| 129 | describe('_isAgentic property', () => { |
| 130 | it('should be agentic', () => { |
| 131 | expect(provider._isAgentic).toBe(true); |
| 132 | }); |
| 133 | }); |
| 134 | |
| 135 | describe('wrapTool', () => { |
| 136 | it('should wrap a tool in Mastra createTool format', () => { |
| 137 | const wrapped = provider.wrapTool(mockTool, mockExecuteToolFn) as unknown as MockedMastraTool; |
| 138 | |
| 139 | expect(createTool).toHaveBeenCalledWith({ |
| 140 | id: mockTool.slug, |
| 141 | description: mockTool.description, |
| 142 | inputSchema: { type: 'mock-zod-schema', originalSchema: mockTool.inputParameters }, |
| 143 | outputSchema: RELAXED_MOCK_OUTPUT_SCHEMA, |
| 144 | execute: expect.any(Function), |
| 145 | }); |
| 146 | |
| 147 | expect(wrapped._isMockedMastraTool).toBe(true); |
| 148 | }); |
| 149 | |
| 150 | it('should normalize a stringified-JSON input to an object before executing (issue #2406)', async () => { |
| 151 | const wrapped = provider.wrapTool(mockTool, mockExecuteToolFn) as unknown as MockedMastraTool; |
| 152 | const params = { input: 'test-value' }; |
| 153 | |
| 154 | await wrapped.execute(params, {}); |
| 155 | expect(mockExecuteToolFn).toHaveBeenCalledWith(mockTool.slug, params); |
| 156 | |
| 157 | vi.clearAllMocks(); |
| 158 | await wrapped.execute(JSON.stringify(params), {}); |
| 159 | expect(mockExecuteToolFn).toHaveBeenCalledWith(mockTool.slug, params); |
| 160 | }); |
| 161 | |
| 162 | it('should throw a typed error for a malformed-JSON string input (issue #2406)', async () => { |
| 163 | const wrapped = provider.wrapTool(mockTool, mockExecuteToolFn) as unknown as MockedMastraTool; |
| 164 | |
| 165 | await expect(wrapped.execute('{"input":', {})).rejects.toThrow(/not valid JSON/); |
| 166 | }); |
| 167 | |
| 168 | it('should handle tools without input parameters', () => { |
| 169 | const toolWithoutInputParams: Tool = { |
| 170 | ...mockTool, |
| 171 | inputParameters: undefined, |
| 172 | }; |
| 173 | |
| 174 | const wrapped = provider.wrapTool( |
| 175 | toolWithoutInputParams, |
| 176 | mockExecuteToolFn |
| 177 | ) as unknown as MockedMastraTool; |
| 178 | |
| 179 | expect(createTool).toHaveBeenCalledWith({ |
| 180 | id: toolWithoutInputParams.slug, |
| 181 | description: toolWithoutInputParams.description, |
| 182 | inputSchema: { type: 'mock-zod-schema', originalSchema: {} }, |
| 183 | outputSchema: RELAXED_MOCK_OUTPUT_SCHEMA, |
| 184 | execute: expect.any(Function), |
| 185 | }); |
| 186 | |
| 187 | expect(wrapped._isMockedMastraTool).toBe(true); |
| 188 | }); |
| 189 | |
| 190 | it('should handle tools without output parameters', () => { |
| 191 | const toolWithoutOutputParams: Tool = { |
| 192 | ...mockTool, |
| 193 | outputParameters: undefined, |
| 194 | }; |
| 195 | |
| 196 | const wrapped = provider.wrapTool( |
| 197 | toolWithoutOutputParams, |
| 198 | mockExecuteToolFn |
| 199 | ) as unknown as MockedMastraTool; |
| 200 | |
| 201 | expect(createTool).toHaveBeenCalledWith({ |
| 202 | id: toolWithoutOutputParams.slug, |
| 203 | description: toolWithoutOutputParams.description, |
| 204 | inputSchema: { type: 'mock-zod-schema', originalSchema: mockTool.inputParameters }, |
| 205 | outputSchema: { type: 'mock-zod-schema', originalSchema: {} }, |
| 206 | execute: expect.any(Function), |
| 207 | }); |
| 208 | |
| 209 | expect(wrapped._isMockedMastraTool).toBe(true); |
| 210 | }); |
| 211 | |
| 212 | it('should handle tools without description', () => { |
| 213 | const toolWithoutDescription: Tool = { |
| 214 | ...mockTool, |
| 215 | description: undefined, |
| 216 | }; |
| 217 | |
| 218 | const wrapped = provider.wrapTool( |
| 219 | toolWithoutDescription, |
| 220 | mockExecuteToolFn |
| 221 | ) as unknown as MockedMastraTool; |
| 222 | |
| 223 | expect(createTool).toHaveBeenCalledWith({ |
| 224 | id: toolWithoutDescription.slug, |
| 225 | description: '', |
| 226 | inputSchema: { type: 'mock-zod-schema', originalSchema: mockTool.inputParameters }, |
| 227 | outputSchema: RELAXED_MOCK_OUTPUT_SCHEMA, |
| 228 | execute: expect.any(Function), |
| 229 | }); |
| 230 | |
| 231 | expect(wrapped._isMockedMastraTool).toBe(true); |
| 232 | }); |
| 233 | |
| 234 | it('should create a function that executes the tool with the right parameters', async () => { |
| 235 | provider.wrapTool(mockTool, mockExecuteToolFn) as unknown as MockedMastraTool; |
| 236 | |
| 237 | // Extract the execute function from the call to createTool() |
| 238 | const executeFunction = getCreatedToolExecute(); |
| 239 | |
| 240 | // Test the execute function |
| 241 | const inputData = { input: 'test-value' }; |
| 242 | const result = await executeFunction(inputData); |
| 243 | |
| 244 | expect(mockExecuteToolFn).toHaveBeenCalledWith(mockTool.slug, inputData); |
| 245 | expect(result).toEqual({ |
| 246 | data: { result: 'success' }, |
| 247 | error: null, |
| 248 | successful: true, |
| 249 | }); |
| 250 | }); |
| 251 | |
| 252 | it('should preserve and pass tool version information when executing', async () => { |
| 253 | const toolWithVersion = { ...mockTool, version: '20250101_01' }; |
| 254 | provider.wrapTool(toolWithVersion, mockExecuteToolFn) as unknown as MockedMastraTool; |
| 255 | |
| 256 | // Extract the execute function from the call to createTool() |
| 257 | const executeFunction = getCreatedToolExecute(); |
| 258 | |
| 259 | // Test that the version is passed correctly |
| 260 | const inputData = { input: 'version-test' }; |
| 261 | await executeFunction(inputData); |
| 262 | |
| 263 | expect(mockExecuteToolFn).toHaveBeenCalledWith(toolWithVersion.slug, inputData); |
| 264 | }); |
| 265 | |
| 266 | it('should handle tools without version information', async () => { |
| 267 | const toolWithoutVersion = { ...mockTool, version: undefined }; |
| 268 | provider.wrapTool(toolWithoutVersion, mockExecuteToolFn) as unknown as MockedMastraTool; |
| 269 | |
| 270 | // Extract the execute function from the call to createTool() |
| 271 | const executeFunction = getCreatedToolExecute(); |
| 272 | |
| 273 | // Test that undefined version is passed correctly |
| 274 | const inputData = { input: 'no-version-test' }; |
| 275 | await executeFunction(inputData); |
| 276 | |
| 277 | expect(mockExecuteToolFn).toHaveBeenCalledWith(toolWithoutVersion.slug, inputData); |
| 278 | }); |
| 279 | |
| 280 | it('should handle empty context parameter', async () => { |
| 281 | provider.wrapTool(mockTool, mockExecuteToolFn) as unknown as MockedMastraTool; |
| 282 | |
| 283 | // Extract the execute function from the call to createTool() |
| 284 | const executeFunction = getCreatedToolExecute(); |
| 285 | |
| 286 | // Test the execute function with empty input |
| 287 | const result = await executeFunction({}); |
| 288 | |
| 289 | expect(mockExecuteToolFn).toHaveBeenCalledWith(mockTool.slug, {}); |
| 290 | expect(result).toEqual({ |
| 291 | data: { result: 'success' }, |
| 292 | error: null, |
| 293 | successful: true, |
| 294 | }); |
| 295 | }); |
| 296 | |
| 297 | it('should handle missing context parameter', async () => { |
| 298 | provider.wrapTool(mockTool, mockExecuteToolFn) as unknown as MockedMastraTool; |
| 299 | |
| 300 | // Extract the execute function from the call to createTool() |
| 301 | const executeFunction = getCreatedToolExecute(); |
| 302 | |
| 303 | // Test the execute function without any parameters: a missing payload is |
| 304 | // normalized to an empty object rather than forwarded as undefined (issue #2406). |
| 305 | const result = await executeFunction(undefined); |
| 306 | |
| 307 | expect(mockExecuteToolFn).toHaveBeenCalledWith(mockTool.slug, {}); |
| 308 | expect(result).toEqual({ |
| 309 | data: { result: 'success' }, |
| 310 | error: null, |
| 311 | successful: true, |
| 312 | }); |
| 313 | }); |
| 314 | }); |
| 315 | |
| 316 | describe('wrapTools', () => { |
| 317 | it('should wrap multiple tools in a collection', () => { |
| 318 | const anotherTool: Tool = { |
| 319 | ...mockTool, |
| 320 | slug: 'another-tool', |
| 321 | name: 'Another Tool', |
| 322 | }; |
| 323 | const tools = [mockTool, anotherTool]; |
| 324 | |
| 325 | const wrapped = provider.wrapTools(tools, mockExecuteToolFn); |
| 326 | |
| 327 | // Verify the result has the expected properties |
| 328 | expect(Object.keys(wrapped)).toHaveLength(2); |
| 329 | expect(wrapped['test-tool']).toBeDefined(); |
| 330 | expect(wrapped['another-tool']).toBeDefined(); |
| 331 | |
| 332 | // Verify createTool was called with the right parameters for each tool |
| 333 | expect(createTool).toHaveBeenCalledTimes(2); |
| 334 | expect(createTool).toHaveBeenCalledWith({ |
| 335 | id: mockTool.slug, |
| 336 | description: mockTool.description, |
| 337 | inputSchema: { type: 'mock-zod-schema', originalSchema: mockTool.inputParameters }, |
| 338 | outputSchema: RELAXED_MOCK_OUTPUT_SCHEMA, |
| 339 | execute: expect.any(Function), |
| 340 | }); |
| 341 | expect(createTool).toHaveBeenCalledWith({ |
| 342 | id: anotherTool.slug, |
| 343 | description: anotherTool.description, |
| 344 | inputSchema: { type: 'mock-zod-schema', originalSchema: anotherTool.inputParameters }, |
| 345 | outputSchema: RELAXED_MOCK_OUTPUT_SCHEMA, |
| 346 | execute: expect.any(Function), |
| 347 | }); |
| 348 | }); |
| 349 | |
| 350 | it('should return an empty object for empty tools array', () => { |
| 351 | const wrapped = provider.wrapTools([], mockExecuteToolFn); |
| 352 | expect(wrapped).toEqual({}); |
| 353 | expect(createTool).not.toHaveBeenCalled(); |
| 354 | }); |
| 355 | |
| 356 | it('should use tool slugs as keys in the collection', () => { |
| 357 | const tools = [ |
| 358 | { ...mockTool, slug: 'first-tool' }, |
| 359 | { ...mockTool, slug: 'second-tool' }, |
| 360 | { ...mockTool, slug: 'third-tool' }, |
| 361 | ]; |
| 362 | |
| 363 | const wrapped = provider.wrapTools(tools, mockExecuteToolFn); |
| 364 | |
| 365 | expect(Object.keys(wrapped)).toEqual(['first-tool', 'second-tool', 'third-tool']); |
| 366 | expect(wrapped['first-tool']).toBeDefined(); |
| 367 | expect(wrapped['second-tool']).toBeDefined(); |
| 368 | expect(wrapped['third-tool']).toBeDefined(); |
| 369 | }); |
| 370 | |
| 371 | it('should handle duplicate tool slugs by overwriting', () => { |
| 372 | const tools = [ |
| 373 | { ...mockTool, slug: 'duplicate-tool', name: 'First Tool' }, |
| 374 | { ...mockTool, slug: 'duplicate-tool', name: 'Second Tool' }, |
| 375 | ]; |
| 376 | |
| 377 | const wrapped = provider.wrapTools(tools, mockExecuteToolFn); |
| 378 | |
| 379 | expect(Object.keys(wrapped)).toHaveLength(1); |
| 380 | expect(wrapped['duplicate-tool']).toBeDefined(); |
| 381 | expect(createTool).toHaveBeenCalledTimes(2); |
| 382 | }); |
| 383 | }); |
| 384 | |
| 385 | describe('integration with Mastra', () => { |
| 386 | it('should handle tools without schemas gracefully', () => { |
| 387 | const minimalTool: Tool = { |
| 388 | slug: 'minimal-tool', |
| 389 | name: 'Minimal Tool', |
| 390 | description: 'A minimal tool', |
| 391 | tags: [], |
| 392 | }; |
| 393 | |
| 394 | const wrapped = provider.wrapTool( |
| 395 | minimalTool, |
| 396 | mockExecuteToolFn |
| 397 | ) as unknown as MockedMastraTool; |
| 398 | |
| 399 | expect(createTool).toHaveBeenCalledWith({ |
| 400 | id: 'minimal-tool', |
| 401 | description: 'A minimal tool', |
| 402 | inputSchema: { type: 'mock-zod-schema', originalSchema: {} }, |
| 403 | outputSchema: { type: 'mock-zod-schema', originalSchema: {} }, |
| 404 | execute: expect.any(Function), |
| 405 | }); |
| 406 | |
| 407 | expect(wrapped._isMockedMastraTool).toBe(true); |
| 408 | }); |
| 409 | }); |
| 410 | |
| 411 | describe('error handling', () => { |
| 412 | it('should handle execution failures in wrapped tools', async () => { |
| 413 | const errorExecuteToolFn = vi.fn().mockRejectedValue(new Error('Execution failed')); |
| 414 | |
| 415 | const wrapped = provider.wrapTool( |
| 416 | mockTool, |
| 417 | errorExecuteToolFn |
| 418 | ) as unknown as MockedMastraTool; |
| 419 | const executeFunction = getCreatedToolExecute(); |
| 420 | |
| 421 | await expect(executeFunction({ input: 'test' })).rejects.toThrow('Execution failed'); |
| 422 | }); |
| 423 | |
| 424 | it('should handle tools with malformed schemas', () => { |
| 425 | const toolWithMalformedSchema: Tool = { |
| 426 | ...mockTool, |
| 427 | inputParameters: null as unknown, |
| 428 | outputParameters: undefined, |
| 429 | }; |
| 430 | |
| 431 | expect(() => { |
| 432 | provider.wrapTool(toolWithMalformedSchema, mockExecuteToolFn); |
| 433 | }).not.toThrow(); |
| 434 | }); |
| 435 | }); |
| 436 | |
| 437 | describe('strict mode', () => { |
| 438 | it('should keep optional properties as required-nullable when strict mode is enabled', () => { |
| 439 | const strictProvider = new MastraProvider({ strict: true }); |
| 440 | |
| 441 | const toolWithOptionalProps: Tool = { |
| 442 | ...mockTool, |
| 443 | inputParameters: { |
| 444 | type: 'object', |
| 445 | properties: { |
| 446 | required_field: { |
| 447 | type: 'string', |
| 448 | description: 'Required field', |
| 449 | }, |
| 450 | optional_field: { |
| 451 | type: 'string', |
| 452 | description: 'Optional field', |
| 453 | }, |
| 454 | }, |
| 455 | required: ['required_field'], |
| 456 | }, |
| 457 | }; |
| 458 | |
| 459 | strictProvider.wrapTool(toolWithOptionalProps, mockExecuteToolFn); |
| 460 | |
| 461 | // In strict mode every property is required and closed; optional ones |
| 462 | // stay available and accept null instead of being dropped. |
| 463 | expect(createTool).toHaveBeenCalledWith({ |
| 464 | id: toolWithOptionalProps.slug, |
| 465 | description: toolWithOptionalProps.description, |
| 466 | inputSchema: { |
| 467 | type: 'mock-zod-schema', |
| 468 | originalSchema: { |
| 469 | type: 'object', |
| 470 | properties: { |
| 471 | required_field: { |
| 472 | type: 'string', |
| 473 | description: 'Required field', |
| 474 | }, |
| 475 | optional_field: { |
| 476 | type: ['string', 'null'], |
| 477 | description: 'Optional field', |
| 478 | }, |
| 479 | }, |
| 480 | required: ['required_field', 'optional_field'], |
| 481 | additionalProperties: false, |
| 482 | }, |
| 483 | }, |
| 484 | outputSchema: RELAXED_MOCK_OUTPUT_SCHEMA, |
| 485 | execute: expect.any(Function), |
| 486 | }); |
| 487 | }); |
| 488 | |
| 489 | it('keeps the original schema for tools strict mode cannot express', () => { |
| 490 | const strictProvider = new MastraProvider({ strict: true }); |
| 491 | const toolWithMap: Tool = { |
| 492 | ...mockTool, |
| 493 | inputParameters: { |
| 494 | type: 'object', |
| 495 | properties: { |
| 496 | headers: { type: 'object', additionalProperties: { type: 'string' } }, |
| 497 | name: { type: 'string' }, |
| 498 | }, |
| 499 | required: ['headers'], |
| 500 | }, |
| 501 | }; |
| 502 | |
| 503 | strictProvider.wrapTool(toolWithMap, mockExecuteToolFn); |
| 504 | |
| 505 | expect(createTool).toHaveBeenCalledWith( |
| 506 | expect.objectContaining({ |
| 507 | inputSchema: { type: 'mock-zod-schema', originalSchema: toolWithMap.inputParameters }, |
| 508 | }) |
| 509 | ); |
| 510 | }); |
| 511 | |
| 512 | it('omits null arguments the tool schema rejects before executing under strict mode', async () => { |
| 513 | const strictProvider = new MastraProvider({ strict: true }); |
| 514 | const wrapped = strictProvider.wrapTool( |
| 515 | { |
| 516 | ...mockTool, |
| 517 | inputParameters: { |
| 518 | type: 'object', |
| 519 | properties: { |
| 520 | cfg: { |
| 521 | type: 'object', |
| 522 | properties: { url: { type: 'string' }, note: { type: 'string' } }, |
| 523 | required: ['url'], |
| 524 | }, |
| 525 | clearable: { type: ['string', 'null'] }, |
| 526 | }, |
| 527 | required: ['cfg'], |
| 528 | }, |
| 529 | }, |
| 530 | mockExecuteToolFn |
| 531 | ) as unknown as { execute: (input: unknown, context: unknown) => Promise<unknown> }; |
| 532 | |
| 533 | await wrapped.execute({ cfg: { url: 'u', note: null }, clearable: null }, {}); |
| 534 | |
| 535 | expect(mockExecuteToolFn).toHaveBeenCalledWith(mockTool.slug, { |
| 536 | cfg: { url: 'u' }, |
| 537 | clearable: null, |
| 538 | }); |
| 539 | }); |
| 540 | |
| 541 | it('should use all properties when strict mode is disabled', () => { |
| 542 | const nonStrictProvider = new MastraProvider({ strict: false }); |
| 543 | |
| 544 | const toolWithOptionalProps: Tool = { |
| 545 | ...mockTool, |
| 546 | inputParameters: { |
| 547 | type: 'object', |
| 548 | properties: { |
| 549 | required_field: { |
| 550 | type: 'string', |
| 551 | description: 'Required field', |
| 552 | }, |
| 553 | optional_field: { |
| 554 | type: 'string', |
| 555 | description: 'Optional field', |
| 556 | }, |
| 557 | }, |
| 558 | required: ['required_field'], |
| 559 | }, |
| 560 | }; |
| 561 | |
| 562 | nonStrictProvider.wrapTool(toolWithOptionalProps, mockExecuteToolFn); |
| 563 | |
| 564 | // In non-strict mode, all properties should be passed to jsonSchemaToZodSchema |
| 565 | expect(createTool).toHaveBeenCalledWith({ |
| 566 | id: toolWithOptionalProps.slug, |
| 567 | description: toolWithOptionalProps.description, |
| 568 | inputSchema: { |
| 569 | type: 'mock-zod-schema', |
| 570 | originalSchema: { |
| 571 | type: 'object', |
| 572 | properties: { |
| 573 | required_field: { |
| 574 | type: 'string', |
| 575 | description: 'Required field', |
| 576 | }, |
| 577 | optional_field: { |
| 578 | type: 'string', |
| 579 | description: 'Optional field', |
| 580 | }, |
| 581 | }, |
| 582 | required: ['required_field'], |
| 583 | }, |
| 584 | }, |
| 585 | outputSchema: RELAXED_MOCK_OUTPUT_SCHEMA, |
| 586 | execute: expect.any(Function), |
| 587 | }); |
| 588 | }); |
| 589 | |
| 590 | it('should handle non-object input parameters in strict mode', () => { |
| 591 | const strictProvider = new MastraProvider({ strict: true }); |
| 592 | |
| 593 | const toolWithNonObjectParams: Tool = { |
| 594 | ...mockTool, |
| 595 | inputParameters: { |
| 596 | type: 'string', |
| 597 | description: 'A string parameter', |
| 598 | } as unknown, |
| 599 | }; |
| 600 | |
| 601 | strictProvider.wrapTool(toolWithNonObjectParams, mockExecuteToolFn); |
| 602 | |
| 603 | // Non-object parameters should be passed as-is, even in strict mode |
| 604 | expect(createTool).toHaveBeenCalledWith({ |
| 605 | id: toolWithNonObjectParams.slug, |
| 606 | description: toolWithNonObjectParams.description, |
| 607 | inputSchema: { |
| 608 | type: 'mock-zod-schema', |
| 609 | originalSchema: { |
| 610 | type: 'string', |
| 611 | description: 'A string parameter', |
| 612 | }, |
| 613 | }, |
| 614 | outputSchema: RELAXED_MOCK_OUTPUT_SCHEMA, |
| 615 | execute: expect.any(Function), |
| 616 | }); |
| 617 | }); |
| 618 | |
| 619 | it('should handle undefined input parameters in strict mode', () => { |
| 620 | const strictProvider = new MastraProvider({ strict: true }); |
| 621 | |
| 622 | const toolWithoutInputParams: Tool = { |
| 623 | ...mockTool, |
| 624 | inputParameters: undefined, |
| 625 | }; |
| 626 | |
| 627 | strictProvider.wrapTool(toolWithoutInputParams, mockExecuteToolFn); |
| 628 | |
| 629 | // Undefined parameters should result in empty object being passed to jsonSchemaToZodSchema |
| 630 | expect(createTool).toHaveBeenCalledWith({ |
| 631 | id: toolWithoutInputParams.slug, |
| 632 | description: toolWithoutInputParams.description, |
| 633 | inputSchema: { type: 'mock-zod-schema', originalSchema: {} }, |
| 634 | outputSchema: RELAXED_MOCK_OUTPUT_SCHEMA, |
| 635 | execute: expect.any(Function), |
| 636 | }); |
| 637 | }); |
| 638 | |
| 639 | it('should work correctly with wrapTools in strict mode', () => { |
| 640 | const strictProvider = new MastraProvider({ strict: true }); |
| 641 | |
| 642 | const toolsWithOptionalProps: Tool[] = [ |
| 643 | { |
| 644 | ...mockTool, |
| 645 | slug: 'tool1', |
| 646 | inputParameters: { |
| 647 | type: 'object', |
| 648 | properties: { |
| 649 | required_field: { type: 'string' }, |
| 650 | optional_field: { type: 'string' }, |
| 651 | }, |
| 652 | required: ['required_field'], |
| 653 | }, |
| 654 | }, |
| 655 | { |
| 656 | ...mockTool, |
| 657 | slug: 'tool2', |
| 658 | inputParameters: { |
| 659 | type: 'object', |
| 660 | properties: { |
| 661 | another_required: { type: 'number' }, |
| 662 | another_optional: { type: 'boolean' }, |
| 663 | }, |
| 664 | required: ['another_required'], |
| 665 | }, |
| 666 | }, |
| 667 | ]; |
| 668 | |
| 669 | const wrapped = strictProvider.wrapTools(toolsWithOptionalProps, mockExecuteToolFn); |
| 670 | |
| 671 | expect(Object.keys(wrapped)).toEqual(['tool1', 'tool2']); |
| 672 | expect(createTool).toHaveBeenCalledTimes(2); |
| 673 | |
| 674 | // Both tools should have their schemas filtered for required properties only |
| 675 | expect(createTool).toHaveBeenNthCalledWith( |
| 676 | 1, |
| 677 | expect.objectContaining({ |
| 678 | id: 'tool1', |
| 679 | inputSchema: { |
| 680 | type: 'mock-zod-schema', |
| 681 | originalSchema: expect.objectContaining({ |
| 682 | properties: expect.objectContaining({ |
| 683 | required_field: expect.any(Object), |
| 684 | }), |
| 685 | additionalProperties: false, |
| 686 | }), |
| 687 | }, |
| 688 | }) |
| 689 | ); |
| 690 | |
| 691 | expect(createTool).toHaveBeenNthCalledWith( |
| 692 | 2, |
| 693 | expect.objectContaining({ |
| 694 | id: 'tool2', |
| 695 | inputSchema: { |
| 696 | type: 'mock-zod-schema', |
| 697 | originalSchema: expect.objectContaining({ |
| 698 | properties: expect.objectContaining({ |
| 699 | another_required: expect.any(Object), |
| 700 | }), |
| 701 | additionalProperties: false, |
| 702 | }), |
| 703 | }, |
| 704 | }) |
| 705 | ); |
| 706 | }); |
| 707 | }); |
| 708 | |
| 709 | describe('MCP functionality', () => { |
| 710 | describe('wrapMcpServerResponse', () => { |
| 711 | it('should transform McpUrlResponse to MastraUrlMap format', () => { |
| 712 | const mcpResponse = [ |
| 713 | { name: 'server-1', url: 'https://mcp1.example.com' }, |
| 714 | { name: 'server-2', url: 'https://mcp2.example.com' }, |
| 715 | { name: 'server-3', url: 'https://mcp3.example.com' }, |
| 716 | ]; |
| 717 | |
| 718 | const result = provider.wrapMcpServerResponse(mcpResponse); |
| 719 | |
| 720 | expect(result).toEqual({ |
| 721 | 'server-1': { url: 'https://mcp1.example.com' }, |
| 722 | 'server-2': { url: 'https://mcp2.example.com' }, |
| 723 | 'server-3': { url: 'https://mcp3.example.com' }, |
| 724 | }); |
| 725 | }); |
| 726 | |
| 727 | it('should handle empty array', () => { |
| 728 | const mcpResponse: Array<{ name: string; url: string }> = []; |
| 729 | |
| 730 | const result = provider.wrapMcpServerResponse(mcpResponse); |
| 731 | |
| 732 | expect(result).toEqual({}); |
| 733 | }); |
| 734 | |
| 735 | it('should handle duplicate names by overwriting', () => { |
| 736 | const mcpResponse = [ |
| 737 | { name: 'duplicate', url: 'https://first.example.com' }, |
| 738 | { name: 'duplicate', url: 'https://second.example.com' }, |
| 739 | { name: 'unique', url: 'https://unique.example.com' }, |
| 740 | ]; |
| 741 | |
| 742 | const result = provider.wrapMcpServerResponse(mcpResponse); |
| 743 | |
| 744 | // The second duplicate should overwrite the first |
| 745 | expect(result).toEqual({ |
| 746 | duplicate: { url: 'https://second.example.com' }, |
| 747 | unique: { url: 'https://unique.example.com' }, |
| 748 | }); |
| 749 | }); |
| 750 | |
| 751 | it('should preserve URL format exactly', () => { |
| 752 | const mcpResponse = [ |
| 753 | { name: 'http-server', url: 'http://insecure.example.com' }, |
| 754 | { name: 'https-server', url: 'https://secure.example.com' }, |
| 755 | { name: 'port-server', url: 'https://example.com:8080/path' }, |
| 756 | { name: 'query-server', url: 'https://example.com?param=value' }, |
| 757 | { name: 'fragment-server', url: 'https://example.com#section' }, |
| 758 | ]; |
| 759 | |
| 760 | const result = provider.wrapMcpServerResponse(mcpResponse); |
| 761 | |
| 762 | expect(result).toEqual({ |
| 763 | 'http-server': { url: 'http://insecure.example.com' }, |
| 764 | 'https-server': { url: 'https://secure.example.com' }, |
| 765 | 'port-server': { url: 'https://example.com:8080/path' }, |
| 766 | 'query-server': { url: 'https://example.com?param=value' }, |
| 767 | 'fragment-server': { url: 'https://example.com#section' }, |
| 768 | }); |
| 769 | }); |
| 770 | }); |
| 771 | }); |
| 772 | }); |