graph-docs-mcp
by OraCool
README.md
# graph-docs-mcp
An MCP server that works with documents in **one** SharePoint/OneDrive document library through Microsoft Graph.
It reads text from `.docx`/`.pdf`, fills `.docx` templates, converts to PDF (with a leftover check), replaces text,
writes `.xlsx` tables, stamps text onto PDF backgrounds, and copies/moves files and creates folders.
There is no delete operation.
## Security model
- Authentication is app-only through an Azure managed identity (`IDENTITY_ENDPOINT` / `IDENTITY_HEADER`,
resource `https://graph.microsoft.com`; set `AZURE_CLIENT_ID` for a user-assigned identity). No secrets, no user tokens.
- Grant the identity the Graph application role `Lists.SelectedOperations.Selected`, then grant it access to the one library:
`POST /sites/{site-id}/lists/{list-id}/permissions` with `roles` of `["read"]` or `["write"]`.
Never grant tenant-wide `Files.*` or `Sites.*` roles.
- Fallback if the list-level grant is not available: `Sites.Selected` on a dedicated site that contains only this library.
- The server operates on a single drive (`DOCS_DRIVE_ID`) below an optional root folder (`DOCS_ROOT`).
## Safety rules
- Creates never overwrite: an existing target returns `already_exists` (Graph `conflictBehavior=fail`).
- Overwrites and moves need `if_match` (the etag you read); a changed file returns `changed_since_read`.
- Nothing is ever deleted: no tool and no client method calls Graph DELETE.
- Files whose name starts with a protected prefix (`DOCS_PROTECTED_PREFIXES`) change only with `confirm_template_change=true`.
The prefixes are file-name prefixes, not paths: a value containing `/` or `\` is refused at startup.
The check covers in-place edits, replacing an existing PDF (`docs_convert_to_pdf`), overwriting an existing `.xlsx`
(`docs_write_xlsx`), and both the source and the target name of `docs_move`.
- `docs_fill_template` and `docs_convert_to_pdf` check the PDF before upload. Check codes: `unfilled_token`,
`broken_reference`, `no_text`. Failure is `pdf_check_failed`.
- `docs_stamp_pdf` reports `pdf_check_failed` with these codes (plus the affected field keys, never values):
`missing_field`, `unknown_field`, `invalid_value`, `missing_glyph`, `too_wide`, `outside_page`,
`overlaps_forbidden_box`, `font_not_embedded`, `text_not_extractable`, `page_count_changed`, `unreadable_result`.
- Moving a folder requires `confirm_template_change=true`.
- A file open in Word returns `locked`; there is no retry loop.
## Configuration
Environment variables:
| Variable | Default | Meaning |
| --- | --- | --- |
| `DOCS_DRIVE_ID` | required | Graph drive id of the library |
| `DOCS_ROOT` | empty | Folder inside the drive that acts as the root; all paths are relative to it |
| `DOCS_PROTECTED_PREFIXES` | empty | Comma-separated file-name prefixes (not paths; `/` and `\` are refused) treated as protected templates (case-insensitive), e.g. `_TEMPLATE_` |
| `AZURE_CLIENT_ID` | empty | Client id of a user-assigned managed identity |
| `MAX_FILE_BYTES` | `20000000` | Largest file downloaded or produced |
| `MAX_TEXT_CHARS` | `200000` | Text returned by `docs_read_text` is cut at this length |
| `MCP_HOST` | `127.0.0.1` | Bind address |
| `MCP_PORT` | `8000` | Bind port |
| `LOG_LEVEL` | `WARNING` | `WARNING`, `ERROR` or `CRITICAL`; lower values are treated as `WARNING` |
## Tools
| Tool | What it does |
| --- | --- |
| `docs_list` | List files and folders (optionally recursive) with etags; at most 500 items, `truncated` is set when the cap is reached |
| `docs_read_text` | Text of a `.docx` or `.pdf`; for `.docx` also the `{{KEY}}` placeholders and etag |
| `docs_fill_template` | Fill `{{KEY}}` placeholders (keys must match exactly, at most 200 fields), write `target.docx` and optionally `target.pdf` |
| `docs_convert_to_pdf` | Convert a `.docx` to a checked PDF next to it; replacing an existing PDF needs its etag |
| `docs_replace_text` | Literal find/replace in a `.docx`; up to 50 items, each with `expected_count`, otherwise nothing is written |
| `docs_write_xlsx` | Write a table as `.xlsx` (values only; text starting with `= + - @` stays text); up to 20 sheets, 20000 rows x 200 columns, 200000 cells in total |
| `docs_copy` | Copy a file or folder to a new path (create-only; a name clash returns an error) |
| `docs_move` | Move or rename a file or folder, with `if_match` |
| `docs_create_folder` | Create a folder |
| `docs_stamp_pdf` | Stamp text fields onto a PDF background according to a JSON layout; up to 30 fields of 200 characters each; rotated pages are not supported |
If `docs_fill_template` creates the `.docx` but the PDF step fails, it returns `partial` with the `.docx` etag.
Fix the cause and call `docs_convert_to_pdf`; calling fill again returns `already_exists`.
Errors are JSON: `{"error": "<kind>", "message": "..."}` with kinds `invalid_path`, `not_found`, `already_exists`,
`changed_since_read`, `locked`, `fields_mismatch`, `template_unsupported`, `pdf_check_failed`, `count_mismatch`,
`protected`, `too_large`, `forbidden`, `graph_error`, `partial`. Messages contain no paths or values.
## Path rules
Paths are relative to the configured root: no leading `/`, no empty, `.` or `..` segments, none of
`" * : < > ? \ |`, no control characters, no segment ending in `.` or starting/ending with whitespace.
Characters in the Unicode categories Cc, Cf (for example the zero-width joiner), Zl and Zp are rejected.
A segment is at most 255 characters and the whole path at most 400. Paths are normalized to NFC.
Non-ASCII names, spaces and characters such as `#`, `%`, `&`, `+` are supported and percent-encoded per segment.
## Limits
- Files up to `MAX_FILE_BYTES` (default 20 MB); `.docx` archives are limited to 2000 entries, 200 MB uncompressed
in total and 50 MB per XML part.
- Behind an HTTP gateway with a 1 MB request cap, `docs_write_xlsx` tables are limited to roughly a few thousand rows.
- `docs_read_text` truncates at `MAX_TEXT_CHARS`.
- Logs contain only warnings and errors with tool name, outcome and error kind; never paths, names, values or contents.
## Running
```
export DOCS_DRIVE_ID=<drive id>
graph-docs-mcp
```
The server speaks MCP over streamable HTTP (stateless) on `MCP_HOST:MCP_PORT` (default `127.0.0.1:8000`);
stdio is not supported. The server itself performs no client authentication: run it behind an authenticating
gateway and never expose it directly. The managed identity is only available inside Azure (for example Container Apps).
## Development
```
python -m venv .venv
.venv/bin/pip install -e '.[dev]'
.venv/bin/pytest
```
Privacy test: `tests/test_repo_is_generic.py` scans every tracked file for terms from a deny-list kept outside the repo.
Run it with `PRIVATE_TERMS_FILE=/path/to/terms.txt .venv/bin/pytest tests/test_repo_is_generic.py`
(one term per line, case-insensitive); it is skipped when the variable is unset.
## License
MIT
This server cannot be deployed
Maintenance
ActivityMaintained
ResponsivenessNo recent community issues