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
| Layer | Main code | Responsibility |
|---|---|---|
| Entrypoints | src/index.ts, src/server.ts | Start the stdio or HTTP transport and own process shutdown. |
| MCP server | src/lib/mcp-server.ts | Create an MCP server, register its workflow resource and prompt, select tool domains, and connect tools to one service facade. |
| Tool adapters | src/tools/*.tools.ts | Define MCP schemas and annotations, validate tool input, call a domain service, and curate the MCP result. |
| Service clients | src/services/*.service.ts | Map typed domain operations to Portkey Admin API paths and response contracts. |
| HTTP client base | src/services/base.service.ts | Validate the upstream URL, attach the Portkey API key, apply timeouts, encode requests, and normalize upstream errors. |
| HTTP runtime | src/lib/http-app.ts | Host per-request modern and legacy Streamable HTTP MCP plus health and authorization metadata endpoints. |
| Security and state | src/lib/auth.ts, security.ts, limits.ts | Enforce authentication, origin and network policy, request limits, concurrency bounds, and optional shared Redis rate limiting. |
| Contracts | src/schemas/contracts/ | Validate the current Portkey response shapes used by contract tests. |
Request flow
- An MCP client connects through dual-era
serveStdioorcreateMcpHandlervia the Node HTTP adapter. createMcpServer()registers all 22 tool domains, or the subset allowed byPORTKEY_TOOL_DOMAINS.- 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.
- The tool callback calls its domain client on the shared
PortkeyServicefacade. BaseServicebuilds 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.- 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.tsand expose its client throughsrc/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.