Files
TREK/server/src/nest/platform/discovery-metadata.service.ts
T
Maurice b64da26651 feat(assistant): trip assistant chat with your own AI connection
Adds an in-app assistant that reads and plans the open trip. Users connect
their own provider (Sign in with ChatGPT, or an Anthropic, OpenAI, Gemini or
OpenRouter key) or use the one the admin set up, including a custom
OpenAI-compatible server. It is an addon and only shows while a connection
is usable.

The chat runs on the existing MCP tools, pinned to the trip. Every change is
held as a change card the user applies or discards, and applied changes can
be undone. On the desktop it is a floating window that can be moved and
resized; on the phone it is fullscreen. Search results show as place cards
that can go onto a day, weather lookups as a strip, proposed places as
ghost markers on the map with a before/after switch, and an answer can be
shared into the trip chat. Booking files can be dropped in and are read
into change cards.
2026-10-22 21:30:12 +00:00

56 lines
2.3 KiB
TypeScript

import { getMcpSafeUrl } from '../../app-config';
import { ALL_SCOPES } from '../../mcp/scopes';
import { mcpAuthMetadataRouter } from '@modelcontextprotocol/sdk/server/auth/router';
import type { OAuthMetadata } from '@modelcontextprotocol/sdk/shared/auth';
import { Injectable } from '@nestjs/common';
import type express from 'express';
/**
* OAuth/MCP discovery metadata, built lazily on first request so the issuer URL
* is resolved from the live env, not frozen at buildApp() time. One instance per
* app (DI singleton) replaces the per-applyPlatformTransport-call closures the
* pre-init Express mount used — same laziness, same per-app cache scope.
*
* mcpAuthMetadataRouter serves:
* /.well-known/oauth-authorization-server — RFC 8414 AS metadata
* /.well-known/oauth-protected-resource/mcp — RFC 9728 path-based PRM (fixes issue #959 bug 1)
*/
@Injectable()
export class DiscoveryMetadataService {
private oauthMetadata: OAuthMetadata | null = null;
private sdkMetaRouter: express.Router | null = null;
getOAuthMetadata(): OAuthMetadata {
if (this.oauthMetadata) return this.oauthMetadata;
const base = getMcpSafeUrl().replace(/(?<!\/)\/+$/, '');
this.oauthMetadata = {
issuer: base,
authorization_endpoint: `${base}/oauth/authorize`,
token_endpoint: `${base}/oauth/token`,
revocation_endpoint: `${base}/oauth/revoke`,
registration_endpoint: `${base}/oauth/register`,
response_types_supported: ['code'],
grant_types_supported: ['authorization_code', 'refresh_token', 'client_credentials'],
code_challenge_methods_supported: ['S256'],
token_endpoint_auth_methods_supported: ['client_secret_post', 'none'],
scopes_supported: ALL_SCOPES,
// An https client_id is resolved as its metadata document (nest/oauth/oauth-cimd.ts).
client_id_metadata_document_supported: true,
};
return this.oauthMetadata;
}
getMetaRouter(): express.Router {
if (this.sdkMetaRouter) return this.sdkMetaRouter;
const metadata = this.getOAuthMetadata();
this.sdkMetaRouter = mcpAuthMetadataRouter({
oauthMetadata: metadata,
resourceServerUrl: new URL(`${metadata.issuer}/mcp`),
scopesSupported: ALL_SCOPES as string[],
resourceName: 'TREK MCP',
});
return this.sdkMetaRouter;
}
}