Protecting the routes
Decide who may open the Telescope console, its API, its MCP tools and /metrics with one authorize option.
Telescope has no login of its own. Sign-in stays your app's job (for example with @nestjs/authentication). The console only asks one question, through the authorize option: may this request use it?
authorize
import { type ExecutionContext, Module } from '@nestjs/common';
import { TelescopeModule } from '@dudousxd/nestjs-telescope';
import { TelescopeUiModule } from '@dudousxd/nestjs-telescope-ui';
const isAdmin = (ctx: ExecutionContext) =>
ctx.switchToHttp().getRequest().user?.isAdmin === true;
@Module({
imports: [
TelescopeModule.forRoot({ authorize: isAdmin }),
TelescopeUiModule.forRoot({ authorize: isAdmin }),
],
})
export class AppModule {}authorize runs after your app's authentication (global guards and the decorators you pass), so request.user is set. It takes one of five forms:
// 1. A function
authorize: (ctx) => ctx.switchToHttp().getRequest().user?.isAdmin === true,
// 2. An @Injectable() class (constructor injection works)
authorize: ConsoleAccess,
// 3. A lazy import of such a class (default export)
authorize: () => import('./console-access.js'),
// 4. An @nestjs/authorization policy
authorize: { policy: ConsolePolicy, ability: 'view' },
// 5. A gate name registered with @dudousxd/nestjs-authz
authorize: 'console.telescope',The class form:
import { type ExecutionContext, Injectable } from '@nestjs/common';
import type { ConsoleAuthorizer } from '@dudousxd/nestjs-telescope';
@Injectable()
export default class ConsoleAccess implements ConsoleAuthorizer {
constructor(private readonly audit: AuditService) {}
async authorize(ctx: ExecutionContext) {
const user = ctx.switchToHttp().getRequest().user;
if (!user) return { redirectTo: '/login' };
await this.audit.log('telescope.open', user.id);
return user.roles.includes('admin');
}
}A registered provider is used as is; otherwise Telescope builds one instance with its dependencies injected. The policy form needs @nestjs/authorization installed and AuthorizationModule.forRoot() imported, and the gate-name form needs AuthzModule from @dudousxd/nestjs-authz; without them the app fails at boot with a message saying so.
What the check returns
| Return | API call | Page navigation (UI) |
|---|---|---|
true or { allowed: true } | allowed | allowed |
false or { allowed: false } | 403 { "error": "forbidden" } | 403 "Access denied" page |
{ allowed: false, reason: 'unauthenticated' } | 401 { "error": "unauthenticated" } | 401 "Sign in required" page |
{ redirectTo: '/login' } | 401 { "error": "unauthenticated", "redirectTo": "/login" } | 302 to /login |
| throws | 403 | 403 |
The dashboard follows redirectTo from a 401, and shows an access-denied screen on a 403.
Where it applies
| Option | Covers |
|---|---|
TelescopeModule.forRoot({ authorize }) | the API (TelescopeController), the live feed (StreamController) and telescope's tools on the Aviary dev MCP endpoint (/mcp/aviary) |
TelescopeUiModule.forRoot({ authorize }) | the dashboard page and its assets |
TelescopeOtelMetricsModule.forExporter(exporter, { authorize }) | GET /metrics |
Pass the same authorize to TelescopeModule and TelescopeUiModule. The public client-error beacon takes none.
With forRootAsync, authorize can come from the factory:
TelescopeModule.forRootAsync({
inject: [ConfigService],
useFactory: (config: ConfigService) => ({
authorize: authorizeByToken({ token: config.get('TELESCOPE_TOKEN') }),
}),
});Tokens: authorizeByToken
For callers that send a header instead of a cookie (coding agents on the dev MCP endpoint, Prometheus on /metrics):
import { authorizeByToken } from '@dudousxd/nestjs-telescope';
TelescopeModule.forRoot({
authorize: authorizeByToken({ token: process.env.TELESCOPE_TOKEN }),
});
TelescopeOtelMetricsModule.forExporter(exporter, {
authorize: authorizeByToken({ token: process.env.METRICS_TOKEN }),
});It allows a request with Authorization: Bearer <token> and answers 401 otherwise. Only that header counts (no ?token= query string). An empty or missing token throws when the module is configured, so a missing env var never opens the console. authorizeByToken and the types are also exported by @dudousxd/nestjs-telescope-ui and @dudousxd/nestjs-telescope-otel.
A token or a session: anyAuthorize
To let either kind of caller in, combine the forms with anyAuthorize. A request is allowed when any form allows it. When none does, the answer is 401 if any form said unauthenticated (keeping the first redirectTo), else 403:
import { anyAuthorize, authorizeByToken } from '@dudousxd/nestjs-telescope';
TelescopeModule.forRoot({
authorize: anyAuthorize(
authorizeByToken({ token: process.env.TELESCOPE_TOKEN }),
{ policy: ConsolePolicy, ability: 'view' },
),
});Each form can be any authorize value: a function, a class, a lazy import, { policy, ability } or a gate name.
Without authorize
- Outside production (
NODE_ENV !== 'production') the console is open, so local work needs no setup. - In production every request (page, API, live feed, dev MCP tools,
/metrics) answers 403{ "error": "forbidden" }. - One warning is logged at boot per module, with
routes: falsetoo:
[@dudousxd/nestjs-telescope] console has no `authorize` configured: it is open outside production and denies every request in production. Set `authorize` in TelescopeModule.forRoot().decorators alone do not count: a console with decorators and no authorize still answers 403 in production.
decorators
decorators stays for what runs before authorize: authentication, throttling and the like. It is the same shape as @nestjs/mcp's decorators option:
TelescopeModule.forRoot({
decorators: [UseGuards(AuthenticationGuard)],
authorize: { policy: ConsolePolicy, ability: 'view' },
});With forRootAsync, decorators is a static top-level option next to path (the routes are built before any factory runs). Every mounted controller is marked open to @nestjs/authorization's deny-by-default global AuthorizationGuard (as Can.Anyone() would), so that guard doesn't lock the console out; authorize decides. A Can(...) you pass in decorators is still enforced.
The dashboard page is a full-page navigation: the browser sends your app's cookies, not an Authorization header. The check in front of TelescopeUiModule must read a cookie session. Coding agents on the dev MCP endpoint are the opposite: they send a header, so the authorize that gates telescope's tools there has to accept one.
Mount the routes yourself (routes: false)
TelescopeModule.forRoot({ routes: false }), TelescopeUiModule.forRoot({ routes: false }) and TelescopeOtelMetricsModule.forExporter(exporter, { routes: false }) mount no controller. Every provider keeps running: request capture, watchers, storage, pruning, alerts and the services. Extend the exported controllers and protect them with @UseConsoleAuthorize(), which applies the same authorize contract and denial responses. With no argument it runs TelescopeModule.forRoot({ authorize }):
import { Controller, Module, UseGuards } from '@nestjs/common';
import {
StreamController,
TelescopeController,
TelescopeModule,
UseConsoleAuthorize,
} from '@dudousxd/nestjs-telescope';
import { TelescopeUiController, TelescopeUiModule } from '@dudousxd/nestjs-telescope-ui';
@Controller('ops/telescope/api')
@UseConsoleAuthorize()
@UseGuards(AuthGuard)
class ConsoleApi extends TelescopeController {}
@Controller('ops/telescope/api')
@UseConsoleAuthorize()
@UseGuards(AuthGuard)
class ConsoleStream extends StreamController {}
@Controller('ops/telescope')
@UseConsoleAuthorize()
@UseGuards(AuthGuard)
class ConsolePage extends TelescopeUiController {}
@Module({
imports: [
// `path` is still where the console lives: the request-capture self-skip
// and the page's asset URLs use it.
TelescopeModule.forRoot({
path: 'ops/telescope',
routes: false,
authorize: { policy: ConsolePolicy, ability: 'view' },
}),
TelescopeUiModule.forRoot({ path: 'ops/telescope', routes: false }),
],
controllers: [ConsoleApi, ConsoleStream, ConsolePage],
})
export class OpsModule {}Put @UseConsoleAuthorize() above your authentication guard. Nest applies class decorators bottom-up, so the guard listed lower runs first: your authentication sets request.user, then authorize checks it.
@UseConsoleAuthorize({ authorize }) runs its own check instead of the module's. Without authorize anywhere, it follows the default. A subclass of TelescopeUiController answers denied page navigations with a redirect or an HTML page; mark a page controller of your own with @ConsolePage() for the same. ConsoleAuthorizeGuard (or ConsoleAuthorizeGuard.for({ authorize })) is just the guard, for @UseGuards(...); pair it with @UseFilters(ConsoleAuthorizeFilter) for the redirect and the HTML pages. Mount only what you use:
| Controller to extend | Mount at | Serves |
|---|---|---|
TelescopeController | <path>/api | the API the dashboard reads |
StreamController | <path>/api | the live SSE feed (/stream) |
ClientErrorController | <path>/api/client-errors | front-end error ingestion, only if clientErrors is enabled |
TelescopeUiController (-ui) | <path> | the dashboard page and its assets |
TelescopeOtelMetricsController (-otel) | anywhere | GET metrics (or call exporter.prometheus() from your own handler) |
List ClientErrorController before TelescopeController when you mount it, as TelescopeModule does. With forRootAsync, routes is a static top-level option next to path.
Why routes: false and not path: false like @nestjs/mcp: Telescope's path keeps a job when nothing is mounted (it tells request capture which URLs are the console's own, and rebases the page's asset URLs), so it stays a string and a separate routes flag turns the controllers off. The three modules use the same name.
Public by design: the client-error beacon
POST <path>/api/client-errors takes error reports from any visitor's browser, signed in or not. It is off until clientErrors.enabled (404 while off). Because guests must reach it, TelescopeModule mounts it without your decorators or authorize. It keeps its own abuse dampers: a byte cap (maxBodyBytes) and a per-IP rate limit (rateLimit). To put it behind your own auth, use routes: false and extend ClientErrorController.
Mutations
Queue retry / remove / promote / retry-all / redrive / enqueue, Prune now, request replay and on-demand profiling are mutations. They are a feature switched on with mutations: true; while it is off they answer 403 and the dashboard hides their buttons (meta.mutationsEnabled). Who may call them is decided by authorize, like every other route.
For a rule on mutations stricter than on reads, mount the routes yourself and override those methods with an extra requirement, for example a Can(...) from @nestjs/authorization. An override is a new function, so re-declare its route decorators:
@Controller('ops/telescope/api')
@UseConsoleAuthorize({ authorize: { policy: ConsolePolicy, ability: 'view' } })
class ConsoleApi extends TelescopeController {
@Post('retention/prune')
@HttpCode(200)
@Can(ConsolePolicy, 'operate')
override prune() {
return super.prune();
}
}Migrating
To Telescope 5: the MCP endpoint is gone
Telescope's own MCP endpoint (POST <path>/api/mcp) was removed, with mcp, mcpAuthorize, mcpDecorators, TelescopeMcpController and TelescopeMcpServer. The telescope_* tools stay on the Aviary dev MCP endpoint, /mcp/aviary, served by @dudousxd/nestjs-agent-mcp-server and gated by authorize; point your agent there (see MCP tools). If the agent used mcpAuthorize to sign in with a token, give authorize a form that accepts it, or sign the agent in on the dev endpoint (devTools: { decorators }).
| Removed | Now |
|---|---|
mcp: true, POST <path>/api/mcp | the telescope_* tools on /mcp/aviary |
mcpAuthorize | authorize (it gates the dev tools) |
mcpDecorators | AgentMcpServerModule.forRoot({ devTools: { decorators } }) |
TelescopeMcpController, TelescopeMcpServer, MCP_TOOLS, McpToolArgs | none |
To authorize (Telescope 3)
The production boot refusal is gone: a console with no authorize now boots and answers 403 in production, with one warning. decorators no longer counts as protection on its own; move the authorization part to authorize and keep authentication in decorators.
Before:
TelescopeModule.forRoot({
decorators: [UseGuards(AuthenticationGuard), Can(ConsolePolicy, 'view')],
mcpDecorators: [Authenticate({ providers: [TelescopeAgentKey] }), Can(ConsolePolicy, 'view')],
});
TelescopeOtelMetricsModule.forExporter(exporter, {
requireProtectionInProduction: true,
decorators: [UseGuards(BearerGuard)],
});After:
TelescopeModule.forRoot({
decorators: [UseGuards(AuthenticationGuard)],
authorize: { policy: ConsolePolicy, ability: 'view' },
});
TelescopeOtelMetricsModule.forExporter(exporter, {
authorize: authorizeByToken({ token: process.env.METRICS_TOKEN }),
});| Removed | Now |
|---|---|
the production boot refusal (refusing to mount undecorated routes with NODE_ENV=production) | authorize; omitted = 403 in production + a boot warning |
assertConsoleProtected, CONSOLE_DECORATORS_EXAMPLE, ConsoleProtectionHint | UseConsoleAuthorize / ConsoleAuthorizeGuard.for |
otel requireProtectionInProduction | authorize on TelescopeOtelMetricsModule.forExporter; /metrics now follows the console's default, so an unauthorized scrape answers 403 in production |
/metrics used to answer in production with no options. It now answers 403 there until you set authorize (for Prometheus, authorizeByToken plus authorization.credentials in the scrape config).
From Telescope 1
These options were removed in Telescope 2. The 2.x line starts at 2.0.1: the 2.0.0 on npm is an older build that was published by mistake and is deprecated, so don't install it.
| Old option | Now |
|---|---|
authorizer | authorize |
guards: [MyGuard] (core and UI) | decorators: [UseGuards(MyGuard)] on both modules |
dashboardAuth (login / session modes, unauthenticatedPage, the session cookie, /api/auth/*) | your app's own sign-in; authorize can return { redirectTo: '/login' } |
openTelescopeConsole, useOpenTelescopeConsole, <OpenTelescopeConsoleButton>, ./react/console | a plain link: telescopeConsoleUrl(basePath) from @dudousxd/nestjs-telescope-ui/client |
authorizeAction | mutations: true; a stricter rule via an overriding method with its own @Can(...) |
clientErrors.authorize | routes: false + your own ClientErrorController subclass |
mcp: { token } | the telescope_* tools on the Aviary dev MCP endpoint (/mcp/aviary), gated by authorize (see Telescope 5 above) |
| the built-in "open in dev, closed in production" gate | authorize, omitted = the same default |
AI usage and agent runs
The AI screen — spend by feature, model and payer, agent runs with their queue / model / tools split, and the calls nobody priced — plus the same numbers as Prometheus / OTLP metrics for Grafana.
Organizations
Show each console viewer only the data of the organizations they may see, when your app uses @dudousxd/nestjs-context tenancy.