Skip to main content

Auth, billing, and logging infrastructure for MCP-first servers.

Project description

mcp-core

Release License: MIT Python

Auth, billing, and logging infrastructure for MCP-first servers. Sits between your product code and fastapi-mcp.

Your MCP Server  (product-specific tool handlers)
     mcp-core    (auth, billing, logging, health)
    fastapi-mcp  (MCP protocol: JSON-RPC, SSE, tool discovery)
      FastAPI

Install

pip install mcp-core-auth

The package is published as mcp-core-auth on PyPI (the bare mcp-core name is held by an unrelated project), but the import path is unchanged:

from mcp_core import MCPCore

Quick Start

from contextlib import asynccontextmanager
from fastapi import FastAPI, Request
from mcp_core import MCPCore

core = MCPCore(
    product_name="my-product",
    logto_endpoint="https://your-tenant.logto.app",
    logto_api_resource="https://api.my-product.app",
    mongodb_uri="mongodb+srv://...",
    stripe_secret_key="sk_test_...",
    stripe_price_id="price_...",
    free_credits=30,
    tool_costs={"browse": 0, "generate": 5},
    read_only_tools={"browse"},
)

@asynccontextmanager
async def lifespan(app: FastAPI):
    await core.connect_db()
    yield

app = FastAPI(lifespan=lifespan)
core.install_routes(app)  # /health, /api/billing/credits, webhook, OAuth metadata

@app.post("/api/mcp/generate")
async def generate(request: Request):
    user = await core.auth_and_bill(request, "generate")
    result = do_generation()
    await core.log_tool_call(request, "generate", user=user, duration_ms=1200)
    return result

All config can also come from MCP_CORE_* environment variables.

Modules

Auth (mcp_core.auth.LogtoAuth)

Logto JWT validation via JWKS. Creates MongoDB user records on first auth.

  • RS256/ES256/ES384/ES512 support
  • 30s clock skew tolerance
  • Race-condition-safe user upsert
  • Dev bypass (Bearer dev-bypass) for local development
  • M2M token rejection for paid tools

Billing (mcp_core.billing.StripeBilling)

Stripe metered billing with free credit fallback.

  • Free credits deducted first
  • Stripe metered subscription as fallback
  • 402 with Checkout URL when no credits and no subscription
  • Webhook handler for checkout.session.completed and customer.subscription.created

Tool Logging (mcp_core.tool_logging.ToolLogger)

Audit trail for every MCP tool call. Writes to MongoDB tool_logs collection.

Health (mcp_core.health.HealthCheck)

Composable health check builder. Supports sync and async checks with timeouts.

Testing

# Mock tests (no external services)
pip install -e ".[dev]"
pytest tests/ -v

# Live tests (requires .env.live with real credentials)
RUN_LIVE_TESTS=1 pytest tests/live/ -v

Contributing

Bug reports and PRs welcome. See CONTRIBUTING.md for the workflow and SECURITY.md for vulnerability reporting.

Auth provider abstraction (Auth0, Keycloak, generic OIDC) is tracked in #1 — discussion-first.

License

MIT

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

mcp_core_auth-0.2.1.tar.gz (519.8 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

mcp_core_auth-0.2.1-py3-none-any.whl (26.3 kB view details)

Uploaded Python 3

File details

Details for the file mcp_core_auth-0.2.1.tar.gz.

File metadata

  • Download URL: mcp_core_auth-0.2.1.tar.gz
  • Upload date:
  • Size: 519.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.10

File hashes

Hashes for mcp_core_auth-0.2.1.tar.gz
Algorithm Hash digest
SHA256 4c924fde770fd5e3145930975da80eeefc6731db9d06b6340783a49cfec8b800
MD5 7902d1a20c18f679fccf880fb2e4a08c
BLAKE2b-256 2e5bac70f604b2d4c0dab4325a927c8d574321db0c5b68d53c9ec189791eb8c3

See more details on using hashes here.

File details

Details for the file mcp_core_auth-0.2.1-py3-none-any.whl.

File metadata

  • Download URL: mcp_core_auth-0.2.1-py3-none-any.whl
  • Upload date:
  • Size: 26.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.10

File hashes

Hashes for mcp_core_auth-0.2.1-py3-none-any.whl
Algorithm Hash digest
SHA256 e85adb45b22d9ab763f561e18a40ade6d309dfe20e6e55e829e648018f45e102
MD5 8268f48e743c03386bcc7dcec1f71ce4
BLAKE2b-256 14a08d0c689960b6b835c60ed669915fe641f85d773ebb80eb6329858c4d8ab5

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page