|
| 1 | + import { z } from 'zod'; |
| 2 | + import zodToJsonSchema from 'zod-to-json-schema'; |
| 3 | + |
| 4 | + import { ApifyClient } from '../apify-client.js'; |
| 5 | + import { HelperTools } from '../const.js'; |
| 6 | + import type { InternalTool, ToolEntry } from '../types.js'; |
| 7 | + import { ajv } from '../utils/ajv.js'; |
| 8 | + import { buildMCPResponse } from '../utils/mcp.js'; |
| 9 | + import { mcpDevSummitScheduleCache } from '../state.js'; |
| 10 | + |
| 11 | +// Local backup variable to store the latest data in case cache expires |
| 12 | +let latestScheduleData: string[] | null = null; |
| 13 | + |
| 14 | +// Helper function to fetch schedule data from Apify Actor |
| 15 | +async function fetchScheduleData(): Promise<string[]> { |
| 16 | + const client = new ApifyClient({ token: process.env.APIFY_TOKEN }); |
| 17 | + |
| 18 | + const input = { |
| 19 | + "aggressivePrune": false, |
| 20 | + "blockMedia": true, |
| 21 | + "clickElementsCssSelector": "[aria-expanded=\"false\"]", |
| 22 | + "clientSideMinChangePercentage": 15, |
| 23 | + "crawlerType": "cheerio", |
| 24 | + "debugLog": false, |
| 25 | + "debugMode": false, |
| 26 | + "expandIframes": true, |
| 27 | + "ignoreCanonicalUrl": false, |
| 28 | + "ignoreHttpsErrors": false, |
| 29 | + "includeUrlGlobs": [ |
| 30 | + { |
| 31 | + "glob": "https://mcpdevsummiteurope2025.sched.com/event/**" |
| 32 | + } |
| 33 | + ], |
| 34 | + "keepUrlFragments": false, |
| 35 | + "proxyConfiguration": { |
| 36 | + "useApifyProxy": true |
| 37 | + }, |
| 38 | + "readableTextCharThreshold": 100, |
| 39 | + "removeCookieWarnings": true, |
| 40 | + "removeElementsCssSelector": "nav, footer, script, style, noscript, svg, img[src^='data:'],\n[role=\"alert\"],\n[role=\"banner\"],\n[role=\"dialog\"],\n[role=\"alertdialog\"],\n[role=\"region\"][aria-label*=\"skip\" i],\n[aria-modal=\"true\"]", |
| 41 | + "renderingTypeDetectionPercentage": 10, |
| 42 | + "respectRobotsTxtFile": false, |
| 43 | + "saveFiles": false, |
| 44 | + "saveHtml": false, |
| 45 | + "saveHtmlAsFile": false, |
| 46 | + "saveMarkdown": true, |
| 47 | + "saveScreenshots": false, |
| 48 | + "startUrls": [ |
| 49 | + { |
| 50 | + "url": "https://mcpdevsummiteurope2025.sched.com/list/simple", |
| 51 | + "method": "GET" |
| 52 | + } |
| 53 | + ], |
| 54 | + "useSitemaps": false, |
| 55 | + "excludeUrlGlobs": [], |
| 56 | + "maxCrawlDepth": 20, |
| 57 | + "maxCrawlPages": 9999999, |
| 58 | + "initialConcurrency": 0, |
| 59 | + "maxConcurrency": 200, |
| 60 | + "initialCookies": [], |
| 61 | + "maxSessionRotations": 10, |
| 62 | + "maxRequestRetries": 3, |
| 63 | + "requestTimeoutSecs": 60, |
| 64 | + "minFileDownloadSpeedKBps": 128, |
| 65 | + "dynamicContentWaitSecs": 10, |
| 66 | + "waitForSelector": "", |
| 67 | + "softWaitForSelector": "", |
| 68 | + "maxScrollHeightPixels": 5000, |
| 69 | + "keepElementsCssSelector": "", |
| 70 | + "htmlTransformer": "readableText", |
| 71 | + "maxResults": 9999999 |
| 72 | + }; |
| 73 | + |
| 74 | + const run = await client.actor('apify/website-content-crawler').call(input); |
| 75 | + const { items } = await client.dataset(run.defaultDatasetId).listItems(); |
| 76 | + |
| 77 | + // The crawled markdown already contains all the event details |
| 78 | + const data = items.map((item: any) => item.text || ''); |
| 79 | + |
| 80 | + // Update the local backup variable |
| 81 | + latestScheduleData = data; |
| 82 | + |
| 83 | + return data; |
| 84 | +} |
| 85 | + |
| 86 | +// Helper function to schedule background refresh |
| 87 | +function scheduleBackgroundRefresh(): void { |
| 88 | + // Use setTimeout to schedule refresh after response is sent |
| 89 | + setTimeout(async () => { |
| 90 | + try { |
| 91 | + // Remove expired entry |
| 92 | + (mcpDevSummitScheduleCache as any).cache.remove('mcp-dev-summit-schedule'); |
| 93 | + const freshData = await fetchScheduleData(); |
| 94 | + mcpDevSummitScheduleCache.set('mcp-dev-summit-schedule', freshData); |
| 95 | + // Update local backup as well |
| 96 | + latestScheduleData = freshData; |
| 97 | + } catch (error) { |
| 98 | + console.error('Background refresh of MCP Dev Summit schedule failed:', error); |
| 99 | + } |
| 100 | + }, 0); |
| 101 | +} |
| 102 | + |
| 103 | +// Custom cache check that serves expired data and refreshes in background |
| 104 | +function getCachedOrFetch(): { data: string[] | null, isExpired: boolean } { |
| 105 | + const cacheKey = 'mcp-dev-summit-schedule'; |
| 106 | + const entry = (mcpDevSummitScheduleCache as any).cache.get(cacheKey); |
| 107 | + |
| 108 | + if (!entry) { |
| 109 | + return { data: null, isExpired: false }; |
| 110 | + } |
| 111 | + |
| 112 | + const isExpired = entry.expiresAt <= Date.now(); |
| 113 | + |
| 114 | + if (isExpired) { |
| 115 | + // Return expired data |
| 116 | + return { data: entry.value, isExpired: true }; |
| 117 | + } |
| 118 | + |
| 119 | + return { data: entry.value, isExpired: false }; |
| 120 | +} |
| 121 | + |
| 122 | + |
| 123 | + |
| 124 | +export const getMcpDevSummitSchedule: ToolEntry = { |
| 125 | + type: 'internal', |
| 126 | + tool: { |
| 127 | + name: HelperTools.GET_MCP_DEV_SUMMIT_SCHEDULE, |
| 128 | + actorFullName: HelperTools.GET_MCP_DEV_SUMMIT_SCHEDULE, |
| 129 | + description: `Retrieve the schedule for the MCP Dev Summit Europe 2025. |
| 130 | +Fetches and parses the schedule from https://mcpdevsummiteurope2025.sched.com/list/simple to provide |
| 131 | +structured information about sessions, speakers, and timing. |
| 132 | +
|
| 133 | +USAGE: |
| 134 | +- Use when you need information about MCP Dev Summit sessions, schedule, or speakers. |
| 135 | +
|
| 136 | +USAGE EXAMPLES: |
| 137 | +- user_input: What sessions are scheduled for the MCP Dev Summit? |
| 138 | +- user_input: Who are the speakers at the MCP Dev Summit?`, |
| 139 | + inputSchema: zodToJsonSchema(z.object({})), |
| 140 | + ajvValidate: ajv.compile(zodToJsonSchema(z.object({}))), |
| 141 | + call: async () => { |
| 142 | + const { data: cachedData, isExpired } = getCachedOrFetch(); |
| 143 | + |
| 144 | + if (cachedData) { |
| 145 | + // Serve cached data immediately |
| 146 | + if (isExpired) { |
| 147 | + // Schedule background refresh for expired data |
| 148 | + scheduleBackgroundRefresh(); |
| 149 | + } |
| 150 | + return buildMCPResponse(cachedData); |
| 151 | + } |
| 152 | + |
| 153 | + // No cached data, check local backup |
| 154 | + if (latestScheduleData) { |
| 155 | + // Serve local backup data immediately |
| 156 | + scheduleBackgroundRefresh(); |
| 157 | + return buildMCPResponse(latestScheduleData); |
| 158 | + } |
| 159 | + |
| 160 | + // No cached or backup data, fetch fresh data |
| 161 | + const freshData = await fetchScheduleData(); |
| 162 | + |
| 163 | + // Cache the fresh data |
| 164 | + mcpDevSummitScheduleCache.set('mcp-dev-summit-schedule', freshData); |
| 165 | + |
| 166 | + return buildMCPResponse(freshData); |
| 167 | + }, |
| 168 | + } as InternalTool, |
| 169 | +}; |
0 commit comments