Composio powers 1000+ toolkits, tool search, context management, authentication, and a sandboxed workbench to help you build AI agents that turn intent into action.

next
ts 171 lines 5.94 KB
Raw
1 /**
2 * Registration boundary tests for the Claude Agent SDK provider.
3 *
4 * The sibling suite mocks `tool()`, which proves what the provider *passes* but not what the SDK
5 * *does* with it. Root object constraints are precisely the part a mock cannot check: they survive
6 * or vanish inside `createSdkMcpServer`, when the registered Zod schema is projected to the JSON
7 * Schema the model sees and used to validate incoming arguments.
8 *
9 * These tests therefore drive the real SDK over an in-memory MCP transport and assert on the
10 * emitted `inputSchema` and on real `tools/call` results.
11 */
12 import { describe, it, expect, vi } from 'vitest';
13 import { Client } from '@modelcontextprotocol/sdk/client/index.js';
14 import { InMemoryTransport } from '@modelcontextprotocol/sdk/inMemory.js';
15 import { createSdkMcpServer } from '@anthropic-ai/claude-agent-sdk';
16 import type { Tool, GlobalExecuteToolFn } from '@composio/core';
17 import { ClaudeAgentSDKProvider } from '../src';
18
19 type ToolCallResult = {
20 isError?: boolean;
21 content?: Array<{ type: string; text?: string }>;
22 };
23
24 const buildTool = (inputParameters: Tool['inputParameters']): Tool =>
25 ({
26 slug: 'TEST_TOOL',
27 name: 'Test Tool',
28 description: 'A tool under test',
29 version: '20250909_00',
30 availableVersions: ['20250909_00'],
31 inputParameters,
32 tags: [],
33 }) as Tool;
34
35 /**
36 * Register one Composio tool through the provider and connect a real MCP client to it.
37 *
38 * Returns the tool's advertised input schema plus a `call` helper, so each test can assert on both
39 * what the model is shown and what actually reaches execution.
40 */
41 const registerTool = async (composioTool: Tool) => {
42 const executeToolFn = vi.fn().mockResolvedValue({
43 data: { result: 'success' },
44 error: null,
45 successful: true,
46 }) as unknown as GlobalExecuteToolFn;
47
48 const provider = new ClaudeAgentSDKProvider();
49 provider._setExecuteToolFn(executeToolFn);
50
51 const server = createSdkMcpServer({
52 name: 'composio-test',
53 version: '1.0.0',
54 tools: [provider.wrapTool(composioTool, executeToolFn)],
55 });
56
57 const [clientTransport, serverTransport] = InMemoryTransport.createLinkedPair();
58 const client = new Client({ name: 'test-client', version: '1.0.0' });
59 await server.instance.server.connect(serverTransport);
60 await client.connect(clientTransport);
61
62 const { tools } = await client.listTools();
63
64 return {
65 executeToolFn,
66 inputSchema: tools[0].inputSchema as Record<string, unknown>,
67 call: (args: Record<string, unknown>) =>
68 client.callTool({ name: composioTool.slug, arguments: args }) as Promise<ToolCallResult>,
69 };
70 };
71
72 describe('ClaudeAgentSDKProvider registration boundary', () => {
73 it('carries a free-form object argument through registration and execution intact', async () => {
74 const { inputSchema, executeToolFn, call } = await registerTool(
75 buildTool({
76 type: 'object',
77 properties: { dataset_query: { type: 'object' } },
78 required: ['dataset_query'],
79 })
80 );
81
82 expect(inputSchema.properties).toMatchObject({ dataset_query: { type: 'object' } });
83
84 const dataset_query = { database: 1, query: { 'source-table': 2, aggregation: [['count']] } };
85 const result = await call({ dataset_query });
86
87 expect(result.isError ?? false).toBe(false);
88 expect(executeToolFn).toHaveBeenCalledWith(
89 'TEST_TOOL',
90 expect.objectContaining({ dataset_query })
91 );
92 });
93
94 it('rejects an unknown key instead of silently dropping it', async () => {
95 // A raw property shape cannot express a root `additionalProperties`, so the SDK stripped
96 // unknown keys and executed anyway. Registering the complete schema makes the root strict.
97 const { inputSchema, executeToolFn, call } = await registerTool(
98 buildTool({
99 type: 'object',
100 properties: { to: { type: 'string' } },
101 required: ['to'],
102 })
103 );
104
105 expect(inputSchema.additionalProperties).toBe(false);
106
107 const result = await call({ to: 'someone@example.com', hallucinated: 'value' });
108
109 expect(result.isError).toBe(true);
110 expect(executeToolFn).not.toHaveBeenCalled();
111 });
112
113 it('preserves root patternProperties so matching keys are accepted and validated', async () => {
114 const { executeToolFn, call } = await registerTool(
115 buildTool({
116 type: 'object',
117 properties: {},
118 patternProperties: { '^meta_': { type: 'string' } },
119 })
120 );
121
122 const accepted = await call({ meta_source: 'crm' });
123 expect(accepted.isError ?? false).toBe(false);
124 expect(executeToolFn).toHaveBeenCalledWith(
125 'TEST_TOOL',
126 expect.objectContaining({ meta_source: 'crm' })
127 );
128
129 executeToolFn.mockClear();
130
131 const rejected = await call({ meta_source: 42 });
132 expect(rejected.isError).toBe(true);
133 expect(executeToolFn).not.toHaveBeenCalled();
134 });
135
136 it('preserves schema-valued additionalProperties through MCP registration', async () => {
137 const { inputSchema, executeToolFn, call } = await registerTool(
138 buildTool({
139 type: 'object',
140 properties: { name: { type: 'string' } },
141 additionalProperties: { type: 'number' },
142 })
143 );
144
145 expect(inputSchema.additionalProperties).toEqual({ type: 'number' });
146
147 const accepted = await call({ name: 'report', count: 2 });
148 expect(accepted.isError ?? false).toBe(false);
149 expect(executeToolFn).toHaveBeenCalledWith(
150 'TEST_TOOL',
151 expect.objectContaining({ name: 'report', count: 2 })
152 );
153
154 executeToolFn.mockClear();
155
156 const rejected = await call({ name: 'report', count: 'two' });
157 expect(rejected.isError).toBe(true);
158 expect(executeToolFn).not.toHaveBeenCalled();
159 });
160
161 it('keeps a tool with no input parameters closed', async () => {
162 const { inputSchema, executeToolFn, call } = await registerTool(buildTool(undefined));
163
164 expect(inputSchema.additionalProperties).toBe(false);
165
166 const result = await call({ unexpected: 'value' });
167
168 expect(result.isError).toBe(true);
169 expect(executeToolFn).not.toHaveBeenCalled();
170 });
171 });