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.0.tar.gz (98.7 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.0-py3-none-any.whl (23.7 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: mcp_microsoft_graph_auth-0.1.0.tar.gz
  • Upload date:
  • Size: 98.7 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.0.tar.gz
Algorithm Hash digest
SHA256 08b1eccfd8d26dab839c1bfe56383ca51469ac59a946f56990be2047239c9099
MD5 06f134f661222f404486ede4186adf3d
BLAKE2b-256 9a0f6f84715a54d126c4f306e3708b0eb667fdd0104dc70648d6eb4f6cb098d8

See more details on using hashes here.

File details

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

File metadata

  • Download URL: mcp_microsoft_graph_auth-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 23.7 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.0-py3-none-any.whl
Algorithm Hash digest
SHA256 3c532ed3ee0844181c14706502b4705eefa40b11c1c368f20c06f0a5194fd633
MD5 55af9f51a4ec37561a3f0da3b59fcbd9
BLAKE2b-256 800c3757d8066782208c6d5db3e474807c0145da5a9d00b3a1da2aeeb333bc2b

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