office-connect
Microsoft 365 superpowers for Python and AI agents.
office-connect is a Python library and stdio MCP server that gives both human developers and AI agents structured access to Microsoft 365 — mail, calendar, Teams, chats, files, directory, profile — through the Microsoft Graph API, behind a simple token-based authentication flow.
Features
Platform
- Stdio MCP server -- 38 tools, plug into Claude Desktop, Cursor, or any MCP client
- Device-code sign-in --
office-connect loginruns the OAuth flow from the terminal; no admin token export needed - Self-healing token refresh -- proactive (15 min before expiry) + reactive (on a 401 from Graph), persisted back to the keyfile; clients never need to restart for a token refresh
- Permission tiers --
read_only/drafts/all, enforced both atlist_toolsand atcall_tool; a global policy file acts as a host-wide ceiling - Path-mode attachments -- explicit allow-list of filesystem roots from which the MCP may read attachments and message bodies
Microsoft 365 surfaces
- Mail -- List, read with attachments, search, create/update drafts, send, move, delete, flag read, manage categories
- Calendar -- List calendars, query and search events, create events, free/busy schedules, room availability
- Teams -- Joined teams, channels, channel messages, members
- Chat -- Recent 1:1, group, and meeting chats with message history
- Files / OneDrive -- Browse drives, list folders, download content, search; on-the-fly previews for PDF / XLSX / DOCX
- SharePoint -- Search sites and list document libraries
- Directory -- Users, managers, profile photos
- Profile -- Authenticated user's profile
Development
- Mock transport -- Full synthetic data layer for testing without a real O365 account; mock is fail-closed on production environments
Quick Start
1. Install
poetry add office-connect
2. Sign in (device-code flow)
# First time — supply your Azure AD app credentials once
office-connect login --client-id <APP_ID> --tenant-id <TENANT_ID> [--client-secret <SECRET>]
# Every subsequent re-auth — zero arguments
office-connect login
office-connect login prints a https://microsoft.com/devicelogin URL plus a short code, polls Microsoft until you finish signing in, and then verifies the new token end-to-end by calling /me and /me/mailFolders/inbox — you'll see your name, title, and inbox count right after sign-in. Two files are written, both 0600:
~/.config/office-connect/token.json— access + refresh tokens (the keyfile)~/.config/office-connect/config.json— Azure AD app credentials, so re-auths need no arguments
Your Azure AD app registration must have "Allow public client flows" enabled in its authentication manifest for device-code flow to work.
Narrow scopes with repeatable --scope (default: all eight groups — profile, directory, mail, calendar, chat, teams, drive, tasks).
Not sure which command you need? Run
office-connectwith no arguments — it prints a banner listing theloginandimport-tokensubcommands plus the server invocation.
3. Wire into your MCP client
For Claude Desktop, add to ~/Library/Application Support/Claude/claude_desktop_config.json:
"office-connect": {
"command": "/usr/local/bin/python",
"args": [
"-m", "office_con.mcp_server",
"--keyfile", "/Users/<you>/.config/office-connect/token.json",
"--permission-level", "drafts",
"--attachment-root", "/Users/<you>/Downloads"
]
}
Restart the client once to pick up the new MCP entry. From then on, tokens stay fresh on their own — the MCP refreshes proactively within 15 min of expiry, reactively on a 401, and persists every refresh back to the keyfile.
Standalone CLI (rare)
office-connect --keyfile path/to/token.json
This launches the stdio MCP server directly and requires --keyfile. If you just want to (re-)authenticate, you want office-connect login instead — running bare office-connect prints the subcommand banner to point you there rather than erroring on the missing flag.
Authentication & permissions
Tokens stay fresh automatically
- Proactive refresh when the cached access token has under 15 min of life left
- Reactive refresh-and-retry if Graph rejects a request with 401 even though the JWT
expis still in the future (e.g. server-side revoke) - Persisted back to the keyfile on every successful refresh, so a process restart never loads stale credentials
- Hot-reloaded — the MCP
mtime-watches the keyfile, so updates fromimport-tokenor anotherloginare picked up on the next tool call
Manual top-ups only matter if the refresh token itself has been invalidated (typically after ~90 days of inactivity, or by tenant policy).
When the session is truly dead, the tools say so
If the refresh token is invalidated, the server no longer pretends everything is fine. Instead of letting a 401 turn into an empty inbox or a null profile, the failing tool returns a clear, actionable message:
⚠️ Office 365 authentication is not working, so this request could not be completed. … Reconnect by running:
office-connect login
The MCP server also advertises this in its instructions, so an assistant like Claude Desktop knows to surface the re-auth step rather than reporting "0 mails" as if the mailbox were empty. To check the connection explicitly, call the read-only o365_check_connection tool — it returns {connected: true, email, display_name} when healthy, or the same re-auth guidance when not. (Ask Claude "are you still connected to my mail?" and it will run this tool.)
Refreshing tokens externally
If a host application exports tokens through an admin endpoint, drop the exported JSON at the canonical keyfile location:
office-connect import-token ~/Downloads/token_export.json
# override destination with --dest /custom/path/token.json
Both login and import-token write atomically with 0600 permissions.
Permission tiers
| tier | what it allows |
|---|---|
read_only |
list / get / search / peek — no mutation of Microsoft 365 state |
drafts (default) |
read_only + create and update draft emails. No sending. |
all |
drafts + send mail, move/delete mail, flag read, set categories, create calendar events |
Three places can set the tier — the most restrictive of the ones that are set wins:
- MCP launcher CLI flag:
--permission-level read_only|drafts|all - Environment variable:
OFFICE_CONNECT_PERMISSION_LEVEL=read_only - Global policy file: a JSON object at
~/.config/office-connect/policy.json(override via--policy-file PATHor$OFFICE_CONNECT_POLICY):{ "permission_level": "drafts" }
The policy file acts as a host-wide ceiling: regardless of how any individual MCP launcher is configured, the resolver clamps the effective tier down to the most restrictive value found. A launcher can always tighten further on top. Tools above the effective tier are removed from list_tools and refused by call_tool (defense in depth; unknown tool names are fail-closed too).
Keyfile format
office-connect login and import-token write this format; you can also create it by hand if you have tokens from another source:
{
"app": "MyApp",
"email": "user@example.com",
"access_token": "eyJ...",
"refresh_token": "1.AUs...",
"client_id": "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx",
"client_secret": "...",
"tenant_id": "common"
}
client_secret is only needed for confidential-client refresh flows; device-code tokens (from office-connect login) refresh without one and the field can be omitted or empty.
Mock Transport
A full mock layer for development and testing — no real O365 account needed.
from office_con.msgraph.ms_graph_handler import MsGraphInstance
from office_con.testing.fixtures import default_mock_profile
profile = default_mock_profile()
graph = MsGraphInstance(endpoint="https://graph.microsoft.com/v1.0/")
graph.enable_mock(profile)
# Now use graph exactly like the real thing
mail = graph.get_mail()
inbox = await mail.email_index_async(limit=10)
The mock provides:
- 18+ inbox messages (rich HTML with signatures, newsletters, notifications)
- 9 mail folders with subfolders, fake downloadable attachments
- 127 calendar events across 3 months (OOF, Teams calls, tentative, free blocks)
- 25 directory users with org hierarchy, departments, and profile photos
- Teams, chats, categories, OneDrive stubs
- Synthetic JWT tokens
Face photos can be loaded from JPEG files via set_faces_dir() or the FACES_DIR env var. Falls back to generated SVG initials.
Safety: mock is automatically blocked on Azure App Service and production URLs.
Project Structure
office-connect/
├── office_con/
│ ├── auth/ # Azure AD OAuth, scopes, background refresh
│ ├── db/ # Company directory builder and storage
│ ├── msgraph/ # MS Graph API handlers
│ │ ├── ms_graph_handler.py # Central class: MsGraphInstance
│ │ ├── mail_handler.py # List, get, search, draft, send, move, delete
│ │ ├── mail_filter.py # KQL builder for mail/event search
│ │ ├── calendar_handler.py # Events, schedules, timezones
│ │ ├── places_handler.py # Meeting rooms, room availability
│ │ ├── directory_handler.py # Users, managers, photos
│ │ ├── teams_handler.py # Teams, channels, messages
│ │ ├── chat_handler.py # 1:1, group, meeting chats
│ │ ├── files_handler.py # OneDrive, SharePoint
│ │ └── profile_handler.py # /me profile
│ ├── testing/ # Mock transport, fixtures, synthetic tokens
│ ├── web/ # FastAPI helpers (image cache router)
│ ├── utils/ # Misc utilities (excel parser, …)
│ ├── peek.py # PDF / xlsx / docx preview generation
│ ├── mcp_permissions.py # Permission tiers + policy file resolution
│ └── mcp_server.py # MCP server entry point + CLI subcommands
├── tests/
├── scripts/ # Standalone scripts (room availability, …)
├── docs/
├── pyproject.toml
└── LICENSE
Development
poetry install
poetry run pytest # full suite — mocks always run; integration tests auto-discover a token
poetry run ruff check # lint
The mock-based unit tests need no credentials. Integration tests resolve a token from, in order:
tests/msgraph_test_token.json(legacy, gitignored)~/.config/office-connect/token.json(written byoffice-connect login)token_filepath insidetests/test_config.json(gitignored)
Once you've run office-connect login once, the integration suite picks the keyfile up automatically and the bulk of the suite runs without any extra config. A handful of tests stay skipped until you supply tenant-specific expectations (room names, team names, presence users) in tests/test_config.json.
MCP Tools (38)
Every tool is gated by the permission tier in the third column. At read_only only the read_only rows are advertised; drafts adds the drafts rows; all exposes everything.
Profile & directory
| Tool | Description | Tier |
|---|---|---|
o365_check_connection |
Verify the session is authenticated (health check) | read_only |
o365_get_profile |
Current user's profile | read_only |
o365_list_users |
Organization directory | read_only |
o365_get_user_manager |
A user's manager | read_only |
Mail results carry recipient lists (to_recipients/cc_recipients), conversation_id, and internet_message_id on both list/search and get — so an agent can thread a conversation and filter senders/recipients in-memory without N extra fetches. List/search return header metadata only (no body); fetch bodies with o365_get_mail/o365_get_mails (default body_format="text", truncated at 50 000 chars with body_truncated).
| Tool | Description | Tier |
|---|---|---|
o365_list_mail |
List emails in a folder (header metadata + recipients); folder / exclude_folders |
read_only |
o365_get_mail |
Single email; body_format=text|html|none, max_body_chars; event_id for meeting requests |
read_only |
o365_get_mails |
Batch-fetch many emails in one $batch round trip (e.g. a whole thread) |
read_only |
o365_search_mail |
KQL search via Graph $search; folder / exclude_folders |
read_only |
o365_unread_counts |
Per-folder unread/total counts without paging | read_only |
o365_get_mail_categories |
Outlook categories | read_only |
o365_create_mail_draft |
Create a draft (NOT sent) | drafts |
o365_update_mail_draft |
Update an existing draft | drafts |
o365_send_mail |
Send an email immediately | all |
o365_send_mail_draft |
Send an existing draft | all |
o365_reply_to_mail |
Reply / reply-all and send (keeps threading) | all |
o365_forward_mail |
Forward to new recipients and send | all |
o365_delete_mail |
Soft-delete (Deleted Items) | all |
o365_move_mail |
Move to another folder | all |
o365_flag_mail_read |
Mark read / unread | all |
o365_set_mail_categories |
Set categories on a message | all |
Calendar & rooms
| Tool | Description | Tier |
|---|---|---|
o365_list_calendars |
User's calendars | read_only |
o365_get_events |
Events in a date range | read_only |
o365_search_events |
Search events via $filter |
read_only |
o365_get_schedule |
Free/busy availability | read_only |
o365_list_rooms |
Meeting rooms with capacity / building / floor | read_only |
o365_get_room_availability |
Today's room availability | read_only |
o365_create_event |
Create event (sends invites if attendees) | all |
o365_update_event |
Update an existing event (PATCH; notifies attendees) | all |
o365_send_event_invite |
Create a meeting + send invites end-to-end | all |
Teams & chat
| Tool | Description | Tier |
|---|---|---|
o365_list_teams |
Joined teams | read_only |
o365_list_channels |
Channels in a team | read_only |
o365_get_channel_messages |
Recent channel messages | read_only |
o365_get_team_members |
Team members | read_only |
o365_list_chats |
Recent chats (1:1, group, meeting) | read_only |
o365_get_chat_messages |
Recent chat messages | read_only |
o365_get_chat_members |
Chat members | read_only |
o365_search_messages |
Search Teams + chat messages in one call | read_only |
Files / OneDrive / SharePoint
| Tool | Description | Tier |
|---|---|---|
o365_get_my_drive |
Default OneDrive info | read_only |
o365_list_drive_items |
Files and folders | read_only |
o365_get_file_content |
Download content (UTF-8 or base64) | read_only |
o365_peek_drive_file |
Preview a PDF / xlsx / docx without full download | read_only |
o365_peek_mail_attachment |
Preview an email attachment without full download | read_only |
o365_search_files |
Search OneDrive by name or content | read_only |
o365_search_sites |
Search SharePoint sites | read_only |
o365_get_site_drives |
Site document libraries | read_only |
License
MIT -- Copyright (c) 2026 Michael Ikemann
Metadata
Release files for office-connect 0.1.9
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| office_connect-0.1.9.tar.gz | 190.8 kB | Without provenance | Details for office_connect-0.1.9.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| office_connect-0.1.9-py3-none-any.whl | Python 3 | none | any | Details for office_connect-0.1.9-py3-none-any.whl |
Total release size: 399.3 kB
Release files / office_connect-0.1.9.tar.gz
| Download URL | office_connect-0.1.9.tar.gz |
|---|---|
| Size | 190.8 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
97f67a51605170e37b5ea2beeda30eafe1171d8ead04851df1d096a489ecb3f6
|
|
BLAKE2b-256 checksum How to use checksums |
e29528f1aeb162b5506c3725d1688c857a5fa48bc3afed058f701b446b6d3816
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
poetry/2.1.1 CPython/3.13.11 Darwin/25.5.0
|
Release files / office_connect-0.1.9-py3-none-any.whl
| Download URL | office_connect-0.1.9-py3-none-any.whl |
|---|---|
| Size | 208.5 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
f91edc06b1e996b715b149ce6f624a8c2891a934c45433509f4a3f29e3c6573a
|
|
BLAKE2b-256 checksum How to use checksums |
65628e7a7c6f086dda898ae52ba446f2b580685bfe8a3ac1dbc0c711d3e28b50
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
poetry/2.1.1 CPython/3.13.11 Darwin/25.5.0
|
Security for office-connect 0.1.9
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.