|
| 1 | +/** |
| 2 | + * E2E test for claude-agent-sdk-integration.ts example |
| 3 | + * |
| 4 | + * Tests the setup of StackOne tools with Claude Agent SDK. |
| 5 | + * |
| 6 | + * Note: The Claude Agent SDK spawns a subprocess to run claude-code, which |
| 7 | + * requires the ANTHROPIC_API_KEY environment variable and a running claude-code |
| 8 | + * installation. This test validates the tool setup and MCP server creation, |
| 9 | + * but does not test the actual query execution. |
| 10 | + */ |
| 11 | + |
| 12 | +import { tool, createSdkMcpServer } from '@anthropic-ai/claude-agent-sdk'; |
| 13 | +import { z } from 'zod'; |
| 14 | +import { StackOneToolSet } from '../src'; |
| 15 | + |
| 16 | +describe('claude-agent-sdk-integration example e2e', () => { |
| 17 | + beforeEach(() => { |
| 18 | + vi.stubEnv('STACKONE_API_KEY', 'test-key'); |
| 19 | + }); |
| 20 | + |
| 21 | + afterEach(() => { |
| 22 | + vi.unstubAllEnvs(); |
| 23 | + }); |
| 24 | + |
| 25 | + it('should fetch tools and create Claude Agent SDK tool wrapper', async () => { |
| 26 | + const toolset = new StackOneToolSet({ |
| 27 | + accountId: 'your-bamboohr-account-id', |
| 28 | + baseUrl: 'https://api.stackone.com', |
| 29 | + }); |
| 30 | + |
| 31 | + // Fetch all tools for this account via MCP |
| 32 | + const tools = await toolset.fetchTools(); |
| 33 | + expect(tools.length).toBeGreaterThan(0); |
| 34 | + |
| 35 | + // Get a specific tool |
| 36 | + const employeeTool = tools.getTool('bamboohr_get_employee'); |
| 37 | + expect(employeeTool).toBeDefined(); |
| 38 | + assert(employeeTool !== undefined); |
| 39 | + |
| 40 | + // Create Claude Agent SDK tool from StackOne tool |
| 41 | + const getEmployeeTool = tool( |
| 42 | + employeeTool.name, |
| 43 | + employeeTool.description, |
| 44 | + { |
| 45 | + id: z.string().describe('The employee ID'), |
| 46 | + }, |
| 47 | + async (args) => { |
| 48 | + const result = await employeeTool.execute(args); |
| 49 | + return { |
| 50 | + content: [{ type: 'text' as const, text: JSON.stringify(result) }], |
| 51 | + }; |
| 52 | + }, |
| 53 | + ); |
| 54 | + |
| 55 | + expect(getEmployeeTool.name).toBe('bamboohr_get_employee'); |
| 56 | + expect(getEmployeeTool.description).toContain('employee'); |
| 57 | + expect(getEmployeeTool.inputSchema).toHaveProperty('id'); |
| 58 | + expect(typeof getEmployeeTool.handler).toBe('function'); |
| 59 | + }); |
| 60 | + |
| 61 | + it('should create MCP server with StackOne tools', async () => { |
| 62 | + const toolset = new StackOneToolSet({ |
| 63 | + accountId: 'your-bamboohr-account-id', |
| 64 | + baseUrl: 'https://api.stackone.com', |
| 65 | + }); |
| 66 | + |
| 67 | + const tools = await toolset.fetchTools(); |
| 68 | + const employeeTool = tools.getTool('bamboohr_get_employee'); |
| 69 | + assert(employeeTool !== undefined); |
| 70 | + |
| 71 | + // Create Claude Agent SDK tool |
| 72 | + const getEmployeeTool = tool( |
| 73 | + employeeTool.name, |
| 74 | + employeeTool.description, |
| 75 | + { |
| 76 | + id: z.string().describe('The employee ID'), |
| 77 | + }, |
| 78 | + async (args) => { |
| 79 | + const result = await employeeTool.execute(args); |
| 80 | + return { |
| 81 | + content: [{ type: 'text' as const, text: JSON.stringify(result) }], |
| 82 | + }; |
| 83 | + }, |
| 84 | + ); |
| 85 | + |
| 86 | + // Create an MCP server with the StackOne tool |
| 87 | + const mcpServer = createSdkMcpServer({ |
| 88 | + name: 'stackone-tools', |
| 89 | + version: '1.0.0', |
| 90 | + tools: [getEmployeeTool], |
| 91 | + }); |
| 92 | + |
| 93 | + // Verify MCP server was created |
| 94 | + expect(mcpServer).toBeDefined(); |
| 95 | + expect(mcpServer.name).toBe('stackone-tools'); |
| 96 | + expect(mcpServer.instance).toBeDefined(); |
| 97 | + }); |
| 98 | + |
| 99 | + it('should execute tool handler directly', async () => { |
| 100 | + const toolset = new StackOneToolSet({ |
| 101 | + accountId: 'your-bamboohr-account-id', |
| 102 | + baseUrl: 'https://api.stackone.com', |
| 103 | + }); |
| 104 | + |
| 105 | + const tools = await toolset.fetchTools(); |
| 106 | + const employeeTool = tools.getTool('bamboohr_get_employee'); |
| 107 | + assert(employeeTool !== undefined); |
| 108 | + |
| 109 | + // Create Claude Agent SDK tool |
| 110 | + const getEmployeeTool = tool( |
| 111 | + employeeTool.name, |
| 112 | + employeeTool.description, |
| 113 | + { |
| 114 | + id: z.string().describe('The employee ID'), |
| 115 | + }, |
| 116 | + async (args) => { |
| 117 | + const result = await employeeTool.execute(args); |
| 118 | + return { |
| 119 | + content: [{ type: 'text' as const, text: JSON.stringify(result) }], |
| 120 | + }; |
| 121 | + }, |
| 122 | + ); |
| 123 | + |
| 124 | + // Execute the tool handler directly |
| 125 | + const result = await getEmployeeTool.handler( |
| 126 | + { id: 'c28xIQaWQ6MzM5MzczMDA2NzMzMzkwNzIwNA' }, |
| 127 | + {} as unknown, |
| 128 | + ); |
| 129 | + |
| 130 | + expect(result).toBeDefined(); |
| 131 | + expect(result.content).toHaveLength(1); |
| 132 | + expect(result.content[0]?.type).toBe('text'); |
| 133 | + |
| 134 | + // Parse the result text and verify it contains employee data |
| 135 | + const textContent = result.content[0]; |
| 136 | + assert(textContent?.type === 'text'); |
| 137 | + const data = JSON.parse(textContent.text) as unknown; |
| 138 | + expect(data).toHaveProperty('data'); |
| 139 | + }); |
| 140 | +}); |
0 commit comments