|
| 1 | +import * as path from "path" |
| 2 | + |
| 3 | +import { LLMock } from "@copilotkit/aimock" |
| 4 | + |
| 5 | +const TEST_DIR_NAME = "use-mcp-tool-fixture" |
| 6 | +const FILESYSTEM_SERVER_NAME = "filesystem" |
| 7 | + |
| 8 | +type UseMcpToolFixture = { |
| 9 | + userMessagePattern: string |
| 10 | + toolCallId: string |
| 11 | + toolName: string |
| 12 | + toolArguments?: Record<string, unknown> |
| 13 | + serverName?: string |
| 14 | + result: string |
| 15 | + id: string |
| 16 | +} |
| 17 | + |
| 18 | +export function addUseMcpToolResultFixtures(mock: InstanceType<typeof LLMock>, workspaceDir: string) { |
| 19 | + const readFilePath = path.join(workspaceDir, TEST_DIR_NAME, "mcp-read-target.txt") |
| 20 | + const writeFilePath = path.join(workspaceDir, TEST_DIR_NAME, "mcp-write-target.txt") |
| 21 | + |
| 22 | + const fixtures: UseMcpToolFixture[] = [ |
| 23 | + { |
| 24 | + userMessagePattern: "USE_MCP_TOOL_READ_FILE_SMOKE", |
| 25 | + toolCallId: "call_use_mcp_tool_read_file_001", |
| 26 | + toolName: "read_file", |
| 27 | + toolArguments: { path: readFilePath }, |
| 28 | + result: "Read the requested file through the MCP filesystem server.", |
| 29 | + id: "call_use_mcp_tool_read_file_002", |
| 30 | + }, |
| 31 | + { |
| 32 | + userMessagePattern: "USE_MCP_TOOL_WRITE_FILE_SMOKE", |
| 33 | + toolCallId: "call_use_mcp_tool_write_file_001", |
| 34 | + toolName: "write_file", |
| 35 | + toolArguments: { path: writeFilePath, content: "Hello from MCP!" }, |
| 36 | + result: "Created the requested file through the MCP filesystem server.", |
| 37 | + id: "call_use_mcp_tool_write_file_002", |
| 38 | + }, |
| 39 | + { |
| 40 | + userMessagePattern: "USE_MCP_TOOL_LIST_DIRECTORY_SMOKE", |
| 41 | + toolCallId: "call_use_mcp_tool_list_directory_001", |
| 42 | + toolName: "list_directory", |
| 43 | + toolArguments: { path: path.join(workspaceDir, TEST_DIR_NAME) }, |
| 44 | + result: "Listed the requested directory through the MCP filesystem server.", |
| 45 | + id: "call_use_mcp_tool_list_directory_002", |
| 46 | + }, |
| 47 | + { |
| 48 | + userMessagePattern: "USE_MCP_TOOL_DIRECTORY_TREE_SMOKE", |
| 49 | + toolCallId: "call_use_mcp_tool_directory_tree_001", |
| 50 | + toolName: "directory_tree", |
| 51 | + toolArguments: { path: path.join(workspaceDir, TEST_DIR_NAME) }, |
| 52 | + result: "Returned the directory tree through the MCP filesystem server.", |
| 53 | + id: "call_use_mcp_tool_directory_tree_002", |
| 54 | + }, |
| 55 | + { |
| 56 | + userMessagePattern: "USE_MCP_TOOL_GET_FILE_INFO_SMOKE", |
| 57 | + toolCallId: "call_use_mcp_tool_get_file_info_001", |
| 58 | + toolName: "get_file_info", |
| 59 | + toolArguments: { path: readFilePath }, |
| 60 | + result: "Returned the requested file metadata through the MCP filesystem server.", |
| 61 | + id: "call_use_mcp_tool_get_file_info_002", |
| 62 | + }, |
| 63 | + { |
| 64 | + userMessagePattern: "USE_MCP_TOOL_UNKNOWN_SERVER_SMOKE", |
| 65 | + toolCallId: "call_use_mcp_tool_unknown_server_001", |
| 66 | + serverName: "nonexistent-server", |
| 67 | + toolName: "read_file", |
| 68 | + toolArguments: { path: readFilePath }, |
| 69 | + result: "Handled the missing MCP server gracefully.", |
| 70 | + id: "call_use_mcp_tool_unknown_server_002", |
| 71 | + }, |
| 72 | + ] |
| 73 | + |
| 74 | + for (const fixture of fixtures) { |
| 75 | + mock.addFixture({ |
| 76 | + match: { |
| 77 | + userMessage: new RegExp(fixture.userMessagePattern), |
| 78 | + }, |
| 79 | + response: { |
| 80 | + toolCalls: [ |
| 81 | + { |
| 82 | + name: "use_mcp_tool", |
| 83 | + arguments: JSON.stringify({ |
| 84 | + server_name: fixture.serverName ?? FILESYSTEM_SERVER_NAME, |
| 85 | + tool_name: fixture.toolName, |
| 86 | + arguments: fixture.toolArguments, |
| 87 | + }), |
| 88 | + id: fixture.toolCallId, |
| 89 | + }, |
| 90 | + ], |
| 91 | + }, |
| 92 | + }) |
| 93 | + |
| 94 | + mock.addFixture({ |
| 95 | + match: { |
| 96 | + toolCallId: fixture.toolCallId, |
| 97 | + }, |
| 98 | + response: { |
| 99 | + toolCalls: [ |
| 100 | + { |
| 101 | + name: "attempt_completion", |
| 102 | + arguments: JSON.stringify({ result: fixture.result }), |
| 103 | + id: fixture.id, |
| 104 | + }, |
| 105 | + ], |
| 106 | + }, |
| 107 | + }) |
| 108 | + } |
| 109 | +} |
0 commit comments