Skip to main content
Glama
mcp-server-typescript-oauth

mcp-server-typescript-oauth

by gregale-dev
README.md
# Remote MCP server in TypeScript with OAuth 2.1

A remote [Model Context Protocol](https://modelcontextprotocol.io) server,
written with the official TypeScript SDK, that checks OAuth 2.1 access tokens
itself. Your authorization server, such as Auth0, WorkOS, Keycloak or your
own, signs users in and issues the tokens; this server does what the
[MCP authorization spec](https://modelcontextprotocol.io/specification/latest/basic/authorization)
asks of an OAuth resource server:

- **Publishes protected resource metadata** (RFC 9728) at
  `/.well-known/oauth-protected-resource/mcp`, so MCP clients know where to
  sign in.
- **Answers a call without a valid token with `401`** and a
  `WWW-Authenticate` header that points to that metadata.
- **Accepts only tokens issued for itself:** the signature, the issuer, the
  expiry and the audience (RFC 8707) are checked with
  [jose](https://github.com/panva/jose) against your authorization server's
  published keys.
- **Answers `403 insufficient_scope`** when a valid token lacks `mcp:tools`.

It serves stateless Streamable HTTP: each call is complete on its own, so any
instance can answer any call, and the server can stop between calls. Two
example tools, `whoami` and `add`, show how a tool reads the caller's
identity. `npm test` checks all of the above against a stand-in issuer.

It runs anywhere that runs Node.js 22 or later. On
[Gregale](https://gregale.dev) it pauses between calls, so idle time isn't
billed.

## Run it locally

```sh
npm install
npm run dev-issuer
```

`dev-issuer` is a stand-in authorization server for local testing. It
publishes metadata and a signing key on port 9000, and prints
`OAUTH_ISSUER`, `MCP_RESOURCE` and a one-hour `TOKEN`. In a second
terminal, export those three lines, then:

```sh
npm run build && npm start
```

Call a tool with the token, then without it:

```sh
curl -s localhost:8080/mcp -H "authorization: Bearer $TOKEN" \
  -H 'content-type: application/json' -H 'accept: application/json, text/event-stream' \
  -d '{"jsonrpc":"2.0","id":1,"method":"tools/call","params":{"name":"whoami","arguments":{}}}'

curl -si localhost:8080/mcp -H 'content-type: application/json' -d '{}'
```

The first returns your token's subject and scopes. The second returns `401`
with the `WWW-Authenticate` header MCP clients follow.

Run the tests, which need no account or outside service:

```sh
npm test
```

## Configuration

| Variable | What it is |
|---|---|
| `OAUTH_ISSUER` | Your authorization server's issuer URL, exactly as it appears in the tokens' `iss` claim. |
| `MCP_RESOURCE` | This server's public `/mcp` URL, such as `https://notes-mcp.gregale.app/mcp`. Tokens must carry it as their audience. |
| `OAUTH_METADATA_URL` | Optional. The server reads the authorization server's metadata from `/.well-known/oauth-authorization-server` at the issuer's root. Set this when your provider documents another URL, as Keycloak does for `/realms/<name>`. |
| `PORT` | The port to listen on. Defaults to `8080`. |

In your authorization server, register this server as an API or resource
whose identifier is `MCP_RESOURCE`, with a `mcp:tools` scope. The MCP spec
says authorization servers and clients should support OAuth Client ID
Metadata Documents for client registration; Dynamic Client Registration is
deprecated and kept for older servers. Check which your provider supports.

## Deploy to Gregale

```sh
gregale deploy --create-only --name notes-mcp
gregale env set --app notes-mcp OAUTH_ISSUER=https://auth.example.com MCP_RESOURCE=https://notes-mcp.gregale.app/mcp
gregale deploy
```

Gregale installs dependencies from the lockfile, runs the `build` script and
starts the app with the `start` script. `access: public` in `gregale.yaml`
matters: callers present your OAuth tokens, not Gregale API keys, and
without it an app on Hobby and above asks every caller for a Gregale API key
before the request reaches your code.

Check what an unauthenticated client sees:

```sh
curl -si https://notes-mcp.gregale.app/mcp -H 'content-type: application/json' -d '{}'
curl -s https://notes-mcp.gregale.app/.well-known/oauth-protected-resource/mcp
```

A tool call must finish within the app's request timeout, 30 seconds by
default. Hobby allows up to 60 seconds and Scale up to 900:

```sh
gregale app update notes-mcp --request-timeout 60
```

MCP hosting is on the Hobby plan and higher. The walkthrough, with what we
measured on Gregale and how other platforms handle MCP authorization, is
[Host a TypeScript MCP server with OAuth 2.1](https://gregale.dev/articles/host-typescript-mcp-server-oauth-scale-to-zero).

## What we measured

We deployed this server on Gregale on 2026-11-27, with a stand-in
authorization server and tokens we signed ourselves:

- **Memory:** 112 MB while serving.
- **Waking up:** the snapshot restore took 65 ms. An authenticated
  `tools/call` to a parked server returned its first byte at 368 ms,
  including fetching the authorization server's keys.

## Other examples

- [mcp-server-node](https://github.com/gregale-dev/mcp-server-node): a
  JavaScript MCP server with resources, prompts, streaming progress and
  per-tool scopes, made by `gregale mcp server init`.
- In version 2 of the TypeScript SDK, `requireBearerAuth`,
  `mcpAuthMetadataRouter` and `getOAuthProtectedResourceMetadataUrl` move to
  the `@modelcontextprotocol/express` package.

## Using a coding agent

`AGENTS.md` tells Codex, Cursor, Claude Code and other agents how to deploy
and check this server with the `gregale` CLI. See also
[Host an MCP server](https://gregale.dev/docs/mcp-hosting) and
[Use Gregale from coding agents](https://gregale.dev/docs/agents).

## License

MIT