-
Notifications
You must be signed in to change notification settings - Fork 15.7k
Expand file tree
/
Copy pathdiscovery.test.ts
More file actions
162 lines (142 loc) · 4.39 KB
/
discovery.test.ts
File metadata and controls
162 lines (142 loc) · 4.39 KB
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
162
import { describe, expect, test, mock } from 'bun:test'
import { discoverTools, createCachedToolDiscovery } from '../discovery.js'
import type { DiscoveryOptions } from '../discovery.js'
import type { ConnectedMCPServer } from '../types.js'
import type { McpClientDependencies } from '../interfaces.js'
function createMockDeps(): McpClientDependencies {
return {
logger: {
debug: mock(() => {}),
info: mock(() => {}),
warn: mock(() => {}),
error: mock(() => {}),
},
httpConfig: {
getUserAgent: () => 'test-agent/1.0',
},
}
}
describe('discoverTools', () => {
test('returns empty array when capabilities.tools is missing', async () => {
const result = await discoverTools({
serverName: 'test',
client: {} as any,
capabilities: {},
deps: createMockDeps(),
})
expect(result).toEqual([])
})
test('fetches and transforms tools from server', async () => {
const mockClient = {
request: mock(() =>
Promise.resolve({
tools: [
{
name: 'search',
description: 'Search for items',
inputSchema: { type: 'object' },
annotations: { readOnlyHint: true, title: 'Search Items' },
},
],
}),
),
}
const result = await discoverTools({
serverName: 'my-server',
client: mockClient as any,
capabilities: { tools: {} },
deps: createMockDeps(),
})
expect(result).toHaveLength(1)
const tool = result[0]
expect(tool.name).toBe('mcp__my-server__search')
expect(tool.mcpInfo).toEqual({ serverName: 'my-server', toolName: 'search' })
expect(tool.isMcp).toBe(true)
expect(tool.isReadOnly({} as any)).toBe(true)
expect(tool.userFacingName(undefined)).toBe('Search Items')
expect(await tool.description({} as any, { isNonInteractiveSession: false, toolPermissionContext: {}, tools: [] })).toBe('Search for items')
})
test('respects skipPrefix option', async () => {
const mockClient = {
request: mock(() =>
Promise.resolve({
tools: [{ name: 'search', description: 'Search' }],
}),
),
}
const result = await discoverTools({
serverName: 'my-server',
client: mockClient as any,
capabilities: { tools: {} },
skipPrefix: true,
deps: createMockDeps(),
})
expect(result[0].name).toBe('search')
})
test('returns empty array on fetch error', async () => {
const mockClient = {
request: mock(() => Promise.reject(new Error('Connection lost'))),
}
const deps = createMockDeps()
const result = await discoverTools({
serverName: 'failing-server',
client: mockClient as any,
capabilities: { tools: {} },
deps,
})
expect(result).toEqual([])
expect(deps.logger.warn).toHaveBeenCalled()
})
test('sanitizes tool data', async () => {
const mockClient = {
request: mock(() =>
Promise.resolve({
tools: [
{
name: 'tool\x00with\x07control',
description: 'desc',
},
],
}),
),
}
const result = await discoverTools({
serverName: 'test',
client: mockClient as any,
capabilities: { tools: {} },
deps: createMockDeps(),
})
expect(result[0].name).not.toContain('\x00')
})
})
describe('createCachedToolDiscovery', () => {
test('caches results by server name', async () => {
const deps = createMockDeps()
const { discover, cache } = createCachedToolDiscovery(deps)
const mockConn = {
type: 'connected' as const,
name: 'cached-server',
client: {
request: mock(() =>
Promise.resolve({
tools: [{ name: 'tool1', description: 'Tool 1' }],
}),
),
},
capabilities: { tools: {} },
} as unknown as ConnectedMCPServer
// First call — should fetch
const result1 = await discover(mockConn)
expect(result1).toHaveLength(1)
// Second call — should use cache
const result2 = await discover(mockConn)
expect(result2).toHaveLength(1)
// Request was called only once
expect(mockConn.client.request).toHaveBeenCalledTimes(1)
// Cache delete works
cache.delete('cached-server')
const result3 = await discover(mockConn)
expect(result3).toHaveLength(1)
expect(mockConn.client.request).toHaveBeenCalledTimes(2)
})
})