summaryrefslogtreecommitdiff
path: root/packages/server/src/tools/mcp-tool.test.ts
blob: e28cf5861a4f6d17ec78981c83479117b85a4491 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
/**
 * @license
 * Copyright 2025 Google LLC
 * SPDX-License-Identifier: Apache-2.0
 */

/* eslint-disable @typescript-eslint/no-explicit-any */
import {
  describe,
  it,
  expect,
  vi,
  beforeEach,
  afterEach,
  Mocked,
} from 'vitest';
import {
  DiscoveredMCPTool,
  MCP_TOOL_DEFAULT_TIMEOUT_MSEC,
} from './mcp-tool.js';
import { Client } from '@modelcontextprotocol/sdk/client/index.js';
import { ToolResult } from './tools.js';

// Mock MCP SDK Client
vi.mock('@modelcontextprotocol/sdk/client/index.js', () => {
  const MockClient = vi.fn();
  MockClient.prototype.callTool = vi.fn();
  return { Client: MockClient };
});

describe('DiscoveredMCPTool', () => {
  let mockMcpClient: Mocked<Client>;
  const toolName = 'test-mcp-tool';
  const serverToolName = 'actual-server-tool-name';
  const baseDescription = 'A test MCP tool.';
  const inputSchema = {
    type: 'object' as const,
    properties: { param: { type: 'string' } },
  };

  beforeEach(() => {
    // Create a new mock client for each test to reset call history
    mockMcpClient = new (Client as any)({
      name: 'test-client',
      version: '0.0.1',
    }) as Mocked<Client>;
    vi.mocked(mockMcpClient.callTool).mockClear();
  });

  afterEach(() => {
    vi.restoreAllMocks();
  });

  describe('constructor', () => {
    it('should set properties correctly and augment description', () => {
      const tool = new DiscoveredMCPTool(
        mockMcpClient,
        toolName,
        baseDescription,
        inputSchema,
        serverToolName,
      );

      expect(tool.name).toBe(toolName);
      expect(tool.schema.name).toBe(toolName);
      expect(tool.schema.description).toContain(baseDescription);
      expect(tool.schema.description).toContain('This MCP tool was discovered');
      // Corrected assertion for backticks and template literal
      expect(tool.schema.description).toContain(
        `tools/call\` method for tool name \`${toolName}\``,
      );
      expect(tool.schema.parameters).toEqual(inputSchema);
      expect(tool.serverToolName).toBe(serverToolName);
      expect(tool.timeout).toBeUndefined();
    });

    it('should accept and store a custom timeout', () => {
      const customTimeout = 5000;
      const tool = new DiscoveredMCPTool(
        mockMcpClient,
        toolName,
        baseDescription,
        inputSchema,
        serverToolName,
        customTimeout,
      );
      expect(tool.timeout).toBe(customTimeout);
    });
  });

  describe('execute', () => {
    it('should call mcpClient.callTool with correct parameters and default timeout', async () => {
      const tool = new DiscoveredMCPTool(
        mockMcpClient,
        toolName,
        baseDescription,
        inputSchema,
        serverToolName,
      );
      const params = { param: 'testValue' };
      const expectedMcpResult = { success: true, details: 'executed' };
      vi.mocked(mockMcpClient.callTool).mockResolvedValue(expectedMcpResult);

      const result: ToolResult = await tool.execute(params);

      expect(mockMcpClient.callTool).toHaveBeenCalledWith(
        {
          name: serverToolName,
          arguments: params,
        },
        undefined,
        {
          timeout: MCP_TOOL_DEFAULT_TIMEOUT_MSEC,
        },
      );
      const expectedOutput = JSON.stringify(expectedMcpResult, null, 2);
      expect(result.llmContent).toBe(expectedOutput);
      expect(result.returnDisplay).toBe(expectedOutput);
    });

    it('should call mcpClient.callTool with custom timeout if provided', async () => {
      const customTimeout = 15000;
      const tool = new DiscoveredMCPTool(
        mockMcpClient,
        toolName,
        baseDescription,
        inputSchema,
        serverToolName,
        customTimeout,
      );
      const params = { param: 'anotherValue' };
      const expectedMcpResult = { result: 'done' };
      vi.mocked(mockMcpClient.callTool).mockResolvedValue(expectedMcpResult);

      await tool.execute(params);

      expect(mockMcpClient.callTool).toHaveBeenCalledWith(
        expect.anything(),
        undefined,
        {
          timeout: customTimeout,
        },
      );
    });

    it('should propagate rejection if mcpClient.callTool rejects', async () => {
      const tool = new DiscoveredMCPTool(
        mockMcpClient,
        toolName,
        baseDescription,
        inputSchema,
        serverToolName,
      );
      const params = { param: 'failCase' };
      const expectedError = new Error('MCP call failed');
      vi.mocked(mockMcpClient.callTool).mockRejectedValue(expectedError);

      await expect(tool.execute(params)).rejects.toThrow(expectedError);
    });
  });
});