Browse Source

refactor(api): use mcp sdk metadata utilities and guard html template middleware

pull/8401/head
KenTandrian 3 weeks ago
parent
commit
b6b09a2014
  1. 2
      CHANGELOG.md
  2. 2
      apps/api/src/app/endpoints/oauth/oauth-metadata.controller.spec.ts
  3. 32
      apps/api/src/app/endpoints/oauth/oauth-metadata.controller.ts
  4. 29
      apps/api/src/helper/mcp-oauth.helper.spec.ts
  5. 38
      apps/api/src/helper/mcp-oauth.helper.ts
  6. 1
      apps/api/src/middlewares/html-template.middleware.ts

2
CHANGELOG.md

@ -9,7 +9,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Added ### 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 ### Changed

2
apps/api/src/app/endpoints/oauth/oauth-metadata.controller.spec.ts

@ -53,7 +53,6 @@ describe('OAuthMetadataController', () => {
expect(result).toEqual({ expect(result).toEqual({
authorization_servers: ['http://localhost:3333'], authorization_servers: ['http://localhost:3333'],
bearer_methods_supported: ['header'],
resource: 'http://localhost:3333/mcp', resource: 'http://localhost:3333/mcp',
resource_name: 'Ghostfolio' resource_name: 'Ghostfolio'
}); });
@ -68,7 +67,6 @@ describe('OAuthMetadataController', () => {
expect(result).toEqual({ expect(result).toEqual({
authorization_servers: ['https://ghostfol.io'], authorization_servers: ['https://ghostfol.io'],
bearer_methods_supported: ['header'],
resource: 'https://ghostfol.io/mcp', resource: 'https://ghostfol.io/mcp',
resource_name: 'Ghostfolio' resource_name: 'Ghostfolio'
}); });

32
apps/api/src/app/endpoints/oauth/oauth-metadata.controller.ts

@ -1,10 +1,12 @@
import { import {
getMcpOAuthBaseUrl, getMcpAuthorizationServerMetadata,
getMcpResourceUrl,
isMcpOAuthEnabled isMcpOAuthEnabled
} from '@ghostfolio/api/helper/mcp-oauth.helper'; } from '@ghostfolio/api/helper/mcp-oauth.helper';
import { ConfigurationService } from '@ghostfolio/api/services/configuration/configuration.service'; import { ConfigurationService } from '@ghostfolio/api/services/configuration/configuration.service';
import { MCP_ENDPOINT } from '@ghostfolio/common/config'; import { MCP_ENDPOINT } from '@ghostfolio/common/config';
import { buildOAuthProtectedResourceMetadata } from '@modelcontextprotocol/server';
import { import {
Controller, Controller,
Get, Get,
@ -26,14 +28,13 @@ export class OAuthMetadataController {
throw new NotFoundException(); throw new NotFoundException();
} }
const rootUrl = getMcpOAuthBaseUrl(this.configurationService); return buildOAuthProtectedResourceMetadata({
oauthMetadata: getMcpAuthorizationServerMetadata(
return { this.configurationService
authorization_servers: [rootUrl], ),
bearer_methods_supported: ['header'], resourceName: 'Ghostfolio',
resource: `${rootUrl}${MCP_ENDPOINT}`, resourceServerUrl: getMcpResourceUrl(this.configurationService)
resource_name: 'Ghostfolio' });
};
} }
@Get('oauth-authorization-server') @Get('oauth-authorization-server')
@ -43,17 +44,6 @@ export class OAuthMetadataController {
throw new NotFoundException(); throw new NotFoundException();
} }
const rootUrl = getMcpOAuthBaseUrl(this.configurationService); return getMcpAuthorizationServerMetadata(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']
};
} }
} }

29
apps/api/src/helper/mcp-oauth.helper.spec.ts

@ -1,8 +1,10 @@
import { ConfigurationService } from '@ghostfolio/api/services/configuration/configuration.service'; import { ConfigurationService } from '@ghostfolio/api/services/configuration/configuration.service';
import { import {
getMcpAuthorizationServerMetadata,
getMcpOAuthBaseUrl, getMcpOAuthBaseUrl,
getMcpProtectedResourceMetadataUrl, getMcpProtectedResourceMetadataUrl,
getMcpResourceUrl,
isMcpOAuthEnabled isMcpOAuthEnabled
} from './mcp-oauth.helper'; } from './mcp-oauth.helper';
@ -24,6 +26,23 @@ describe('mcp-oauth.helper', () => {
} as unknown as ConfigurationService; } 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', () => { describe('getMcpOAuthBaseUrl', () => {
it('returns the root URL without trailing slashes', () => { it('returns the root URL without trailing slashes', () => {
mockConfig.ROOT_URL = 'https://ghostfol.io///'; 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', () => { describe('isMcpOAuthEnabled', () => {
it('returns true when both ENABLE_FEATURE_MCP and ENABLE_FEATURE_MCP_OAUTH are true', () => { it('returns true when both ENABLE_FEATURE_MCP and ENABLE_FEATURE_MCP_OAUTH are true', () => {
mockConfig.ENABLE_FEATURE_MCP = true; mockConfig.ENABLE_FEATURE_MCP = true;

38
apps/api/src/helper/mcp-oauth.helper.ts

@ -1,6 +1,32 @@
import { ConfigurationService } from '@ghostfolio/api/services/configuration/configuration.service'; import { ConfigurationService } from '@ghostfolio/api/services/configuration/configuration.service';
import { MCP_ENDPOINT } from '@ghostfolio/common/config'; 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 * 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. * 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( export function getMcpProtectedResourceMetadataUrl(
configurationService: ConfigurationService 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}`);
} }
/** /**

1
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; const rootUrl = process.env.ROOT_URL || environment.rootUrl;
if ( if (
path.startsWith('/.well-known/') ||
path.startsWith('/api/') || path.startsWith('/api/') ||
path.startsWith(STORYBOOK_PATH) || path.startsWith(STORYBOOK_PATH) ||
this.isFileRequest(path) || this.isFileRequest(path) ||

Loading…
Cancel
Save