mirror of
https://github.com/MBombeck/HealthLog.git
synced 2026-11-19 00:12:18 +00:00
feat(mcp): add a confirmed, audience-bound write surface for logging measurements and mood
Register two write tools — log_measurement and log_mood — only for a
health:write-scoped session; a read-only session never sees them advertised.
Each write mirrors the proven Telegram capture path (same range guard,
canonical unit, cache invalidation and rollup recompute) pinned to the MCP
source, and is idempotent on a hashed externalId derived from a client key, so
a retried call is a no-op rather than a duplicate.
Because the remote wire is stateless (no elicitation), the tools carry a
protocol-level confirm gate: without confirm:true they write nothing and
return the exact record they would write plus an instruction to confirm the
value with the user and re-call with the same idempotency key. Commits run
under a tighter per-credential write budget on top of the per-binding request
limiter.
Harden the resource-server audience binding: a token whose scopes are all in
{health:read, health:write} is bound to the MCP surface and refused on every
REST write/delete. A health:write token therefore admits the in-process MCP
write tools only and can never become a general REST write credential; admin
stays unreachable (cookie-only). Refuse, by construction, any medication, lab,
delete/update, or data-export tool on this surface.
This commit is contained in:
1 parent
90c8768407
commit
6de61de010
15 files changed
+1263
-34
No files matched your search
@@ -288,7 +288,7 @@ describe("/mcp — rate limit", () => {
|
||||
});
|
||||
});
|
||||
|
||||
describe("/mcp — admin unreachable over HTTP (read-only, cookie-only boundary)", () => {
|
||||
describe("/mcp — admin unreachable over HTTP (cookie-only boundary)", () => {
|
||||
it("never consults a cookie session, even with a wildcard token", async () => {
|
||||
validToken(["*"]);
|
||||
const res = await POST(postRpc(TOOLS_LIST));
|
||||
@@ -299,12 +299,16 @@ describe("/mcp — admin unreachable over HTTP (read-only, cookie-only boundary)
|
||||
(t) => t.name,
|
||||
);
|
||||
|
||||
// A `["*"]` token still sees ONLY the read tools — no write tool is
|
||||
// registered and no admin tool exists (read-only by construction).
|
||||
expect(names.sort()).toEqual(READ_TOOLS);
|
||||
// A `["*"]` token carries write capability (`tokenAllowsWrite`), so it
|
||||
// sees the confirmed write tools alongside the read tools — but NO admin
|
||||
// tool exists, and admin stays unreachable here by construction.
|
||||
for (const read of READ_TOOLS) {
|
||||
expect(names).toContain(read);
|
||||
}
|
||||
expect(names).toContain("log_measurement");
|
||||
expect(names).toContain("log_mood");
|
||||
for (const name of names) {
|
||||
expect(name).not.toMatch(/admin/i);
|
||||
expect(name).not.toMatch(/^(log_|create_|update_|delete_|write_)/);
|
||||
}
|
||||
|
||||
// The route authenticates purely by Bearer: a cookie session is never
|
||||
@@ -314,6 +318,33 @@ describe("/mcp — admin unreachable over HTTP (read-only, cookie-only boundary)
|
||||
});
|
||||
});
|
||||
|
||||
describe("/mcp — write tools gate on health:write", () => {
|
||||
it("a health:read token lists NO write tools", async () => {
|
||||
validToken(["health:read"]);
|
||||
const res = await POST(postRpc(TOOLS_LIST));
|
||||
expect(res.status).toBe(200);
|
||||
const body = await res.json();
|
||||
const names = (body.result.tools as Array<{ name: string }>).map(
|
||||
(t) => t.name,
|
||||
);
|
||||
expect(names.sort()).toEqual(READ_TOOLS);
|
||||
expect(names).not.toContain("log_measurement");
|
||||
expect(names).not.toContain("log_mood");
|
||||
});
|
||||
|
||||
it("a health:read+write token can list (and thus invoke) the write tools", async () => {
|
||||
validToken(["health:read", "health:write"]);
|
||||
const res = await POST(postRpc(TOOLS_LIST));
|
||||
expect(res.status).toBe(200);
|
||||
const body = await res.json();
|
||||
const names = (body.result.tools as Array<{ name: string }>).map(
|
||||
(t) => t.name,
|
||||
);
|
||||
expect(names).toContain("log_measurement");
|
||||
expect(names).toContain("log_mood");
|
||||
});
|
||||
});
|
||||
|
||||
describe("/mcp — GET transport", () => {
|
||||
it("is wired for GET (SSE stream open) behind the same gates", async () => {
|
||||
validToken();
|
||||
|
||||
@@ -31,11 +31,14 @@
|
||||
* (`sessionIdGenerator: undefined`, `enableJsonResponse: true`), torn
|
||||
* down once the buffered JSON-RPC response is built.
|
||||
*
|
||||
* Read-only: the factory registers read tools only and `requireAdmin()` is
|
||||
* cookie-only, so neither writes nor admin can be reached over this wire
|
||||
* regardless of token scope — including `["*"]` (ADR-003 / ADR-005). The
|
||||
* `health:write` scope stays reserved and unreachable until the write
|
||||
* tools land in a later phase.
|
||||
* Write surface: the factory registers the confirmed write tools
|
||||
* (`log_measurement` / `log_mood`) ONLY for a `health:write`-scoped session;
|
||||
* a read-only token never sees them. Writes happen in-process here and are
|
||||
* audience-bound to `/mcp` — a `health:write` MCP token is refused on every
|
||||
* REST write/delete by the resource-server guard in `api-handler.ts`, so it
|
||||
* can never become a general REST write credential. `requireAdmin()` stays
|
||||
* cookie-only, so admin is unreachable over this wire regardless of token
|
||||
* scope — including `["*"]` (ADR-003 / ADR-005).
|
||||
*
|
||||
* Same-origin / no-CORS by construction: no `Access-Control-Allow-Origin`
|
||||
* header is emitted anywhere here, matching the app-wide posture.
|
||||
|
||||
Reference in new issue
Block a user