OneDrive文件共享服务器

一个基于Microsoft Graph API的轻量级、安全的OneDrive文件共享MCP服务器,提供文件管理、搜索和共享链接生成等功能。

  • by MrFixit96
  • Python
  • 本地部署
  • 7 星标
  • 更新于
在 GitHub 查看 MrFixit96/onedrive-mcp-server

使用说明

OneDrive MCP服务器

最小,安全 主控程序 用于通过Microsoft Graph API共享OneDrive文件的服务器。

作为一种安全的替代方案 安全审计 在中发现了4个关键漏洞 elyxlz/microsoft-mcp 和官方 microsoft/files-mcp-server 缺少共享链接支持。

特性

工具 说明
list_files 列出任何OneDrive路径中的文件和文件夹
get_file_metadata 获取文件的大小、类型、日期和创建者
search_files 跨文件名和内容的全文搜索
upload_file 上传本地文件(可恢复>4 MB)
download_file 下载具有路径遍历保护的文件
create_sharing_link 生成查看或编辑共享链接

快速开始

选项A:HTTP模式(推荐用于企业——零配置SSO)

pip install git+https://github.com/MrFixit96/onedrive-mcp-server.git
onedrive-mcp --http

MCP客户端(VS代码、Claude代码)自动处理所有OAuth。 当它首次需要OneDrive访问权限时,VS Code会显示其标准的“使用Microsoft登录”提示。 没有设备代码,没有应用程序注册,没有环境变量。

选项B:Stdio模式(带开关设备代码认证)

pip install git+https://github.com/MrFixit96/onedrive-mcp-server.git
onedrive-mcp auth   # One-time sign-in
onedrive-mcp        # Start server

安全

  • 零配置身份验证(HTTP模式):MCP客户端通过RFC 9728受保护资源元数据处理OAuth——无客户端ID、无应用程序注册、无环境变量
  • 企业友好:适用于阻止第三方应用程序同意的租户——MCP客户端使用其自己的预先批准的凭据
  • 直通令牌验证:解码用于审计日志记录的承载令牌;Microsoft Graph对每个API调用进行加密验证
  • Windows SSO:WAM代理集成使用您现有的Windows Microsoft帐户
  • OS密钥环令牌存储:Windows凭据保险库、macOS钥匙链或Linux SecretService(退回到仅限所有者的文件)
  • 缩小OAuth范围:只有 Files.ReadWrite + User.Read --无法访问邮件、日历或联系人
  • 错误清理:在到达LLM之前,从所有错误消息中删除承载令牌、相关性ID和内部URL
  • 审计日志:结构化JSON到stderr——每个工具调用都记录了时间、编辑参数和状态
  • 路径遍历保护:根据安全基目录验证下载路径
  • 无eval/literal_eval:零使用 eval, ast.literal_eval,或 exec

安装

# Clone and install
git clone https://github.com/MrFixit96/onedrive-mcp-server.git
cd onedrive-mcp-server
pip install -e .

# Optional: install Windows SSO broker support
pip install -e ".[broker]"

# Optional: install dev tools (pytest, ruff)
pip install -e ".[dev]"

认证

HTTP模式(推荐)

onedrive-mcp --http              # Start on default port 3001
onedrive-mcp --http --port 8080  # Custom port

无需设置。MCP客户端(VS代码)处理整个OAuth流程:

  1. 客户端通过以下方式发现所需的作用域 /.well-known/oauth-protected-resource
  2. 客户端使用其自己批准的凭据与Microsoft进行身份验证
  3. 客户端在每次请求时都会将承载令牌传递给服务器
  4. Microsoft Graph在服务器端验证令牌

标准模式(回退)

# Just sign in — no environment variables needed
onedrive-mcp auth

在Windows上 (安装了代理):使用您现有的Windows Microsoft帐户会出现一个本机SSO弹出窗口。单击一下,完成。

在其他平台上:设备代码流——访问显示的URL,输入代码,使用您的公司电子邮件登录。您所在组织的SSO/身份提供者会自动处理登录。

令牌安全地缓存在您的操作系统密钥环中(Windows凭据保险库/macOS密钥链/Linux SecretService)。

高级:自定义Azure AD应用程序

如果您的组织需要特定的应用程序注册,您可以覆盖默认值:

export ONEDRIVE_MCP_CLIENT_ID=<your-client-id>
export ONEDRIVE_MCP_TENANT_ID=<your-tenant-id>
onedrive-mcp auth

MCP服务器配置

克劳德代码(HTTP模式——推荐)

{
  "mcpServers": {
    "onedrive": {
      "type": "http",
      "url": "http://localhost:3001/mcp"
    }
  }
}

克劳德码(stdio模式)

{
  "mcpServers": {
    "onedrive": {
      "command": "onedrive-mcp"
    }
  }
}

VS代码副本(HTTP模式)

{
  "mcp": {
    "servers": {
      "onedrive": {
        "type": "http",
        "url": "http://localhost:3001/mcp"
      }
    }
  }
}

环境变量

变量 必填 默认 描述
ONEDRIVE_MCP_CLIENT_ID 否 Office桌面ID 用自定义Azure AD应用程序客户端ID覆盖(仅限stdio模式)
ONEDRIVE_MCP_TENANT_ID 没有 organizations Azure AD租户(仅限stdio模式)
ONEDRIVE_MCP_PORT 没有 3001 HTTP服务器端口(仅限HTTP模式)
ONEDRIVE_MCP_DOWNLOAD_DIR 没有 . (cwd) 文件下载的基本目录
ONEDRIVE_MCP_LOG_LEVEL 没有 INFO 审核日志级别(调试、信息、警告、错误)

使用示例

一旦配置为MCP服务器,您的AI助手就可以使用工具:

"List my OneDrive files"
→ calls list_files("/")

"Upload docs/report.docx to my OneDrive Documents folder"
→ calls upload_file("C:\path\to\docs\report.docx", "Documents/report.docx")

"Create a sharing link for Documents/report.docx"
→ calls create_sharing_link("Documents/report.docx", "view", "organization")

"Search for files about quarterly budget"
→ calls search_files("quarterly budget")

发展

# Install dev dependencies
pip install -e ".[dev]"

# Run tests (43 tests)
pytest tests/ -v

# Lint
ruff check src/ tests/

# Auto-fix lint issues
ruff check src/ tests/ --fix

建筑

src/onedrive_mcp/
├── __init__.py       # Package metadata
├── __main__.py       # CLI: auth subcommand + server startup
├── auth.py           # SSO broker + device-code flow + OS keyring cache
├── graph.py          # Async Microsoft Graph client (httpx) + error sanitization
└── server.py         # FastMCP server with 6 tools + audit logging
  • ~400条线路 核心逻辑(身份验证+图形+服务器)
  • 47项测试 涵盖身份验证、图形API、服务器工具、错误清除、HTTP模式、令牌验证器
  • 双重运输:stdio(STROAuth)+HTTP(RFC 9728承载令牌传递)
  • 零 第三方MCP包装器——使用官方 mcp 软件开发工具包
  • 异步 httpx用于HTTP,同步通过 asyncio.to_thread
  • 工作室 传输(标准MCP协议)

身份验证流程

HTTP Mode (--http):
  MCP Client (VS Code)
      │
      ├─ GET /.well-known/oauth-protected-resource
      │     → learns required scopes + auth server
      │
      ├─ OAuth with Microsoft (client's own credentials)
      │
      └─ POST /mcp with Authorization: Bearer <token>
            → server passes token to Graph API

Stdio Mode (default):
  onedrive-mcp auth
      │
      ├─ Cached token? ──→ Silent refresh ──→ Done
      │
      ├─ Windows + broker? ──→ Native SSO popup ──→ Done
      │
      └─ Device code flow ──→ microsoft.com/devicelogin
                                   │
                                   └─ Org SSO ──→ Done

许可证

麻省理工学院