Self-hosted, Self-Improving, Federated Everything App — dev management, agent orchestration, persistent mind, digital twin, second brain, and creative pipeline from a single dashboard over Tailscale to port the user into the OS
JavaScript ai-agentsdashboarddeveloper-toolsdigital-twinquantified-selfsecond-brainself-hostedtailscale
docs: audit API and MCP tool contracts
Adam Eivy5657927fdcee
diff --git a/docs/API.md b/docs/API.mdindex 64837c58f2..8d3d248dab 100644--- a/docs/API.md+++ b/docs/API.md@@ -12,6 +12,11 @@ When a TLS cert is provisioned (`npm run setup:cert`), `:5555` serves HTTPS instThis document covers the most commonly used endpoints plus a [route-domain index](#route-domain-index). The in-app **API Explorer** at `/api-reference/catalog` is the exhaustive, generated reference:+For the bridge between these HTTP/event inventories and model-facing tools,+see [API and MCP Unified Tool Contract](./API_TOOL_CONTRACT.md). It records+the shipped semantic registry, MCP context/action schemas, authority matrix,+and the current-vs-proposed boundary.+- `GET /api/api-docs/catalog.json` — searchable metadata for every mounted HTTP operation.- `GET /api/api-docs/internal/openapi.json` — OpenAPI 3.1 for the complete internal HTTP surface.- `GET /api/api-docs/openapi.json` — OpenAPI 3.1 for only the external APIs currently exposed in Settings.diff --git a/docs/API_TOOL_CONTRACT.md b/docs/API_TOOL_CONTRACT.mdnew file mode 100644index 0000000000..888a5e9277--- /dev/null+++ b/docs/API_TOOL_CONTRACT.md@@ -0,0 +1,295 @@+# PortOS API and MCP Unified Tool Contract++Status: current implementation audit+Date: 2026-08-28++This document is the implementation-facing bridge between PortOS's exhaustive+HTTP inventory, the governed semantic registry, Persistent Mind, and Agent+Tools (MCP). It records what is shipped and separates it from the long-running+job and confirmation extensions retained in the [unified design spec](./superpowers/specs/2026-08-27-unified-cos-tool-calling-interface.md).++## Audit result++The current source contains:++- 145 mounted HTTP prefixes, 2,067 deduplicated HTTP operations, and 2,070+ route declarations in `server/lib/apiRouteCatalog.generated.json`.+- 103 Socket.IO source files and 253 events in+ `server/lib/socketEventCatalog.generated.json`.+- 22 provider-neutral semantic tools: one `cos.create-task` tool and 21+ semantic adapters inherited from the voice registry.+- Five read-only context tools on the Agent Tools MCP transport. The MCP+ transport may additionally advertise the 21 semantic adapters when its+ separate read/write grants are enabled.++The generated route catalog and event catalog are the exhaustive maps. The+in-app API Explorer and the following endpoints expose them at runtime:++| Surface | Endpoint | Contract |+|---|---|---|+| HTTP inventory | `GET /api/api-docs/catalog.json` | Searchable route metadata, domains, access classification, side-effect classification, and modeled/generated status. |+| Internal HTTP spec | `GET /api/api-docs/internal/openapi.json` | OpenAPI 3.1 for every mounted HTTP operation. Generated operations have path parameters and a default response; modeled operations add richer contracts. |+| Public HTTP spec | `GET /api/api-docs/openapi.json` | OpenAPI 3.1 for only APIs exposed through Settings → API Access. |+| Socket.IO inventory | `GET /api/api-docs/events.json` | Searchable event names, direction, source, and payload-contract status. |+| Socket.IO spec | `GET /api/api-docs/asyncapi.json` | AsyncAPI 3 document for the Socket.IO transport. |++Run `npm run generate:api-docs` after route or event declarations change. The+generated manifests are source-derived artifacts; they are not a second+handwritten endpoint list.++## Endpoint map++### Governed semantic registry++| Method | Endpoint | Request and response behavior |+|---|---|---|+| `GET` | `/api/cos/tools` | Query: `scope=all\|agent\|mind\|ui\|voice`, optional `intent` (trimmed, ≤500 characters), and `format=portos\|openai\|anthropic\|mcp`. Returns a catalog with an `ETag`; `If-None-Match` returns `304`. |+| `POST` | `/api/cos/tools/call` | Body is a strict `portos_tool_call`. Optional `Idempotency-Key` must equal `requestId`. Authority is derived as the HTTP `ui` principal from the server auth context. |+| `GET` | `/api/cos/tools/calls/:requestId` | Returns the retained normalized result for a process-local call, or `404 TOOL_CALL_NOT_FOUND`. Retention is in memory, so callers must not treat this as durable job history. |++The semantic registry is not a route proxy. A caller supplies a catalog tool+name and typed arguments; it cannot supply a URL, route, shell command, SQL+query, or adapter name. The registry resolves the tool, validates its closed+input schema, checks scope and capabilities, then invokes a named adapter.++### Agent Tools MCP++| Method | Endpoint | Request and response behavior |+|---|---|---|+| `GET` | `/api/agent-context/manifest` | Loopback/origin-checked manifest. It remains readable when the feature is disabled and reports `enabled`, profile, scopes, grants, limits, exclusions, and advertised tool schemas. |+| `POST` | `/api/agent-context/mcp` | Stateless MCP Streamable HTTP. Requires `Accept: application/json, text/event-stream`; supports `initialize`, `ping`, `tools/list`, and `tools/call`. Notifications receive `202`; unsupported GET/DELETE receive `405`. |++The transport accepts loopback socket addresses only and rejects a non-loopback+`Origin`. The normal PortOS authentication gate still applies when an instance+password is configured. Context tools remain read-only. Semantic actions are a+separate default-off grant and are executed through the same registry as the+HTTP and Persistent Mind paths.++### Persistent Mind authority inventory++`GET /api/cos/mind/tools` remains a separate authority view. It reports the+Persistent Mind capability schema, boundaries, task catalog, and grant state;+it is not the generic semantic catalog. Persistent Mind execution uses the+same registry internally and has a five-call semantic/tool budget plus a+five-task-per-turn budget. No new authority is implied by the broader HTTP+inventory.++## Canonical registry entry++The runtime shape is defined by `server/services/cosToolRegistry.js` and+`server/lib/cosToolContracts.js`:++```json+{+ "type": "portos_tool",+ "name": "brain.search",+ "version": 1,+ "providerName": "brain_search",+ "aliases": ["brain_search"],+ "description": "Search the user's brain inbox.",+ "input_schema": {+ "type": "object",+ "required": ["query"],+ "properties": {+ "query": {"type": "string"},+ "limit": {"type": "integer"}+ },+ "additionalProperties": false+ },+ "output_schema": {"type": "object", "additionalProperties": true},+ "policy": {+ "scopes": ["agent", "mind", "ui", "voice"],+ "requiredCapabilities": ["readPortos"],+ "sideEffect": "read",+ "idempotent": true,+ "async": false,+ "confirmation": "none"+ }+}+```++The runtime currently uses `write` and `read` as semantic side-effect values;+the broader design spec's future `local-write`, `external-write`, and+`process-control` taxonomy is not yet the shipped registry vocabulary.+`adapter` is an internal field and is not exposed in public catalog responses.++## Shipped semantic tool definitions++The canonical name is stable; the provider name and legacy alias preserve the+existing voice-tool contract. `readPortos` and `writePortos` are independent+grants. The input details below summarize the source schemas; the live catalog+is authoritative for descriptions and JSON Schema.++| Canonical name | Provider/legacy name | Arguments | Side effect |+|---|---|---|---|+| `brain.capture` | `brain_capture` | `text`; required | write |+| `brain.search` | `brain_search` | `query`; optional `limit` | read |+| `brain.recent` | `brain_list_recent` | optional `limit` | read |+| `health.log.drink` | `meatspace_log_drink` | `name`; optional `count`, `oz`, `abv` | write |+| `health.log.nicotine` | `meatspace_log_nicotine` | `product`; optional `count`, `mgPerUnit` | write |+| `health.today` | `meatspace_summary_today` | none | read |+| `health.log.weight` | `meatspace_log_weight` | `weight`; optional `unit`, `date` | write |+| `health.log.workout` | `meatspace_log_workout` | `type`; optional `durationMinutes`, `intensity`, `notes`, `date` | write |+| `goals.list` | `goal_list` | optional `limit` | read |+| `goals.update-progress` | `goal_update_progress` | `goalQuery`; `progress` | write |+| `goals.log-note` | `goal_log_note` | `goalQuery`; `note`; optional `durationMinutes` | write |+| `system.processes.status` | `pm2_status` | none | read |+| `feeds.digest` | `feeds_digest` | optional `limit` | read |+| `feeds.mark-read` | `feeds_mark_read` | optional `query`, `all`, `feedQuery` | write |+| `journal.append` | `daily_log_append` | `text`; optional `date` | write |+| `journal.read` | `daily_log_read` | optional `date` | read |+| `time.now` | `time_now` | none | read |+| `calendar.today` | `calendar_today` | optional `limit` | read |+| `calendar.next` | `calendar_next` | none | read |+| `weather.now` | `weather_now` | optional `lat`, `lon` | read |+| `cos.agents.status` | `code_agent_status` | none | read |+| `catalog.search` | `catalog_lookup` | `query`; optional `type`, `limit` | read |++All 21 entries are scope-eligible for `agent`, `ui`, and `voice`; the+Persistent Mind `mind` scope additionally includes `cos.create-task`. The+task tool is not in the Agent MCP catalog. It is a Persistent Mind-only+capability and validates its app, provider, model, effort, mode, required+checks, tracker, readiness, and landing policy before queueing.++## MCP context tool definitions++These five tools are advertised from `server/lib/agentContextValidation.js`+and executed by `server/services/agentContextMcp.js`:++| Tool | Input | Output and requirement |+|---|---|---|+| `context_profile` | `{}` | Active profile, enabled scopes, semantic grants, limits, and exclusions. |+| `search_context` | `{ query: string (1–200), scopes?: unique scope[], limit?: 1–25 }` | Bounded items, `total`, `truncated`, `sourceTruncated`, and `sourceStatus`. |+| `get_context` | `{ ref: string (1–180) }` | One item or `null`, plus source truncation/status. An unavailable scope returns `item: null`. |+| `list_context` | `{ scope, cursor?: integer ≥0, limit?: 1–25 }` | Bounded page, `total`, nullable `nextCursor`, truncation, and source status. |+| `resolve_navigation` | `{ query: string (1–200) }` | A navigation match or `null`; requires the `navigation` scope. |++Context items are limited to Navigation, Workspaces, Brain projections, and+Identity-export sections. Profiles are `metadata` and `summary`; summaries+apply high-confidence redaction but are not an anonymization guarantee. The+transport excludes privacy-vault data, credentials, federation/network and+machine identity, repository paths and branches, browser/message history,+health records, and raw personal exports.++## Canonical call and result++The strict call envelope is:++```json+{+ "type": "portos_tool_call",+ "requestId": "example-call-01",+ "name": "brain.search",+ "version": 1,+ "arguments": {"query": "example", "limit": 5}+}+```++`requestId` is required, trimmed, limited to 200 characters, and restricted to+letters, numbers, `.`, `_`, `:`, and `-`. `name` is similarly constrained and+must resolve to a canonical name, provider name, or legacy alias. Unknown+fields are rejected. `type` and `version` default to the current values when+omitted; a future version must be rejected until its compatibility path is+implemented.++A normalized outcome is shaped as follows:++```json+{+ "type": "portos_tool_result",+ "requestId": "example-call-01",+ "name": "brain.search",+ "version": 1,+ "state": "completed",+ "duplicate": false,+ "result": {"ok": true, "count": 0, "hits": [], "summary": "No matches."}+}+```++Adapter failures use `state: "failed"` and a bounded `error` string. A+replayed request with the same canonical tool and normalized arguments returns+the retained result with `duplicate: true`; a reused request ID with different+content returns `409 TOOL_IDEMPOTENCY_CONFLICT`. Results and fingerprints are+process-local and retained only for bounded in-memory windows.++Provider translations are mechanical projections of the same entry:++- OpenAI uses `{ type: "function", function: { name: providerName,+ description, parameters: input_schema } }`.+- Anthropic uses `{ name: providerName, description, input_schema }`.+- MCP uses `name`, `description`, `inputSchema`, `outputSchema`, and standard+ read-only/destructive/idempotent/open-world annotations.++## Authority and bridge++```text+Persistent Mind ─┐+Authenticated UI ─┼─> portos_tool_call -> resolve -> validate -> authorize -> adapter+Agent MCP ───────┘ │+ └─> portos_tool_result++Agent MCP context tools ───────> read-only context handlers (separate path)+REST/OpenAPI route inventory ──> discovery only; raw routes are never tools+```++| Caller | Server-derived authority | Default | Allowed mutation path |+|---|---|---|---|+| Persistent Mind | `scope: mind`, persisted capability grant | off | `cos.create-task`, semantic writes when separately granted |+| Agent MCP | `scope: agent`, Agent Tools action grant | off | semantic writes when separately granted |+| HTTP registry | `scope: ui`, PortOS auth context | reads may be anonymous on a passwordless install | writes require an authenticated PortOS session |+| Voice adapter | existing voice pipeline context | existing voice policy | existing voice-side confirmation/pipeline controls |++The request body cannot claim a scope, principal, or capability. Catalog+`scope` is a filter, not proof of authority; an `intent` filter only reduces+the advertised voice subset and never authorizes a call. The public API+registry remains separate: CoS tools and Agent MCP do not become externally+exposable merely because they appear in the internal OpenAPI inventory.++## Findings and follow-up boundary++1. **Resolved documentation drift.** The prior unified spec said 2,066 HTTP+ operations, 23 Persistent Mind tools, 22 semantic tools, and a `cursor`+ catalog query. The current generated inventory is 2,067 operations; the+ runtime registry is 22 tools total (21 semantic plus `cos.create-task`),+ and the implemented catalog query is `scope`, `intent`, and `format`.+ The point-in-time spec's implemented-foundation text is corrected in this+ PR.+2. **OpenAPI completeness is intentionally staged.** The internal document is+ exhaustive as an operation inventory, but most operations are marked+ `generated` and expose only path parameters plus a default response. It+ must not be used as if every route already had a stable request/response+ schema. Rich contracts belong in `server/lib/apiOperationContracts.js` and+ must reuse route Zod schemas.+3. **Long-running job semantics are not shipped in the registry yet.** The+ current `cos.create-task` adapter returns a completed tool call whose nested+ result says `state: "queued"`; there is no `/jobs/:jobId/events`, cancel,+ confirm, or durable result endpoint. Those routes remain proposed design+ backlog and must not be advertised by a client generated from this current+ contract.+4. **The five-call budget is a Persistent Mind budget.** It is enforced by the+ Persistent Mind adapter across its bounded turn loop. It is not a generic+ HTTP/MCP rate limit; callers using those transports must rely on grants,+ typed adapters, idempotency, and the deployment trust boundary.+5. **Disabled MCP manifest nuance.** The MCP execution route is blocked while+ the feature is disabled, but a previously saved semantic action grant can+ still appear in the readable manifest because advertised semantic tools are+ derived from action grants independently of `enabled`. This is a discovery+ inconsistency, not an execution bypass; a follow-up should either suppress+ semantic advertisements while disabled or document the intentionally+ inspectable grant state more prominently.++Evidence anchors for these findings are the generated manifest `stats`,+`VOICE_ADAPTERS` in `server/services/cosToolRegistry.js`, the catalog query+schema in `server/lib/cosToolContracts.js`, the async task policy and normalized+result path in `server/services/cosToolRegistry.js`, and+`semanticToolsForConfig`/`getManifest` in `server/services/agentContextMcp.js`.++## Validation++- `node scripts/generate-api-route-catalog.js` — regenerated deterministic+ HTTP manifest: 2,067 operations / 2,070 declarations / 145 mounts.+- `node scripts/generate-socket-event-catalog.js` — deterministic 253-event+ Socket.IO manifest.+- Focused Vitest execution was attempted but this isolated worktree has no+ installed `server/node_modules` (`vitest: command not found`). No live+ database, provider, MCP client, or personal records were used.diff --git a/docs/README.md b/docs/README.mdindex 30add4b297..667b1f0f6f 100644--- a/docs/README.md+++ b/docs/README.md@@ -8,6 +8,7 @@ Index of everything under `docs/`. Start with the [root README](../README.md) fo|-----|--------|| [ARCHITECTURE.md](./ARCHITECTURE.md) | System design: React client, Express server, PM2 satellites, PostgreSQL + `data/` files || [API.md](./API.md) | REST endpoints, complete route-domain index, Socket.IO events |+| [API_TOOL_CONTRACT.md](./API_TOOL_CONTRACT.md) | Unified semantic tool, Persistent Mind, and Agent Tools MCP contract || [COMPANION_APP_API.md](./COMPANION_APP_API.md) | PortDeck native iOS companion client discovery and HTTP API contract || [REMOTE_DESKTOP.md](./REMOTE_DESKTOP.md) | PortDeck VNC broker security, host setup, and session flow || [FEDERATED_MEDIA_PROVIDERS.md](./FEDERATED_MEDIA_PROVIDERS.md) | Authenticated, capacity-aware peer audio provider wire contract and setup |diff --git a/docs/superpowers/specs/2026-08-27-unified-cos-tool-calling-interface.md b/docs/superpowers/specs/2026-08-27-unified-cos-tool-calling-interface.mdindex 7ec08f7383..5c63844866 100644--- a/docs/superpowers/specs/2026-08-27-unified-cos-tool-calling-interface.md+++ b/docs/superpowers/specs/2026-08-27-unified-cos-tool-calling-interface.md@@ -13,9 +13,9 @@ Audience: PortOS server, Persistent Mind, voice, palette, and future agent integThe initial production slice is implemented. This section supersedes proposal language elsewhere in the document when the two differ.-- The generated HTTP inventory contains 2,066 mounted operations and feeds `GET /api/api-docs/catalog.json` plus the complete OpenAPI 3.1 document at `/api/api-docs/internal/openapi.json`.+- The generated HTTP inventory contains 2,067 mounted operations and feeds `GET /api/api-docs/catalog.json` plus the complete OpenAPI 3.1 document at `/api/api-docs/internal/openapi.json`.- A generated inventory of 253 Socket.IO events feeds `/api/api-docs/events.json` and the AsyncAPI 3 document at `/api/api-docs/asyncapi.json`.-- `/api/cos/tools` exposes 23 Persistent Mind tools and 22 CoS-agent/UI/voice tools through the versioned provider-neutral catalog. OpenAI, Anthropic, and MCP translations derive from the same entries.+- `/api/cos/tools` exposes 22 provider-neutral tools: one Persistent Mind task tool plus 21 semantic voice adapters. The `mind` scope can see all 22 when granted; the `agent`, `ui`, and `voice` scopes can see the 21 semantic adapters. OpenAI, Anthropic, and MCP translations derive from the same entries.- `/api/cos/tools/call` and `/api/cos/tools/calls/:requestId` implement server-derived UI authority, schema validation, normalized results, and replay conflict detection. Raw routes are not callable.- Persistent Mind has separate default-off read, write, and CoS-task grants and a bounded multi-round tool loop. `cos.create-task` reuses the existing scheduler, worktree, review, CI, and landing-policy path.- The existing loopback-only Agent Context MCP transport now optionally advertises the same semantic registry under separate default-off CoS-agent read/write grants. Its original bounded context scopes and privacy behavior remain intact.@@ -34,7 +34,7 @@ The interface is not a generic HTTP proxy. A raw endpoint proxy would expose inc| Method | Endpoint | Purpose ||---|---|---|-| `GET` | `/api/cos/tools` | Return the current, scope-filtered tool catalog and capability state. Supports `scope`, `intent`, `cursor`, and conditional `ETag` requests. |+| `GET` | `/api/cos/tools` | Return the current, scope-filtered tool catalog and capability state. Supports `scope`, `intent`, `format`, and conditional `ETag` requests. || `POST` | `/api/cos/tools/call` | Execute one tool call synchronously or start its asynchronous job. Requires an idempotency key for mutations. || `GET` | `/api/cos/tools/calls/:requestId` | Read the normalized outcome of a prior call, including a job reference when work is still running. || `POST` | `/api/cos/tools/calls/:requestId/approve` | Record explicit approval from a separate trusted human/UI session for a pending confirmation. |diff --git a/server/lib/apiRouteCatalog.generated.json b/server/lib/apiRouteCatalog.generated.jsonindex 9fc334e3ec..a483493c25 100644--- a/server/lib/apiRouteCatalog.generated.json+++ b/server/lib/apiRouteCatalog.generated.json@@ -5578,7 +5578,7 @@"sources": [{"source": "server/routes/cosMindRoutes.js",- "line": 271+ "line": 280}]},@@ -5589,7 +5589,7 @@"sources": [{"source": "server/routes/cosMindRoutes.js",- "line": 256+ "line": 265}]},@@ -5600,7 +5600,7 @@"sources": [{"source": "server/routes/cosMindRoutes.js",- "line": 261+ "line": 270}]},@@ -5622,7 +5622,7 @@"sources": [{"source": "server/routes/cosMindRoutes.js",- "line": 297+ "line": 306}]},@@ -5633,7 +5633,7 @@"sources": [{"source": "server/routes/cosMindRoutes.js",- "line": 310+ "line": 319}]},@@ -5644,7 +5644,7 @@"sources": [{"source": "server/routes/cosMindRoutes.js",- "line": 242+ "line": 251}]},@@ -5655,7 +5655,7 @@"sources": [{"source": "server/routes/cosMindRoutes.js",- "line": 248+ "line": 257}]},@@ -5666,7 +5666,7 @@"sources": [{"source": "server/routes/cosMindRoutes.js",- "line": 266+ "line": 275}]},@@ -5677,7 +5677,7 @@"sources": [{"source": "server/routes/cosMindRoutes.js",- "line": 283+ "line": 292}]},@@ -5688,7 +5688,7 @@"sources": [{"source": "server/routes/cosMindRoutes.js",- "line": 289+ "line": 298}]},@@ -5699,7 +5699,7 @@"sources": [{"source": "server/routes/cosMindRoutes.js",- "line": 223+ "line": 232}]},@@ -5710,7 +5710,7 @@"sources": [{"source": "server/routes/cosMindRoutes.js",- "line": 279+ "line": 288}]},@@ -5721,7 +5721,7 @@"sources": [{"source": "server/routes/cosMindRoutes.js",- "line": 293+ "line": 302}]},@@ -5743,7 +5743,7 @@"sources": [{"source": "server/routes/cosMindRoutes.js",- "line": 232+ "line": 241}]},