diff --git a/CHANGELOG.md b/CHANGELOG.md index 3ec22cda08..6b03c037d9 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,7 +9,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Added -- Added discovery and metadata endpoints for Model Context Protocol (MCP) OAuth 2.1 (RFC 8414 & RFC 9728) +- Added discovery and metadata endpoints for Model Context Protocol (MCP) OAuth 2.1 (RFC 8414 & RFC 9728) (experimental) ### Changed diff --git a/apps/api/src/app/app.module.ts b/apps/api/src/app/app.module.ts index 639cd97e11..cd3d1e43da 100644 --- a/apps/api/src/app/app.module.ts +++ b/apps/api/src/app/app.module.ts @@ -147,10 +147,6 @@ import { PortfolioModule } from './portfolio/portfolio.module'; rootPath: join(__dirname, '..', 'client') }), ServeStaticModule.forRoot({ - exclude: [ - '/.well-known/oauth-authorization-server', - '/.well-known/oauth-protected-resource/*wildcard' - ], rootPath: join(__dirname, '..', 'client', '.well-known'), serveRoot: '/.well-known' }), diff --git a/apps/api/src/app/endpoints/oauth/oauth-metadata.controller.spec.ts b/apps/api/src/app/endpoints/oauth/oauth-metadata.controller.spec.ts index eec1ee7879..9c17cb393f 100644 --- a/apps/api/src/app/endpoints/oauth/oauth-metadata.controller.spec.ts +++ b/apps/api/src/app/endpoints/oauth/oauth-metadata.controller.spec.ts @@ -7,14 +7,15 @@ import { OAuthMetadataController } from './oauth-metadata.controller'; describe('OAuthMetadataController', () => { let configurationService: ConfigurationService; let controller: OAuthMetadataController; - - const mockConfig: Record = { - ENABLE_FEATURE_MCP: true, - ENABLE_FEATURE_MCP_OAUTH: true, - ROOT_URL: 'http://localhost:3333' - }; + let mockConfig: Record; beforeEach(() => { + mockConfig = { + ENABLE_FEATURE_MCP: true, + ENABLE_FEATURE_MCP_OAUTH: true, + ROOT_URL: 'http://localhost:3333' + }; + configurationService = { get: jest.fn((key: string) => { return mockConfig[key]; @@ -102,7 +103,6 @@ describe('OAuthMetadataController', () => { expect(result).toEqual({ authorization_endpoint: 'http://localhost:3333/oauth/authorize', - authorization_response_iss_parameter_supported: true, client_id_metadata_document_supported: true, code_challenge_methods_supported: ['S256'], grant_types_supported: ['authorization_code', 'refresh_token'], @@ -122,7 +122,6 @@ describe('OAuthMetadataController', () => { expect(result).toEqual({ authorization_endpoint: 'https://ghostfol.io/oauth/authorize', - authorization_response_iss_parameter_supported: true, client_id_metadata_document_supported: true, code_challenge_methods_supported: ['S256'], grant_types_supported: ['authorization_code', 'refresh_token'], diff --git a/apps/api/src/app/endpoints/oauth/oauth-metadata.controller.ts b/apps/api/src/app/endpoints/oauth/oauth-metadata.controller.ts index 76635bf5e7..c7b681f3f8 100644 --- a/apps/api/src/app/endpoints/oauth/oauth-metadata.controller.ts +++ b/apps/api/src/app/endpoints/oauth/oauth-metadata.controller.ts @@ -1,10 +1,13 @@ +import { + getMcpOAuthBaseUrl, + isMcpOAuthEnabled +} from '@ghostfolio/api/helper/mcp-oauth.helper'; import { ConfigurationService } from '@ghostfolio/api/services/configuration/configuration.service'; import { MCP_ENDPOINT } from '@ghostfolio/common/config'; import { Controller, Get, - Header, NotFoundException, VERSION_NEUTRAL, Version @@ -16,21 +19,14 @@ export class OAuthMetadataController { private readonly configurationService: ConfigurationService ) {} - @Get('oauth-protected-resource/mcp') - @Header('Cache-Control', 'public, max-age=3600') + @Get(`oauth-protected-resource${MCP_ENDPOINT}`) @Version(VERSION_NEUTRAL) public getProtectedResourceMetadata() { - if ( - !this.configurationService.get('ENABLE_FEATURE_MCP') || - !this.configurationService.get('ENABLE_FEATURE_MCP_OAUTH') - ) { + if (!isMcpOAuthEnabled(this.configurationService)) { throw new NotFoundException(); } - const rootUrl = (this.configurationService.get('ROOT_URL') || '').replace( - /\/+$/, - '' - ); + const rootUrl = getMcpOAuthBaseUrl(this.configurationService); return { authorization_servers: [rootUrl], @@ -41,24 +37,16 @@ export class OAuthMetadataController { } @Get('oauth-authorization-server') - @Header('Cache-Control', 'public, max-age=3600') @Version(VERSION_NEUTRAL) public getAuthorizationServerMetadata() { - if ( - !this.configurationService.get('ENABLE_FEATURE_MCP') || - !this.configurationService.get('ENABLE_FEATURE_MCP_OAUTH') - ) { + if (!isMcpOAuthEnabled(this.configurationService)) { throw new NotFoundException(); } - const rootUrl = (this.configurationService.get('ROOT_URL') || '').replace( - /\/+$/, - '' - ); + const rootUrl = getMcpOAuthBaseUrl(this.configurationService); return { authorization_endpoint: `${rootUrl}/oauth/authorize`, - authorization_response_iss_parameter_supported: true, client_id_metadata_document_supported: true, code_challenge_methods_supported: ['S256'], grant_types_supported: ['authorization_code', 'refresh_token'], diff --git a/apps/api/src/helper/mcp-oauth.helper.spec.ts b/apps/api/src/helper/mcp-oauth.helper.spec.ts new file mode 100644 index 0000000000..287baf3fc1 --- /dev/null +++ b/apps/api/src/helper/mcp-oauth.helper.spec.ts @@ -0,0 +1,69 @@ +import { ConfigurationService } from '@ghostfolio/api/services/configuration/configuration.service'; + +import { + getMcpOAuthBaseUrl, + getMcpProtectedResourceMetadataUrl, + isMcpOAuthEnabled +} from './mcp-oauth.helper'; + +describe('mcp-oauth.helper', () => { + let configurationService: ConfigurationService; + let mockConfig: Record; + + beforeEach(() => { + mockConfig = { + ENABLE_FEATURE_MCP: true, + ENABLE_FEATURE_MCP_OAUTH: true, + ROOT_URL: 'http://localhost:3333' + }; + + configurationService = { + get: jest.fn((key: string) => { + return mockConfig[key]; + }) + } as unknown as ConfigurationService; + }); + + describe('getMcpOAuthBaseUrl', () => { + it('returns the root URL without trailing slashes', () => { + mockConfig.ROOT_URL = 'https://ghostfol.io///'; + + expect(getMcpOAuthBaseUrl(configurationService)).toBe( + 'https://ghostfol.io' + ); + }); + }); + + describe('getMcpProtectedResourceMetadataUrl', () => { + it('returns the full protected resource metadata URL', () => { + mockConfig.ROOT_URL = 'http://localhost:3333/'; + + expect(getMcpProtectedResourceMetadataUrl(configurationService)).toBe( + 'http://localhost:3333/.well-known/oauth-protected-resource/mcp' + ); + }); + }); + + describe('isMcpOAuthEnabled', () => { + it('returns true when both ENABLE_FEATURE_MCP and ENABLE_FEATURE_MCP_OAUTH are true', () => { + mockConfig.ENABLE_FEATURE_MCP = true; + mockConfig.ENABLE_FEATURE_MCP_OAUTH = true; + + expect(isMcpOAuthEnabled(configurationService)).toBe(true); + }); + + it('returns false when ENABLE_FEATURE_MCP is false', () => { + mockConfig.ENABLE_FEATURE_MCP = false; + mockConfig.ENABLE_FEATURE_MCP_OAUTH = true; + + expect(isMcpOAuthEnabled(configurationService)).toBe(false); + }); + + it('returns false when ENABLE_FEATURE_MCP_OAUTH is false', () => { + mockConfig.ENABLE_FEATURE_MCP = true; + mockConfig.ENABLE_FEATURE_MCP_OAUTH = false; + + expect(isMcpOAuthEnabled(configurationService)).toBe(false); + }); + }); +}); diff --git a/apps/api/src/helper/mcp-oauth.helper.ts b/apps/api/src/helper/mcp-oauth.helper.ts new file mode 100644 index 0000000000..69b6eda53d --- /dev/null +++ b/apps/api/src/helper/mcp-oauth.helper.ts @@ -0,0 +1,31 @@ +import { ConfigurationService } from '@ghostfolio/api/services/configuration/configuration.service'; +import { MCP_ENDPOINT } from '@ghostfolio/common/config'; + +/** + * Gives the root URL without a trailing slash. The issuer and the resource in + * the metadata have to be identical to the URL which the client uses. + */ +export function getMcpOAuthBaseUrl(configurationService: ConfigurationService) { + return configurationService.get('ROOT_URL').replace(/\/+$/, ''); +} + +/** + * Gives the URL of the protected resource metadata (RFC 9728) which the + * challenge of the authorization middleware names + */ +export function getMcpProtectedResourceMetadataUrl( + configurationService: ConfigurationService +) { + return `${getMcpOAuthBaseUrl(configurationService)}/.well-known/oauth-protected-resource${MCP_ENDPOINT}`; +} + +/** + * Tells if the authorization with OAuth 2.1 is enabled. It depends on the + * server of the Model Context Protocol (MCP). + */ +export function isMcpOAuthEnabled(configurationService: ConfigurationService) { + return ( + configurationService.get('ENABLE_FEATURE_MCP') && + configurationService.get('ENABLE_FEATURE_MCP_OAUTH') + ); +} diff --git a/apps/api/src/middlewares/mcp-authorization.middleware.spec.ts b/apps/api/src/middlewares/mcp-authorization.middleware.spec.ts index f69e4b1d84..1d8fe63860 100644 --- a/apps/api/src/middlewares/mcp-authorization.middleware.spec.ts +++ b/apps/api/src/middlewares/mcp-authorization.middleware.spec.ts @@ -11,19 +11,20 @@ import { createMcpAuthorizationMiddleware } from './mcp-authorization.middleware describe('createMcpAuthorizationMiddleware', () => { let configurationService: ConfigurationService; let impersonationService: ImpersonationService; - let userService: UserService; + let mockConfig: Record; let mockRequest: Partial; let mockResponse: Partial; let nextFunction: jest.Mock; - - const mockConfig: Record = { - ENABLE_FEATURE_MCP: true, - ENABLE_FEATURE_MCP_OAUTH: false, - ENABLE_FEATURE_SUBSCRIPTION: false, - ROOT_URL: 'http://localhost:3333' - }; + let userService: UserService; beforeEach(() => { + mockConfig = { + ENABLE_FEATURE_MCP: true, + ENABLE_FEATURE_MCP_OAUTH: false, + ENABLE_FEATURE_SUBSCRIPTION: false, + ROOT_URL: 'http://localhost:3333' + }; + configurationService = { get: jest.fn((key: string) => { return mockConfig[key]; diff --git a/apps/api/src/middlewares/mcp-authorization.middleware.ts b/apps/api/src/middlewares/mcp-authorization.middleware.ts index 8b99083d12..569c63a50c 100644 --- a/apps/api/src/middlewares/mcp-authorization.middleware.ts +++ b/apps/api/src/middlewares/mcp-authorization.middleware.ts @@ -1,12 +1,12 @@ import { getAccessIdOfBearerToken } from '@ghostfolio/api/helper/bearer-token.helper'; +import { + getMcpProtectedResourceMetadataUrl, + isMcpOAuthEnabled +} from '@ghostfolio/api/helper/mcp-oauth.helper'; import { ConfigurationService } from '@ghostfolio/api/services/configuration/configuration.service'; import { ImpersonationService } from '@ghostfolio/api/services/impersonation/impersonation.service'; import { UserService } from '@ghostfolio/api/services/user/user.service'; -import { - HEADER_KEY_TOKEN, - MCP_ENDPOINT, - MCP_REALM -} from '@ghostfolio/common/config'; +import { HEADER_KEY_TOKEN, MCP_REALM } from '@ghostfolio/common/config'; import { hasRole } from '@ghostfolio/common/permissions'; import type { ImpersonationContext } from '@ghostfolio/common/types'; @@ -115,17 +115,9 @@ function sendUnauthorizedResponse({ }) { const parameters = [`realm="${MCP_REALM}"`]; - if ( - configurationService.get('ENABLE_FEATURE_MCP') && - configurationService.get('ENABLE_FEATURE_MCP_OAUTH') - ) { - const rootUrl = (configurationService.get('ROOT_URL') || '').replace( - /\/+$/, - '' - ); - + if (isMcpOAuthEnabled(configurationService)) { parameters.push( - `resource_metadata="${rootUrl}/.well-known/oauth-protected-resource${MCP_ENDPOINT}"` + `resource_metadata="${getMcpProtectedResourceMetadataUrl(configurationService)}"` ); } diff --git a/apps/api/src/services/configuration/configuration.service.ts b/apps/api/src/services/configuration/configuration.service.ts index 056db13edc..ea2312d5bf 100644 --- a/apps/api/src/services/configuration/configuration.service.ts +++ b/apps/api/src/services/configuration/configuration.service.ts @@ -75,6 +75,8 @@ export class ConfigurationService { // TODO: Change the default to true, when the server of the Model Context // Protocol (MCP) was moved from experimental to general availability ENABLE_FEATURE_MCP: bool({ default: false }), + // TODO: Change the default to true, when the authorization with OAuth 2.1 + // was moved from experimental to general availability ENABLE_FEATURE_MCP_OAUTH: bool({ default: false }), ENABLE_FEATURE_RATE_LIMITING: bool({ default: false }), ENABLE_FEATURE_READ_ONLY_MODE: bool({ default: false }),