Skip to main content

Analytics MCP Server — Claude over OAuth 2.1

A remote MCP server that lets Claude query a workforce-analytics API, with its own OAuth 2.1 authorization server, safety rails on every sensitive tool, and a port from a long-running process to serverless.

  • TypeScript
  • Node.js
  • MCP SDK
  • OAuth 2.1 + PKCE
  • Upstash Redis
  • Vercel Functions

Built against a production API that holds employee data, so the source and endpoint stay private; this write-up covers the design only.

  • Protocol & API Design
  • OAuth 2.1 Authorization Server
  • Serverless Port
  • LLM Tool Safety

The problem

A manager’s questions about their team — who worked on what yesterday, which time claims are pending, what a given afternoon looked like — each take several screens of a workforce-analytics dashboard to answer. The Model Context Protocol lets Claude call tools directly, so I built a remote MCP server that exposes the product’s API as twelve tools across four areas: identity, time tracking, time claims and screenshots.

The interesting part is not the tools. It is that the data is real employee monitoring data, reached by a language model, on behalf of whoever signed in — so authorization, blast radius and auditability had to be designed first.

Claude connects to the MCP endpoint; the server runs its own OAuth 2.1 authorization server, keeps auth state in Redis, and calls the analytics API as the signed-in user.
  1. Client: Claude (custom connector)
  2. Server: MCP endpoint (stateless per request), OAuth 2.1 server (PKCE, refresh), 12 tools (with safety rails)
  3. State: Upstash Redis (TTL per key)
  4. Upstream: Product login (credential check), Analytics API (as the signed-in user)

Its own authorization server

Claude’s connectors speak OAuth 2.1 with dynamic client registration and PKCE; the analytics product speaks username and password. The server bridges the two: it implements the OAuth endpoints and discovery documents itself, and delegates the one step that matters — checking the credentials — to the product’s own login. The token it issues is bound to that identity, so every tool call runs with exactly the permissions the person already has in the product, and no more.

Safety rails that are not optional

A language model will call whatever it is given, as often as seems useful. So the limits live in the tools, not in a prompt asking nicely.

  • No fetch-all

    The screenshot tool requires an explicit, non-empty list of paths, caps the batch, and defaults to thumbnails.

  • Every access audited

    Each screenshot fetch writes to the product’s own audit log — in parallel with the image request, so the safeguard costs no latency.

  • Writes are dry runs

    Submitting a time claim does nothing unless confirm=true is passed, and no tool can approve, reject or delete.

  • Refuses unsafe config

    Start-up aborts on unsafe combinations — no durable state on serverless, or production without a fixed public URL — instead of running degraded.

  • A cache that can’t leak

    The member list is cached per caller, not per organisation. The API authorises that list by token, so an org-wide key could serve one person’s fuller view to someone who should see less.

  • A response budget

    Screenshot batches stop at 3.5 MB and name what was skipped, rather than failing against the platform’s 4.5 MB response limit.

From a process to serverless

The first build was a long-running Node process holding sessions and auth state in five in-memory maps. On Vercel, any request can land on a fresh instance, so everything that has to outlive a request moved to Redis, each key with the lifetime it actually needs. Each request now gets a fresh MCP server and transport, with no session to route back to.

KeyHoldsLifetime
client:<id>Registered OAuth clientNone — losing it breaks every connector
pending:<txn>In-flight authorization request10 minutes
code:<code>Authorization code10 minutes, deleted on use with GETDEL
token:<token>Access token and the identity it is bound toToken lifetime
refresh:<token>Pointer to its access tokenToken lifetime + 30 days
roster:<org>:<user>Cached member list — a read cache, not auth state90 seconds

Authorization codes are redeemed with GETDEL, so two concurrent redemptions of the same code cannot both succeed. The store had to be Redis rather than an edge config store: those propagate writes over seconds, and an authorization code must be readable the instant after it is written. The original process build still runs from the same code, so the port is not a one-way door.

Fewer calls, correct dates

Nearly every tool resolves a person’s name to an id through the member list, so one conversation fetched the same list over and over, and a tool that took ten names awaited ten sequential fetches of it. A 90-second per-caller cache and a resolver that matches all names against one fetch removed both. Keep-alive stopped each call paying a fresh TLS handshake, and reports now carry server-side totals with durations emitted once, roughly halving the payload the model has to read.

Relative dates — today, last week — are resolved on the server in the organisation’s timezone. That is a correctness fix, not a convenience: the API answers a wrong date range with an empty success, which is indistinguishable from “this person did no work”. Its timestamps also end in a literal Z that is not a UTC marker, so sending a genuine UTC instant shifts every report by the local offset. Twenty-six tests pin those rules down.

What serverless costs here

Written down rather than discovered later: full-size screenshot batches are capped by the response limit; cold starts add latency to the first call after idle; access tokens for the product now live in a second third-party processor; and screenshots transit the host’s infrastructure. The last two are compliance questions rather than engineering ones, and the documentation flags them for review against the product’s SOC 2 and GDPR position instead of assuming them away.