Skip to main content

Shared Microsoft Graph auth helpers (Device Code, token cache, BYO client) for MCP servers.

Project description

mcp-microsoft-graph-auth

Shared Microsoft Graph authentication helpers for MCP servers.

PyPI version CI Python License: MIT OR Apache-2.0

Why this exists

mcp-server-sharepoint and mcp-server-outlook (and likely future siblings — Teams, OneDrive, …) all need the same auth machinery against Microsoft Graph: OAuth Device Code flow, token cache with sensible storage backends, optional service-principal mode, multi-profile support.

This library factors that machinery out so each MCP server adds only what's specific to its API surface (scopes + tool names), not yet another reimplementation of Device Code + token storage.

What's in the box

  • device_code — primitives for the OAuth 2.0 Device Code flow against Microsoft Identity v2.0 (request_device_code, poll_for_token, refresh_access_token).
  • service_principal — client-credentials grant for unattended automation (CI runners, scheduled jobs).
  • token_store — three pluggable storage backends:
    • OS keyring (macOS Keychain / Windows Credential Locker / Linux Secret Service) when available.
    • Plain file mode 0600 (~/.cache/<your-app>/<profile>/token.json) — same convention as gh auth, aws configure.
    • Encrypted file with passphrase (Fernet + Scrypt KDF) for paranoid setups or shared CI cache.
  • tokensCachedToken dataclass with sensible JSON serialisation.
  • login_sessionLoginSession + LoginSessionRegistry for MCP-tool-driven login flows (the in-process state your *_login_begin / *_login_status tools share).

Public API contract

The library is prefix-agnostic: it does not read environment variables on its own. Each consumer (an MCP server) is responsible for reading its own env-var conventions and passing values explicitly. This keeps the library reusable by any MCP server without env-var collisions.

Minimal sketch (subject to v0.1.0 release)

from mcp_microsoft_graph_auth import (
    request_device_code,
    poll_for_token,
    refresh_access_token,
    CachedToken,
    PlainFileTokenStore,
)

# Initiate Device Code flow
device_code, challenge = request_device_code(
    client_id="<your-app-id>",
    tenant="organizations",  # or a specific tenant GUID
    scopes=("Files.ReadWrite.All", "Sites.ReadWrite.All", "User.Read", "offline_access"),
)
print(f"Open {challenge.verification_uri} and enter code {challenge.user_code}")

# Poll until the user completes sign-in
token = poll_for_token(
    device_code=device_code,
    client_id="<your-app-id>",
    interval=challenge.interval,
)

# Persist for later
store = PlainFileTokenStore(base_dir="~/.cache/my-mcp-server")
store.set("default", token.to_json().encode())

Compatibility

  • Python 3.11+
  • Microsoft Identity v2.0 endpoints
  • Tested on Linux + macOS (Windows should work; not yet covered by CI)

License

Dual-licensed under MIT or Apache-2.0 at your option. See LICENSE-MIT and LICENSE-APACHE.

Status

Pre-1.0. The public API is stable enough for use by mcp-server-sharepoint and mcp-server-outlook — the two consumers driving its design. External consumption is welcome but expect occasional breaking changes until v1.0.

See docs/RELEASING.md for release process and CHANGELOG.md for what's shipped.

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_microsoft_graph_auth-0.1.1.tar.gz (98.9 kB view details)

Uploaded Source

Built Distribution

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

mcp_microsoft_graph_auth-0.1.1-py3-none-any.whl (23.9 kB view details)

Uploaded Python 3

File details

Details for the file mcp_microsoft_graph_auth-0.1.1.tar.gz.

File metadata

  • Download URL: mcp_microsoft_graph_auth-0.1.1.tar.gz
  • Upload date:
  • Size: 98.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.11.11 {"installer":{"name":"uv","version":"0.11.11","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 mcp_microsoft_graph_auth-0.1.1.tar.gz
Algorithm Hash digest
SHA256 7d41a67a9a8b70eba510505b86f4aaa24fed9df340a264a59a43e88a5b604d00
MD5 f775d04dafc1b7e9a5510496d430fd56
BLAKE2b-256 4e533766371a74d217d1ef7ec3dbe95acf46b902c62c62ad3fa83c5a26d9f5d6

See more details on using hashes here.

File details

Details for the file mcp_microsoft_graph_auth-0.1.1-py3-none-any.whl.

File metadata

  • Download URL: mcp_microsoft_graph_auth-0.1.1-py3-none-any.whl
  • Upload date:
  • Size: 23.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.11.11 {"installer":{"name":"uv","version":"0.11.11","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 mcp_microsoft_graph_auth-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 557dcb3cd9d8a0cde917049d67f159d084149fc6552efd3156a659b49956dad8
MD5 1a0b78541a4207e710292887feb439aa
BLAKE2b-256 98db220759a47967637309b740831b1bd713a86be504021d717e579e3d877993

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