Skip to main content
Glama
mcp-secure-server

mcp-secure-server

by SkT97

mcp-secure-server

An executable Python reference for MCP authorization. Use the official SDK with verified token audiences and tenant-scoped reads. The demo uses synthetic data.

Quick start

Requires Python 3.12+ and uv. From this repository:

uv run --locked mcp-secure-server

The demo creates ephemeral RSA keys, binds loopback HTTP explicitly, discovers protected-resource metadata and calls the tools through the official SDK client. It verifies that a request without credentials is rejected and a north-tenant token cannot read a south-tenant record. No model, external credentials or Docker is required.

Related MCP server: pg_mcp_qauth

How it works

The verified identity supplies the tenant. Tool arguments cannot change it. A record from another tenant returns the same result as an unknown record. The downstream exchange adapter is standalone.

trusted issuer → verified RS256 token → SDK authentication context → tenant-scoped records
                                             ↓
                                fixed downstream exchange adapter
  • Validate signature, issuer, resource audience, expiry and required identity claims.

  • Require records:read; take tenant_id exclusively from the verified principal.

  • Return the same response for another tenant's record and an unknown record.

  • Bound tool arguments and synthetic output; enable SDK host protection.

  • Require HTTPS in the client and exchange adapter, with an explicit loopback-only demo exception.

  • Exchange a validated subject token at a fixed endpoint; refuse redirects and unchanged subject tokens. Never forward an inbound token implicitly.

tenant_id, azp and record permissions are this reference's application policy. They are not universal MCP requirements. The exchange adapter is standalone; record tools do not make downstream calls.

Sources checked 16 November 2026: MCP authorization, 2026-07-28, RFC 9728, RFC 8707, RFC 9700 and RFC 8693. The standards map connects each boundary to its executable check.

The official Python SDK supplies protocol handling and auth context. This project concentrates on application boundaries. Keycloak's supported standard token exchange is tested separately from mocked adapter failures.

Verification

The recorded local run passed 85 tests and the Keycloak lab. See verification and the threat model.

With Docker available:

uv run --locked python examples/keycloak_integration.py

This creates a dedicated, loopback-only Keycloak 26.8.2 container pinned by image digest, random temporary credentials and synthetic service accounts. It checks both tenant directions, wrong audience, missing scope, discovery and a real RFC 8693 exchange to a downstream audience. It verifies the exchanged signature and preserved subject, then removes its own container.

The local run on 16 November 2026 passed these checks:

Probe

Observed result

No token / wrong audience

HTTP 401 / 401

Missing required scope

HTTP 403

Protected-resource metadata

HTTP 200; configured resource matches

Two authenticated tenants

Five own records each

Cross-tenant reads, both directions

Denied; indistinguishable from unknown ID

Tenant supplied as a tool argument

Escalation prevented

Downstream token exchange

Distinct signed token; target audience; original subject

See recorded verification and safe integration output.

uv sync --locked --group dev
uv run --locked python -m pytest -q
uv run --locked ruff check .
uv run --locked ruff format --check .

uv.lock pins the tested dependency graph. CI is configured for Linux and Windows, with the Keycloak lab on Linux.

Limits

This is a small read-only reference, not a production deployment or security certification. The demo and Docker lab use synthetic service accounts; they do not implement interactive user login, consent or a complete OAuth client.

The trusted RSA public key is operator-configured. Production integration needs a reviewed key-rotation strategy, TLS termination, ingress body/rate limits, request budgets, operational logging and deployment hardening. Authentication does not make retrieved content safe against prompt injection. See the threat model.

License

Apache-2.0.

Tool Schema Changelog

Recent tool additions, removals, and schema changes observed during successful MCP inspections.

Loading tool schema changelog…

    Related MCP Connectors

    Related MCP Servers

    • A
      license
      Not graded
      quality
      B
      maintenance
      Enables secure read-only PostgreSQL access from MCP clients via OAuth 2.1, with JWT validation, role-based and row-level isolation, and an AST-based SQL guard for audited, safe queries.
      MIT
    • A
      license
      A
      quality
      C
      maintenance
      Enables MCP-capable AI clients to fetch read-only Microsoft Graph audit evidence for security posture, MFA coverage, stale accounts, and privileged access.
      4
      MIT
    • A
      license
      Not graded
      quality
      B
      maintenance
      Enables validating Microsoft Entra access tokens to determine whether an MCP caller is a signed-in user or only an application, illustrating MCP authentication methods.
      MIT