|
| 1 | +/** @vitest-environment node */ |
| 2 | +import { Client } from '@modelcontextprotocol/sdk/client/index.js' |
| 3 | +import { InMemoryTransport } from '@modelcontextprotocol/sdk/inMemory.js' |
| 4 | +import { resetEnvFlagsMock, setEnvFlags } from '@sim/testing' |
| 5 | +import { NextRequest } from 'next/server' |
| 6 | +import { beforeEach, describe, expect, it, vi } from 'vitest' |
| 7 | + |
| 8 | +const mocks = vi.hoisted(() => ({ |
| 9 | + liveSearch: vi.fn(), |
| 10 | + liveRead: vi.fn(), |
| 11 | + indexedSearch: vi.fn(), |
| 12 | + indexedRead: vi.fn(), |
| 13 | +})) |
| 14 | +vi.mock('@/lib/core/utils/after-response', () => ({ afterResponse: vi.fn() })) |
| 15 | +vi.mock('@/lib/knowledge/mcp/activity', () => ({ recordOrganizationSearchMcpActivity: vi.fn() })) |
| 16 | +vi.mock('@/lib/api/server/routes/v2-json-route', () => ({ |
| 17 | + v2RateLimits: { publicApi: { enforce: vi.fn().mockResolvedValue(null) } }, |
| 18 | +})) |
| 19 | +vi.mock('@/lib/knowledge/application/search', () => ({ |
| 20 | + searchKnowledge: { execute: mocks.indexedSearch }, |
| 21 | +})) |
| 22 | +vi.mock('@/lib/knowledge/application/read-indexed-document', () => ({ |
| 23 | + readIndexedKnowledgeDocument: { execute: mocks.indexedRead }, |
| 24 | +})) |
| 25 | +vi.mock('@/lib/sim-search/live/application', () => ({ |
| 26 | + searchLiveKnowledge: { execute: mocks.liveSearch }, |
| 27 | + readLiveDocument: { execute: mocks.liveRead }, |
| 28 | +})) |
| 29 | +vi.mock('@/lib/core/utils/urls', () => ({ getBaseUrl: () => 'http://localhost' })) |
| 30 | + |
| 31 | +import { createKnowledgeMcpServer } from '@/lib/knowledge/mcp/server' |
| 32 | + |
| 33 | +describe('Search MCP protocol', () => { |
| 34 | + beforeEach(() => { |
| 35 | + vi.clearAllMocks() |
| 36 | + resetEnvFlagsMock() |
| 37 | + }) |
| 38 | + |
| 39 | + it.each([true, false])( |
| 40 | + 'lists and calls the selected backend through the SDK (live: %s)', |
| 41 | + async (live) => { |
| 42 | + setEnvFlags({ isLiveEnterpriseSearchEnabled: live }) |
| 43 | + const documentId = live ? `live:${'a'.repeat(500)}` : 'doc-1' |
| 44 | + const row = { |
| 45 | + documentId, |
| 46 | + knowledgeBaseId: live ? '' : 'index-1', |
| 47 | + documentName: 'Release notes', |
| 48 | + sourceUrl: 'https://example.com/notes', |
| 49 | + connectorType: 'google_drive', |
| 50 | + content: 'Evidence', |
| 51 | + chunkIndex: 0, |
| 52 | + similarity: 0.5, |
| 53 | + } |
| 54 | + mocks.liveSearch.mockResolvedValue({ |
| 55 | + results: [row], |
| 56 | + live: { backend: 'live', accounts: [], guidance: '' }, |
| 57 | + }) |
| 58 | + mocks.indexedSearch.mockResolvedValue({ results: [row] }) |
| 59 | + mocks.liveRead.mockResolvedValue({ |
| 60 | + ...row, |
| 61 | + knowledgeBaseName: 'Drive', |
| 62 | + chunks: [{ chunkIndex: 0, content: 'Read evidence' }], |
| 63 | + hasMore: false, |
| 64 | + next: null, |
| 65 | + }) |
| 66 | + mocks.indexedRead.mockResolvedValue({ |
| 67 | + ...row, |
| 68 | + title: row.documentName, |
| 69 | + chunks: [{ chunkIndex: 0, content: 'Read evidence' }], |
| 70 | + }) |
| 71 | + |
| 72 | + const server = createKnowledgeMcpServer({ |
| 73 | + organizationId: 'org-1', |
| 74 | + searchIndexId: live ? null : 'index-1', |
| 75 | + request: new NextRequest('http://localhost/api/mcp/search/organizations/org-1'), |
| 76 | + auth: { |
| 77 | + principal: { kind: 'personal_api_key', userId: 'user-1', keyId: 'key-1' }, |
| 78 | + keyType: 'personal', |
| 79 | + keyExpiresAt: null, |
| 80 | + rateLimitSubjectIds: ['user-1'], |
| 81 | + rateLimitSubscription: null, |
| 82 | + }, |
| 83 | + }) |
| 84 | + const client = new Client({ name: 'Search test', version: '1.0.0' }) |
| 85 | + const [clientTransport, serverTransport] = InMemoryTransport.createLinkedPair() |
| 86 | + try { |
| 87 | + await server.connect(serverTransport) |
| 88 | + await client.connect(clientTransport) |
| 89 | + const { tools } = await client.listTools() |
| 90 | + expect(tools.map((tool) => tool.name)).toEqual(['search', 'read_document', 'chat']) |
| 91 | + const searchSchema = tools.find((tool) => tool.name === 'search')?.inputSchema |
| 92 | + const readSchema = tools.find((tool) => tool.name === 'read_document')?.inputSchema |
| 93 | + if (live) { |
| 94 | + expect(searchSchema?.properties).toHaveProperty('nativeQueries') |
| 95 | + expect(readSchema?.properties).toHaveProperty('startOffset') |
| 96 | + expect(readSchema?.properties).not.toHaveProperty('url') |
| 97 | + } else { |
| 98 | + expect(searchSchema?.properties).not.toHaveProperty('nativeQueries') |
| 99 | + expect(readSchema?.properties).toHaveProperty('offset') |
| 100 | + expect(readSchema?.properties).toHaveProperty('url') |
| 101 | + } |
| 102 | + const search = await client.callTool({ name: 'search', arguments: { query: 'release' } }) |
| 103 | + expect(search.isError).not.toBe(true) |
| 104 | + expect(search.content).toEqual([ |
| 105 | + { type: 'text', text: expect.stringContaining(documentId) }, |
| 106 | + ]) |
| 107 | + const read = await client.callTool({ name: 'read_document', arguments: { documentId } }) |
| 108 | + expect(read.isError).not.toBe(true) |
| 109 | + expect(read.content).toEqual([ |
| 110 | + { type: 'text', text: expect.stringContaining('Read evidence') }, |
| 111 | + ]) |
| 112 | + expect(live ? mocks.liveSearch : mocks.indexedSearch).toHaveBeenCalledOnce() |
| 113 | + expect(live ? mocks.liveRead : mocks.indexedRead).toHaveBeenCalledOnce() |
| 114 | + expect(live ? mocks.indexedSearch : mocks.liveSearch).not.toHaveBeenCalled() |
| 115 | + expect(live ? mocks.indexedRead : mocks.liveRead).not.toHaveBeenCalled() |
| 116 | + } finally { |
| 117 | + await client.close() |
| 118 | + await server.close() |
| 119 | + } |
| 120 | + } |
| 121 | + ) |
| 122 | +}) |
0 commit comments