Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -104,13 +104,13 @@ The host serves one MCP server at `/mcp` (Streamable HTTP, stateless, [ADR 0021]
- **Adding tools:** a context's `.Mcp` adapter (its folder of `RimBank.Mcp`, [ADR 0025](docs/adr/0025-one-api-project.md)) exposes one `IMcpServerBuilder` extension, for example `AddPartyReferenceDataDirectoryTools()`, which calls `WithTools<…>()`. The context's module calls only that extension in `AddMcpTools`. Never call `AddMcpServer`, `WithHttpTransport` or `MapMcp` from a context. A tool name registered twice stops the host.
- **Names:** one tool per BIAN operation, `<service_domain>_<operation>[_<behavior_qualifier>]` in snake_case, set explicitly with `[McpServerTool(Name = …)]`. The description is the BIAN operation's description.
- **Authorisation:** every context tool carries `[ToolOperation(OperationId)]` from `RimBank.Mcp`, with the same BIAN operation id as its endpoint's `RequireOperation`, so it needs the same policy and scope ([ADR 0038](docs/adr/0038-per-operation-authorisation-policies.md), [ADR 0039](docs/adr/0039-party-mcp-tools.md)). A caller without the scope does not see the tool in `tools/list`, and a call gets `isError` with the `shared.forbidden` (403) Problem Details. Do not use `[Authorize]` on tools: the SDK would refuse the call with a JSON-RPC error instead.
- **Arguments and results:** arguments are the tool method's camelCase parameters. A command tool takes a required `idempotencyKey` and builds the same core command as its endpoint. A tool's result is its own record in its `.Mcp` folder that mirrors the endpoint's response (same BIAN property names) plus `version`, the entry's stream version that HTTP sends as `ETag`. Answer with `ToolResults.Structured(...)`. `.Mcp` never references `.Api`, so a parity test in `RimBank.Mcp.Tests` proves both channels give the same JSON and the same Problem Details.
- **Arguments and results:** arguments are the tool method's camelCase parameters. Ids are named after the BIAN path parameter (`partyReferenceDataDirectoryEntryId`, `referenceId`, …). A command tool takes a required `idempotencyKey`, a required `expectedVersion` when it changes a record (the `version` of the last Retrieve or command, HTTP's `If-Match`), and its BIAN request body as one `request` object that mirrors the endpoint's request record, so the partial-update and refusal rules are the same ([ADR 0040](docs/adr/0040-party-mcp-command-tools.md)). It builds the same core command as its endpoint. A tool's result is its own record in its `.Mcp` folder that mirrors the endpoint's response (same BIAN property names) plus `version`, the entry's stream version that HTTP sends as `ETag`. Answer with `ToolResults.Structured(...)`. `.Mcp` never references `.Api`, so a parity test in `RimBank.Mcp.Tests` proves both channels give the same JSON and the same Problem Details.
- **Annotations:** set `ReadOnly`, `Destructive`, `Idempotent` and `OpenWorld` on every tool, following the table in ADR 0021. `Retrieve` tools are read-only queries. Every other tool is a command with an idempotency key.
- **Errors:** a tool dispatches through `IMessageBus` and answers with `result.ToToolResult(...)` from `RimBank.Mcp`. A failed `Result` becomes `isError` with the same Problem Details as HTTP. Never throw for a business failure. The host's tool error filter adds `instance` and `traceId`, turns arguments the SDK cannot bind into `shared.tool-arguments-invalid` (400), and turns any exception into `shared.unexpected` (500) with no exception text.
- **Caller:** a tool call carries the token's subject to the message bus like an HTTP call. Do not read the token in a tool.
- **Connecting a local client:** start the AppHost, then point MCP Inspector (`npx @modelcontextprotocol/inspector`) or Claude Code at the host's `/mcp` URL from the Aspire dashboard. Use client ID `rimbank-mcp-client` (public, PKCE, loopback redirects `http://127.0.0.1:<any port>` and `http://localhost:6274`). Sign in as `rimbank-developer` / `rimbank-developer-local-only`. Both are dev-only and published on purpose.
- **Calling the Party tools:** once signed in, call `party_reference_data_directory_register` with `{ "partyType": "Person", "idempotencyKey": "demo-1" }`, then `party_reference_data_directory_retrieve` with the returned `PartyReferenceDataDirectoryEntryId` as `partyReferenceDataDirectoryEntryId`. An unknown id answers `isError` with the 404 Problem Details.
- **Tests** connect the SDK's `McpClient` in-process over `HttpClientTransport`, using the test server's `HttpClient` with a `TestTokens` bearer token (`McpTestHost` in `tests/RimBank.Host.Tests/Mcp`). A context's tools are tested in `tests/RimBank.Mcp.Tests/<BusinessDomain>/<ServiceDomain>/` against the real host and PostgreSQL (`PartyMcpHost`), including a tool-catalog test (each implemented operation has its tool, named after it, with its policy and annotations) and HTTP parity tests.
- **Calling the Party tools:** once signed in, call `party_reference_data_directory_register` with `{ "partyType": "Person", "idempotencyKey": "demo-1" }`, then `party_reference_data_directory_retrieve` with the returned `PartyReferenceDataDirectoryEntryId` as `partyReferenceDataDirectoryEntryId`. An unknown id answers `isError` with the 404 Problem Details. To change the entry, call for example `party_reference_data_directory_update_reference` with the entry id, the `ReferenceId`, `"request": { "ResidentialAddress": { "TownName": "Paris", "Country": { "Code": "FR" } } }`, the `version` you last read as `expectedVersion` and a new `idempotencyKey`. The answer carries the new `version`, and sending the old one again is `isError` with the 409 `party.version-conflict` Problem Details.
- **Tests** connect the SDK's `McpClient` in-process over `HttpClientTransport`, using the test server's `HttpClient` with a `TestTokens` bearer token (`McpTestHost` in `tests/RimBank.Host.Tests/Mcp`). A context's tools are tested in `tests/RimBank.Mcp.Tests/<BusinessDomain>/<ServiceDomain>/` against the real host and PostgreSQL (`PartyMcpHost`), including a tool-catalog test (the tools are exactly the implemented operations, each named after it, with its policy, annotations and the arguments of its kind) and HTTP parity tests for every tool.

### Errors

Expand Down
2 changes: 1 addition & 1 deletion docs/adr/0039-party-mcp-tools.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ Mission principle 6 says an AI agent reaches every service domain through MCP to
- If a later SDK lets a filter see its authorization denial, or answers it as a tool error, this decision can be revisited. The attribute would then become `[Authorize]`, and the tests stay.
- RimBank's tool list filter replaces the SDK's for these tools. Resources and prompts are not used.
- Follow-ups:
- **1.17:** Update and BQ tools with `expectedVersion`, and an empty catalog list.
- **1.17:** Update and BQ tools with `expectedVersion`, and an empty catalog list. Done: a command's body is one `request` argument, and Update and Exchange tools require `expectedVersion` ([ADR 0040](0040-party-mcp-command-tools.md)).
- **4.14:** money-moving tools behind their own scope, off by default.
- **7.9:** rate limiting and the tool-call audit query.
- Proven by:
Expand Down
77 changes: 77 additions & 0 deletions docs/adr/0040-party-mcp-command-tools.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
# 0040. Party MCP command tools: a BIAN body object and expected versions

- Status: Accepted
- Date: 2026-11-09
- Roadmap phase: 1.17

## Context

Roadmap 1.17 adds an MCP tool for each Party Reference Data Directory operation that 1.16 left out:
- the Control Record's Update;
- Demographics Update, Exchange and Retrieve;
- Reference Update and Retrieve;
- Associations Register, Update and Retrieve;
- BankRelations Update and Retrieve.

[ADR 0024](0024-party-reference-data-directory-mapping.md) maps these operations. [ADR 0039](0039-party-mcp-tools.md) fixed the conventions of the first two tools. Their results mirror the HTTP responses plus `version`, a command takes an `idempotencyKey`, and `[ToolOperation]` carries the policy. ADR 0039 also chose flat camelCase parameters for arguments, so that agents do not nest them.

That choice works for Register, whose body has one field. It does not fit the bodies of 1.17:
- **Size.** The Reference body has about 25 BIAN fields ([ADR 0029](0029-reference-fields-in-stages.md), [ADR 0032](0032-reference-legal-standing-and-documents.md), [ADR 0034](0034-reference-corporate-and-officer-references.md)). Several of them are objects: three ISO 20022 postal addresses, a government document with a validity period, and the corporate and officer references.
- **Partial updates.** The Update operations are partial updates with three states per field: left out keeps the value, empty clears it, and a value sets it. An object such as an address replaces the whole of it.
- **Refusals.** BIAN parts RimBank does not record are refused by path, for example `party.reference-field-not-supported`, through `[JsonExtensionData]` on each model.

The Update and Exchange tools also need MCP's form of HTTP's `If-Match` ([ADR 0027](0027-optimistic-concurrency-for-commands.md)), which ADR 0039 left to this phase.

## Options considered

**Arguments of a command with a body**
1. **Flat parameters**, one per BIAN field, with objects flattened (for example `residentialAddressTownName`). The schema is simplest, but Reference would take more than 60 parameters. Their names would no longer be BIAN's. The "left out, empty or set" rules and the refusal by BIAN path would have to be written again for MCP, outside the core inputs that HTTP shares.
2. **One parameter per top-level BIAN field, with BIAN objects as nested records.** This is ADR 0039's style at the top level, with BIAN names below it. It is still about 25 parameters with mixed naming, and the refusal of unknown top-level fields has no `[JsonExtensionData]` to hang on.
3. **One `request` object shaped like the BIAN request body**, next to the flat ids, `expectedVersion` and `idempotencyKey`. The JSON is the HTTP body, so the same mirror model, the same core input and the same refusals apply, and the parity tests compare like with like. Agents nest the body once, under a name that points to the BIAN schema.

**Expected version**
1. **Optional**, with the current version used when it is left out. This drops the lost-update protection that ADR 0027 requires of every command that changes a record.
2. **Required `long` parameter `expectedVersion`.** It is MCP's `If-Match`, taken from a Retrieve's or a previous command's `version`.

## Decision

**Option 3 for arguments, and a required `expectedVersion`.**

- **Shape.** A command tool whose operation has a BIAN request body takes it as one required `request` argument. `request` has the body's BIAN property names and mirrors the `.Api` request record in its own `.Mcp` folder, with the same nested models and `[JsonExtensionData]`.
- **Flat parameters.** The ids, `expectedVersion` and `idempotencyKey` stay flat camelCase parameters (ADR 0039). An id is named after its BIAN path parameter: `partyReferenceDataDirectoryEntryId`, `demographicsId`, `referenceId`, `associationsId` and `bankRelationsId`.
- **Register.** The Control Record's Register keeps its flat `partyType`, since its body has a single field (ADR 0039).
- **Translation.** The mirror translates the JSON into the existing core inputs (`DemographicsChanges`, `PartyReferenceInput`, `PartyAssociationInput`, `BankRelationsInput`), so every rule and error stays in `RimBank.Core`.
- **The partial-update rules carry over unchanged:**
- a field left out keeps its value;
- an empty value clears it;
- a given address or document replaces the whole of it;
- an unrecorded BIAN part is refused by its BIAN path, with the HTTP code;
- a property that is not BIAN's is ignored.
- **`expectedVersion`.** Every Update and Exchange tool requires it, a `long`, and passes it to the command as HTTP passes `If-Match`.
- A stale version is the same `party.version-conflict` (409) as on HTTP.
- A missing one is `shared.tool-arguments-invalid` (400), because the SDK refuses a missing required argument before the tool runs (ADR 0022). HTTP answers `shared.if-match-required` (428) instead. A missing value is the one case where MCP and HTTP differ.
- Register tools take no `expectedVersion`: they create a record.
- **Version per aggregate.** The `version` of a result is the version of the stream the operation wrote:
- the entry's for the Control Record, Demographics, Reference and BankRelations;
- the association's own stream for the three Associations tools, because an association is its own aggregate ([ADR 0033](0033-party-associations.md)).

It is the same value HTTP sends as `ETag`.
- **Schema.** With SDK 2.3.1, the generated input schema of `request` is a nested object with the BIAN property names. A `request` that is missing or not an object is `shared.tool-arguments-invalid`. The tests pin both.
- **Descriptions.** Each tool's description is its endpoint's, written for `request` and `expectedVersion` instead of a body and `If-Match`.
- **Guard.** The tool-list guard of ADR 0039 no longer has an allow-list. The tools are exactly the 13 implemented operations: 8 commands and 5 queries.

## Consequences

- Tech-stack §6 (Naming) and CONTRIBUTING "MCP" describe `request` and `expectedVersion`. ADR 0039's 1.17 follow-up points here.
- Agents nest a command's body once, under `request`, and can use the BIAN schema of the operation to fill it.
- Each new body-carrying tool adds a mirror of its `.Api` request, response and nested models. This doubles the model code, as ADR 0039 already accepted. The per-operation parity tests catch drift. In particular, they cover a full Reference with every recorded field, the refused parts, and the validation, not-found, stale-version and wrong-type-reference errors.
- Money-moving tools (4.14) follow the same shape. Their `destructive` and scope rules are added then.
- Proven by `tests/RimBank.Mcp.Tests/Party/PartyReferenceDataDirectory/`:
- `PartyToolCatalogTests`:
- the tool list equals the implemented operations;
- each tool has its policy and the annotations of its kind;
- the required arguments of each kind;
- the nested `request` schema;
- a read-scope caller sees only the 5 Retrieve tools and gets `shared.forbidden` on a command;
- `VersionRoundTripTests`: Retrieve, then Update, then a stale version is 409, a replay gives the same answer, and a missing `expectedVersion` is refused;
- the tool and parity tests of each BQ: `Update/`, `Demographics/`, `BankRelations/`, `Associations/` and `Reference/`.
Loading
Loading