|
| 1 | +import type { Server } from '@modelcontextprotocol/sdk/server.js'; |
| 2 | +import express from 'express'; |
| 3 | +import { afterEach, beforeEach, describe, expect, it } from 'vitest'; |
| 4 | + |
| 5 | +import { log } from '../src/logger.js'; |
| 6 | +import { ApifyMcpServer } from '../src/mcp-server.js'; |
| 7 | + |
| 8 | +describe('ApifyMcpServer initialization', () => { |
| 9 | + let app: express.Express; |
| 10 | + let server: ApifyMcpServer; |
| 11 | + let mcpServer: Server; |
| 12 | + const testPort = 7357; |
| 13 | + |
| 14 | + beforeEach(async () => { |
| 15 | + app = express(); |
| 16 | + server = new ApifyMcpServer(); |
| 17 | + log.setLevel(log.LEVELS.OFF); |
| 18 | + |
| 19 | + // Setup basic express route to trigger server initialization |
| 20 | + app.get('/', async (req, res) => { |
| 21 | + await server.processParamsAndUpdateTools(req.url); |
| 22 | + res.sendStatus(200); |
| 23 | + }); |
| 24 | + |
| 25 | + // Start test server |
| 26 | + await new Promise<void>((resolve) => { |
| 27 | + mcpServer = app.listen(testPort, () => resolve()); |
| 28 | + }); |
| 29 | + }); |
| 30 | + |
| 31 | + afterEach(async () => { |
| 32 | + await new Promise<void>((resolve) => { |
| 33 | + mcpServer.close(() => resolve()); |
| 34 | + }); |
| 35 | + }); |
| 36 | + |
| 37 | + it('should load actors from query parameters', async () => { |
| 38 | + // Test with multiple actors including different username cases |
| 39 | + const testActors = ['apify/rag-web-browser', 'apify/instagram-scraper']; |
| 40 | + |
| 41 | + // Make request to trigger server initialization |
| 42 | + const response = await fetch(`http://localhost:${testPort}/?actors=${testActors.join(',')}`); |
| 43 | + expect(response.status).toBe(200); |
| 44 | + |
| 45 | + // Verify loaded tools |
| 46 | + const toolNames = server.getToolNames(); |
| 47 | + expect(toolNames).toEqual(expect.arrayContaining([ |
| 48 | + 'apify-slash-rag-web-browser', |
| 49 | + 'apify-slash-instagram-scraper', |
| 50 | + ])); |
| 51 | + expect(toolNames.length).toBe(testActors.length); |
| 52 | + }); |
| 53 | + |
| 54 | + it('should enable auto-loading tools when flag is set', async () => { |
| 55 | + const response = await fetch(`http://localhost:${testPort}/?enableActorAutoLoading=true`); |
| 56 | + expect(response.status).toBe(200); |
| 57 | + |
| 58 | + const toolNames = server.getToolNames(); |
| 59 | + expect(toolNames).toEqual([ |
| 60 | + 'add-actor-to-tools', |
| 61 | + 'remove-actor-from-tools', |
| 62 | + ]); |
| 63 | + }); |
| 64 | +}); |
0 commit comments