diffmem-mcp-server 0.1.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- diffmem_mcp_server-0.1.0/.gitignore +65 -0
- diffmem_mcp_server-0.1.0/CHANGELOG.md +16 -0
- diffmem_mcp_server-0.1.0/CONTEXT.md +62 -0
- diffmem_mcp_server-0.1.0/Dockerfile +41 -0
- diffmem_mcp_server-0.1.0/PKG-INFO +15 -0
- diffmem_mcp_server-0.1.0/PRODUCTION_DEPLOYMENT.md +189 -0
- diffmem_mcp_server-0.1.0/README.md +234 -0
- diffmem_mcp_server-0.1.0/diffmem_mcp/__init__.py +1 -0
- diffmem_mcp_server-0.1.0/diffmem_mcp/api_client.py +137 -0
- diffmem_mcp_server-0.1.0/diffmem_mcp/asgi.py +171 -0
- diffmem_mcp_server-0.1.0/diffmem_mcp/auth.py +158 -0
- diffmem_mcp_server-0.1.0/diffmem_mcp/context.py +124 -0
- diffmem_mcp_server-0.1.0/diffmem_mcp/router.py +108 -0
- diffmem_mcp_server-0.1.0/diffmem_mcp/scopes.py +89 -0
- diffmem_mcp_server-0.1.0/diffmem_mcp/security.py +243 -0
- diffmem_mcp_server-0.1.0/diffmem_mcp/server.py +151 -0
- diffmem_mcp_server-0.1.0/diffmem_mcp/settings.py +63 -0
- diffmem_mcp_server-0.1.0/diffmem_mcp/tools/__init__.py +22 -0
- diffmem_mcp_server-0.1.0/diffmem_mcp/tools/files.py +147 -0
- diffmem_mcp_server-0.1.0/diffmem_mcp/tools/memory.py +248 -0
- diffmem_mcp_server-0.1.0/diffmem_mcp/tools/project.py +80 -0
- diffmem_mcp_server-0.1.0/diffmem_mcp/tools/spaces.py +187 -0
- diffmem_mcp_server-0.1.0/pyproject.toml +38 -0
- diffmem_mcp_server-0.1.0/tests/__init__.py +0 -0
- diffmem_mcp_server-0.1.0/tests/test_auth.py +96 -0
- diffmem_mcp_server-0.1.0/tests/test_memory_tools.py +250 -0
- diffmem_mcp_server-0.1.0/tests/test_router.py +158 -0
- diffmem_mcp_server-0.1.0/tests/test_scope_enforcement.py +203 -0
- diffmem_mcp_server-0.1.0/tests/test_security.py +98 -0
- diffmem_mcp_server-0.1.0/tests/test_self_containment.py +95 -0
- diffmem_mcp_server-0.1.0/tests/test_space_allowlist.py +124 -0
- diffmem_mcp_server-0.1.0/uv.lock +765 -0
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
# Dependencies
|
|
2
|
+
node_modules
|
|
3
|
+
.pnp
|
|
4
|
+
.pnp.js
|
|
5
|
+
|
|
6
|
+
# Local env files
|
|
7
|
+
.env
|
|
8
|
+
.env.local
|
|
9
|
+
.env.development.local
|
|
10
|
+
.env.test.local
|
|
11
|
+
.env.production.local
|
|
12
|
+
|
|
13
|
+
# Testing
|
|
14
|
+
coverage
|
|
15
|
+
|
|
16
|
+
# Turbo
|
|
17
|
+
.turbo
|
|
18
|
+
|
|
19
|
+
# Vercel
|
|
20
|
+
.vercel
|
|
21
|
+
|
|
22
|
+
# Build Outputs
|
|
23
|
+
dist
|
|
24
|
+
|
|
25
|
+
# Astro
|
|
26
|
+
.astro
|
|
27
|
+
|
|
28
|
+
# Debug
|
|
29
|
+
npm-debug.log*
|
|
30
|
+
|
|
31
|
+
# Misc
|
|
32
|
+
.DS_Store
|
|
33
|
+
*.pem
|
|
34
|
+
|
|
35
|
+
# Terraform
|
|
36
|
+
**/.terraform/
|
|
37
|
+
*.tfstate
|
|
38
|
+
*.tfstate.*
|
|
39
|
+
*.tfvars
|
|
40
|
+
!**/environments/**/*.tfvars
|
|
41
|
+
.terraform.lock.hcl
|
|
42
|
+
|
|
43
|
+
# Docker
|
|
44
|
+
*.log
|
|
45
|
+
|
|
46
|
+
# IDE
|
|
47
|
+
.vscode/
|
|
48
|
+
.idea/
|
|
49
|
+
*.swp
|
|
50
|
+
*.tmp
|
|
51
|
+
|
|
52
|
+
# Playwright
|
|
53
|
+
playwright-report/
|
|
54
|
+
test-results/
|
|
55
|
+
|
|
56
|
+
# Lighthouse CI
|
|
57
|
+
.lighthouseci/
|
|
58
|
+
|
|
59
|
+
# k6
|
|
60
|
+
*.k6
|
|
61
|
+
|
|
62
|
+
# Secrets
|
|
63
|
+
*.pem
|
|
64
|
+
*.key
|
|
65
|
+
*.crt
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
# Changelog
|
|
2
|
+
|
|
3
|
+
All notable changes to the `diffmem-mcp-server` package are documented here.
|
|
4
|
+
Format follows [Keep a Changelog](https://keepachangelog.com/en/1.1.0/); versions follow [SemVer](https://semver.org/).
|
|
5
|
+
|
|
6
|
+
## [0.1.0] — 2026-10-30
|
|
7
|
+
|
|
8
|
+
### Added
|
|
9
|
+
- MCP server exposing DiffMem memory APIs as tools: `list_spaces`, `get_space_info`, `list_files`, `read_file`, `get_project_status`, `ingest_memory`, `retrieve_context`.
|
|
10
|
+
- Transports: stdio (desktop clients), streamable HTTP (stateless, recommended remote), legacy SSE (deprecated).
|
|
11
|
+
- Security-first design: API-key-derived project scope, server-side space access validation, pre-write quota enforcement, per-key token-bucket rate limiting, audit logging with hashed key IDs.
|
|
12
|
+
- Regional routing (ADR-030): region derived from `project_id` prefix; no fallback region, no cache — malformed/undeployed regions fail loud.
|
|
13
|
+
- Streamable HTTP is stateless (ADR-039): no session pinning, JSON responses, proxy-friendly.
|
|
14
|
+
- Auth bridge keys validated via the DiffMem API (ADR-008) — no local Firestore lookup.
|
|
15
|
+
- Console script `diffmem-mcp-server` (stdio by default; `--sse --host --port` for HTTP transports).
|
|
16
|
+
- Wheel self-containment test: build → scratch-env install → console script `--help` → standalone import with no repo context.
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
# MCP Server
|
|
2
|
+
|
|
3
|
+
## Purpose
|
|
4
|
+
Standalone MCP (Model Context Protocol) server that exposes DiffMem's public memory APIs as discoverable tools for AI platforms (Claude, Cursor, etc.). Security is the primary design constraint: the API key deterministically derives project scope, space access is validated server-side, and quota is enforced before every write. The LLM never controls project or space authorization.
|
|
5
|
+
|
|
6
|
+
## User Stories
|
|
7
|
+
- As an AI platform user, I can ingest memory content into my DiffMem project without knowing which space it belongs in (smart routing).
|
|
8
|
+
- As an AI platform user, I can retrieve context from my memory spaces by query or explicit space selection.
|
|
9
|
+
- As an AI platform user, I can list and read files in my memory spaces.
|
|
10
|
+
- As a platform operator, I can be certain that no MCP tool call can access another project's data, regardless of what the LLM provides as parameters.
|
|
11
|
+
|
|
12
|
+
## Information Flow
|
|
13
|
+
- **Inputs:** MCP tool calls from AI clients, authenticated via `DIFFMEM_API_KEY` env var (stdio) or `Authorization` header (SSE via AuthHeaderMiddleware in asgi.py)
|
|
14
|
+
- **Outputs:** Tool results proxied from regional-plane APIs
|
|
15
|
+
- **Calls:** Regional-plane public endpoints (`/p/{project_id}/v1/...`) via HTTP with Bearer API key
|
|
16
|
+
|
|
17
|
+
## Terminology
|
|
18
|
+
- **MCP:** Model Context Protocol — standardized protocol for LLM tool use
|
|
19
|
+
- **stdio transport:** MCP transport over stdin/stdout (current implementation)
|
|
20
|
+
- **SSE transport:** MCP transport over HTTP Server-Sent Events (implemented via asgi.py with AuthHeaderMiddleware)
|
|
21
|
+
- **Smart routing:** When `space_id` is omitted for ingest, the regional-plane's router agent classifies content and selects the best public space
|
|
22
|
+
- **Distributed package:** `diffmem-mcp-server` on PyPI (OIDC trusted publishing via `publish-mcp-server.yml`; operator checklist `docs/PUBLISH-mcp-server.md`) — the artifact external users consume, invoked as `uvx diffmem-mcp-server` (stdio) or `uvx diffmem-mcp-server --sse` (HTTP). Nothing about the server's operation requires a repo checkout.
|
|
23
|
+
- **Dev-only checkout invocation:** running `python -m diffmem_mcp.server` or `pip install -e .` from this monorepo — for developing the bridge inside the platform. Never an onboarding or user path; docs must not present it as one.
|
|
24
|
+
|
|
25
|
+
## Key Files
|
|
26
|
+
- `diffmem_mcp/server.py` — MCP server instance; console-script entry `cli()` → `diffmem-mcp-server` (stdio default, `--sse` HTTP) and SSE entry (`main_sse`) kept for direct/local runs (the GLB never rewrites paths — remote traffic enters via the RP's project-scoped mount instead) + root `/health`
|
|
27
|
+
- `diffmem_mcp/asgi.py` — ASGI app factory: AuthHeaderMiddleware (Bearer → per-request contextvar identity — NEVER os.environ; pre-ADR-030 env bridge was a cross-tenant race) + `/health` route + FastMCP SSE app mounted at `/`; **prefix-agnostic** (RP mounts it at `/p/{project_id}/mcp`)
|
|
28
|
+
- `diffmem_mcp/context.py` — DiffMemContext: per-key-hash auth cache with 60s TTL re-validation (revocations/scope edits propagate within the bounded window); rejects key/project mismatch from the project-scoped mount (`project_mismatch`)
|
|
29
|
+
- `diffmem_mcp/scopes.py` — scope vocabulary mirror + enforce_scope (fail-closed; mirrors regional-plane `app/core/scopes.py`)
|
|
30
|
+
- `diffmem_mcp/auth.py` — API key validation via DiffMem API `GET /v1/auth/validate` (no local Firestore; 401 → `invalid_api_key`, 5xx/network → transient `auth_unavailable`)
|
|
31
|
+
- `diffmem_mcp/security.py` — Space-allowlist gates, rate limiting, audit logging (space-existence/quota checks are plane-enforced)
|
|
32
|
+
- `diffmem_mcp/router.py` — Region routing (project_id → region → API URL); public-language errors (`region_not_deployed` → "check the DIFFMEM_API_URLS setting")
|
|
33
|
+
- `diffmem_mcp/settings.py` — Environment-based configuration; `DIFFMEM_API_URLS` env (JSON region→URL map; deprecated alias `REGIONAL_PLANE_URLS` warns and works until its window closes — de-topologized 2026-10-26 per ADR-037 vocabulary)
|
|
34
|
+
- `diffmem_mcp/tools/` — MCP tool implementations (spaces, memory, files, project)
|
|
35
|
+
|
|
36
|
+
## External Dependencies
|
|
37
|
+
- Schema: `docs/schema.md` § Regional-Plane Data (high level only — regional collections `api_keys`/`tenant_configs`/`diffmem_spaces` have no dedicated sections yet; detail lives in `services/regional-plane/app/models/`)
|
|
38
|
+
- APIs: Regional-plane public endpoints (`/p/{project_id}/v1/memory/process`, `/p/{project_id}/v1/context`, etc.)
|
|
39
|
+
|
|
40
|
+
## Constraints
|
|
41
|
+
- `project_id` is NEVER a tool parameter — derived from the validated API key
|
|
42
|
+
- `space_id` is validated against the key's project before every operation
|
|
43
|
+
- Scoped keys (spec #77): every tool declares a required scope pinned to the regional-plane route table (`scopes.py`); memory/files tools additionally enforce the key's `space_allowlist` (`ensure_space_permitted` → `space_denied`); introspection tools keep invisible-not-403 semantics (`ensure_space_allowed`/`filter_spaces_to_allowlist`). Legacy keys (null scopes) pass everywhere with `LEGACY_KEY_FULL_ACCESS` logged. Check order: scope → space-allowlist → quota → handler
|
|
44
|
+
- Quota and space existence are enforced by the regional plane itself (scope → space-allowlist → quota → handler, spec #77 §7.3); plane denials pass through to tool output verbatim (ADR-038)
|
|
45
|
+
- Rate limiting is per-API-key, in-memory token bucket
|
|
46
|
+
- All tool invocations are audit-logged with structured events
|
|
47
|
+
- Distribution posture: external users get the **distributed package** (`uvx diffmem-mcp-server`); the checkout invocation is dev-only (see Terminology). Repo docs must never present the checkout form as a user/onboarding path — client configs use `command: uvx`.
|
|
48
|
+
- The bridge has NO Firestore access and NO GCP dependencies: `diffmem_mcp` depends only on mcp/httpx/pydantic (all storage access lives behind the regional-plane API; the public `/v1/auth/validate`, `/p/{id}/v1/spaces`, and `/p/{id}/v1/project/status` routes serve the introspection surface the bridge used to read from Firestore directly)
|
|
49
|
+
- ARCHITECTURE (ADR-030, implemented in SESSION_SPEC_2026-10-16-002): the MCP is co-located inside every regional plane at `/p/{project_id}/mcp` and edge-routed by the GLB's `/p/proj_{region}_*` rules. The standalone service, GLB `/mcp/*` rule, `RegionalRouter` Firestore machinery, and every `DEFAULT_REGION` fallback are DECOMMISSIONED. stdio keeps a pure prefix parser (`router.py`) that fails loud (`ROUTER_UNPARSEABLE_PROJECT` / `ROUTER_REGION_NOT_DEPLOYED`). Do NOT write `region` into `tenant_configs`, do NOT reintroduce a default-region fallback, and keep the build context at `diffMemPlatform/services/` (the RP Dockerfile ships `diffmem_mcp`).
|
|
50
|
+
|
|
51
|
+
## Attention Guidance
|
|
52
|
+
- When changing tool signatures: also update `diffmem_mcp/security.py` validation
|
|
53
|
+
- When adding a tool: declare its required scope from `scopes.py` (pinned to the RP route table in spec #77 §7.4) and enforce it before any I/O
|
|
54
|
+
- When changing the scope vocabulary: mirror regional-plane `app/core/scopes.py` exactly — additive entries only, unknown scopes fail closed
|
|
55
|
+
- When changing auth flow: also update regional-plane `app/core/auth.py` for consistency
|
|
56
|
+
- For SSE/HTTP transport: API key comes from `Authorization: Bearer <key>` header (handled by AuthHeaderMiddleware in asgi.py)
|
|
57
|
+
- Package was renamed from `app` to `diffmem_mcp` to avoid namespace conflicts with regional-plane
|
|
58
|
+
|
|
59
|
+
## Deployment (as of 2026-10-16 — deployed to prod)
|
|
60
|
+
- **Production** (ADR-030): the MCP sub-app ships INSIDE every regional-plane image (RP Dockerfile COPYs `diffmem_mcp`; build context `diffMemPlatform/services/`) and mounts at `/p/{project_id}/mcp`, edge-routed by the GLB's `/p/proj_{region}_*` rules. `ENABLE_MCP_SERVER=true` in every env. SSE auth: per-request contextvar identity + per-key TTL cache (no env-var bridge). Old `/mcp/*` routes 404 by design. Full runbook: `PRODUCTION_DEPLOYMENT.md`.
|
|
61
|
+
- **Staging**: Co-located with regional-plane at `/mcp` (set `ENABLE_MCP_SERVER=true` — STAGING-ONLY flag; do not enable in prod). The ASGI app is prefix-agnostic; RP owns the `/mcp` mount.
|
|
62
|
+
- Dockerfile copies `diffmem_mcp/` (NOT the pre-rename `app/`) and runs `python -m diffmem_mcp.server --sse`.
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
# ── Stage 1: Dependencies ────────────────────────────────────────────────────
|
|
2
|
+
FROM python:3.12-slim AS builder
|
|
3
|
+
|
|
4
|
+
WORKDIR /build
|
|
5
|
+
|
|
6
|
+
# Install system dependencies and build tools
|
|
7
|
+
RUN apt-get update && apt-get install -y --no-install-recommends \
|
|
8
|
+
gcc \
|
|
9
|
+
&& rm -rf /var/lib/apt/lists/* \
|
|
10
|
+
&& python -m pip install --no-cache-dir --upgrade pip hatchling
|
|
11
|
+
|
|
12
|
+
COPY pyproject.toml ./
|
|
13
|
+
RUN python -m pip install --no-cache-dir --prefix=/install .
|
|
14
|
+
|
|
15
|
+
# ── Stage 2: Runtime ─────────────────────────────────────────────────────────
|
|
16
|
+
FROM python:3.12-slim AS runtime
|
|
17
|
+
|
|
18
|
+
RUN groupadd --system --gid 1001 diffmem \
|
|
19
|
+
&& useradd --system --uid 1001 --gid diffmem --no-create-home diffmem
|
|
20
|
+
|
|
21
|
+
WORKDIR /app
|
|
22
|
+
|
|
23
|
+
COPY --from=builder /install /usr/local
|
|
24
|
+
|
|
25
|
+
COPY diffmem_mcp/ ./diffmem_mcp/
|
|
26
|
+
|
|
27
|
+
USER diffmem
|
|
28
|
+
|
|
29
|
+
ENV PYTHONUNBUFFERED=1 \
|
|
30
|
+
PYTHONDONTWRITEBYTECODE=1 \
|
|
31
|
+
MCP_PORT=8003
|
|
32
|
+
|
|
33
|
+
EXPOSE 8003
|
|
34
|
+
|
|
35
|
+
HEALTHCHECK --interval=30s --timeout=10s --start-period=15s --retries=3 \
|
|
36
|
+
CMD python -c "import urllib.request; urllib.request.urlopen('http://localhost:8003/health')"
|
|
37
|
+
|
|
38
|
+
# Streamable HTTP + legacy SSE transports for remote clients (production
|
|
39
|
+
# standalone deployment). For every real environment, the MCP server is
|
|
40
|
+
# mounted as a sub-app in regional-plane (ADR-030/ADR-039).
|
|
41
|
+
CMD ["python", "-m", "diffmem_mcp.server", "--sse", "--host", "0.0.0.0", "--port", "8003"]
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
Metadata-Version: 2.5
|
|
2
|
+
Name: diffmem-mcp-server
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: DiffMem MCP Server — exposes DiffMem memory APIs as MCP tools with security-first design
|
|
5
|
+
Requires-Python: >=3.12
|
|
6
|
+
Requires-Dist: httpx>=0.27.0
|
|
7
|
+
Requires-Dist: mcp<2,>=1.0.0
|
|
8
|
+
Requires-Dist: pydantic-settings>=2.3.0
|
|
9
|
+
Requires-Dist: pydantic>=2.7.0
|
|
10
|
+
Requires-Dist: sse-starlette>=2.0.0
|
|
11
|
+
Requires-Dist: uvicorn>=0.30.0
|
|
12
|
+
Provides-Extra: dev
|
|
13
|
+
Requires-Dist: pytest-asyncio>=0.23.0; extra == 'dev'
|
|
14
|
+
Requires-Dist: pytest>=8.2.0; extra == 'dev'
|
|
15
|
+
Requires-Dist: ruff>=0.4.0; extra == 'dev'
|
|
@@ -0,0 +1,189 @@
|
|
|
1
|
+
# MCP Server Production Deployment
|
|
2
|
+
|
|
3
|
+
> **ADR-030 (2026-10-16) supersedes the standalone-service architecture this
|
|
4
|
+
> file previously described.** The standalone Cloud Run service
|
|
5
|
+
> (`diffmem-mcp-server-{env}`), its GLB `/mcp/*` routing, and the
|
|
6
|
+
> `mcp_image_tag` dispatch input were decommissioned in
|
|
7
|
+
> SESSION_SPEC_2026-10-16-002. There is no standalone MCP service to deploy
|
|
8
|
+
> anymore — this document describes the architecture that replaced it.
|
|
9
|
+
|
|
10
|
+
## Architecture (ADR-030: regional, co-located, edge-routed)
|
|
11
|
+
|
|
12
|
+
The MCP server is **co-located inside every regional-plane service** and
|
|
13
|
+
routed **entirely by the GLB edge**:
|
|
14
|
+
|
|
15
|
+
```
|
|
16
|
+
client ──► api.diffmem.com/p/{project_id}/mcp/* (GLB)
|
|
17
|
+
│ url-map rule /p/proj_{region}_* (same rules as the RP API)
|
|
18
|
+
▼
|
|
19
|
+
regional-plane-{region} (Cloud Run, per region)
|
|
20
|
+
│ FastAPI mounts the MCP ASGI sub-app at /p/{project_id}/mcp
|
|
21
|
+
▼
|
|
22
|
+
co-located engine / Gitea (region-pinned data plane)
|
|
23
|
+
```
|
|
24
|
+
|
|
25
|
+
Why this shape (see `.pi/DECISIONS.md` ADR-030 for the full decision record):
|
|
26
|
+
|
|
27
|
+
1. **The URL carries the project.** `project_id` (`proj_{region}_{8-char
|
|
28
|
+
base32}`) is the same routing key the RP API uses; the GLB's existing
|
|
29
|
+
`/p/proj_{region}_*` prefix rules route the MCP subtree with ZERO new
|
|
30
|
+
backends, NEGs, or path rules.
|
|
31
|
+
2. **Residency holds end-to-end.** Memory content never transits a foreign
|
|
32
|
+
region — the pre-ADR-030 global MCP in europe-west1 proxied every region's
|
|
33
|
+
content through itself, defeating the property the `/p/{project_id}` design
|
|
34
|
+
guarantees at the edge.
|
|
35
|
+
3. **No internal region router exists.** The edge already decided. The old
|
|
36
|
+
Firestore-lookup router (and its silent `DEFAULT_REGION` fallback — a
|
|
37
|
+
residency violation waiting for the second region) is deleted; stdio keeps
|
|
38
|
+
a pure prefix parser that fails loud.
|
|
39
|
+
4. **Single key validation per request.** The RP's per-router auth
|
|
40
|
+
dependencies don't cover the mount; validation happens once inside
|
|
41
|
+
`diffmem_mcp` (per-request contextvar identity, per-key TTL cache). A key
|
|
42
|
+
whose project differs from the mount path fails loud at handshake
|
|
43
|
+
(`project_mismatch`) — the project-scoped path is a cross-tenant guard,
|
|
44
|
+
not a hint.
|
|
45
|
+
|
|
46
|
+
## Project-scoped mount contract
|
|
47
|
+
|
|
48
|
+
`regional-plane/app/mcp_mount.py::ProjectScopedMCP` guards the mount:
|
|
49
|
+
|
|
50
|
+
- Validates `project_id` against `proj_{region}_{8-char base32}` — malformed
|
|
51
|
+
IDs get **400 `invalid_project_id`** (`MCP_MOUNT_BAD_PROJECT_ID` error log),
|
|
52
|
+
never a silent default.
|
|
53
|
+
- Publishes the path project into `scope["state"]["mcp_path_project_id"]`;
|
|
54
|
+
the MCP auth seam compares it against the validated key's project.
|
|
55
|
+
- The sub-app sees `/health`, `/mcp`, `/mcp/sse`, `/mcp/messages` (the Mount
|
|
56
|
+
strips the `/p/{project_id}` prefix) — every previously published URL is
|
|
57
|
+
intact, plus the clean streamable URL `/p/{project_id}/mcp` (ADR-039).
|
|
58
|
+
|
|
59
|
+
Keep in sync (a drift test asserts agreement):
|
|
60
|
+
|
|
61
|
+
- `control-plane/app/utils/project_id.py` — `PROJECT_ID_PATTERN`
|
|
62
|
+
- `global-load-balancer/url-map.tf` — region path rules
|
|
63
|
+
- `regional-plane/app/mcp_mount.py`, `mcp-server/diffmem_mcp/router.py`
|
|
64
|
+
|
|
65
|
+
## Client connection
|
|
66
|
+
|
|
67
|
+
### Streamable-HTTP endpoint (recommended)
|
|
68
|
+
|
|
69
|
+
```
|
|
70
|
+
https://api.diffmem.com/p/{project_id}/mcp
|
|
71
|
+
```
|
|
72
|
+
|
|
73
|
+
`{project_id}` is the project's ID (`proj_eu_x7k2p3mz` shape — the same ID
|
|
74
|
+
embedded in the RP API paths). Clients configure the FULL URL; the edge does
|
|
75
|
+
the rest. The endpoint is stateless (no `Mcp-Session-Id`), so requests ride
|
|
76
|
+
any Cloud Run instance.
|
|
77
|
+
|
|
78
|
+
### Legacy SSE endpoint (deprecated — ADR-039)
|
|
79
|
+
|
|
80
|
+
```
|
|
81
|
+
https://api.diffmem.com/p/{project_id}/mcp/sse
|
|
82
|
+
```
|
|
83
|
+
|
|
84
|
+
Still served for clients not yet migrated (the `/mcp/sse` + `/mcp/messages`
|
|
85
|
+
pair works exactly as before). New integrations should use the streamable URL
|
|
86
|
+
above; SSE is scheduled for removal once migrated clients confirm.
|
|
87
|
+
|
|
88
|
+
### Authentication
|
|
89
|
+
|
|
90
|
+
`Authorization: Bearer <API key>` — the same `dm_` key the RP API uses. One
|
|
91
|
+
validation per request; revocations propagate within the 60s auth TTL.
|
|
92
|
+
|
|
93
|
+
**Migration note (breaking, by design):** clients configured with the old
|
|
94
|
+
`https://api.diffmem.com/mcp/sse` URL now get a **404** (the `/mcp/*` GLB rule
|
|
95
|
+
is gone). Reconfigure to the project-scoped URL above — the failure is loud,
|
|
96
|
+
never a silent misroute.
|
|
97
|
+
|
|
98
|
+
### MCP client configuration (Claude Desktop)
|
|
99
|
+
|
|
100
|
+
```json
|
|
101
|
+
{
|
|
102
|
+
"mcpServers": {
|
|
103
|
+
"diffmem": {
|
|
104
|
+
"url": "https://api.diffmem.com/p/proj_eu_x7k2p3mz/mcp",
|
|
105
|
+
"headers": { "Authorization": "Bearer dm_..." }
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
```
|
|
110
|
+
|
|
111
|
+
### MCP client configuration (Cursor)
|
|
112
|
+
|
|
113
|
+
```json
|
|
114
|
+
{
|
|
115
|
+
"mcp.servers": {
|
|
116
|
+
"diffmem": {
|
|
117
|
+
"url": "https://api.diffmem.com/p/proj_eu_x7k2p3mz/mcp",
|
|
118
|
+
"headers": { "Authorization": "Bearer dm_..." }
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
```
|
|
123
|
+
|
|
124
|
+
### stdio (local)
|
|
125
|
+
|
|
126
|
+
External users run the **published package** — no repo checkout is ever
|
|
127
|
+
needed. Dev-only checkout invocation (`python -m diffmem_mcp.server`) is
|
|
128
|
+
covered in [README.md](README.md) § "Running Directly".
|
|
129
|
+
|
|
130
|
+
```json
|
|
131
|
+
{
|
|
132
|
+
"mcpServers": {
|
|
133
|
+
"diffmem": {
|
|
134
|
+
"command": "uvx",
|
|
135
|
+
"args": ["diffmem-mcp-server"],
|
|
136
|
+
"env": {
|
|
137
|
+
"DIFFMEM_API_KEY": "dm_...",
|
|
138
|
+
"DIFFMEM_API_URLS": "{\"eu\": \"https://eu-api.diffmem.com\"}"
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
```
|
|
144
|
+
|
|
145
|
+
stdio has no GLB, so the server derives the region from the `project_id`
|
|
146
|
+
prefix (`router.py`) and picks the base URL from `DIFFMEM_API_URLS`.
|
|
147
|
+
There is **no default region**: a malformed project ID or a region absent
|
|
148
|
+
from the map fails loud (`ROUTER_UNPARSEABLE_PROJECT` /
|
|
149
|
+
`ROUTER_REGION_NOT_DEPLOYED`).
|
|
150
|
+
|
|
151
|
+
## Environment variables
|
|
152
|
+
|
|
153
|
+
Co-located (streamable HTTP / SSE): the RP container needs nothing beyond what
|
|
154
|
+
it already has — the RP's own env plus `ENABLE_MCP_SERVER` (true in every
|
|
155
|
+
environment; the image ships `diffmem_mcp` via the regional-plane Dockerfile,
|
|
156
|
+
build context `diffMemPlatform/services/`).
|
|
157
|
+
|
|
158
|
+
stdio only:
|
|
159
|
+
|
|
160
|
+
| Variable | Required | Purpose |
|
|
161
|
+
|---|---|---|
|
|
162
|
+
| `DIFFMEM_API_KEY` | Yes | API key (`dm_...`) |
|
|
163
|
+
| `DIFFMEM_API_URLS` | Yes | JSON map region → base URL (one entry per deployed region). Deprecated alias `REGIONAL_PLANE_URLS` still works (warns) |
|
|
164
|
+
| `GOOGLE_CLOUD_PROJECT` | No | Firestore project for local key validation |
|
|
165
|
+
|
|
166
|
+
(`DEFAULT_REGION` and `MCP_IMAGE_TAG` are GONE — the first was the silent
|
|
167
|
+
misroute hazard, the second belonged to the decommissioned service.)
|
|
168
|
+
|
|
169
|
+
## Deploy & rollback
|
|
170
|
+
|
|
171
|
+
There is nothing to deploy separately: shipping an RP image (with
|
|
172
|
+
`diffmem_mcp` inside — automatic since the ADR-030 Dockerfile) and the GLB
|
|
173
|
+
prefix rules IS the deployment. `mcp_image_tag` no longer exists in either
|
|
174
|
+
workflow.
|
|
175
|
+
|
|
176
|
+
- **Rollback**: re-dispatch `deploy-production.yml` with the previous
|
|
177
|
+
`rp_image_tag` (pinned tags, never `latest`).
|
|
178
|
+
- **Disabling the MCP surface** for a region: set
|
|
179
|
+
`ENABLE_MCP_SERVER=false` for that plane — the routes 404 immediately.
|
|
180
|
+
- **Logs**: RP Cloud Run logs (mount events: `MCP_SERVER_MOUNTED`,
|
|
181
|
+
`MCP_MOUNT_BAD_PROJECT_ID`); MCP auth events (`AUTH_IDENTITY_SET`,
|
|
182
|
+
`project_mismatch`) appear in the same stream.
|
|
183
|
+
|
|
184
|
+
## Health probes
|
|
185
|
+
|
|
186
|
+
- `GET /p/{project_id}/health` → 200 (unauthenticated; inside the
|
|
187
|
+
project-scoped mount, used by CI smoke and the runbook).
|
|
188
|
+
- Unauthenticated `/p/{project_id}/mcp/sse` → immediate app-level error
|
|
189
|
+
(421/401 class) — proves the surface is alive and auth-gated.
|
|
@@ -0,0 +1,234 @@
|
|
|
1
|
+
# DiffMem MCP Server
|
|
2
|
+
|
|
3
|
+
MCP (Model Context Protocol) server exposing DiffMem's memory APIs as discoverable tools for AI platforms.
|
|
4
|
+
|
|
5
|
+
## Security Model
|
|
6
|
+
|
|
7
|
+
Security is the primary design constraint:
|
|
8
|
+
|
|
9
|
+
- **API key determines project scope** — `project_id` is never a tool parameter; it's derived from the validated key
|
|
10
|
+
- **Space access validated server-side** — every space operation checks the space belongs to the authenticated project
|
|
11
|
+
- **Quota enforced before writes** — deterministic Firestore flag checks, not LLM judgment
|
|
12
|
+
- **Rate limiting per API key** — token-bucket algorithm, configurable RPM
|
|
13
|
+
- **All operations audit-logged** — structured events with hashed key IDs
|
|
14
|
+
|
|
15
|
+
## Tools
|
|
16
|
+
|
|
17
|
+
| Tool | Type | Description |
|
|
18
|
+
|------|------|-------------|
|
|
19
|
+
| `list_spaces` | Read | List all memory spaces in your project |
|
|
20
|
+
| `get_space_info` | Read | Get detailed metadata for a specific space |
|
|
21
|
+
| `list_files` | Read | List files in a memory space |
|
|
22
|
+
| `read_file` | Read | Read a specific file from a space |
|
|
23
|
+
| `get_project_status` | Read | Get project config, plan, and quota state |
|
|
24
|
+
| `ingest_memory` | Write | Ingest content (smart routing or targeted) |
|
|
25
|
+
| `retrieve_context` | Write | Retrieve context from memory spaces |
|
|
26
|
+
|
|
27
|
+
## Transport Modes
|
|
28
|
+
|
|
29
|
+
### stdio (Desktop Clients)
|
|
30
|
+
Default mode for local development with Claude Desktop, Cursor, etc. Communicates over stdin/stdout.
|
|
31
|
+
|
|
32
|
+
### Streamable HTTP (Remote/Cloud) — recommended
|
|
33
|
+
Streamable HTTP transport for remote deployment at `/p/{project_id}/mcp`. Requires `Authorization: Bearer <key>` header. Stateless (ADR-039).
|
|
34
|
+
|
|
35
|
+
### SSE (Remote/Cloud) — deprecated
|
|
36
|
+
Legacy Server-Sent Events transport at `/p/{project_id}/mcp/sse`. Still served until clients migrate; new integrations use streamable HTTP.
|
|
37
|
+
|
|
38
|
+
## Setup
|
|
39
|
+
|
|
40
|
+
### Prerequisites
|
|
41
|
+
|
|
42
|
+
- Python 3.12+
|
|
43
|
+
- Firebase project with Firestore enabled
|
|
44
|
+
- DiffMem API key (create via dashboard or regional-plane API)
|
|
45
|
+
|
|
46
|
+
### Installation
|
|
47
|
+
|
|
48
|
+
End users get the server from a package index — nothing requires a repo
|
|
49
|
+
checkout:
|
|
50
|
+
|
|
51
|
+
```bash
|
|
52
|
+
# Ad-hoc run (recommended for MCP clients: uvx fetches and caches)
|
|
53
|
+
uvx diffmem-mcp-server --help
|
|
54
|
+
|
|
55
|
+
# Or install into an environment
|
|
56
|
+
pip install diffmem-mcp-server
|
|
57
|
+
diffmem-mcp-server --help
|
|
58
|
+
```
|
|
59
|
+
|
|
60
|
+
For DiffMem platform development only (repository checkout):
|
|
61
|
+
|
|
62
|
+
```bash
|
|
63
|
+
cd diffMemPlatform/services/mcp-server
|
|
64
|
+
pip install -e .
|
|
65
|
+
```
|
|
66
|
+
|
|
67
|
+
### Environment Variables
|
|
68
|
+
|
|
69
|
+
| Variable | Required | Default | Description |
|
|
70
|
+
|----------|----------|---------|-------------|
|
|
71
|
+
| `DIFFMEM_API_KEY` | Yes | — | DiffMem API key for authentication |
|
|
72
|
+
| `GOOGLE_CLOUD_PROJECT` | No | `demo-local` | GCP project for Firestore |
|
|
73
|
+
| `REGIONAL_PLANE_URLS` | No | `{"us": "http://localhost:8080"}` | JSON mapping of region → URL (stdio resolution) |
|
|
74
|
+
| `MCP_RATE_LIMIT_RPM` | No | `60` | Rate limit per API key (requests/min) |
|
|
75
|
+
|
|
76
|
+
ADR-030: region is derived from the `project_id` prefix (`proj_{region}_...`)
|
|
77
|
+
— there is NO `DEFAULT_REGION` fallback and NO region cache. Malformed IDs or
|
|
78
|
+
undeployed regions fail loud (`ROUTER_UNPARSEABLE_PROJECT` /
|
|
79
|
+
`ROUTER_REGION_NOT_DEPLOYED`).
|
|
80
|
+
|
|
81
|
+
## MCP Client Configuration
|
|
82
|
+
|
|
83
|
+
### Claude Desktop (stdio)
|
|
84
|
+
|
|
85
|
+
Add to `~/Library/Application Support/Claude/claude_desktop_config.json`:
|
|
86
|
+
|
|
87
|
+
```json
|
|
88
|
+
{
|
|
89
|
+
"mcpServers": {
|
|
90
|
+
"diffmem": {
|
|
91
|
+
"command": "uvx",
|
|
92
|
+
"args": ["diffmem-mcp-server"],
|
|
93
|
+
"env": {
|
|
94
|
+
"DIFFMEM_API_KEY": "dm_your_api_key_here",
|
|
95
|
+
"REGIONAL_PLANE_URLS": "{\"eu\": \"https://eu-api.diffmem.com\", \"us\": \"https://us-api.diffmem.com\"}"
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
```
|
|
101
|
+
|
|
102
|
+
### Claude Desktop (Streamable HTTP - Remote)
|
|
103
|
+
|
|
104
|
+
```json
|
|
105
|
+
{
|
|
106
|
+
"mcpServers": {
|
|
107
|
+
"diffmem": {
|
|
108
|
+
"url": "https://api.diffmem.com/p/proj_eu_x7k2p3mz/mcp",
|
|
109
|
+
"headers": {
|
|
110
|
+
"Authorization": "Bearer dm_your_api_key_here"
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
```
|
|
116
|
+
|
|
117
|
+
### Cursor (stdio)
|
|
118
|
+
|
|
119
|
+
Add to `.cursor/mcp.json`:
|
|
120
|
+
|
|
121
|
+
```json
|
|
122
|
+
{
|
|
123
|
+
"mcpServers": {
|
|
124
|
+
"diffmem": {
|
|
125
|
+
"command": "uvx",
|
|
126
|
+
"args": ["diffmem-mcp-server"],
|
|
127
|
+
"env": {
|
|
128
|
+
"DIFFMEM_API_KEY": "dm_your_api_key_here",
|
|
129
|
+
"REGIONAL_PLANE_URLS": "{\"eu\": \"https://eu-api.diffmem.com\"}"
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
```
|
|
135
|
+
|
|
136
|
+
### Cursor (Streamable HTTP - Remote)
|
|
137
|
+
|
|
138
|
+
```json
|
|
139
|
+
{
|
|
140
|
+
"mcpServers": {
|
|
141
|
+
"diffmem": {
|
|
142
|
+
"url": "https://api.diffmem.com/p/proj_eu_x7k2p3mz/mcp",
|
|
143
|
+
"env": {
|
|
144
|
+
"DIFFMEM_API_KEY": "dm_your_api_key_here"
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
```
|
|
150
|
+
|
|
151
|
+
## Running Directly (dev-only — requires a repository checkout)
|
|
152
|
+
|
|
153
|
+
> **Note:** the invocation below is for developing the bridge inside this
|
|
154
|
+
> monorepo. End users run the published package: `uvx diffmem-mcp-server`
|
|
155
|
+
> (stdio) or `uvx diffmem-mcp-server --sse` (HTTP). Nothing about the server's
|
|
156
|
+
> operation depends on having a checkout.
|
|
157
|
+
|
|
158
|
+
### stdio Mode (Local)
|
|
159
|
+
|
|
160
|
+
```bash
|
|
161
|
+
# Set environment variables
|
|
162
|
+
export DIFFMEM_API_KEY="dm_your_api_key_here"
|
|
163
|
+
export REGIONAL_PLANE_URLS='{"eu": "https://eu-api.diffmem.com"}' # stdio only
|
|
164
|
+
|
|
165
|
+
# Run the server
|
|
166
|
+
python -m diffmem_mcp.server
|
|
167
|
+
```
|
|
168
|
+
|
|
169
|
+
### HTTP Mode (Remote: streamable + legacy SSE)
|
|
170
|
+
|
|
171
|
+
```bash
|
|
172
|
+
# Set environment variables
|
|
173
|
+
export DIFFMEM_API_KEY="dm_your_api_key_here"
|
|
174
|
+
export REGIONAL_PLANE_URLS='{"eu": "https://eu-api.diffmem.com"}' # stdio only
|
|
175
|
+
|
|
176
|
+
# Run with HTTP transports (streamable at /mcp, legacy SSE at /sse)
|
|
177
|
+
python -m diffmem_mcp.server --sse --host 0.0.0.0 --port 3001
|
|
178
|
+
```
|
|
179
|
+
|
|
180
|
+
## Deployment
|
|
181
|
+
|
|
182
|
+
### Co-located with the Regional Plane (ADR-030 — the ONLY deployment shape)
|
|
183
|
+
|
|
184
|
+
The MCP sub-app is mounted inside every regional-plane at
|
|
185
|
+
`/p/{project_id}` and edge-routed by the GLB's `/p/proj_{region}_*` rules.
|
|
186
|
+
`ENABLE_MCP_SERVER=true` (the default) mounts it; `false` removes the surface.
|
|
187
|
+
|
|
188
|
+
```bash
|
|
189
|
+
# Local: mount point is project-scoped
|
|
190
|
+
# Access at: https://{regional-plane-url}/p/{project_id}/mcp (streamable) or .../mcp/sse (legacy)
|
|
191
|
+
```
|
|
192
|
+
|
|
193
|
+
### Production (Standalone Cloud Run)
|
|
194
|
+
|
|
195
|
+
See [PRODUCTION_DEPLOYMENT.md](PRODUCTION_DEPLOYMENT.md) for:
|
|
196
|
+
- Terraform resources
|
|
197
|
+
- Cloud Run configuration
|
|
198
|
+
- GLB path routing
|
|
199
|
+
- Client connection details
|
|
200
|
+
|
|
201
|
+
## Development
|
|
202
|
+
|
|
203
|
+
> Repository-checkout invocation is dev-only — end users install from a package
|
|
204
|
+
> index (`uvx diffmem-mcp-server`). See `docs/PUBLISH-mcp-server.md` (repo root)
|
|
205
|
+
> for the one-time PyPI trusted-publishing checklist and release procedure.
|
|
206
|
+
|
|
207
|
+
```bash
|
|
208
|
+
# Install dev dependencies
|
|
209
|
+
pip install -e ".[dev]"
|
|
210
|
+
|
|
211
|
+
# Run tests
|
|
212
|
+
pytest tests/ -x -q
|
|
213
|
+
|
|
214
|
+
# Run linter
|
|
215
|
+
ruff check diffmem_mcp/ tests/
|
|
216
|
+
```
|
|
217
|
+
|
|
218
|
+
## Architecture
|
|
219
|
+
|
|
220
|
+
```
|
|
221
|
+
diffmem_mcp/
|
|
222
|
+
├── __init__.py
|
|
223
|
+
├── asgi.py # HTTP ASGI adapter (streamable + SSE) for FastAPI mounting
|
|
224
|
+
├── auth.py # API key validation (SHA-256 hash lookup)
|
|
225
|
+
├── router.py # Regional routing (project → region → URL)
|
|
226
|
+
├── security.py # Space validation, quota, rate limiting
|
|
227
|
+
├── server.py # MCP server instance & entry point
|
|
228
|
+
├── settings.py # Environment configuration
|
|
229
|
+
└── tools/ # MCP tool implementations
|
|
230
|
+
├── files.py # File operations
|
|
231
|
+
├── memory.py # Memory ingest/retrieve
|
|
232
|
+
├── project.py # Project status
|
|
233
|
+
└── spaces.py # Space management
|
|
234
|
+
```
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
"""DiffMem MCP Server — exposes DiffMem memory APIs as MCP tools."""
|