m365-mcp-server

m365-mcp-server

A production-ready MCP server that provides secure, delegated access to Microsoft 365 services including Email, SharePoint, OneDrive, and Calendar. It enables AI models to search messages, browse files, manage calendar events, and parse document contents using OAuth 2.1 authentication.

Category
访问服务器

README

m365-mcp-server

A production-ready MCP (Model Context Protocol) server for Microsoft 365, providing secure access to Email, SharePoint, and OneDrive through Azure AD/Entra ID authentication with OAuth 2.1 + PKCE.

Features

  • Email Access: List folders, search messages, read email content (including shared mailboxes)
  • Calendar Access: List calendars, browse events, expand recurring events with date ranges
  • SharePoint/OneDrive: Browse sites, drives, folders, and read file content
  • Document Parsing: Extracts readable text from PDF, Word, Excel, PowerPoint, CSV, and HTML files
  • OAuth 2.1 + PKCE: Secure authentication via Azure AD/Entra ID
  • Delegated Permissions: Users access only their authorized content
  • Open WebUI Compatible: Works with native MCP or MCPO proxy
  • Production Ready: Docker support, security hardening, structured audit logging
  • Token Revocation: RFC 7009 compliant token revocation endpoint

Quick Start

1. Azure AD Setup

Follow docs/entra-app-registration.md to create an Azure AD app registration with these permissions:

  • openid, offline_access (OIDC)
  • User.Read, Mail.Read, Mail.Read.Shared, Files.Read, Sites.Read.All, Calendars.Read (Microsoft Graph)

2. Configuration

Create a .env file:

# Azure AD / Entra ID (required)
AZURE_CLIENT_ID=your-client-id
AZURE_CLIENT_SECRET=your-client-secret
AZURE_TENANT_ID=your-tenant-id

# Server
MCP_SERVER_PORT=3000
MCP_SERVER_BASE_URL=http://localhost:3000
SESSION_SECRET=$(openssl rand -hex 32)

# Optional
LOG_LEVEL=info
REDIS_URL=redis://localhost:6379

# OAuth signing keys (required in production)
# OAUTH_SIGNING_KEY_PRIVATE=<base64-encoded PEM>
# OAUTH_SIGNING_KEY_PUBLIC=<base64-encoded PEM>

3. Run Locally

# Install dependencies
npm install

# Development mode
npm run dev

# Production build
npm run build
npm start

4. Authenticate

  1. Open http://localhost:3000/auth/login in a browser
  2. Sign in with your Microsoft 365 account
  3. Note the session ID returned after login

Docker Deployment

Basic

cd docker
docker-compose up -d m365-mcp-server redis

With Open WebUI

cd docker
docker-compose --profile with-webui up -d

With MCPO Proxy

cd docker
docker-compose --profile with-mcpo up -d

Open WebUI Integration

Option A: Native MCP (Recommended)

  1. In Open WebUI, go to Admin Settings > Tools
  2. Add MCP Server:
    {
      "url": "http://localhost:3000/mcp",
      "transport": "streamable-http"
    }
    
  3. Complete OAuth login when prompted

Option B: Via MCPO Proxy

  1. Start MCPO with the provided config:
    mcpo --config docker/mcpo-config.json --port 8000
    
  2. In Open WebUI, add as OpenAPI Tool:
    http://localhost:8000/openapi.json
    

MCP Tools

Email Tools

Tool Description
mail_list_messages List messages with optional filters (supports shared mailboxes)
mail_get_message Get full message details with body (HTML→text), CC/BCC, and attachment metadata
mail_list_folders List mail folders or subfolders (supports shared mailboxes)
mail_get_attachment Read and parse email attachments (PDF, Word, Excel, PowerPoint, CSV, HTML→text). Max 20MB

All email tools accept an optional mailbox parameter (email address or user ID) to access shared mailboxes. Omit to use your personal mailbox. Requires Mail.Read.Shared permission with admin consent.

SharePoint/OneDrive Tools

Tool Description
sp_list_sites Search and list SharePoint sites
sp_list_drives List drives (OneDrive/document libraries)
sp_list_children List folder contents
sp_get_file Get file content with automatic document parsing (PDF, Word, Excel, PowerPoint → text). Max 20MB

OneDrive Tools

Tool Description
od_my_drive Get personal OneDrive info including drive ID and storage quota
od_list_files List files and folders in personal OneDrive (root or subfolder)
od_get_file Get file content by item_id with automatic document parsing (PDF, Word, Excel, PowerPoint). Max 20MB
od_search Search for files in personal OneDrive only
od_recent List recently accessed files
od_shared_with_me List files shared with you by others

Calendar Tools

Tool Description
cal_list_calendars List all calendars with metadata
cal_list_events List events with optional date range (expands recurring events)
cal_get_event Get full event details including body/description

Requires Calendars.Read permission (no admin consent needed). Provide start_date and end_date to expand recurring events into individual occurrences.

API Endpoints

Endpoint Method Description
/health GET Health check
/auth/login GET Initiate OAuth login
/auth/callback GET OAuth callback
/auth/logout GET Logout and revoke session
/auth/status GET Check authentication status
/revoke POST Token revocation (RFC 7009)
/mcp POST MCP JSON-RPC endpoint
/mcp GET MCP SSE stream endpoint
/mcp DELETE Terminate MCP session

Security

  • OAuth 2.1 + PKCE: Required for all authentication flows
  • Delegated Permissions Only: No app-only access, read-only Graph scopes (Mail.Read.Shared requires admin consent)
  • Token Encryption: AES-256-GCM encryption for session tokens at rest
  • PII Redaction: Sensitive data (tokens, emails, secrets) filtered from logs
  • Structured Audit Logging: Security events logged with correlation IDs
  • Rate Limiting: 100 req/min general, 5/hour for client registration
  • Security Headers: HSTS, CSP (no unsafe-inline), Permissions-Policy, X-Frame-Options via Helmet
  • Input Validation: Zod schemas + regex validation for all Graph API resource IDs
  • DCR Protection: Redirect URI pattern whitelist, rate limiting, audit logging
  • Production Enforcement: Config validation requires Redis, HTTPS, persistent signing keys

See docs/security/threat-model.md for full security analysis.

Architecture

┌─────────────────────────────────────────────────────────────┐
│                      Open WebUI / Client                     │
└─────────────────────────────┬───────────────────────────────┘
                              │ MCP Protocol (Streamable HTTP)
                              ▼
┌─────────────────────────────────────────────────────────────┐
│                    m365-mcp-server                           │
│  ┌─────────────┐  ┌─────────────┐  ┌──────────────────────┐ │
│  │ OAuth 2.1   │  │ MCP Handler │  │ Microsoft Graph      │ │
│  │ + PKCE      │  │ (JSON-RPC)  │  │ Client               │ │
│  └──────┬──────┘  └─────────────┘  └──────────┬───────────┘ │
└─────────│────────────────────────────────────│──────────────┘
          │                                    │
          ▼                                    ▼
┌──────────────────────┐            ┌─────────────────────────┐
│  Azure AD / Entra ID │            │   Microsoft Graph API   │
│  (Authorization)     │            │   (Data Access)         │
└──────────────────────┘            └─────────────────────────┘

Environment Variables

Variable Required Default Description
AZURE_CLIENT_ID Yes - Azure AD app client ID
AZURE_CLIENT_SECRET Yes - Azure AD app client secret
AZURE_TENANT_ID Yes - Azure AD tenant ID
SESSION_SECRET Yes - Session encryption key (32+ chars)
MCP_SERVER_PORT No 3000 Server port
MCP_SERVER_BASE_URL No http://localhost:3000 Public URL (HTTPS required in production)
REDIS_URL Prod - Redis URL (required in production)
OAUTH_SIGNING_KEY_PRIVATE Prod - RSA private key PEM (required in production)
OAUTH_SIGNING_KEY_PUBLIC Prod - RSA public key PEM (required in production)
OAUTH_ALLOWED_REDIRECT_PATTERNS No - Comma-separated URI patterns for DCR
LOG_LEVEL No info Log level (trace/debug/info/warn/error)
NODE_ENV No development Environment mode
FILE_PARSE_TIMEOUT_MS No 30000 Document parsing timeout
FILE_PARSE_MAX_OUTPUT_KB No 500 Max parsed text output size

Development

# Install dependencies
npm install

# Run tests
npm test

# Run tests with coverage
npm run test:coverage

# Lint
npm run lint

# Type check
npm run typecheck

# Build
npm run build

MCP Registry

This server is published to the MCP Registry. Add to your MCP client:

{
  "mcpServers": {
    "m365": {
      "command": "npx",
      "args": ["-y", "@anthropic/m365-mcp-server"],
      "env": {
        "AZURE_CLIENT_ID": "your-client-id",
        "AZURE_CLIENT_SECRET": "your-client-secret",
        "AZURE_TENANT_ID": "your-tenant-id",
        "SESSION_SECRET": "your-session-secret"
      }
    }
  }
}

Documentation

Supported Document Formats

sp_get_file automatically extracts readable text from these formats:

Format Extensions Library
PDF .pdf pdf-parse
Word .docx, .doc mammoth
Excel .xlsx, .xls exceljs
PowerPoint .pptx, .ppt Built-in ZIP/XML
CSV .csv Built-in
HTML .html Built-in

Other binary formats are returned as base64. Parsed text output is limited to 500KB by default.

Known Limitations

  • Maximum file download size: 20MB
  • Parsed text output capped at 500KB (configurable via FILE_PARSE_MAX_OUTPUT_KB)
  • SharePoint site listing requires search query (Graph API limitation)
  • Refresh tokens limited to 24 hours for SPA scenarios
  • No write operations (read-only by design)
  • Access tokens (JWTs) are stateless and cannot be directly revoked (expire naturally)

License

MIT

Contributing

  1. Fork the repository
  2. Create a feature branch
  3. Submit a pull request

Please ensure all tests pass and the code follows the existing style.

推荐服务器

Baidu Map

Baidu Map

百度地图核心API现已全面兼容MCP协议,是国内首家兼容MCP协议的地图服务商。

官方
精选
JavaScript
Playwright MCP Server

Playwright MCP Server

一个模型上下文协议服务器,它使大型语言模型能够通过结构化的可访问性快照与网页进行交互,而无需视觉模型或屏幕截图。

官方
精选
TypeScript
Magic Component Platform (MCP)

Magic Component Platform (MCP)

一个由人工智能驱动的工具,可以从自然语言描述生成现代化的用户界面组件,并与流行的集成开发环境(IDE)集成,从而简化用户界面开发流程。

官方
精选
本地
TypeScript
Audiense Insights MCP Server

Audiense Insights MCP Server

通过模型上下文协议启用与 Audiense Insights 账户的交互,从而促进营销洞察和受众数据的提取和分析,包括人口统计信息、行为和影响者互动。

官方
精选
本地
TypeScript
VeyraX

VeyraX

一个单一的 MCP 工具,连接你所有喜爱的工具:Gmail、日历以及其他 40 多个工具。

官方
精选
本地
graphlit-mcp-server

graphlit-mcp-server

模型上下文协议 (MCP) 服务器实现了 MCP 客户端与 Graphlit 服务之间的集成。 除了网络爬取之外,还可以将任何内容(从 Slack 到 Gmail 再到播客订阅源)导入到 Graphlit 项目中,然后从 MCP 客户端检索相关内容。

官方
精选
TypeScript
Kagi MCP Server

Kagi MCP Server

一个 MCP 服务器,集成了 Kagi 搜索功能和 Claude AI,使 Claude 能够在回答需要最新信息的问题时执行实时网络搜索。

官方
精选
Python
e2b-mcp-server

e2b-mcp-server

使用 MCP 通过 e2b 运行代码。

官方
精选
Neon MCP Server

Neon MCP Server

用于与 Neon 管理 API 和数据库交互的 MCP 服务器

官方
精选
Exa MCP Server

Exa MCP Server

模型上下文协议(MCP)服务器允许像 Claude 这样的 AI 助手使用 Exa AI 搜索 API 进行网络搜索。这种设置允许 AI 模型以安全和受控的方式获取实时的网络信息。

官方
精选