This is the finished application of the tutorial Build an MCP server in Node.js in the NestJS documentation. The tutorial builds it step by step and explains every decision; this repository is the result, ready to clone.
An MCP server for a helpdesk, built with @nestjs/mcp, that lets a support team's AI assistant work with tickets:
- Two tools:
search-tickets(read-only) andreply-to-ticket, with Zod schemas written for the model and validation errors it can read. - Each ticket as a resource,
tickets://{ticketId}, with its whole thread, and adraft-replyprompt that embeds the ticket. - Streamable HTTP at
POST /mcp, as an OAuth 2.1 resource server: it accepts only JWT access tokens issued for it (issuer, audience,typ: at+jwt, signature checked against the provider's JWKS), and answers a request without one with a 401 that points at its protected resource metadata. - Every query scoped to the team named in the token: an agent of one team never reads or changes another team's tickets, whatever the model asks for. Replying needs the
tickets:writescope. - Requests from web pages on other sites, or with a
Hostit doesn't answer to, get 403. - The same tools over stdio (
node dist/stdio.js), for an assistant that launches the server locally, with the team taken fromHELPDESK_TEAM. - Tickets, replies and teams in PostgreSQL through Drizzle; every call reported to NestJS Observe once it has a key pair.
There is no identity provider here. scripts/dev-issuer.ts stands in for one on your machine: it holds a key pair, serves its public key as a JWKS, and signs access tokens for any agent, team and scopes you ask for. The tests use it too.
Warning: the development issuer gives a token to anyone who asks. It is for local use only: never deploy it. It refuses to start with
NODE_ENV=production, listens on the loopback interface only, and lives inscripts/, which the build doesn't compile. It is not an authorization server either: it has no sign-in page, so clients that sign users in through OAuth can't use it.
You need Node.js 22.18 or later (the development issuer runs TypeScript directly), and Docker for PostgreSQL.
git clone https://github.com/nestjs/example-mcp-server.git
cd example-mcp-server
npm install
cp .env.example .env
docker compose up -d
npm run db:migrate
npm run build
npm run db:seed # two teams, four tickets; it empties the tables firstStart the development issuer in one terminal. It serves its keys at http://localhost:3001/jwks.json and tokens at POST http://localhost:3001/token:
npm run dev:issuerStart the server in another:
npm run startThe MCP endpoint is http://localhost:3000/mcp. Get a token for Ada, of the billing team, and search her team's tickets:
TOKEN=$(curl -s localhost:3001/token -d username=ada -d team=billing -d 'scope=tickets:read tickets:write' | jq -r .access_token)
curl localhost:3000/mcp -H 'content-type: application/json' -H 'accept: application/json, text/event-stream' \
-H "Authorization: Bearer $TOKEN" \
-d '{"jsonrpc":"2.0","id":1,"method":"tools/call","params":{"name":"search-tickets","arguments":{"text":"refund"}}}'The tutorial's Try it section goes further with curl and the MCP Inspector: the 401 challenge, reading a ticket, replying, another team's agent, a read-only token. Connect an assistant shows how to add the server to Claude Code, Cursor, VS Code and other clients, over HTTP with the token as a header, or as a stdio command:
{
"mcpServers": {
"helpdesk": {
"command": "node",
"args": ["/path/to/example-mcp-server/dist/stdio.js"],
"env": { "HELPDESK_TEAM": "billing" }
}
}
}Use node with the built file's absolute path, not npm run, which prints its own lines to stdout, the protocol's channel. The rest of the configuration comes from the project's .env.
In production, point the configuration at your identity provider instead of the development issuer (see Use a real identity provider):
MCP_RESOURCE: the server's publichttpsURL, such ashttps://helpdesk.example.com/mcp. Create an API in the provider whose identifier is this URL, with the scopestickets:readandtickets:write, and a claim for the team.OIDC_ISSUER: the exactissof the provider's access tokens.OIDC_JWKS_URL: thejwks_uriof its/.well-known/openid-configurationdocument.OIDC_AUDIENCE: the tokens'aud, only when the provider can't make itMCP_RESOURCE(Entra ID puts the API's client ID there).OIDC_TOKEN_TYPE: thetypheader of its access tokens,at+jwtby default; set it empty for a provider that sends none (Entra ID, Okta).OIDC_TEAM_CLAIM: the claim that names the agent's team,teamby default.MCP_ALLOWED_HOSTS: the hostnames clients reach the server by.
The tutorial has a table of the differences between Auth0, Microsoft Entra ID, Okta and WorkOS AuthKit. It summarizes each provider's documentation and wasn't run against them: decode one access token from your provider and compare it with the configuration before you deploy. With a real provider, most MCP clients sign the user in themselves, from the 401 and the metadata.
| Operation | Section |
|---|---|
Tools search-tickets and reply-to-ticket |
Write tools for the model |
Resource template tickets://{ticketId}, prompt draft-reply |
Serve tickets as resources |
POST /mcp, GET /.well-known/oauth-protected-resource/mcp |
Verify OAuth access tokens |
node dist/stdio.js |
Serve the same tools over stdio |
GET /jwks.json, POST /token on the development issuer |
Get a token for development |
| Path | What it holds |
|---|---|
src/main.ts |
the HTTP server on the Node adapter: JSON logs in production, security headers, shutdown hooks |
src/stdio.ts |
the stdio server, from an application context, with every log line on stderr |
src/app.module.ts |
NestJS Observe, config, database, and McpModule with its HTTP and auth options |
src/config.ts |
the environment's schema, checked at startup |
src/database/ |
the Drizzle tables (schema.ts) and the development seed (seed.ts) |
src/auth/ |
AccessTokenVerifier: verifies the provider's access tokens and reads the scopes and the team |
src/tickets/ |
the tools, resource template and prompt (tickets.tools.ts), the team-scoped queries (tickets.service.ts), and @CurrentAgent() |
scripts/dev-issuer.ts |
the development issuer, for your machine and the tests only |
test/ |
e2e tests on PostgreSQL: in memory, over HTTP, and over stdio |
drizzle/ |
migrations |
TEST_DATABASE_URL=postgres://postgres:helpdesk@localhost:5432/helpdesk_test npm run test:e2edocker compose creates the helpdesk_test database. The tests truncate its tables, so never point them at your development database. The script builds first, because the stdio tests launch dist/stdio.js, which reads the rest of its configuration from .env: copy .env.example to .env before running them. They serve the development issuer's keys on 127.0.0.1:3162 and the application on 127.0.0.1:3163.
NestJS 12 on the Node adapter, @nestjs/mcp on the MCP TypeScript SDK, Zod, @nestjs/config, JwtVerifier from @nestjs/authentication, Drizzle and PostgreSQL, NestJS Observe, and Vitest.
Read the tutorial's production checklist first: https://docs.nestjs.com/tutorials/mcp-server#production-checklist. Never deploy the development issuer, or a token endpoint like it.
NestJS Observe is wired in src/app.module.ts and sends nothing until it has a key pair. npx @nestjs/observe link writes the key pair (OBSERVE_APP_KEY, OBSERVE_APP_SECRET) into .env; in production, set them in the platform's environment, since start:prod doesn't load .env. Observe then records each MCP call under its operation, such as tools/call search-tickets: https://docs.nestjs.com/observability/overview. For the stdio server, add the key pair to the client's env, since the SDK reads it before .env is loaded.
MIT