# Optional agent access through MCP

An existing local agent with command execution can use the op-sys CLI directly;
start with [the agent getting-started guide](/docs/raw/manuals/agent-getting-started.md). MCP is an
alternative transport for the same capabilities, not a required onboarding step
or a separate agent identity.

`op-sys` exposes organizational memory to agents through a local stdio MCP server. The server uses the same tool catalog as the SDK and CLI, so agent access stays aligned with `opsys.tools.call()` and `op-sys tools call`.

The [shared public catalog](/docs/raw/manuals/public-capabilities.md) includes records, hosted
discovery/content, setup, state portability, and retained coordination operations.
MCP is a primary consumer interface; it is not limited to read-only memory queries.

Use `op_sys_ledgers_create`, `op_sys_ledgers_inspect`, `op_sys_records_append`,
`op_sys_records_get` and `op_sys_records_list` for ledger operations. Save operation
IDs before writes and respect returned outcome, coverage and recovery fields.
See [how ledgers work](/docs/raw/manuals/ledger-core.md) for the complete contract.

## Local Setup

Initialize or choose a local state root:

```bash
op-sys init
op-sys mcp doctor
```

Emit MCP client configuration:

```bash
op-sys mcp config
```

The payload contains an `mcpServers` object:

```json
{
  "mcpServers": {
    "op-sys": {
      "command": "op-sys",
      "args": ["mcp", "serve"],
      "env": {
        "OP_SYS_STATE_ROOT": "/absolute/path/.op-sys",
        "OP_SYS_SCOPE": "project"
      }
    }
  }
}
```

For project-scoped ledgers with a resolved repository root, the config also includes `OP_SYS_REPO_ROOT`.

The config is secret-free. Cloud API keys remain in the local state root `cloud/generations/<revision>/credentials.json` and are not emitted.

## Hosted ledger access

Authenticate a named connection and select it using the shared client context.
MCP uses the same record tools locally or remotely. Direct hosted operations need
no enrollment or local replica. Synchronization and authority transfer are explicit
choices; see [connections](/docs/raw/manuals/client-connections.md) and [replicas](/docs/raw/manuals/ledger-replicas.md).

For either link, call `op_sys_invitations_inspect` with `invitation`. Check
`target_type` and the offered access, then call `op_sys_invitations_accept` with
the same link. Follow the returned sign-in or resume action. Keep invitation URLs
out of shared transcripts.
[Ledger sharing](/docs/raw/manuals/ledger-core.md#invite-a-participant-to-a-ledger) ·
[Workspace onboarding](/docs/raw/manuals/team-onboarding.md)

## Runtime Command

Agent runtimes should launch:

```bash
op-sys mcp serve
```

The server writes only newline-delimited JSON-RPC MCP messages to stdout. Logs and diagnostics should go through `op-sys mcp doctor`, not through the server stdout stream.

## Protocol and lifecycle

The implemented compatibility set is `2025-11-25` only. A client must send a
valid `initialize` request with that `protocolVersion`, `clientInfo` name/version
and object `capabilities`, then `notifications/initialized`. Tools are available
only in `ready`; duplicate/out-of-order initialization does not advance state.
Unsupported versions return `-32602` with the supported list; arbitrary versions
are never echoed. `ping` is allowed before readiness. EOF closes the server.

Only tools are advertised (`listChanged: false`). Resources, prompts, task
cancellation and progress production are not implemented. Well-formed unknown
notifications have no response; cancellation/progress notifications are checked
but do not interrupt a synchronous tool call. No shutdown/exit extension is
advertised. The base stdio behavior follows the
[MCP lifecycle](https://modelcontextprotocol.io/specification/2025-11-25/basic/lifecycle)
and [transport specification](https://modelcontextprotocol.io/specification/2025-11-25/basic/transports).

The transport accepts one JSON-RPC 2.0 object per UTF-8 line; batches, null/bool
request IDs, invalid params and nonfinite JSON are rejected. Input is bounded to
1 MiB, tool arguments to 256 KiB, and serialized results to 32 MiB. Oversized input
closes the transport. Application tool errors use `isError`; internal errors
expose only a correlation ID. `OP_SYS_MCP_DIAGNOSTICS=1` enables secret-redacted
structured stderr diagnostics, never stdout logging. Doctor exercises the real
initialize → initialized → tools/list sequence for each supported version.

Use an MCP client that supports the documented protocol and initialization
sequence. Check its tool discovery and error handling when connecting.
