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 { AnthropicProvider, AnthropicToolUseBlock } from '../src'; |
| 3 | import type { AnthropicTool } from '../src/types'; |
| 4 | import type { Tool } from '@composio/core'; |
| 5 | import Anthropic from '@anthropic-ai/sdk'; |
| 6 | |
| 7 | vi.mock('@anthropic-ai/sdk', () => { |
| 8 | return { |
| 9 | default: vi.fn().mockImplementation(() => ({ |
| 10 | messages: { |
| 11 | create: vi.fn().mockResolvedValue({ |
| 12 | id: 'msg_123', |
| 13 | content: [ |
| 14 | { type: 'text', text: 'Hello' }, |
| 15 | { |
| 16 | type: 'tool_use', |
| 17 | id: 'tu_123', |
| 18 | name: 'test-tool', |
| 19 | input: { input: 'test-value' }, |
| 20 | }, |
| 21 | ], |
| 22 | }), |
| 23 | }, |
| 24 | })), |
| 25 | }; |
| 26 | }); |
| 27 | |
| 28 | describe('AnthropicProvider', () => { |
| 29 | let provider: AnthropicProvider; |
| 30 | let mockTool: Tool; |
| 31 | let mockExecuteToolFn: unknown; |
| 32 | |
| 33 | beforeEach(() => { |
| 34 | provider = new AnthropicProvider(); |
| 35 | |
| 36 | mockExecuteToolFn = vi.fn().mockResolvedValue({ |
| 37 | data: { result: 'success' }, |
| 38 | error: null, |
| 39 | successful: true, |
| 40 | }); |
| 41 | provider._setExecuteToolFn(mockExecuteToolFn); |
| 42 | |
| 43 | mockTool = { |
| 44 | slug: 'test-tool', |
| 45 | name: 'Test Tool', |
| 46 | description: 'A tool for testing', |
| 47 | inputParameters: { |
| 48 | type: 'object', |
| 49 | properties: { |
| 50 | input: { |
| 51 | type: 'string', |
| 52 | description: 'Test input', |
| 53 | }, |
| 54 | }, |
| 55 | required: ['input'], |
| 56 | }, |
| 57 | tags: [], |
| 58 | }; |
| 59 | |
| 60 | vi.clearAllMocks(); |
| 61 | }); |
| 62 | |
| 63 | describe('name property', () => { |
| 64 | it('should have the correct name', () => { |
| 65 | expect(provider.name).toBe('anthropic'); |
| 66 | }); |
| 67 | }); |
| 68 | |
| 69 | describe('_isAgentic property', () => { |
| 70 | it('should be non-agentic', () => { |
| 71 | expect(provider._isAgentic).toBe(false); |
| 72 | }); |
| 73 | }); |
| 74 | |
| 75 | describe('wrapTool', () => { |
| 76 | it('should wrap a tool in Anthropic format', () => { |
| 77 | const wrapped = provider.wrapTool(mockTool) as AnthropicTool; |
| 78 | |
| 79 | expect(wrapped).toEqual({ |
| 80 | name: mockTool.slug, |
| 81 | description: mockTool.description, |
| 82 | input_schema: { |
| 83 | type: 'object', |
| 84 | properties: mockTool.inputParameters?.properties, |
| 85 | required: mockTool.inputParameters?.required, |
| 86 | }, |
| 87 | }); |
| 88 | }); |
| 89 | |
| 90 | it('should handle tools without input parameters', () => { |
| 91 | const toolWithoutParams: Tool = { |
| 92 | ...mockTool, |
| 93 | inputParameters: undefined, |
| 94 | }; |
| 95 | |
| 96 | const wrapped = provider.wrapTool(toolWithoutParams) as AnthropicTool; |
| 97 | |
| 98 | expect(wrapped).toEqual({ |
| 99 | name: toolWithoutParams.slug, |
| 100 | description: toolWithoutParams.description, |
| 101 | input_schema: { |
| 102 | type: 'object', |
| 103 | properties: {} as Record<string, unknown>, |
| 104 | required: [] as string[], |
| 105 | }, |
| 106 | }); |
| 107 | }); |
| 108 | |
| 109 | it('deduplicates required entries at every object-schema level', () => { |
| 110 | const toolWithDuplicateRequired: Tool = { |
| 111 | ...mockTool, |
| 112 | inputParameters: { |
| 113 | type: 'object', |
| 114 | properties: { |
| 115 | owner: { type: 'string' }, |
| 116 | options: { |
| 117 | type: 'object', |
| 118 | properties: { |
| 119 | name: { type: 'string' }, |
| 120 | }, |
| 121 | required: ['name', 'name'], |
| 122 | }, |
| 123 | }, |
| 124 | required: ['owner', 'options', 'owner'], |
| 125 | }, |
| 126 | }; |
| 127 | |
| 128 | const wrapped = provider.wrapTool(toolWithDuplicateRequired) as AnthropicTool; |
| 129 | const properties = wrapped.input_schema.properties as Record< |
| 130 | string, |
| 131 | { |
| 132 | required?: string[]; |
| 133 | } |
| 134 | >; |
| 135 | |
| 136 | expect(wrapped.input_schema.required).toEqual(['owner', 'options']); |
| 137 | expect(properties.options?.required).toEqual(['name']); |
| 138 | }); |
| 139 | }); |
| 140 | |
| 141 | describe('wrapTools', () => { |
| 142 | it('should wrap multiple tools', () => { |
| 143 | const anotherTool: Tool = { |
| 144 | ...mockTool, |
| 145 | slug: 'another-tool', |
| 146 | name: 'Another Tool', |
| 147 | }; |
| 148 | const tools = [mockTool, anotherTool]; |
| 149 | |
| 150 | const wrapped = provider.wrapTools(tools); |
| 151 | |
| 152 | expect(wrapped).toHaveLength(2); |
| 153 | |
| 154 | expect(wrapped[0]).toEqual({ |
| 155 | name: mockTool.slug, |
| 156 | description: mockTool.description, |
| 157 | input_schema: { |
| 158 | type: 'object', |
| 159 | properties: mockTool.inputParameters?.properties, |
| 160 | required: mockTool.inputParameters?.required, |
| 161 | }, |
| 162 | }); |
| 163 | |
| 164 | expect(wrapped[1]).toEqual({ |
| 165 | name: anotherTool.slug, |
| 166 | description: anotherTool.description, |
| 167 | input_schema: { |
| 168 | type: 'object', |
| 169 | properties: anotherTool.inputParameters?.properties, |
| 170 | required: anotherTool.inputParameters?.required, |
| 171 | }, |
| 172 | }); |
| 173 | }); |
| 174 | |
| 175 | it('should return an empty array for empty tools array', () => { |
| 176 | const wrapped = provider.wrapTools([]); |
| 177 | expect(wrapped).toEqual([]); |
| 178 | }); |
| 179 | |
| 180 | it('should place a single cache_control breakpoint on the last tool when caching is enabled', () => { |
| 181 | const cachingProvider = new AnthropicProvider({ cacheTools: true }); |
| 182 | const tools: Tool[] = [ |
| 183 | { ...mockTool, slug: 'tool-a' }, |
| 184 | { ...mockTool, slug: 'tool-b' }, |
| 185 | { ...mockTool, slug: 'tool-c' }, |
| 186 | ]; |
| 187 | |
| 188 | const wrapped = cachingProvider.wrapTools(tools); |
| 189 | |
| 190 | expect(wrapped[0]).not.toHaveProperty('cache_control'); |
| 191 | expect(wrapped[1]).not.toHaveProperty('cache_control'); |
| 192 | expect(wrapped[2].cache_control).toEqual({ type: 'ephemeral' }); |
| 193 | }); |
| 194 | |
| 195 | it('should still cache a single-tool batch', () => { |
| 196 | const cachingProvider = new AnthropicProvider({ cacheTools: true }); |
| 197 | |
| 198 | const wrapped = cachingProvider.wrapTools([mockTool]); |
| 199 | |
| 200 | expect(wrapped[0].cache_control).toEqual({ type: 'ephemeral' }); |
| 201 | }); |
| 202 | |
| 203 | it('should omit cache_control from every tool when caching is disabled', () => { |
| 204 | const tools: Tool[] = [ |
| 205 | { ...mockTool, slug: 'tool-a' }, |
| 206 | { ...mockTool, slug: 'tool-b' }, |
| 207 | ]; |
| 208 | |
| 209 | const wrapped = provider.wrapTools(tools); |
| 210 | |
| 211 | for (const tool of wrapped) { |
| 212 | expect(tool).not.toHaveProperty('cache_control'); |
| 213 | } |
| 214 | }); |
| 215 | }); |
| 216 | |
| 217 | describe('executeToolCall', () => { |
| 218 | it('should execute a tool call and return the result as string', async () => { |
| 219 | const userId = 'test-user'; |
| 220 | const toolUse: AnthropicToolUseBlock = { |
| 221 | type: 'tool_use', |
| 222 | id: 'tu_123', |
| 223 | name: 'test-tool', |
| 224 | input: { input: 'test-value' }, |
| 225 | }; |
| 226 | |
| 227 | const result = await provider.executeToolCall(userId, toolUse); |
| 228 | |
| 229 | expect(mockExecuteToolFn).toHaveBeenCalledWith( |
| 230 | 'test-tool', |
| 231 | { |
| 232 | arguments: { input: 'test-value' }, |
| 233 | userId: 'test-user', |
| 234 | connectedAccountId: undefined, |
| 235 | customAuthParams: undefined, |
| 236 | }, |
| 237 | undefined |
| 238 | ); |
| 239 | expect(result).toBe(JSON.stringify({ result: 'success' })); |
| 240 | }); |
| 241 | |
| 242 | it('should expose session execution errors without changing successful results', async () => { |
| 243 | const toolUse: AnthropicToolUseBlock = { |
| 244 | type: 'tool_use', |
| 245 | id: 'tu_123', |
| 246 | name: 'test-tool', |
| 247 | input: { input: 'test-value' }, |
| 248 | }; |
| 249 | const session = { |
| 250 | execute: vi |
| 251 | .fn() |
| 252 | .mockResolvedValueOnce({ |
| 253 | data: { result: 'success' }, |
| 254 | error: null, |
| 255 | logId: 'log-success', |
| 256 | resultType: 'completed', |
| 257 | }) |
| 258 | .mockResolvedValueOnce({ |
| 259 | data: {}, |
| 260 | error: 'Tool execution failed', |
| 261 | logId: 'log-failure', |
| 262 | resultType: 'failed', |
| 263 | }), |
| 264 | }; |
| 265 | |
| 266 | const successfulResult = await provider.executeToolCall(session, toolUse); |
| 267 | const failedResult = await provider.executeToolCall(session, toolUse); |
| 268 | |
| 269 | expect(successfulResult).toBe(JSON.stringify({ result: 'success' })); |
| 270 | expect(failedResult).toBe(JSON.stringify({ error: 'Tool execution failed' })); |
| 271 | }); |
| 272 | |
| 273 | it('should normalize a stringified-JSON input to an object before executing (issue #2406)', async () => { |
| 274 | const params = { input: 'test-value' }; |
| 275 | const toolUse: AnthropicToolUseBlock = { |
| 276 | type: 'tool_use', |
| 277 | id: 'tu_123', |
| 278 | name: 'test-tool', |
| 279 | input: JSON.stringify(params) as unknown as Record<string, unknown>, |
| 280 | }; |
| 281 | |
| 282 | await provider.executeToolCall('test-user', toolUse); |
| 283 | |
| 284 | expect(mockExecuteToolFn).toHaveBeenCalledWith( |
| 285 | 'test-tool', |
| 286 | expect.objectContaining({ arguments: params }), |
| 287 | undefined |
| 288 | ); |
| 289 | }); |
| 290 | |
| 291 | it('should throw a typed error for a malformed-JSON string input (issue #2406)', async () => { |
| 292 | const toolUse: AnthropicToolUseBlock = { |
| 293 | type: 'tool_use', |
| 294 | id: 'tu_123', |
| 295 | name: 'test-tool', |
| 296 | input: '{"input":' as unknown as Record<string, unknown>, |
| 297 | }; |
| 298 | |
| 299 | await expect(provider.executeToolCall('test-user', toolUse)).rejects.toThrow( |
| 300 | /not valid JSON/ |
| 301 | ); |
| 302 | }); |
| 303 | |
| 304 | it('should pass options to executeTool', async () => { |
| 305 | const userId = 'test-user'; |
| 306 | const toolUse: AnthropicToolUseBlock = { |
| 307 | type: 'tool_use', |
| 308 | id: 'tu_123', |
| 309 | name: 'test-tool', |
| 310 | input: { input: 'test-value' }, |
| 311 | }; |
| 312 | |
| 313 | const options = { |
| 314 | connectedAccountId: 'conn-123', |
| 315 | customAuthParams: { |
| 316 | parameters: [{ name: 'token', value: 'abc123', in: 'header' as const }], |
| 317 | }, |
| 318 | }; |
| 319 | |
| 320 | const modifiers = { |
| 321 | beforeExecute: vi.fn(({ params }) => params), |
| 322 | afterExecute: vi.fn(({ result }) => result), |
| 323 | }; |
| 324 | |
| 325 | await provider.executeToolCall(userId, toolUse, options, modifiers); |
| 326 | |
| 327 | expect(mockExecuteToolFn).toHaveBeenCalledWith( |
| 328 | 'test-tool', |
| 329 | { |
| 330 | arguments: { input: 'test-value' }, |
| 331 | userId: 'test-user', |
| 332 | connectedAccountId: 'conn-123', |
| 333 | customAuthParams: options.customAuthParams, |
| 334 | }, |
| 335 | modifiers |
| 336 | ); |
| 337 | }); |
| 338 | }); |
| 339 | |
| 340 | describe('handleToolCalls', () => { |
| 341 | it('should handle tool calls from Anthropic message', async () => { |
| 342 | const userId = 'test-user'; |
| 343 | const message = { |
| 344 | id: 'msg_123', |
| 345 | content: [ |
| 346 | { type: 'text', text: 'Hello' }, |
| 347 | { |
| 348 | type: 'tool_use', |
| 349 | id: 'tu_123', |
| 350 | name: 'test-tool', |
| 351 | input: { input: 'test-value' }, |
| 352 | }, |
| 353 | ], |
| 354 | } as Anthropic.Message; |
| 355 | |
| 356 | const executeToolCallSpy = vi.spyOn(provider, 'executeToolCall'); |
| 357 | executeToolCallSpy.mockResolvedValue(JSON.stringify({ result: 'success' })); |
| 358 | |
| 359 | const results = await provider.handleToolCalls(userId, message); |
| 360 | |
| 361 | expect(executeToolCallSpy).toHaveBeenCalledWith( |
| 362 | userId, |
| 363 | expect.objectContaining({ |
| 364 | type: 'tool_use', |
| 365 | id: 'tu_123', |
| 366 | name: 'test-tool', |
| 367 | input: { input: 'test-value' }, |
| 368 | }), |
| 369 | undefined, |
| 370 | undefined |
| 371 | ); |
| 372 | expect(results).toEqual([ |
| 373 | { |
| 374 | role: 'user', |
| 375 | content: [ |
| 376 | { |
| 377 | type: 'tool_result', |
| 378 | tool_use_id: 'tu_123', |
| 379 | content: JSON.stringify({ result: 'success' }), |
| 380 | cache_control: undefined, |
| 381 | }, |
| 382 | ], |
| 383 | }, |
| 384 | ]); |
| 385 | }); |
| 386 | |
| 387 | it('should handle multiple tool calls', async () => { |
| 388 | const userId = 'test-user'; |
| 389 | const message = { |
| 390 | id: 'msg_123', |
| 391 | content: [ |
| 392 | { type: 'text', text: 'Hello' }, |
| 393 | { |
| 394 | type: 'tool_use', |
| 395 | id: 'tu_123', |
| 396 | name: 'test-tool-1', |
| 397 | input: { input: 'test-value-1' }, |
| 398 | }, |
| 399 | { |
| 400 | type: 'tool_use', |
| 401 | id: 'tu_456', |
| 402 | name: 'test-tool-2', |
| 403 | input: { input: 'test-value-2' }, |
| 404 | }, |
| 405 | ], |
| 406 | } as Anthropic.Message; |
| 407 | |
| 408 | const executeToolCallSpy = vi.spyOn(provider, 'executeToolCall'); |
| 409 | executeToolCallSpy |
| 410 | .mockResolvedValueOnce(JSON.stringify({ result: 'success-1' })) |
| 411 | .mockResolvedValueOnce(JSON.stringify({ result: 'success-2' })); |
| 412 | |
| 413 | const results = await provider.handleToolCalls(userId, message); |
| 414 | |
| 415 | expect(executeToolCallSpy).toHaveBeenCalledTimes(2); |
| 416 | expect(executeToolCallSpy).toHaveBeenNthCalledWith( |
| 417 | 1, |
| 418 | userId, |
| 419 | expect.objectContaining({ |
| 420 | type: 'tool_use', |
| 421 | id: 'tu_123', |
| 422 | name: 'test-tool-1', |
| 423 | input: { input: 'test-value-1' }, |
| 424 | }), |
| 425 | undefined, |
| 426 | undefined |
| 427 | ); |
| 428 | expect(executeToolCallSpy).toHaveBeenNthCalledWith( |
| 429 | 2, |
| 430 | userId, |
| 431 | expect.objectContaining({ |
| 432 | type: 'tool_use', |
| 433 | id: 'tu_456', |
| 434 | name: 'test-tool-2', |
| 435 | input: { input: 'test-value-2' }, |
| 436 | }), |
| 437 | undefined, |
| 438 | undefined |
| 439 | ); |
| 440 | expect(results).toEqual([ |
| 441 | { |
| 442 | role: 'user', |
| 443 | content: [ |
| 444 | { |
| 445 | type: 'tool_result', |
| 446 | tool_use_id: 'tu_123', |
| 447 | content: JSON.stringify({ result: 'success-1' }), |
| 448 | cache_control: undefined, |
| 449 | }, |
| 450 | { |
| 451 | type: 'tool_result', |
| 452 | tool_use_id: 'tu_456', |
| 453 | content: JSON.stringify({ result: 'success-2' }), |
| 454 | cache_control: undefined, |
| 455 | }, |
| 456 | ], |
| 457 | }, |
| 458 | ]); |
| 459 | }); |
| 460 | |
| 461 | it('should not place cache_control on any tool_result when caching is enabled', async () => { |
| 462 | const cachingProvider = new AnthropicProvider({ cacheTools: true }); |
| 463 | cachingProvider._setExecuteToolFn(mockExecuteToolFn); |
| 464 | const userId = 'test-user'; |
| 465 | const message = { |
| 466 | id: 'msg_123', |
| 467 | content: [ |
| 468 | { type: 'text', text: 'Hello' }, |
| 469 | { type: 'tool_use', id: 'tu_123', name: 'test-tool-1', input: {} }, |
| 470 | { type: 'tool_use', id: 'tu_456', name: 'test-tool-2', input: {} }, |
| 471 | { type: 'tool_use', id: 'tu_789', name: 'test-tool-3', input: {} }, |
| 472 | ], |
| 473 | } as Anthropic.Message; |
| 474 | |
| 475 | const executeToolCallSpy = vi.spyOn(cachingProvider, 'executeToolCall'); |
| 476 | executeToolCallSpy |
| 477 | .mockResolvedValueOnce(JSON.stringify({ result: 'success-1' })) |
| 478 | .mockResolvedValueOnce(JSON.stringify({ result: 'success-2' })) |
| 479 | .mockResolvedValueOnce(JSON.stringify({ result: 'success-3' })); |
| 480 | |
| 481 | const results = await cachingProvider.handleToolCalls(userId, message); |
| 482 | |
| 483 | // Results are appended to the message history every turn, so a breakpoint on them would |
| 484 | // pile up across turns and exceed Anthropic's 4-breakpoint limit. |
| 485 | const outputs = results[0].content as Anthropic.Messages.ToolResultBlockParam[]; |
| 486 | expect(outputs).toHaveLength(3); |
| 487 | for (const output of outputs) { |
| 488 | expect(output).not.toHaveProperty('cache_control'); |
| 489 | } |
| 490 | }); |
| 491 | |
| 492 | it('should handle messages without tool calls', async () => { |
| 493 | const userId = 'test-user'; |
| 494 | const message = { |
| 495 | id: 'msg_123', |
| 496 | content: [{ type: 'text', text: 'Hello' }], |
| 497 | } as Anthropic.Message; |
| 498 | |
| 499 | const executeToolCallSpy = vi.spyOn(provider, 'executeToolCall'); |
| 500 | |
| 501 | const results = await provider.handleToolCalls(userId, message); |
| 502 | |
| 503 | expect(executeToolCallSpy).not.toHaveBeenCalled(); |
| 504 | expect(results).toEqual([]); |
| 505 | }); |
| 506 | }); |
| 507 | |
| 508 | describe('MCP functionality', () => { |
| 509 | describe('wrapMcpServerResponse', () => { |
| 510 | it('should transform McpUrlResponse to AnthropicMcpServerGetResponse format', () => { |
| 511 | const mcpResponse = [ |
| 512 | { name: 'server-1', url: 'https://mcp1.example.com' }, |
| 513 | { name: 'server-2', url: 'https://mcp2.example.com' }, |
| 514 | { name: 'server-3', url: 'https://mcp3.example.com' }, |
| 515 | ]; |
| 516 | |
| 517 | const result = provider.wrapMcpServerResponse(mcpResponse); |
| 518 | |
| 519 | expect(Array.isArray(result)).toBe(true); |
| 520 | expect(result).toHaveLength(3); |
| 521 | expect(result[0]).toEqual({ |
| 522 | url: 'https://mcp1.example.com', |
| 523 | name: 'server-1', |
| 524 | type: 'url', |
| 525 | }); |
| 526 | expect(result[1]).toEqual({ |
| 527 | url: 'https://mcp2.example.com', |
| 528 | name: 'server-2', |
| 529 | type: 'url', |
| 530 | }); |
| 531 | expect(result[2]).toEqual({ |
| 532 | url: 'https://mcp3.example.com', |
| 533 | name: 'server-3', |
| 534 | type: 'url', |
| 535 | }); |
| 536 | }); |
| 537 | |
| 538 | it('should handle empty array', () => { |
| 539 | const mcpResponse: Array<{ name: string; url: string }> = []; |
| 540 | |
| 541 | const result = provider.wrapMcpServerResponse(mcpResponse); |
| 542 | |
| 543 | expect(Array.isArray(result)).toBe(true); |
| 544 | expect(result).toHaveLength(0); |
| 545 | }); |
| 546 | |
| 547 | it('should preserve URL strings exactly', () => { |
| 548 | const mcpResponse = [ |
| 549 | { name: 'http-server', url: 'http://insecure.example.com' }, |
| 550 | { name: 'https-server', url: 'https://secure.example.com' }, |
| 551 | { name: 'port-server', url: 'https://example.com:8080/path' }, |
| 552 | { name: 'query-server', url: 'https://example.com?param=value' }, |
| 553 | { name: 'fragment-server', url: 'https://example.com#section' }, |
| 554 | ]; |
| 555 | |
| 556 | const result = provider.wrapMcpServerResponse(mcpResponse); |
| 557 | |
| 558 | expect(result).toHaveLength(5); |
| 559 | |
| 560 | // Verify URLs are preserved as strings exactly |
| 561 | expect(result[0].url).toBe('http://insecure.example.com'); |
| 562 | expect(result[1].url).toBe('https://secure.example.com'); |
| 563 | expect(result[2].url).toBe('https://example.com:8080/path'); |
| 564 | expect(result[3].url).toBe('https://example.com?param=value'); |
| 565 | expect(result[4].url).toBe('https://example.com#section'); |
| 566 | |
| 567 | // All should have type 'url' |
| 568 | result.forEach(item => { |
| 569 | expect(item.type).toBe('url'); |
| 570 | }); |
| 571 | }); |
| 572 | }); |
| 573 | }); |
| 574 | }); |