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