Merge pull request #9186 from ever-co/fix/mcp-redis-configuration

Fix/mcp redis configuration
This commit is contained in:
samuel mbabhazi
2025-11-03 13:22:05 +01:00
committed by GitHub
parent 9ffe14bf39
commit 442c2b171f
2 changed files with 64 additions and 12 deletions
@@ -34,6 +34,33 @@ export class McpOAuthService implements OnModuleInit {
const port = process.env.MCP_AUTH_PORT || 3003;
const baseUrl = process.env.MCP_AUTH_JWT_ISSUER || `http://localhost:${port}`;
// Build Redis URL from REDIS_URL or individual parameters
const redisUrl = (() => {
if (process.env.REDIS_ENABLED !== 'true') {
return undefined;
}
const { REDIS_URL, REDIS_HOST, REDIS_PORT, REDIS_USER, REDIS_PASSWORD, REDIS_TLS } = process.env;
// If REDIS_URL is provided, use it directly
if (REDIS_URL) {
return REDIS_URL;
}
// If individual parameters are provided, construct the URL
if (REDIS_HOST && REDIS_PORT) {
const redisProtocol = REDIS_TLS === 'true' ? 'rediss' : 'redis';
const auth = REDIS_USER && REDIS_PASSWORD ? `${REDIS_USER}:${REDIS_PASSWORD}@` : '';
return `${redisProtocol}://${auth}${REDIS_HOST}:${REDIS_PORT}`;
}
// If neither REDIS_URL nor required individual parameters are provided, log warning
console.warn(
'Redis is enabled but neither REDIS_URL nor REDIS_HOST/REDIS_PORT are configured. Redis will not be used.'
);
return undefined;
})();
this.oauthServer = new OAuth2AuthorizationServer({
issuer: process.env.MCP_AUTH_JWT_ISSUER || baseUrl,
baseUrl: baseUrl,
@@ -47,20 +74,20 @@ export class McpOAuthService implements OnModuleInit {
introspectionEndpoint: '/oauth2/introspect',
userInfoEndpoint: '/oauth2/userinfo',
sessionSecret: process.env.MCP_AUTH_SESSION_SECRET,
redisUrl: process.env.REDIS_URL, // Optional
redisUrl: redisUrl,
// Wire providers up-front to satisfy constructor validation
userAuthenticator: async ({ email, password }) => {
const user = await this.userService.authenticateMcpUser(email, password);
if (!user) return null;
const user = await this.userService.authenticateMcpUser(email, password);
if (!user) return null;
return this.mapMcpUserToAuthPayload(user);
},
userInfoProvider: async (userId: string) => {
const user = await this.userService.getMcpUserInfo(userId);
if (!user) return null;
return this.mapMcpUserToAuthPayload(user);
},
userInfoProvider: async (userId: string) => {
const user = await this.userService.getMcpUserInfo(userId);
if (!user) return null;
return this.mapMcpUserToAuthPayload(user);
}
return this.mapMcpUserToAuthPayload(user);
}
});
}
@@ -138,8 +138,33 @@ export class ConfigManager {
rateLimitTtl: this.getEnvNumber('MCP_RATE_LIMIT_TTL', 60000), // 1 minute
rateLimitMax: this.getEnvNumber('MCP_RATE_LIMIT_MAX', 100),
// Redis
redisUrl: this.getEnvString('REDIS_URL'),
// Redis - construct URL from REDIS_URL or individual parameters
redisUrl: (() => {
if (process.env.REDIS_ENABLED !== 'true') {
console.log('Redis is not enabled for MCP.');
return undefined;
}
const { REDIS_URL, REDIS_HOST, REDIS_PORT, REDIS_USER, REDIS_PASSWORD, REDIS_TLS } = process.env;
// If REDIS_URL is provided, use it directly
if (REDIS_URL) {
return REDIS_URL;
}
// If individual parameters are provided, construct the URL
if (REDIS_HOST && REDIS_PORT) {
const redisProtocol = REDIS_TLS === 'true' ? 'rediss' : 'redis';
const auth = REDIS_USER && REDIS_PASSWORD ? `${REDIS_USER}:${REDIS_PASSWORD}@` : '';
return `${redisProtocol}://${auth}${REDIS_HOST}:${REDIS_PORT}`;
}
// If neither REDIS_URL nor required individual parameters are provided, log warning
console.warn(
'Redis is enabled but neither REDIS_URL nor REDIS_HOST/REDIS_PORT are configured. Redis will not be used.'
);
return undefined;
})(),
// OAuth 2.0
oauth: this.loadOAuthConfiguration()