From b6b09a2014275e9c014d555edd8db0ad4b535704 Mon Sep 17 00:00:00 2001 From: KenTandrian Date: Thu, 5 Nov 2026 14:19:47 +0000 Subject: [PATCH] refactor(api): use mcp sdk metadata utilities and guard html template middleware --- CHANGELOG.md | 2 +- .../oauth/oauth-metadata.controller.spec.ts | 2 - .../oauth/oauth-metadata.controller.ts | 32 ++++++---------- apps/api/src/helper/mcp-oauth.helper.spec.ts | 29 ++++++++++++++ apps/api/src/helper/mcp-oauth.helper.ts | 38 ++++++++++++++++++- .../middlewares/html-template.middleware.ts | 1 + 6 files changed, 79 insertions(+), 25 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 6b03c037d9..b451973e92 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) (experimental) +- Added the discovery endpoints of OAuth 2.1 to the server of the Model Context Protocol (MCP) (experimental) ### Changed 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 9c17cb393f..6cc078fbf9 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 @@ -53,7 +53,6 @@ describe('OAuthMetadataController', () => { expect(result).toEqual({ authorization_servers: ['http://localhost:3333'], - bearer_methods_supported: ['header'], resource: 'http://localhost:3333/mcp', resource_name: 'Ghostfolio' }); @@ -68,7 +67,6 @@ describe('OAuthMetadataController', () => { expect(result).toEqual({ authorization_servers: ['https://ghostfol.io'], - bearer_methods_supported: ['header'], resource: 'https://ghostfol.io/mcp', resource_name: 'Ghostfolio' }); 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 c7b681f3f8..b91855ba48 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,12 @@ import { - getMcpOAuthBaseUrl, + getMcpAuthorizationServerMetadata, + getMcpResourceUrl, 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 { buildOAuthProtectedResourceMetadata } from '@modelcontextprotocol/server'; import { Controller, Get, @@ -26,14 +28,13 @@ export class OAuthMetadataController { throw new NotFoundException(); } - const rootUrl = getMcpOAuthBaseUrl(this.configurationService); - - return { - authorization_servers: [rootUrl], - bearer_methods_supported: ['header'], - resource: `${rootUrl}${MCP_ENDPOINT}`, - resource_name: 'Ghostfolio' - }; + return buildOAuthProtectedResourceMetadata({ + oauthMetadata: getMcpAuthorizationServerMetadata( + this.configurationService + ), + resourceName: 'Ghostfolio', + resourceServerUrl: getMcpResourceUrl(this.configurationService) + }); } @Get('oauth-authorization-server') @@ -43,17 +44,6 @@ export class OAuthMetadataController { throw new NotFoundException(); } - const rootUrl = getMcpOAuthBaseUrl(this.configurationService); - - return { - authorization_endpoint: `${rootUrl}/oauth/authorize`, - client_id_metadata_document_supported: true, - code_challenge_methods_supported: ['S256'], - grant_types_supported: ['authorization_code', 'refresh_token'], - issuer: rootUrl, - response_types_supported: ['code'], - token_endpoint: `${rootUrl}/api/v1/oauth/token`, - token_endpoint_auth_methods_supported: ['none'] - }; + return getMcpAuthorizationServerMetadata(this.configurationService); } } diff --git a/apps/api/src/helper/mcp-oauth.helper.spec.ts b/apps/api/src/helper/mcp-oauth.helper.spec.ts index 287baf3fc1..7e7b57c123 100644 --- a/apps/api/src/helper/mcp-oauth.helper.spec.ts +++ b/apps/api/src/helper/mcp-oauth.helper.spec.ts @@ -1,8 +1,10 @@ import { ConfigurationService } from '@ghostfolio/api/services/configuration/configuration.service'; import { + getMcpAuthorizationServerMetadata, getMcpOAuthBaseUrl, getMcpProtectedResourceMetadataUrl, + getMcpResourceUrl, isMcpOAuthEnabled } from './mcp-oauth.helper'; @@ -24,6 +26,23 @@ describe('mcp-oauth.helper', () => { } as unknown as ConfigurationService; }); + describe('getMcpAuthorizationServerMetadata', () => { + it('returns RFC 8414 authorization server metadata', () => { + mockConfig.ROOT_URL = 'http://localhost:3333/'; + + expect(getMcpAuthorizationServerMetadata(configurationService)).toEqual({ + authorization_endpoint: 'http://localhost:3333/oauth/authorize', + client_id_metadata_document_supported: true, + code_challenge_methods_supported: ['S256'], + grant_types_supported: ['authorization_code', 'refresh_token'], + issuer: 'http://localhost:3333', + response_types_supported: ['code'], + token_endpoint: 'http://localhost:3333/api/v1/oauth/token', + token_endpoint_auth_methods_supported: ['none'] + }); + }); + }); + describe('getMcpOAuthBaseUrl', () => { it('returns the root URL without trailing slashes', () => { mockConfig.ROOT_URL = 'https://ghostfol.io///'; @@ -44,6 +63,16 @@ describe('mcp-oauth.helper', () => { }); }); + describe('getMcpResourceUrl', () => { + it('returns the resource URL as a URL instance', () => { + mockConfig.ROOT_URL = 'http://localhost:3333/'; + + expect(getMcpResourceUrl(configurationService)).toEqual( + new URL('http://localhost:3333/mcp') + ); + }); + }); + describe('isMcpOAuthEnabled', () => { it('returns true when both ENABLE_FEATURE_MCP and ENABLE_FEATURE_MCP_OAUTH are true', () => { mockConfig.ENABLE_FEATURE_MCP = true; diff --git a/apps/api/src/helper/mcp-oauth.helper.ts b/apps/api/src/helper/mcp-oauth.helper.ts index 69b6eda53d..18bbbd22c2 100644 --- a/apps/api/src/helper/mcp-oauth.helper.ts +++ b/apps/api/src/helper/mcp-oauth.helper.ts @@ -1,6 +1,32 @@ import { ConfigurationService } from '@ghostfolio/api/services/configuration/configuration.service'; import { MCP_ENDPOINT } from '@ghostfolio/common/config'; +import { + getOAuthProtectedResourceMetadataUrl, + OAuthMetadata +} from '@modelcontextprotocol/server'; + +/** + * Gives the metadata of the authorization server (RFC 8414). The protected + * resource metadata (RFC 9728) names its issuer. + */ +export function getMcpAuthorizationServerMetadata( + configurationService: ConfigurationService +): OAuthMetadata { + const rootUrl = getMcpOAuthBaseUrl(configurationService); + + return { + authorization_endpoint: `${rootUrl}/oauth/authorize`, + client_id_metadata_document_supported: true, + code_challenge_methods_supported: ['S256'], + grant_types_supported: ['authorization_code', 'refresh_token'], + issuer: rootUrl, + response_types_supported: ['code'], + token_endpoint: `${rootUrl}/api/v1/oauth/token`, + token_endpoint_auth_methods_supported: ['none'] + }; +} + /** * 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. @@ -16,7 +42,17 @@ export function getMcpOAuthBaseUrl(configurationService: ConfigurationService) { export function getMcpProtectedResourceMetadataUrl( configurationService: ConfigurationService ) { - return `${getMcpOAuthBaseUrl(configurationService)}/.well-known/oauth-protected-resource${MCP_ENDPOINT}`; + return getOAuthProtectedResourceMetadataUrl( + getMcpResourceUrl(configurationService) + ); +} + +/** + * Gives the URL of the server of the Model Context Protocol (MCP), which is the + * resource of the protected resource metadata (RFC 9728) + */ +export function getMcpResourceUrl(configurationService: ConfigurationService) { + return new URL(`${getMcpOAuthBaseUrl(configurationService)}${MCP_ENDPOINT}`); } /** diff --git a/apps/api/src/middlewares/html-template.middleware.ts b/apps/api/src/middlewares/html-template.middleware.ts index e148e8e255..0b064df946 100644 --- a/apps/api/src/middlewares/html-template.middleware.ts +++ b/apps/api/src/middlewares/html-template.middleware.ts @@ -215,6 +215,7 @@ export class HtmlTemplateMiddleware implements NestMiddleware { const rootUrl = process.env.ROOT_URL || environment.rootUrl; if ( + path.startsWith('/.well-known/') || path.startsWith('/api/') || path.startsWith(STORYBOOK_PATH) || this.isFileRequest(path) ||