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
5 changes: 4 additions & 1 deletion CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -103,11 +103,14 @@ 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.
- **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.
- **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`).
- **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.

### Errors

Expand Down
2 changes: 1 addition & 1 deletion docs/adr/0021-mcp-wiring-and-tool-conventions.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ How contexts register tools:
| Update the Demographics Behavior Qualifier | `party_reference_data_directory_update_demographics` |
| Retrieve a Current Account Payment | `current_account_retrieve_payment` |

The name is set explicitly with `[McpServerTool(Name = …)]`. The description is the BIAN operation's description, and the input schema reuses the `.Api` request model (tech-stack §6).
The name is set explicitly with `[McpServerTool(Name = …)]`. The description is the BIAN operation's description, and the input schema reuses the `.Api` request model (tech-stack §6). *Amended by [ADR 0039](0039-party-mcp-tools.md):* tools take camelCase parameters, and their results mirror the `.Api` response models with a `version`, proven equal by parity tests, because `.Mcp` never references `.Api`.

**Annotations.** Every tool sets all four hints explicitly, so it never relies on the SDK or the protocol defaults (the protocol default for `destructiveHint` is `true`):

Expand Down
1 change: 1 addition & 0 deletions docs/adr/0022-mcp-authorization-and-tool-errors.md
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ The realm also gains the user `rimbank-developer` (password `rimbank-developer-l

## Consequences

- *Amended by [ADR 0039](0039-party-mcp-tools.md):* `RimBank.Mcp` also holds `ToolResults.Structured`, `[ToolOperation]` and the `ToolAuthorizationFilter` added by `AddRimBankToolAuthorization()`. A call refused by its operation's policy is an `isError` result with `shared.forbidden` (403), and the SDK's `AddAuthorizationFilters` is not used.
- *Amended by [ADR 0025](0025-one-api-project.md):* a context's MCP tools are a folder of `RimBank.Mcp` (namespace `RimBank.<BusinessDomain>.<ServiceDomain>.Mcp`), next to these conventions, not a project of their own.
- A `.Mcp` adapter (1.16) answers with `await bus.InvokeAsync<Result<T>>(…)` and then `.ToToolResult(…)`. The filter takes care of the trace, `instance`, unbindable arguments and unexpected failures. CONTRIBUTING "MCP" says so.
- MCP Inspector or Claude Code can connect to a local AppHost: they follow the 401 to the metadata, sign `rimbank-developer` in through `rimbank-mcp-client`, and call `/mcp` with the token. CONTRIBUTING describes it.
Expand Down
2 changes: 1 addition & 1 deletion docs/adr/0038-per-operation-authorisation-policies.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ The Party Reference Data Directory has 13 operations: 8 commands (Register, Upda
- Test tokens grant every catalog scope by default (`TestTokens.Issue(scopes: …)`), so tests that are not about authorisation keep working. Denial tests pass explicit scopes.
- Deployments must grant callers the scopes they need. A token without any RimBank scope can no longer call a BIAN operation.
- Follow-ups:
- **1.16 / 1.17:** each MCP tool carries its operation's policy (`[Authorize(Policy = …)]` with `AddAuthorizationFilters`), and a test checks that the tool and the endpoint share it.
- **1.16 / 1.17:** each MCP tool carries its operation's policy, and a test checks that the tool and the endpoint share it. *Done in [ADR 0039](0039-party-mcp-tools.md) with `[ToolOperation(OperationId)]` instead of `[Authorize]`, so that a refused call is the `shared.forbidden` Problem Details.*
- **4.14:** the money-moving scope, a third scope next to read and write for the Deposit and Payment operations.
- **7.2 / later:** resource-level authorisation (which party a caller may see) is not part of this decision.
- **7.x:** scope provisioning in the production identity provider.
Expand Down
75 changes: 75 additions & 0 deletions docs/adr/0039-party-mcp-tools.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
# 0039. Party MCP tools: arguments, results and authorisation

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

## Context

Mission principle 6 says an AI agent reaches every service domain through MCP tools that are thin adapters over the same core as HTTP. Principle 8 says an MCP error is the same Problem Details as on HTTP. Roadmap 1.16 adds the first context tools: Register and Retrieve of the Party Reference Data Directory's Control Record ([PartyReferenceDataDirectory.yaml](../../doc-0/release14.0.0/apis-iso20022_ext-ddd/oas3/yamls/PartyReferenceDataDirectory.yaml), [ADR 0024](0024-party-reference-data-directory-mapping.md)). The earlier decisions left four questions open:

- **Models.** Tech-stack §6 and [ADR 0021](0021-mcp-wiring-and-tool-conventions.md) said input schemas "reuse the `.Api` request models", but the rule `mcp-dependencies` forbids `.Mcp` → `.Api`.
- **Version.** HTTP sends an entry's stream version as `ETag`. MCP has no headers, and the Update tools of 1.17 need an `expectedVersion` ([ADR 0027](0027-optimistic-concurrency-for-commands.md)).
- **Authorisation.** [ADR 0038](0038-per-operation-authorisation-policies.md) planned `[Authorize(Policy = …)]` with the SDK's `AddAuthorizationFilters()`. Pinned on SDK 2.3.1 in this phase:
- the SDK's filter always runs before any other call-tool filter, whatever the order they are added in;
- it refuses a call with a JSON-RPC error (`-32600`, "Access forbidden"), with no Problem Details;
- without that filter, a tool that carries `[Authorize]` fails with "Authorization filter was not invoked".
- **Where the code lives.** `RimBank.Mcp` held only the shared conventions, which reference no core and no Wolverine.

## Options considered

**Models**
1. **Relax `mcp-dependencies`** so that `.Mcp` uses the `.Api` request and response types. There would be one type per body, but the two driving adapters would be coupled and could no longer move separately when a context is extracted (tech-stack §2).
2. **Mirror records in the `.Mcp` folder**, proven equal to the HTTP bodies by parity tests.

**Denied calls**
1. **Keep `[Authorize]`** and accept the JSON-RPC error. This breaks principle 8.
2. **Catch the SDK's exception** in `ToolErrorFilter`. This is not possible, because the SDK's filter runs first.
3. **A RimBank attribute and RimBank filters.** `[ToolOperation(OperationId)]` names the policy. A RimBank call-tool filter checks it and answers `shared.forbidden`, and a list-tools filter hides the tool. The tool carries no `IAuthorizeData`, so the SDK's authorization filters are not needed.

## Decision

**Models (option 2).** Each tool has its own result record in its `.Mcp` folder. It has the BIAN property names of the endpoint's response, plus **`version`**: the entry's stream version, the value HTTP sends as `ETag` and the `expectedVersion` of later commands. There is no `If-None-Match`/304 on MCP. `ToolResults.Structured(value)` (root of `RimBank.Mcp`) puts the JSON, written with the web defaults HTTP uses, in `structuredContent` and in one text block.

**Arguments.** Tools take camelCase method parameters with a description, not a body record. Only the arguments that change the outcome are taken:
- Register takes `partyType` and `idempotencyKey`. BIAN's ignored `PartyReference` and `DirectoryEntryDate*` fields are not offered to agents.
- Retrieve takes `partyReferenceDataDirectoryEntryId`.
- Both arguments of Register are required in the input schema, so a missing one is `shared.tool-arguments-invalid` (ADR 0022). The values go through the same core validation as HTTP:
- `IdempotencyKey.TryCreate`, so an empty or invalid key is `shared.idempotency-key-invalid`;
- `PartyTypes.FromBianName`;
- `PartyReferenceDataDirectoryEntryId.TryCreate`.

They then dispatch the same `RegisterParty` and `RetrievePartyReferenceDataDirectoryEntry` through `IMessageBus`. The command is idempotent through the existing policy ([ADR 0018](0018-idempotency-keys-for-commands.md)), and the query reads only the inline view.

**Authorisation (option 3).** A tool carries `[ToolOperation("<operation id>")]`, the same id and so the same policy and scope as its endpoint's `RequireOperation` (ADR 0038). `AddRimBankToolAuthorization()`, added by the host after `AddRimBankToolErrors()`, registers `ToolAuthorizationFilter`:
- **`tools/call`:** it finds the tool by name and evaluates the named policy with `IAuthorizationService`. If the policy is not met, the answer is `isError` with the `shared.forbidden` (403) Problem Details, and nothing is dispatched or stored. The tool error filter adds `instance` and `traceId`.
- **`tools/list`:** it removes the tools whose policy the caller does not meet, as the SDK's filter would.
- An unknown policy name throws, so the call is `shared.unexpected` and never runs without its policy.
- A tool without the attribute needs only the authenticated caller of `/mcp`. Only test fakes are like that. Every context tool has one, which the tool catalog test checks.

**Layout.** `RimBank.Mcp` references `RimBank.Core` and `WolverineFx` for its context folders. Those folders follow the `.Mcp` row of tech-stack §4. The root namespace `RimBank.Mcp` still uses neither, which `shared-mcp-dependencies` keeps checking. The Party tools are `RegisterPartyTool` and `RetrievePartyReferenceDataDirectoryEntryTool` in `src/RimBank.Mcp/Party/PartyReferenceDataDirectory/`, namespace `RimBank.Party.PartyReferenceDataDirectory.Mcp`. They are registered by `AddPartyReferenceDataDirectoryTools()`, which the module's `AddMcpTools` calls.

**Tool catalog guard (pulled forward from 1.17).** A test checks:
- the tool list equals the implemented operations of the `OperationPolicyCatalog`, minus a named list of the operations whose tools 1.17 adds;
- every tool is named after its `[ToolOperation]` id by the ADR 0021 rule;
- every tool carries the ADR 0021 annotations of its kind (a query is `ReadOnly`; every tool is non-destructive, idempotent and closed-world).

1.17 empties the list.

## Consequences

- Tech-stack §3.3, §4 and §6 and CONTRIBUTING "MCP" are updated. ADR 0021 ("input schemas reuse the `.Api` request models"), ADR 0022 (call-tool filters) and ADR 0038 (`[Authorize]` on tools) get notes that point here.
- A mirror record can drift from its HTTP model. The parity tests catch drift in members and values for every implemented tool, and 1.17 adds them for each new tool.
- 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.
- **4.14:** money-moving tools behind their own scope, off by default.
- **7.9:** rate limiting and the tool-call audit query.
- Proven by:
- `tests/RimBank.Host.Tests/Mcp/McpToolAuthorizationTests`: list filtering, `shared.forbidden` in both directions, and allowed calls;
- `tests/RimBank.Mcp.Tests/Party/PartyReferenceDataDirectory/`:
- `RegisterPartyToolTests` and `RetrievePartyReferenceDataDirectoryEntryToolTests`: results, idempotency, errors, authorisation, and nothing stored on refusal;
- `PartyToolCatalogTests`;
- `PartyMcpParityTests`: bodies, `version` against `ETag`, and Problem Details for an invalid party type, a reused key, a malformed or unknown id and a missing scope;
- the unchanged architecture rules `mcp-dependencies`, `shared-mcp-dependencies` and `http-and-mcp-adapters-live-in-shared-projects`.
Loading
Loading