|
| 1 | +const fp = require('fastify-plugin') |
| 2 | +const { LRUCache } = require('lru-cache') |
| 3 | +// decorate the app with the expert helpers and cache utilities |
| 4 | + |
| 5 | +module.exports = fp(async function (app, _opts) { |
| 6 | + // Get the assistant service configuration |
| 7 | + const serviceEnabled = app.config.expert?.enabled === true |
| 8 | + const expertUrl = app.config.expert?.service?.url |
| 9 | + const serviceToken = app.config.expert?.service?.token |
| 10 | + const requestTimeout = app.config.expert?.service?.requestTimeout || 60000 |
| 11 | + |
| 12 | + const TOKEN_TTL = app.config.expert?.tokenCache?.ttl || 5 * 60 * 1000 // Default 5 minutes |
| 13 | + const TOKEN_REMAINING_LIMIT = 15000 // token life edge window (avoid using tokens about to expire) |
| 14 | + const mcpAccessTokenCache = new LRUCache({ |
| 15 | + name: 'ExpertMCPAccessTokenCache', // for testing purposes |
| 16 | + max: app.config.expert?.tokenCache?.max || 1000, |
| 17 | + ttl: TOKEN_TTL, |
| 18 | + updateAgeOnGet: false // do not update the age on get, we want it to expire after the original ttl |
| 19 | + }) |
| 20 | + |
| 21 | + function clearMcpAccessTokenCache (cacheKey) { |
| 22 | + if (cacheKey) { |
| 23 | + mcpAccessTokenCache.delete(cacheKey) |
| 24 | + } else { |
| 25 | + mcpAccessTokenCache.clear() |
| 26 | + } |
| 27 | + } |
| 28 | + |
| 29 | + async function getOrCreateMcpAccessToken (instance, instanceType, instanceId, teamHttpSecurityFeatureEnabled) { |
| 30 | + let mcpAccessToken |
| 31 | + if (mcpAccessTokenCache.has(instanceId)) { |
| 32 | + const remainingTTL = mcpAccessTokenCache.getRemainingTTL(instanceId) |
| 33 | + if (remainingTTL > TOKEN_REMAINING_LIMIT) { // only use cached token if it has more than 5 second remaining |
| 34 | + mcpAccessToken = mcpAccessTokenCache.get(instanceId) |
| 35 | + } |
| 36 | + } |
| 37 | + |
| 38 | + if (!mcpAccessToken) { |
| 39 | + const instanceSettings = await instance.getSetting('settings') |
| 40 | + const httpNodeAuth = instanceSettings?.httpNodeAuth |
| 41 | + const tokenName = 'FlowFuse Expert MCP Access Token' |
| 42 | + const scope = ['ff-expert:mcp', instanceType] |
| 43 | + if (httpNodeAuth?.type === 'flowforge-user' && teamHttpSecurityFeatureEnabled) { |
| 44 | + // FlowFuse auth is enabled for this instance |
| 45 | + const expiresAt = new Date(Date.now() + (TOKEN_TTL)) |
| 46 | + const token = await app.db.controllers.AccessToken.createHTTPNodeToken(instance, tokenName, scope, expiresAt) |
| 47 | + mcpAccessToken = { |
| 48 | + scheme: 'Bearer', |
| 49 | + scope, |
| 50 | + token: token.token |
| 51 | + } |
| 52 | + } else if (httpNodeAuth?.type === 'basic') { |
| 53 | + // Basic auth is enabled - MCP client will need to use basic auth |
| 54 | + mcpAccessToken = { |
| 55 | + scheme: 'Basic', |
| 56 | + scope, |
| 57 | + token: '' // basic auth is not supported - we have no access to the password. For now, just return an empty string. |
| 58 | + } |
| 59 | + } else { |
| 60 | + // default - no auth |
| 61 | + mcpAccessToken = { |
| 62 | + scheme: '', |
| 63 | + scope, |
| 64 | + token: null |
| 65 | + } |
| 66 | + } |
| 67 | + mcpAccessTokenCache.set(instanceId, mcpAccessToken) |
| 68 | + } |
| 69 | + return mcpAccessToken |
| 70 | + } |
| 71 | + |
| 72 | + function getCachedMcpAccessToken (instanceId) { |
| 73 | + if (mcpAccessTokenCache.has(instanceId)) { |
| 74 | + const remainingTTL = mcpAccessTokenCache.getRemainingTTL(instanceId) |
| 75 | + if (remainingTTL > 15000) { // only use cached token if it is not about to expire |
| 76 | + return mcpAccessTokenCache.get(instanceId) |
| 77 | + } |
| 78 | + } |
| 79 | + return null |
| 80 | + } |
| 81 | + |
| 82 | + app.decorate('expert', { |
| 83 | + serviceEnabled, |
| 84 | + expertUrl, |
| 85 | + serviceToken, |
| 86 | + requestTimeout, |
| 87 | + mcp: { |
| 88 | + clearTokenCache: clearMcpAccessTokenCache, |
| 89 | + getCachedToken: getCachedMcpAccessToken, |
| 90 | + getOrCreateToken: getOrCreateMcpAccessToken |
| 91 | + } |
| 92 | + }) |
| 93 | +}, { name: 'app.expert' }) |
0 commit comments