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.2.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.2-py3-none-any.whl (26.3 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: mcp_core_auth-0.2.2.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.2.tar.gz
Algorithm Hash digest
SHA256 ca44b23ac4645b0ddcc9a61de0791449fd86c2d7dbe9812ed00fff2ba8d09647
MD5 dd5692b4df02ed107c26d279e3267f58
BLAKE2b-256 2d0e2c8411adb54d767f0a1829cd9c27bb450cd9729dfd1469ce7612aebf9353

See more details on using hashes here.

File details

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

File metadata

  • Download URL: mcp_core_auth-0.2.2-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.2-py3-none-any.whl
Algorithm Hash digest
SHA256 a98074da2775ac1b37dfc6c3c53bee704880bbb074b354010478d136a768e133
MD5 0049f90665baff1891273e3ee786c1e7
BLAKE2b-256 c62b4bcd77ed26e56e9e34f53769fe54b0da2ba7acb32d715a86c59073a3f89c

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