Skip to main content

Add your description here

Project description

belgie-mcp

[!WARNING] This package follows the current MCP Python SDK auth APIs. Expect small compatibility updates as the SDK evolves.

belgie-mcp connects Belgie's OAuth server to the MCP Python SDK. It builds MCP AuthSettings, provides a token verifier that understands both local OAuth provider state and HTTP introspection, and includes a helper for resolving the current user from the active access token.

Installation

uv add belgie[mcp] belgie[oauth]

[!NOTE] The belgie.mcp re-exports are only available when the mcp extra is installed. The quick-start also imports belgie.oauth.server, which requires the oauth extra.

Quick Start

Here is a complete setup for a single FastAPI app that hosts both Belgie OAuth and an MCP server:

Project Structure:

my-app/
├── main.py
└── ...

main.py:

from fastapi import FastAPI
from mcp.server.mcpserver import MCPServer

from belgie import Belgie
from belgie.alchemy.oauth_server import OAuthServerAdapter
from belgie.mcp import Mcp, get_user_from_access_token
from belgie.oauth.server import OAuthServerResource, OAuthServer

app = FastAPI()

belgie = Belgie(
    settings=...,  # your BelgieSettings
    adapter=...,  # your database adapter
    database=...,  # async DB dependency
)

oauth_adapter = OAuthServerAdapter(
    oauth_client=...,  # your OAuth client model
    oauth_authorization_state=...,  # your OAuth authorization state model
    oauth_authorization_code=...,  # your OAuth authorization code model
    oauth_access_token=...,  # your OAuth access token model
    oauth_refresh_token=...,  # your OAuth refresh token model
    oauth_consent=...,  # your OAuth consent model
)

oauth_settings = OAuthServer(
    adapter=oauth_adapter,
    base_url="https://auth.local",
    prefix="/oauth",
    client_id="demo-client",
    client_secret="demo-secret",  # noqa: S106
    redirect_uris=["http://localhost:3030/callback"],
    default_scope="user",
    login_url="/login",
    resources=[OAuthServerResource(prefix="/mcp", scopes=["user"])],
)

_ = belgie.add_plugin(oauth_settings)
mcp_plugin = belgie.add_plugin(
    Mcp(
        oauth=oauth_settings,
        base_url="https://app.local",
    ),
)

mcp_server = MCPServer(
    name="Belgie MCP",
    instructions="MCP server protected by Belgie OAuth",
    token_verifier=mcp_plugin.token_verifier,
    auth=mcp_plugin.auth,
)

app.include_router(belgie.router)
app.mount(
    mcp_plugin.server_path,
    mcp_server.streamable_http_app(
        streamable_http_path="/",
    ),
)


@mcp_server.tool()
async def get_user_email() -> dict[str, str | None]:
    user = await get_user_from_access_token(belgie)
    return {
        "individual_id": str(user.id) if user else None,
        "user_email": user.email if user else None,
    }

McpPlugin gives you the MCP auth settings, token verifier, and derived server_path. Your app owns the transport mounting and any streamable_http_app(...) options.

Notes

  • OAuthServer.adapter is required because the MCP token verifier expects OAuth server state to be persistent.
  • If the MCP server shares a host with Belgie, set base_url and let Mcp derive the resource URL from server_path.
  • If you pass a server_url directly, it takes precedence over base_url and server_path.
  • oauth_strict=True enables strict resource validation against the issued token audience.
  • get_user_from_access_token resolves the current user from a co-located OAuth provider first, then falls back to decoding a JWT sub claim.
  • The package is designed to work with the example in examples/mcp.

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

belgie_mcp-0.15.2.tar.gz (6.1 kB view details)

Uploaded Source

Built Distribution

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

belgie_mcp-0.15.2-py3-none-any.whl (8.1 kB view details)

Uploaded Python 3

File details

Details for the file belgie_mcp-0.15.2.tar.gz.

File metadata

  • Download URL: belgie_mcp-0.15.2.tar.gz
  • Upload date:
  • Size: 6.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.11.6 {"installer":{"name":"uv","version":"0.11.6","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

File hashes

Hashes for belgie_mcp-0.15.2.tar.gz
Algorithm Hash digest
SHA256 11d15d138b6282559f350827731435a79fa5c1140d7bb4cf0e8564a36202e3c1
MD5 ee8afb049b1aa86b55093d684be371e1
BLAKE2b-256 f496e745f42082fb59a586226a186d7e08d28b9d91d5b8af2b328fc55791dc56

See more details on using hashes here.

File details

Details for the file belgie_mcp-0.15.2-py3-none-any.whl.

File metadata

  • Download URL: belgie_mcp-0.15.2-py3-none-any.whl
  • Upload date:
  • Size: 8.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.11.6 {"installer":{"name":"uv","version":"0.11.6","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

File hashes

Hashes for belgie_mcp-0.15.2-py3-none-any.whl
Algorithm Hash digest
SHA256 745e9c3d93e565e2b54ec28599fa2e46736da1c1736ae46af3886a21a8a7a11f
MD5 d6e2cf93f0aca7d4dc1b610c7502e45b
BLAKE2b-256 62c2dbcc0d3b8c006e94d44d376393778574bec3bdc9a4cca1e6b9e1c4b01101

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