Giving My AI Assistant the Keys to Outlook (Only the Ones It Needs)

I self-hosted a Microsoft 365 MCP server in Docker so my AI assistant could read my calendar and manage my to-dos, with its own app registration, an explicit tool allowlist, and every dependency pinned. Here's the build, and the three bugs that bit me.

An ornate Norse key with a glowing gold rune; its teeth are neon calendar, checklist and mail icons, the mail icon crossed out in red.

I run a “second brain”: a set of notes an AI assistant reads every morning to brief me on what’s coming. It was missing the most obvious input: my calendar. I wanted the assistant to read my personal Outlook and keep a to-do list I can tick off on my phone.

What I didn’t want was to hand an AI my whole Microsoft account through somebody else’s connector. So I built it myself, the way I’d want it built at work.

The rules I set before writing anything

  • Self-hosted. The server runs on my own machine, bound to localhost. Nothing exposed to the internet.
  • My own identity plumbing. My own app registrationapp registration An application's identity in Microsoft Entra ID: what it's called, who can sign in to it, and exactly which permissions it may ask for., personal accounts only, no client secret sitting anywhere.
  • Least privilegeleast privilege Give every person, service and tool only the access it needs for its job, and nothing more. When something is compromised, the damage stays small., provably. Read mail, never send it. Write to the calendar and To Do, never delete.
  • Nothing updates on its own. Every dependency pinned, with a local copy I control.

The stack

MCPMCP Model Context Protocol. An open standard for connecting AI assistants to tools and data through small servers, each exposing only the actions you choose. (Model Context Protocol) is the standard way AI assistants call tools. I used an open-source Microsoft 365 MCP server, built into a small DockerDocker A tool for packaging software and everything it needs into containers, so it runs the same way on any machine. image from a pinned npm version, running as a non-root user with all Linux capabilities dropped. It speaks HTTP on port 3000, published only on 127.0.0.1. The desktop AI app reaches it through a small bridge (mcp-remote).

Sign-in goes through an app registration I created in my own personal Entra directory: personal Microsoft accounts only, public client flows on, delegated permissions only, and no client secret. How the sign-in itself works changed a day after I first published this; that’s bug three below.

Least privilege you can check

The server ships with dozens of tools. A read-only flag is a good start, but I needed some writes. So instead of flags, I gave it an explicit allowlist: a regex of tool names. Reads for mail, calendar, To Do and contacts; create and update for calendar events and tasks; nothing else. No delete, no send, no drafts, no calendar sharing.

command:
  - "--http"
  - "3000"
  - "--enabled-tools"
  - "^(get-(calendar|mail|todo)[a-z-]*|list-(calendar|mail|contact|todo)[a-z-]*|(create|update)-(calendar-event|todo-task))$$"

(Simplified. Note the $$: Docker ComposeDocker Compose A single file that describes a set of containers, their settings and how they connect, so a whole service can be started or rebuilt with one command. would otherwise try to interpolate the $.)

The part I like most: the server derives the Microsoft Graph permissions it requests from the tools you enable, and it will print them. So before touching the app registration, I ran it with --list-permissions and checked the answer:

"permissions": [
  "Calendars.ReadWrite",
  "Contacts.Read",
  "Mail.Read",
  "MailboxSettings.Read",
  "Tasks.ReadWrite"
]

No Mail.Send, nothing ending in .All. That check is now a step in my upgrade procedure: if a new version ever asks for more, the upgrade stops.

Bug one: OAuth refuses plain HTTP (correctly)

I’d planned a tidy local hostname so I could move the containercontainer A lightweight, isolated package of an application and its dependencies. It shares the host's kernel, so it starts in seconds, unlike a full virtual machine. to my lab server later without reconfiguring clients. The bridge refused: InsecureTokenEndpointError: Refusing to send credentials to non-https token endpoint. Loopback addresses are exempt; anything else needs TLSTLS Transport Layer Security. The encryption behind HTTPS that keeps traffic between your browser and a site private and tamper-proof.. Fair enough. For now clients use localhost. When it moves to the lab it gets a reverse proxyreverse proxy A server that sits in front of your applications and receives requests on their behalf, handling TLS, routing and access checks in one place. with a real certificate and its own login in front (bug three explains why the login matters), and that stable HTTPSHTTPS HTTP over TLS: web traffic that's encrypted and tied to a certificate, so you know you're talking to the real site. name makes every later move a DNSDNS Domain Name System. The internet's phone book, turning names like cyberviking.ca into the IP addresses computers actually connect to. change.

Bug two: the error that said nothing

After adding the write permissions, sign-in failed with “Internal server error during token exchange”. The container log was empty. Running the server with verbose logging told the real story:

error: Token endpoint error: fetch failed {"cause":{"code":"ENETUNREACH"}}

ENETUNREACH on an outbound call to Microsoft: Node was resolving the sign-in endpoint to an IPv6IPv6 The newer internet addressing scheme, with vastly more addresses than IPv4. Mixed IPv4/IPv6 setups are a classic source of 'it works here but not there' bugs. address, and the container has no IPv6 route. It worked on some attempts and not others, which is what made it confusing. One line got sign-in working:

environment:
  NODE_OPTIONS: "--dns-result-order=ipv4first"

Update, a day later: it came back. Token refreshes started failing with the same ENETUNREACH. This time I checked what the container actually got back from DNS, and the answer was the whole story: only IPv6 addresses for Microsoft’s sign-in host, not a single IPv4 one. “Prefer IPv4” can’t prefer an address that isn’t there. The image was built on Alpine; a Debian-based container on the same Docker host resolved the same name to IPv4 without trouble. Rebuilding on node:24-slim fixed it for good, and the one-liner stays as a belt-and-braces setting. The check that settled it:

docker compose exec m365-mcp node -e "require('dns').lookup('login.microsoftonline.com',{all:true},(e,a)=>console.log(a))"

Lessons re-learned: when an error message is generic, turn on verbose logging before forming a theory. And when it’s a network error, look at what DNS returns inside the container, not on the host.

Bug three: it worked for exactly an hour

In the first version, the bridge did the sign-in: the desktop app ran an OAuthOAuth A standard that lets an app act on your behalf with a limited, revocable token, without ever seeing your password. flow, held the token, and passed it to the server on every call. Nothing ever refreshed it. When Microsoft’s access token expired after about an hour, the server relayed the 401 and every tool failed until I signed in again.

The fix was to let the server hold its own sign-in. I sign in once with a device code (--login); the server keeps its token cache in a Docker volume and refreshes it itself, and it’s pinned to my account so it can’t quietly end up signed in as someone else:

command:
  - "--http"
  - "3000"
  - "--trust-proxy-auth"
  - "--expected-username"
  - "<my account>"
  - "--enabled-tools"
  - "..."

That’s a real trade-off, and worth saying plainly. There is now a standing credential: an encrypted refresh token in that volume, with its key stored alongside it. And with --trust-proxy-auth the server accepts any caller that can reach it, so the only thing keeping others out is that the port is bound to 127.0.0.1. On a single-user machine that’s acceptable. Before it moves to a lab server, it needs an authenticating proxy in front.

One more twist: after switching, it still failed. docker ps -a showed why. An old container, started by hand during testing and given a random name, was still holding port 3000 and answering every request the old way. The new container had never started. Lesson: when a fix “doesn’t work”, first confirm that the thing you fixed is the thing that’s running.

Pin it, mirror it, own it

The bridge was being launched with npx -y mcp-remote, which quietly fetches the latest version every time the app starts. That’s a supply-chain risk on a component that sits in the path of every request. Everything is now pinned to an exact version in one file in my homelabhomelab A small lab of servers and network gear at home for learning, testing and self-hosting, where breaking things is part of the point. repo, and a script I run once a month keeps local copies of each upstream repo, the exact npm packages, and the Docker images. If an author deletes a repo or unpublishes a package, I can still rebuild. Upgrades happen when I decide, after reading the release notes.

What it does now

  • The 5:45 a.m. briefing reads my calendar.
  • A to-do list the assistant curates from my open items: I tick things off on my phone and it closes the loop.
  • A capture list: anything I type there on my phone gets filed into my notes.

None of it needed broad access. That’s the point I’d make to anyone wiring AI into their accounts, at home or at work: decide what it may do first, make the system enforce it, and verify it before you trust it.

ᛟ ᛟ ᛟ