DOCUMENTATION / PORTKEY ADMIN MCP

Docs for 0.16.6, the latest release

On this page

Architecture

Portkey Admin MCP is a TypeScript adapter between Model Context Protocol (MCP) clients and Portkey's Admin API.

Portkey Admin MCP is a TypeScript adapter between Model Context Protocol (MCP) clients and Portkey's Admin API. It exposes the same tool catalog over local stdio and an optional HTTP transport while keeping API access, validation, and transport policy in separate layers.

This describes the SDK v2 implementation shipped in 0.15.0. Published 0.14.x retains its session/replay implementation; use the architecture document from that release. See compatibility and recovery before upgrading.

Component map

LayerMain codeResponsibility
Entrypointssrc/index.ts, src/server.tsStart the stdio or HTTP transport and own process shutdown.
MCP serversrc/lib/mcp-server.tsCreate an MCP server, register its workflow resource and prompt, select tool domains, and connect tools to one service facade.
Tool adapterssrc/tools/*.tools.tsDefine MCP schemas and annotations, validate tool input, call a domain service, and curate the MCP result.
Service clientssrc/services/*.service.tsMap typed domain operations to Portkey Admin API paths and response contracts.
HTTP client basesrc/services/base.service.tsValidate the upstream URL, attach the Portkey API key, apply timeouts, encode requests, and normalize upstream errors.
HTTP runtimesrc/lib/http-app.tsHost per-request modern and legacy Streamable HTTP MCP plus health and authorization metadata endpoints.
Security and statesrc/lib/auth.ts, security.ts, limits.tsEnforce authentication, origin and network policy, request limits, concurrency bounds, and optional shared Redis rate limiting.
Contractssrc/schemas/contracts/Validate the current Portkey response shapes used by contract tests.

Request flow

  1. An MCP client connects through dual-era serveStdio or createMcpHandler via the Node HTTP adapter.
  2. createMcpServer() registers all 22 tool domains, or the subset allowed by PORTKEY_TOOL_DOMAINS.
  3. The MCP SDK enforces a 10,000-element budget across nested array elements and object members before validating a tool call against its Zod input schema. The shared tool wrapper adds the standard success/error envelope and annotations.
  4. The tool callback calls its domain client on the shared PortkeyService facade.
  5. BaseService builds the authenticated HTTPS request to the configured Portkey API, then the domain service validates or normalizes the response. It reads the response body through a byte-counting reader capped at 8 MiB (PORTKEY_MAX_UPSTREAM_BODY_BYTES) and fails with an explicit error above it.
  6. The tool adapter returns a compact MCP result instead of forwarding the raw Admin API response. The shared tool wrapper measures the serialized result (text block after JSON escaping, plus structuredContent) and replaces one over 4 MiB (PORTKEY_MAX_TOOL_RESULT_BYTES) with an explicit error. Neither limit truncates anything. A malformed value for either stops startup.

The Portkey API key is used only by the service layer. Tool results, logs, MCP request metadata, and service-cache identifiers do not expose the raw key.

Transport boundaries

The stdio entrypoint is intended for a local MCP client and relies on the client's process boundary. The HTTP entrypoint is a network service and adds a separate policy boundary:

  • bearer-token or Clerk JWT authentication;
  • allowed-origin, host, and public-base-URL validation;
  • body-size, request-rate, and active-request limits;
  • a fresh MCP server for each modern or legacy HTTP request;
  • shared Redis rate limiting without persisted MCP responses; and
  • readiness and graceful-shutdown handling.

HTTP authentication is a server access gate, not Portkey tenant impersonation. All authenticated principals use the same configured PORTKEY_API_KEY and can invoke any enabled tool and scope that credential grants. Different trust levels require separate instances or deployments with separately scoped Portkey credentials and PORTKEY_TOOL_DOMAINS allowlists.

The upstream PORTKEY_BASE_URL must be HTTPS unless both development-only private-network opt-ins are set. Public model-pricing requests use Portkey's public catalog origin and omit the tenant API key.

Change boundaries

  • Add or change Portkey API behavior in the matching domain service first.
  • Keep MCP naming, input validation, output curation, and annotations in the matching tool module.
  • Register a new domain in src/tools/index.ts and expose its client through src/services/index.ts.
  • Put cross-cutting transport, authentication or request-lifecycle behavior in src/lib/, not in individual tools.

tests/contract*.test.ts protect API shapes, the tool suites exercise MCP callbacks, the service and library suites cover the internal boundaries, and tests/mcp-e2e.test.ts verifies the built server through MCP.

Static catalog and discovery results use a private 60-second cache hint on the modern wire. Change-notification capabilities are disabled because no events are emitted. GET replay and DELETE session operations return 405 after the same authentication and Origin checks as POST.

Based on the published 0.16.6 release. The source files are in the product repository.