HEXUXIU/M365-Copilot2API

Microsoft 365 Copilot → OpenAI / Anthropic 兼容 API 网关。

What is M365 Copilot2API?

M365 Copilot2API is a self‑hosted gateway written in Go that lets you use any client that expects an OpenAI‑compatible or Anthropic‑compatible HTTP API (e.g., ChatGPT‑style SDKs, Claude Code, Cursor, OpenCode) to talk to Microsoft 365 Copilot. Internally it speaks the private ChatHub WebSocket protocol used by the M365 Copilot service, then translates those messages into the standard JSON payloads defined by the OpenAI and Anthropic APIs.

Why it exists

  • Microsoft 365 Copilot is only available through a commercial subscription and its API is not publicly documented. The gateway reverse‑engineers the WebSocket protocol and exposes a familiar REST interface.
  • This lets developers reuse existing tooling, libraries, and agents without rewriting them for a proprietary protocol.
  • It also adds a management console, API‑key handling, multi‑account rotation, proxy pools, usage statistics, and caching – features you would normally have to build yourself.

Core features (as described in the README)

Feature What it does
OpenAI‑compatible /v1/chat/completions Accepts the same JSON schema, supports streaming (stream:true) and function‑calling.
Anthropic‑compatible /v1/messages Works with Claude Code, Cursor, etc., using the Anthropic request format.
Responses endpoint (/v1/responses) Compatibility with OpenAI’s older Responses protocol (e.g., Codex).
SSE streaming Returns token‑by‑token events just like the official APIs.
Tool‑call conversion Maps OpenAI function calls to the M365 Copilot tool protocol (two planning modes: router or native).
Content‑key session reuse Identical conversation context is cached; subsequent requests only send the new messages to the upstream, saving tokens.
Explicit session binding Header X‑M365‑Session‑Id forces a request to continue a specific cloud conversation.
Automatic cleanup Idle cloud conversations are reclaimed after a configurable TTL (default 2 h) or when a max‑size limit is hit.
Multi‑account management OAuth/PKCE flow, round‑robin request distribution, and automatic fail‑over if an account becomes invalid.
API‑key management Web UI to create, revoke, and view keys that clients use for authentication.
Proxy pool Supports HTTP, HTTPS, and SOCKS5 proxies with health‑checks and cooldown on failures.
Usage statistics Logs per‑key, per‑account, per‑model usage to usage.jsonl and shows hit‑rate counters on the dashboard.
Multimodal input Accepts image data (base64 data URLs or public HTTPS URLs) and forwards them to M365’s UploadFile endpoint, then injects the file reference into the chat message.
Image generation Exposes /v1/images/generations mirroring OpenAI’s image API.
Web management console Full UI for login, account auth, key management, proxy pool, conversation view, model testing, and settings.

How it works – high‑level architecture

OpenAI/Anthropic client  ──►  HTTP endpoint (/v1/…)  ──►  M365‑Copilot2API (Go)
                                                   │
                                                   │  internal/chathub
                                                   ▼
                                            ChatHub WebSocket (private)
                                                   │
                                                   ▼
                                            Microsoft 365 Copilot (cloud)
  • internal/chathub: Handles the low‑level WebSocket handshake, heart‑beats, and event‑stream parsing for the private ChatHub protocol.
  • internal/web/session_resolver.go: Decides which M365 account and which cloud conversation a request should be bound to, implementing the content‑key reuse logic.
  • Account rotation & fail‑over: If a request hits a rate‑limit, auth error, or other upstream failure, the gateway automatically retries with the next healthy account.

Getting started (quick‑start steps from the README)

  1. Download a pre‑built binary for your OS/architecture from the GitHub Releases page.
  2. Run it – it listens on 127.0.0.1:4141 by default and uses the default admin password admin123 (you’ll be forced to change it on first login).
  3. Open http://127.0.0.1:4141 in a browser, log in, and use the Accounts page to start an OAuth/PKCE flow with your Microsoft 365 credentials.
  4. After the callback URL is pasted back into the UI, create an API Key on the API Keys page.
  5. Call the gateway with any OpenAI‑compatible client, e.g.:
    curl http://127.0.0.1:4141/v1/chat/completions \
         -H "Authorization: Bearer <YOUR_API_KEY>" \
         -H "Content-Type: application/json" \
         -d '{"model":"gpt-5.6-sol","messages":[{"role":"user","content":"你好"}]}'
    

Configuration highlights

All settings are environment variables (a .env.example is provided). Important knobs include:

  • M365_LISTEN – address/port to bind.
  • M365_ADMIN_PASSWORD – admin login password.
  • M365_PROXY_POOL – comma‑separated list of proxies.
  • M365_TOOL_PLANNING_MODErouter (gateway decides tool routing) or native (let the upstream Copilot handle it).
  • Session‑related TTLs (M365_SESSION_TTL_MINUTES, M365_CONTEXT_TTL_MINUTES).
  • Auto‑cleanup controls (M365_AUTO_CLEANUP_*).

Typical use‑cases

  • Developers who want to experiment with Microsoft 365 Copilot using existing OpenAI SDKs without writing a custom client.
  • Teams building internal agents that need to call Copilot but must keep a uniform API surface across providers (OpenAI, Anthropic, M365).
  • Power users who want a local dashboard to monitor usage, rotate multiple Microsoft accounts, and cache conversation context to reduce token consumption.

Limitations & legal notice (as stated by the authors)

  • The project is not an official Microsoft product and has no affiliation with Microsoft, OpenAI, or Anthropic.
  • Accessing Copilot through third‑party accounts or proxy pools may violate the service’s Terms of Service; the user assumes all risk.
  • Intended for personal learning/research only – commercial resale or large‑scale deployment is prohibited.
  • No liability for account bans, data loss, or other damages.

All information above is taken directly from the repository’s README; no additional features have been inferred.

Related

  • Project
  • Project
  • Project
  • Project