npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

@reaatech/multi-tenant-mcp-middleware

v0.1.0

Published

Composable multi-tenant middleware for MCP servers

Readme

@reaatech/multi-tenant-mcp-middleware

npm version License: MIT CI

Status: Pre-1.0 — APIs may change in minor versions. Pin to a specific version in production.

Compose a full multi-tenant middleware stack for MCP servers. Wraps MCP request handlers with a pipeline that enforces tenant resolution, rate limiting, tool/resource/prompt visibility, cost accounting, logging, and metrics in a single call.

Installation

npm install @reaatech/multi-tenant-mcp-middleware @modelcontextprotocol/sdk
# or
pnpm add @reaatech/multi-tenant-mcp-middleware @modelcontextprotocol/sdk

@modelcontextprotocol/sdk is a peer dependency (bring your own version).

Feature Overview

  • Single API callcreateMultiTenantMiddleware(config) returns a middleware object with one method: handle(server, method, handler).
  • Composable pipeline — Every middleware layer (rate limit, visibility, cost accounting, etc.) is optional; only tenantContextStore is required.
  • MCP method-aware — The middleware automatically filters tools/list / resources/list / prompts/list results by tenant policy, and validates */call / */read / */get access.
  • Non-blocking side-effects — Usage emissions and cost tracking run after handler completion; failures are logged but never fail the request.

Quick Start

import { Server } from '@modelcontextprotocol/sdk/server/index.js';
import { createMultiTenantMiddleware } from '@reaatech/multi-tenant-mcp-middleware';
import { HeaderTenantResolver, TenantContextStore } from '@reaatech/multi-tenant-mcp-tenant-resolver';
import { DefaultRateLimiter, MemoryRateLimitStore } from '@reaatech/multi-tenant-mcp-rate-limiter';
import { DefaultCostCalculator, InMemoryCostTracker, CallbackUsageEmitter } from '@reaatech/multi-tenant-mcp-cost-accounting';
import { ConsoleTenantLogger, MetricsCollector } from '@reaatech/multi-tenant-mcp-observability';

const store = new TenantContextStore();
const logger = new ConsoleTenantLogger({ level: 'info' });
const metrics = new MetricsCollector();

const middleware = createMultiTenantMiddleware({
  tenantContextStore: store,
  tenantResolver: new HeaderTenantResolver({ header: 'x-tenant-id' }),
  rateLimiter: new DefaultRateLimiter(
    new MemoryRateLimitStore({ requestsPerMinute: 100, tokensPerMinute: 10_000 }),
  ),
  toolVisibility: {
    'acme-corp': { type: 'allow', items: ['echo', 'status'] },
  },
  costCalculator: new DefaultCostCalculator({
    perCall: { echo: 0.01 },
  }),
  costTracker: new InMemoryCostTracker(),
  usageEmitter: new CallbackUsageEmitter(async (event) => {
    await billingPipeline.record(event);
  }),
  logger,
  metrics,
});

const server = new Server({ name: 'my-mcp', version: '1.0.0' }, { capabilities: {} });

// Wire up tools/list — results are filtered by tenant visibility policy
middleware.handle(server, 'tools/list', () => ({
  tools: [
    { name: 'echo', description: 'Echo back input' },
    { name: 'status', description: 'Server status' },
    { name: 'admin', description: 'Admin operations' },
  ],
}));

// Wire up tools/call — tenant identity, rate limits, and visibility are enforced
middleware.handle(server, 'tools/call', (request) => {
  const req = request as { params: { name: string; arguments?: Record<string, unknown> } };
  switch (req.params.name) {
    case 'echo':
      return { content: [{ type: 'text', text: String(req.params.arguments?.message ?? '') }] };
    case 'status':
      return { content: [{ type: 'text', text: 'ok' }] };
    default:
      return { content: [{ type: 'text', text: 'Unknown tool' }] };
  }
});

Request Pipeline

For every MCP request, the middleware enforces:

1. Tenant Context Retrieval — read from TenantContextStore (ALS)
2. Rate Limit Check         — verify against configured RateLimiter
3. Visibility Filter        — list: filter results; call/read: validate access
4. Handler Execution        — delegate to user-provided handler
5. Cost Accounting          — record usage event (non-blocking)

Exports

| Export | Kind | Description | |--------|------|-------------| | createMultiTenantMiddleware | Function | Compose the full middleware stack from config | | MultiTenantMiddlewareConfig | Interface | Configuration object — all fields optional except tenantContextStore | | MultiTenantMiddleware | Interface | Returned object with handle(server, method, handler) | | RequestHandler<T> | Type | (request: T) => T \| Promise<T> |

Configuration Options

| Property | Type | Required | Description | |----------|------|----------|-------------| | tenantContextStore | TenantContextStore | Yes | ALS-backed store for tenant context propagation | | tenantResolver | TenantResolver | No | Resolver called on each request to identify the tenant | | rateLimiter | RateLimiter | No | Rate limiting engine (default: no-op) | | toolVisibility | Record<string, VisibilityPolicy> | No | Per-tenant tool visibility policies | | resourceVisibility | Record<string, VisibilityPolicy> | No | Per-tenant resource visibility policies | | promptVisibility | Record<string, VisibilityPolicy> | No | Per-tenant prompt visibility policies | | costCalculator | CostCalculator | No | Pricing model for usage-based billing | | costTracker | CostTracker | No | In-memory cost accumulator | | usageEmitter | UsageEventEmitter | No | Callback for forwarding usage events externally | | artifactStore | ArtifactStore | No | Tenant-scoped storage backend | | configStore | TenantConfigStore | No | Per-tenant configuration store | | logger | TenantLogger | No | Structured logger (default: silent) | | metrics | MetricsCollector | No | Metrics collector (default: no-op) |

Related Packages

License

MIT