advisers 0.3.1 → 0.4.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README-zh.md +13 -15
- package/README.md +32 -22
- package/THIRD-PARTY-NOTICES.md +1 -1
- package/bin/advisers.js +2769 -183
- package/index.js +1 -1
- package/lib/brief-archive.js +267 -0
- package/lib/client-pack.js +1016 -0
- package/lib/consent.js +390 -0
- package/lib/credentials.js +195 -0
- package/lib/customer-read.js +524 -0
- package/lib/error-codes.js +516 -7
- package/lib/evidence.js +928 -120
- package/lib/kb-extract.js +626 -0
- package/lib/kb-facts.js +429 -0
- package/lib/kb-pages.js +1084 -0
- package/lib/kb.js +1036 -0
- package/lib/ledger-read.js +109 -1
- package/lib/mail-errors.js +46 -0
- package/lib/mail-extract.js +351 -0
- package/lib/mail-imap.js +446 -0
- package/lib/mail-import.js +176 -0
- package/lib/mail-parse.js +223 -0
- package/lib/mail-store.js +731 -0
- package/lib/mail.js +506 -0
- package/lib/mcp-server.js +397 -11
- package/lib/notify.js +265 -7
- package/lib/policy/decision.js +315 -0
- package/lib/policy/egress.js +268 -0
- package/lib/policy/errors.js +71 -0
- package/lib/policy/index.js +99 -0
- package/lib/policy/rules.js +468 -0
- package/lib/policy/surfaces.js +141 -0
- package/lib/receipt-verify.js +283 -26
- package/lib/receivables.js +1653 -0
- package/lib/scheduler.js +453 -9
- package/lib/shell.js +1406 -0
- package/lib/source.js +98 -3
- package/lib/sqlite.js +501 -49
- package/lib/tos/index.js +2 -2
- package/lib/tos/manifest.json +6 -6
- package/lib/tos/privacy.zh-CN.md +33 -20
- package/lib/tos/safety.zh-CN.md +7 -6
- package/lib/tos/summary.zh-CN.md +3 -2
- package/lib/tos/terms.zh-CN.md +31 -16
- package/lib/tui/labels.js +1 -1
- package/lib/tui/render.js +1 -1
- package/lib/urgency.js +467 -0
- package/package.json +4 -4
package/lib/mcp-server.js
CHANGED
|
@@ -1,10 +1,22 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* Advisers MCP server (JSON-RPC 2.0 over stdio
|
|
2
|
+
* Advisers MCP server (JSON-RPC 2.0 over stdio; legacy '2024-11-05' handshake
|
|
3
|
+
* plus the stateless modern '2026-07-28' revision — dual-era since 0.4.0).
|
|
3
4
|
*
|
|
4
5
|
* This module is the testable library extracted from the MCP section of
|
|
5
6
|
* 'bin/advisers.js'. The binary owns process wiring (home, audit key, sqlite);
|
|
6
7
|
* this module owns the protocol surface and never touches the network.
|
|
7
8
|
*
|
|
9
|
+
* Era rules (0.4.0):
|
|
10
|
+
* - A request is served by the modern path only when it carries the per-request
|
|
11
|
+
* metadata in 'params._meta' (io.modelcontextprotocol/protocolVersion or
|
|
12
|
+
* .../clientCapabilities) and is not 'initialize'. The modern path is a strict
|
|
13
|
+
* allowlist (server/discover, tools/list, tools/call, resources/list,
|
|
14
|
+
* resources/read) that returns the SAME read-model payload wrapped in the
|
|
15
|
+
* modern result envelope (resultType + optional ttlMs/cacheScope + _meta).
|
|
16
|
+
* - Every other request keeps the legacy dispatcher byte-for-byte, so 0.3.x
|
|
17
|
+
* clients observe exactly the old responses. ADVISERS_MCP_MODERN=off disables
|
|
18
|
+
* the era layer entirely.
|
|
19
|
+
*
|
|
8
20
|
* Surface (0.2.0):
|
|
9
21
|
* - 5 read-only tools: today / commit_ls / who / evidence / remind
|
|
10
22
|
* - 2 whitelisted writes:
|
|
@@ -36,6 +48,7 @@ import {
|
|
|
36
48
|
} from './evidence.js'
|
|
37
49
|
import { buildReminders } from './remind.js'
|
|
38
50
|
import { inspectCaptureText } from './capture.js'
|
|
51
|
+
import { listClientRows, readClientStatus } from './customer-read.js'
|
|
39
52
|
import { classify, redact, safetyErrorFrom } from './safety/index.js'
|
|
40
53
|
|
|
41
54
|
/**
|
|
@@ -48,13 +61,65 @@ export const PROTOCOL_VERSION = '2024-11-05'
|
|
|
48
61
|
*
|
|
49
62
|
* The 2025-06-18 / 2025-11-25 revisions keep the same initialize/tools/
|
|
50
63
|
* resources lifecycle this server implements, so echoing the client's
|
|
51
|
-
* supported version is safe; anything else (including
|
|
52
|
-
*
|
|
53
|
-
* PROTOCOL_VERSION. The modern
|
|
54
|
-
*
|
|
55
|
-
*
|
|
64
|
+
* supported version is safe; anything else (including a stray 2026-07-28
|
|
65
|
+
* value, which is modern-era and never negotiated through initialize) falls
|
|
66
|
+
* back to PROTOCOL_VERSION. The modern revision is served statelessly through
|
|
67
|
+
* the per-request `_meta` path below; dual-era clients pick it with
|
|
68
|
+
* `server/discover` and legacy clients keep the handshake.
|
|
56
69
|
*/
|
|
57
70
|
export const SUPPORTED_PROTOCOL_VERSIONS = Object.freeze(['2024-11-05', '2025-06-18', '2025-11-25'])
|
|
71
|
+
/**
|
|
72
|
+
* Modern (stateless) MCP revision served by the era layer (0.4.0).
|
|
73
|
+
*
|
|
74
|
+
* Spec: <https://modelcontextprotocol.io/specification/2026-07-28/>. There is
|
|
75
|
+
* no initialize handshake: every request declares its protocol version and
|
|
76
|
+
* client capabilities in `params._meta` and the server answers it statelessly.
|
|
77
|
+
*/
|
|
78
|
+
export const MODERN_PROTOCOL_VERSION = '2026-07-28'
|
|
79
|
+
export const SUPPORTED_MODERN_PROTOCOL_VERSIONS = Object.freeze([MODERN_PROTOCOL_VERSION])
|
|
80
|
+
/** Reserved modern `_meta` keys (spec: basic/index#_meta). */
|
|
81
|
+
export const META_PROTOCOL_VERSION = 'io.modelcontextprotocol/protocolVersion'
|
|
82
|
+
export const META_CLIENT_CAPABILITIES = 'io.modelcontextprotocol/clientCapabilities'
|
|
83
|
+
export const META_SERVER_INFO = 'io.modelcontextprotocol/serverInfo'
|
|
84
|
+
/**
|
|
85
|
+
* Explicit modern method allowlist. The modern era removed initialize/ping/
|
|
86
|
+
* logging/roots and keeps resources/subscribe out; anything not listed here is
|
|
87
|
+
* answered with -32601 instead of falling through to the legacy dispatcher.
|
|
88
|
+
*/
|
|
89
|
+
export const MODERN_METHODS = Object.freeze(['server/discover', 'tools/list', 'tools/call', 'resources/list', 'resources/read'])
|
|
90
|
+
/**
|
|
91
|
+
* Cache hints for cacheable modern results (spec: server/utilities/caching).
|
|
92
|
+
* The tool/resource catalogue is static for the process lifetime, so it is safe
|
|
93
|
+
* to cache publicly; ledger reads are private and stale immediately.
|
|
94
|
+
*/
|
|
95
|
+
export const MODERN_CACHE_HINTS = Object.freeze({
|
|
96
|
+
discover: Object.freeze({ ttlMs: 300000, cacheScope: 'public' }),
|
|
97
|
+
toolsList: Object.freeze({ ttlMs: 300000, cacheScope: 'public' }),
|
|
98
|
+
resourcesList: Object.freeze({ ttlMs: 300000, cacheScope: 'public' }),
|
|
99
|
+
resourcesRead: Object.freeze({ ttlMs: 0, cacheScope: 'private' }),
|
|
100
|
+
})
|
|
101
|
+
/** ADVISERS_MCP_MODERN values that disable the modern era (default: enabled). */
|
|
102
|
+
const MODERN_OFF_VALUES = Object.freeze(['off', '0', 'false', 'no', 'disabled'])
|
|
103
|
+
/** True unless ADVISERS_MCP_MODERN explicitly disables the modern era. */
|
|
104
|
+
export function resolveModernEnabled(env = process.env) {
|
|
105
|
+
const raw = env?.ADVISERS_MCP_MODERN
|
|
106
|
+
if (raw === undefined || raw === null) return true
|
|
107
|
+
return !MODERN_OFF_VALUES.includes(String(raw).trim().toLowerCase())
|
|
108
|
+
}
|
|
109
|
+
/**
|
|
110
|
+
* Modern per-request metadata, or null for a legacy request. `initialize` is
|
|
111
|
+
* never modern (the handshake IS the legacy-era discriminator), and a request
|
|
112
|
+
* with no modern `_meta` key stays on the legacy path even when `_meta` exists
|
|
113
|
+
* for another reason (e.g. `progressToken`) — era is never guessed.
|
|
114
|
+
*/
|
|
115
|
+
function modernRequestMeta(params) {
|
|
116
|
+
if (!params || typeof params !== 'object' || Array.isArray(params)) return null
|
|
117
|
+
const meta = params._meta
|
|
118
|
+
if (!meta || typeof meta !== 'object' || Array.isArray(meta)) return null
|
|
119
|
+
if (!Object.prototype.hasOwnProperty.call(meta, META_PROTOCOL_VERSION)
|
|
120
|
+
&& !Object.prototype.hasOwnProperty.call(meta, META_CLIENT_CAPABILITIES)) return null
|
|
121
|
+
return meta
|
|
122
|
+
}
|
|
58
123
|
export const SERVER_NAME = 'advisers'
|
|
59
124
|
/** Hard cap for one stdio JSON-RPC line (bytes). */
|
|
60
125
|
export const MAX_LINE_BYTES = 1024 * 1024
|
|
@@ -76,6 +141,11 @@ export const MCP_OUTPUT_LIMITS = Object.freeze({
|
|
|
76
141
|
whoRows: 20,
|
|
77
142
|
reminders: 20,
|
|
78
143
|
evidenceItems: 20,
|
|
144
|
+
clients: 20,
|
|
145
|
+
clientsMax: 50,
|
|
146
|
+
clientTimeline: 20,
|
|
147
|
+
clientTimelineMax: 50,
|
|
148
|
+
clientOpen: 20,
|
|
79
149
|
})
|
|
80
150
|
/**
|
|
81
151
|
* Last-resort byte budget for one tool/resource payload. The per-tool row caps
|
|
@@ -86,7 +156,7 @@ export const MCP_OUTPUT_LIMITS = Object.freeze({
|
|
|
86
156
|
* classifier never turns a large ledger into CONTENT_BLOCKED.
|
|
87
157
|
*/
|
|
88
158
|
export const MCP_MAX_RESULT_BYTES = 64 * 1024
|
|
89
|
-
const PAYLOAD_LIST_KEYS = Object.freeze(['commitments', 'mine', 'waiting', 'reminders', 'sources', 'missing'])
|
|
159
|
+
const PAYLOAD_LIST_KEYS = Object.freeze(['commitments', 'mine', 'waiting', 'reminders', 'sources', 'missing', 'clients', 'projects', 'timeline'])
|
|
90
160
|
/** Receipt directories are small; refuse to slurp anything huge. */
|
|
91
161
|
export const MAX_PACK_FILE_BYTES = 512 * 1024
|
|
92
162
|
export const MAX_PACK_SUMS_BYTES = 64 * 1024
|
|
@@ -104,6 +174,7 @@ export const TOOL_NAMES = Object.freeze([
|
|
|
104
174
|
'advisers_who',
|
|
105
175
|
'advisers_evidence',
|
|
106
176
|
'advisers_remind',
|
|
177
|
+
'advisers_clients',
|
|
107
178
|
'advisers_capture_candidate',
|
|
108
179
|
'advisers_pack_verify',
|
|
109
180
|
])
|
|
@@ -113,6 +184,7 @@ export const RESOURCE_URIS = Object.freeze([
|
|
|
113
184
|
'advisers://commitments',
|
|
114
185
|
'advisers://who',
|
|
115
186
|
'advisers://evidence',
|
|
187
|
+
'advisers://clients',
|
|
116
188
|
])
|
|
117
189
|
|
|
118
190
|
const RESOURCE_DEFINITIONS = Object.freeze([
|
|
@@ -140,6 +212,12 @@ const RESOURCE_DEFINITIONS = Object.freeze([
|
|
|
140
212
|
description: 'Evidence traceback index for commitments, facts and preferences (counts only).',
|
|
141
213
|
mimeType: 'application/json',
|
|
142
214
|
},
|
|
215
|
+
{
|
|
216
|
+
uri: 'advisers://clients',
|
|
217
|
+
name: 'clients',
|
|
218
|
+
description: 'Active client rows as compact read-only projections (exact counts and next due date; no note text or conversation bodies), capped with total/truncated/limit and clearance/protectedExcluded.',
|
|
219
|
+
mimeType: 'application/json',
|
|
220
|
+
},
|
|
143
221
|
])
|
|
144
222
|
|
|
145
223
|
const READ_ONLY_ANNOTATIONS = Object.freeze({
|
|
@@ -455,10 +533,31 @@ export function readReceiptDirectory(dir) {
|
|
|
455
533
|
* (0.2.5 A-2). Every read model excludes records whose matter secrecy is
|
|
456
534
|
* above it and reports `protectedExcluded`; 0 (fail closed) is the
|
|
457
535
|
* default and a client cannot raise it through a tool argument.
|
|
536
|
+
* @param {Function} [options.loadCommitmentView] 0.3.2 P2-1 narrow read model:
|
|
537
|
+
* `(options: {all: boolean, clearance: number}) => {rows, total,
|
|
538
|
+
* protectedExcluded}` where rows are the positional projection from
|
|
539
|
+
* `SqliteStore.projectCommitmentReadModel()` (id/debtor/creditor/action/
|
|
540
|
+
* dueAt/status/matterId/reminderOffsets/hidden). When supplied, today /
|
|
541
|
+
* commit_ls / who / remind read this projection instead of loading the
|
|
542
|
+
* full ledger. Omitted => the legacy ledger path is used unchanged
|
|
543
|
+
* (embedders/tests that pass `ledger` never change behaviour).
|
|
544
|
+
* @param {Function} [options.loadCommitmentRecords] 0.3.2 P2-1 hydration for
|
|
545
|
+
* `advisers_who` (whose payload is full Commitment rows): `(ids: string[])
|
|
546
|
+
* => Map<string, object> | object[]`. Only the capped <= 20 rows per
|
|
547
|
+
* bucket are hydrated; omitted => the narrow who path is not used.
|
|
548
|
+
* @param {boolean} [options.modern] Dual-era (0.4.0): serve the stateless
|
|
549
|
+
* 2026-07-28 revision for requests that carry modern per-request
|
|
550
|
+
* `params._meta`. Defaults to `ADVISERS_MCP_MODERN` (enabled unless the
|
|
551
|
+
* env value is off/0/false/no/disabled); explicit `false` forces
|
|
552
|
+
* legacy-only, `true` forces dual-era even when the env disables it.
|
|
458
553
|
* @returns {{protocolVersion: string, handle: Function, handleLine: Function, serve: Function, tools: Function, resources: Function}}
|
|
459
554
|
*/
|
|
460
|
-
export function createMcpServer({ store, ledger, loadLedger, loadStore, version = '0.0.0', audit, readPack, now = () => new Date(), clearance = 0 } = {}) {
|
|
555
|
+
export function createMcpServer({ store, ledger, loadLedger, loadStore, loadCommitmentView, loadCommitmentRecords, version = '0.0.0', audit, readPack, now = () => new Date(), clearance = 0, modern } = {}) {
|
|
461
556
|
const serverVersion = String(version ?? '0.0.0')
|
|
557
|
+
// 0.4.0 MCP modern: dual-era switch. The env var is read per factory call, so
|
|
558
|
+
// `ADVISERS_MCP_MODERN=off advisers mcp --stdio` is a one-restart rollback to
|
|
559
|
+
// the exact 0.3.2 surface without touching the CLI wiring.
|
|
560
|
+
const modernEnabled = modern === undefined ? resolveModernEnabled() : modern === true
|
|
462
561
|
// Protocol version agreed by the last legacy `initialize` (advisory only:
|
|
463
562
|
// the legacy method surface is identical for every supported revision).
|
|
464
563
|
let negotiatedProtocolVersion = PROTOCOL_VERSION
|
|
@@ -474,6 +573,30 @@ export function createMcpServer({ store, ledger, loadLedger, loadStore, version
|
|
|
474
573
|
let resolvedLedger = ledger
|
|
475
574
|
let resolvedStore = store
|
|
476
575
|
const ledgerCache = new Map()
|
|
576
|
+
// 0.3.2 P2-1: process-level cache of the narrow commitment view, keyed like
|
|
577
|
+
// the legacy ledger cache (the snapshot is taken on first use and reused until
|
|
578
|
+
// the process exits; refresh semantics are unchanged).
|
|
579
|
+
const commitmentViewCache = new Map()
|
|
580
|
+
function commitmentView(options = {}) {
|
|
581
|
+
if (typeof loadCommitmentView !== 'function') return null
|
|
582
|
+
const key = String(options.all === true) + ':' + String(options.clearance ?? clearanceLevel)
|
|
583
|
+
if (commitmentViewCache.has(key)) return commitmentViewCache.get(key)
|
|
584
|
+
const view = loadCommitmentView(options)
|
|
585
|
+
commitmentViewCache.set(key, view)
|
|
586
|
+
return view
|
|
587
|
+
}
|
|
588
|
+
/** Normalize loadCommitmentRecords() output to an id -> record Map. */
|
|
589
|
+
function commitmentRecordsById(ids) {
|
|
590
|
+
const loaded = loadCommitmentRecords(ids)
|
|
591
|
+
if (loaded instanceof Map) return loaded
|
|
592
|
+
const byId = new Map()
|
|
593
|
+
if (Array.isArray(loaded)) {
|
|
594
|
+
for (const record of loaded) {
|
|
595
|
+
if (record && typeof record.id === 'string' && record.id) byId.set(record.id, record)
|
|
596
|
+
}
|
|
597
|
+
}
|
|
598
|
+
return byId
|
|
599
|
+
}
|
|
477
600
|
function ledgerFor(kind = 'full') {
|
|
478
601
|
if (resolvedLedger !== undefined) return resolvedLedger
|
|
479
602
|
if (ledgerCache.has(kind)) return ledgerCache.get(kind)
|
|
@@ -544,6 +667,19 @@ export function createMcpServer({ store, ledger, loadLedger, loadStore, version
|
|
|
544
667
|
/* ------------------------------ read models ------------------------------ */
|
|
545
668
|
|
|
546
669
|
function todayData() {
|
|
670
|
+
const narrow = commitmentView({ all: false, clearance: clearanceLevel })
|
|
671
|
+
if (narrow) {
|
|
672
|
+
const visible = narrow.rows.filter((row) => !row.hidden)
|
|
673
|
+
const commitments = visible.slice(0, MCP_OUTPUT_LIMITS.todayCommitments)
|
|
674
|
+
return {
|
|
675
|
+
open: visible.length,
|
|
676
|
+
commitments: commitments.map((c) => ({ id: c.id, debtor: c.debtor, creditor: c.creditor, action: c.action, dueAt: c.dueAt })),
|
|
677
|
+
clearance: clearanceLevel,
|
|
678
|
+
protectedExcluded: narrow.protectedExcluded,
|
|
679
|
+
total: visible.length,
|
|
680
|
+
truncated: visible.length > commitments.length,
|
|
681
|
+
}
|
|
682
|
+
}
|
|
547
683
|
const open = requireLedger('commitments').openCommitments()
|
|
548
684
|
const view = splitByClearance('commitment', open)
|
|
549
685
|
// Cap AFTER clearance and BEFORE the egress classifier: `open` and `total`
|
|
@@ -560,6 +696,21 @@ export function createMcpServer({ store, ledger, loadLedger, loadStore, version
|
|
|
560
696
|
}
|
|
561
697
|
|
|
562
698
|
function commitListData(all, requestedLimit) {
|
|
699
|
+
const narrow = commitmentView({ all: true, clearance: clearanceLevel })
|
|
700
|
+
if (narrow) {
|
|
701
|
+
const statusFiltered = all ? narrow.rows : narrow.rows.filter((row) => row.status === 'open')
|
|
702
|
+
const visible = statusFiltered.filter((row) => !row.hidden)
|
|
703
|
+
const limit = Number.isInteger(requestedLimit) ? requestedLimit : MCP_OUTPUT_LIMITS.commitRows
|
|
704
|
+
const commitments = visible.slice(0, limit)
|
|
705
|
+
return {
|
|
706
|
+
commitments: commitments.map((c) => ({ id: c.id, status: c.status, action: c.action, dueAt: c.dueAt, creditor: c.creditor })),
|
|
707
|
+
clearance: clearanceLevel,
|
|
708
|
+
protectedExcluded: statusFiltered.length - visible.length,
|
|
709
|
+
total: visible.length,
|
|
710
|
+
truncated: visible.length > commitments.length,
|
|
711
|
+
limit,
|
|
712
|
+
}
|
|
713
|
+
}
|
|
563
714
|
const rows = requireLedger('commitments')
|
|
564
715
|
.listCommitments()
|
|
565
716
|
.filter((c) => all || c.status === 'open')
|
|
@@ -577,6 +728,30 @@ export function createMcpServer({ store, ledger, loadLedger, loadStore, version
|
|
|
577
728
|
}
|
|
578
729
|
|
|
579
730
|
function whoData() {
|
|
731
|
+
const narrow = commitmentView({ all: false, clearance: clearanceLevel })
|
|
732
|
+
if (narrow) {
|
|
733
|
+
const visible = narrow.rows.filter((row) => !row.hidden)
|
|
734
|
+
const mine = visible.filter((c) => c.debtor === 'user' || c.debtor === 'agent')
|
|
735
|
+
const waiting = visible.filter((c) => c.debtor === 'third' || String(c.debtor).startsWith('third:'))
|
|
736
|
+
const limit = MCP_OUTPUT_LIMITS.whoRows
|
|
737
|
+
// who returns full Commitment rows (the frozen v1 shape with
|
|
738
|
+
// evidence/matterId/timezone/...), so hydrate only the capped page.
|
|
739
|
+
const hydrate = (list) => {
|
|
740
|
+
const capped = list.slice(0, limit)
|
|
741
|
+
if (typeof loadCommitmentRecords !== 'function' || !capped.length) return capped
|
|
742
|
+
const byId = commitmentRecordsById(capped.map((row) => row.id))
|
|
743
|
+
return capped.map((row) => byId.get(row.id) ?? row)
|
|
744
|
+
}
|
|
745
|
+
return {
|
|
746
|
+
mine: hydrate(mine),
|
|
747
|
+
waiting: hydrate(waiting),
|
|
748
|
+
clearance: clearanceLevel,
|
|
749
|
+
protectedExcluded: narrow.protectedExcluded,
|
|
750
|
+
mineTotal: mine.length,
|
|
751
|
+
waitingTotal: waiting.length,
|
|
752
|
+
truncated: mine.length > limit || waiting.length > limit,
|
|
753
|
+
}
|
|
754
|
+
}
|
|
580
755
|
const open = requireLedger('commitments').openCommitments()
|
|
581
756
|
const view = splitByClearance('commitment', open)
|
|
582
757
|
const mine = view.visible.filter((c) => c.debtor === 'user' || c.debtor === 'agent')
|
|
@@ -594,6 +769,33 @@ export function createMcpServer({ store, ledger, loadLedger, loadStore, version
|
|
|
594
769
|
}
|
|
595
770
|
|
|
596
771
|
function remindData() {
|
|
772
|
+
const narrow = commitmentView({ all: true, clearance: clearanceLevel })
|
|
773
|
+
if (narrow) {
|
|
774
|
+
const hiddenIds = new Set()
|
|
775
|
+
for (const row of narrow.rows) if (row.hidden) hiddenIds.add(row.id)
|
|
776
|
+
let reminders
|
|
777
|
+
try {
|
|
778
|
+
// buildReminders only needs listCommitments(); the narrow rows carry the
|
|
779
|
+
// fields it reads (id/action/dueAt/status/reminderOffsets).
|
|
780
|
+
reminders = buildReminders({ listCommitments: () => narrow.rows }, { now: nowDate() })
|
|
781
|
+
} catch {
|
|
782
|
+
throw new ToolError('reminder planning failed')
|
|
783
|
+
}
|
|
784
|
+
const visible = []
|
|
785
|
+
let protectedExcluded = 0
|
|
786
|
+
for (const reminder of reminders) {
|
|
787
|
+
if (hiddenIds.has(reminder.commitmentId)) protectedExcluded += 1
|
|
788
|
+
else visible.push(reminder)
|
|
789
|
+
}
|
|
790
|
+
const capped = visible.slice(0, MCP_OUTPUT_LIMITS.reminders)
|
|
791
|
+
return {
|
|
792
|
+
reminders: capped,
|
|
793
|
+
clearance: clearanceLevel,
|
|
794
|
+
protectedExcluded,
|
|
795
|
+
total: visible.length,
|
|
796
|
+
truncated: visible.length > capped.length,
|
|
797
|
+
}
|
|
798
|
+
}
|
|
597
799
|
const ledgerApi = requireLedger('commitments')
|
|
598
800
|
let reminders
|
|
599
801
|
try {
|
|
@@ -700,6 +902,49 @@ export function createMcpServer({ store, ledger, loadLedger, loadStore, version
|
|
|
700
902
|
}
|
|
701
903
|
}
|
|
702
904
|
|
|
905
|
+
/* ----------------------------- customer reads ---------------------------- */
|
|
906
|
+
|
|
907
|
+
/**
|
|
908
|
+
* Customer entities are SQLite-only (schema v21). A JSON store (storeFor()
|
|
909
|
+
* returns undefined), a legacy embedder store without the customer API or a
|
|
910
|
+
* store opened before the customer tables existed is a structured ToolError,
|
|
911
|
+
* never a crash and never fake empty data.
|
|
912
|
+
*/
|
|
913
|
+
function requireCustomerStore() {
|
|
914
|
+
const storeApi = storeFor()
|
|
915
|
+
if (!storeApi || typeof storeApi.read !== 'function' || typeof storeApi.listClients !== 'function'
|
|
916
|
+
|| typeof storeApi.getClient !== 'function' || typeof storeApi.listMattersByClient !== 'function') {
|
|
917
|
+
throw new ToolError('customer data requires the SQLite ledger (ADVISERS_STORE=sqlite)')
|
|
918
|
+
}
|
|
919
|
+
return storeApi
|
|
920
|
+
}
|
|
921
|
+
|
|
922
|
+
function clientsListData(all, requestedLimit) {
|
|
923
|
+
const storeApi = requireCustomerStore()
|
|
924
|
+
const limit = Number.isInteger(requestedLimit) ? requestedLimit : MCP_OUTPUT_LIMITS.clients
|
|
925
|
+
const result = listClientRows(storeApi, { clearance: clearanceLevel, limit, includeArchived: all === true })
|
|
926
|
+
return {
|
|
927
|
+
clients: result.rows,
|
|
928
|
+
total: result.total,
|
|
929
|
+
truncated: result.total > result.rows.length,
|
|
930
|
+
limit: result.limit,
|
|
931
|
+
clearance: clearanceLevel,
|
|
932
|
+
protectedExcluded: result.protectedExcluded,
|
|
933
|
+
}
|
|
934
|
+
}
|
|
935
|
+
|
|
936
|
+
function clientsShowData(args) {
|
|
937
|
+
const id = requireString(args, 'id', { min: 1, max: 120 })
|
|
938
|
+
const timelineLimit = optionalLimit(args, 'timelineLimit', MCP_OUTPUT_LIMITS.clientTimelineMax)
|
|
939
|
+
const view = readClientStatus(requireCustomerStore(), id, {
|
|
940
|
+
clearance: clearanceLevel,
|
|
941
|
+
timelineLimit: timelineLimit === undefined ? MCP_OUTPUT_LIMITS.clientTimeline : timelineLimit,
|
|
942
|
+
openLimit: MCP_OUTPUT_LIMITS.clientOpen,
|
|
943
|
+
})
|
|
944
|
+
if (!view) throw new ToolError('unknown client id')
|
|
945
|
+
return view
|
|
946
|
+
}
|
|
947
|
+
|
|
703
948
|
/* ------------------------------ write tools ------------------------------ */
|
|
704
949
|
|
|
705
950
|
function captureCandidate(args) {
|
|
@@ -834,6 +1079,33 @@ export function createMcpServer({ store, ledger, loadLedger, loadStore, version
|
|
|
834
1079
|
annotations: READ_ONLY_ANNOTATIONS,
|
|
835
1080
|
run: () => remindData(),
|
|
836
1081
|
},
|
|
1082
|
+
{
|
|
1083
|
+
name: 'advisers_clients',
|
|
1084
|
+
description:
|
|
1085
|
+
'Read-only customer projection: list active clients (all=true includes archived) or show one client by id. Rows carry exact counts, nextDueAt, clearance and protectedExcluded; show adds project summaries, open mine/waiting items and a derived delivery/commitment timeline. Note text and conversation bodies are never returned. At most ' +
|
|
1086
|
+
MCP_OUTPUT_LIMITS.clients + ' client rows are returned (limit 1..' + MCP_OUTPUT_LIMITS.clientsMax + '); timelineLimit (1..' + MCP_OUTPUT_LIMITS.clientTimelineMax + ') caps the show timeline. ' +
|
|
1087
|
+
CLEARANCE_NOTE,
|
|
1088
|
+
inputSchema: {
|
|
1089
|
+
type: 'object',
|
|
1090
|
+
properties: {
|
|
1091
|
+
action: { type: 'string', enum: ['list', 'show'], description: 'list: client rows; show: one client view.' },
|
|
1092
|
+
id: { type: 'string', description: 'Client id (cl_…). Required when action is show.' },
|
|
1093
|
+
all: { type: 'boolean', description: 'Include archived clients (action=list).' },
|
|
1094
|
+
limit: { type: 'integer', minimum: 1, maximum: MCP_OUTPUT_LIMITS.clientsMax, description: 'Max client rows for action=list (default ' + MCP_OUTPUT_LIMITS.clients + ', max ' + MCP_OUTPUT_LIMITS.clientsMax + ').' },
|
|
1095
|
+
timelineLimit: { type: 'integer', minimum: 1, maximum: MCP_OUTPUT_LIMITS.clientTimelineMax, description: 'Max timeline rows for action=show (default ' + MCP_OUTPUT_LIMITS.clientTimeline + ', max ' + MCP_OUTPUT_LIMITS.clientTimelineMax + ').' },
|
|
1096
|
+
},
|
|
1097
|
+
required: ['action'],
|
|
1098
|
+
},
|
|
1099
|
+
annotations: READ_ONLY_ANNOTATIONS,
|
|
1100
|
+
run: (args) => {
|
|
1101
|
+
const action = optionalEnum(args, 'action', ['list', 'show'], undefined)
|
|
1102
|
+
if (action === undefined) throw invalidParams('action must be one of list, show')
|
|
1103
|
+
if (action === 'list') {
|
|
1104
|
+
return clientsListData(optionalBoolean(args, 'all'), optionalLimit(args, 'limit', MCP_OUTPUT_LIMITS.clientsMax))
|
|
1105
|
+
}
|
|
1106
|
+
return clientsShowData(args)
|
|
1107
|
+
},
|
|
1108
|
+
},
|
|
837
1109
|
{ ...CAPTURE_TOOL, run: (args) => captureCandidate(args) },
|
|
838
1110
|
{ ...PACK_VERIFY_TOOL, run: (args) => verifyReceipt(args) },
|
|
839
1111
|
].map((tool) => ({ ...tool, write: tool.write === true }))
|
|
@@ -885,6 +1157,108 @@ export function createMcpServer({ store, ledger, loadLedger, loadStore, version
|
|
|
885
1157
|
}
|
|
886
1158
|
}
|
|
887
1159
|
|
|
1160
|
+
/* -------------------------- modern (2026-07-28) -------------------------- */
|
|
1161
|
+
|
|
1162
|
+
/** Modern result envelope: resultType is mandatory; the payload is untouched. */
|
|
1163
|
+
function modernResult(result, cache) {
|
|
1164
|
+
const envelope = {
|
|
1165
|
+
resultType: 'complete',
|
|
1166
|
+
...result,
|
|
1167
|
+
_meta: { [META_SERVER_INFO]: { name: SERVER_NAME, version: serverVersion } },
|
|
1168
|
+
}
|
|
1169
|
+
if (cache) {
|
|
1170
|
+
envelope.ttlMs = cache.ttlMs
|
|
1171
|
+
envelope.cacheScope = cache.cacheScope
|
|
1172
|
+
}
|
|
1173
|
+
return envelope
|
|
1174
|
+
}
|
|
1175
|
+
|
|
1176
|
+
/** Modern JSON-RPC error response (errors never carry resultType). */
|
|
1177
|
+
function modernError(id, error) {
|
|
1178
|
+
const payload = { code: error.code, message: error.message }
|
|
1179
|
+
if (error.data !== undefined) payload.data = error.data
|
|
1180
|
+
return { jsonrpc: '2.0', id, error: payload }
|
|
1181
|
+
}
|
|
1182
|
+
|
|
1183
|
+
/**
|
|
1184
|
+
* Wrap a legacy result response in the modern envelope. The payload object
|
|
1185
|
+
* itself is spread unchanged, so tools/call content[0].text and resources/read
|
|
1186
|
+
* contents[0].text stay byte-identical to the legacy response.
|
|
1187
|
+
*/
|
|
1188
|
+
function modernWrappedResult(legacyResponse, cache) {
|
|
1189
|
+
const result = legacyResponse?.result
|
|
1190
|
+
if (!result || typeof result !== 'object' || Array.isArray(result)) return legacyResponse
|
|
1191
|
+
return { jsonrpc: '2.0', id: legacyResponse.id, result: modernResult(result, cache) }
|
|
1192
|
+
}
|
|
1193
|
+
|
|
1194
|
+
/**
|
|
1195
|
+
* Validate the required per-request modern `_meta` fields. Missing or
|
|
1196
|
+
* ill-typed metadata is a malformed request (-32602); a well-formed but
|
|
1197
|
+
* unsupported version is -32022 with the supported list (client retries).
|
|
1198
|
+
*/
|
|
1199
|
+
function modernMetaError(meta) {
|
|
1200
|
+
const requested = meta[META_PROTOCOL_VERSION]
|
|
1201
|
+
if (typeof requested !== 'string' || !requested.trim()) {
|
|
1202
|
+
return { code: -32602, message: 'invalid params: _meta field ' + META_PROTOCOL_VERSION + ' must be a string' }
|
|
1203
|
+
}
|
|
1204
|
+
const version = requested.trim()
|
|
1205
|
+
if (!SUPPORTED_MODERN_PROTOCOL_VERSIONS.includes(version)) {
|
|
1206
|
+
return { code: -32022, message: 'Unsupported protocol version', data: { supported: [...SUPPORTED_MODERN_PROTOCOL_VERSIONS], requested: version } }
|
|
1207
|
+
}
|
|
1208
|
+
const capabilities = meta[META_CLIENT_CAPABILITIES]
|
|
1209
|
+
if (capabilities === undefined || capabilities === null || typeof capabilities !== 'object' || Array.isArray(capabilities)) {
|
|
1210
|
+
return { code: -32602, message: 'invalid params: _meta field ' + META_CLIENT_CAPABILITIES + ' must be an object' }
|
|
1211
|
+
}
|
|
1212
|
+
return null
|
|
1213
|
+
}
|
|
1214
|
+
|
|
1215
|
+
/** This server returns a single static page; any client cursor is invalid. */
|
|
1216
|
+
function assertNoModernCursor(params) {
|
|
1217
|
+
if (Object.prototype.hasOwnProperty.call(params, 'cursor')) throw invalidParams('invalid cursor')
|
|
1218
|
+
}
|
|
1219
|
+
|
|
1220
|
+
/**
|
|
1221
|
+
* Stateless modern dispatcher (strict allowlist). Methods that only exist in
|
|
1222
|
+
* the legacy lifecycle (initialize/ping/...) answer -32601 here instead of
|
|
1223
|
+
* leaking into the legacy dispatcher; notifications are never answered.
|
|
1224
|
+
*/
|
|
1225
|
+
async function handleModernRequest(hasId, id, method, params, meta) {
|
|
1226
|
+
if (!hasId) return null
|
|
1227
|
+
const metaError = modernMetaError(meta)
|
|
1228
|
+
if (metaError) return modernError(id, metaError)
|
|
1229
|
+
if (method.startsWith('notifications/')) return null
|
|
1230
|
+
if (!MODERN_METHODS.includes(method)) return rpcError(id, -32601, 'unknown method')
|
|
1231
|
+
try {
|
|
1232
|
+
if (method === 'server/discover') {
|
|
1233
|
+
return reply(id, modernResult({
|
|
1234
|
+
supportedVersions: [...SUPPORTED_MODERN_PROTOCOL_VERSIONS],
|
|
1235
|
+
capabilities: { tools: { listChanged: false }, resources: { listChanged: false } },
|
|
1236
|
+
}, MODERN_CACHE_HINTS.discover))
|
|
1237
|
+
}
|
|
1238
|
+
if (method === 'tools/list') {
|
|
1239
|
+
assertNoModernCursor(params)
|
|
1240
|
+
return reply(id, modernResult({ tools: toolsList() }, MODERN_CACHE_HINTS.toolsList))
|
|
1241
|
+
}
|
|
1242
|
+
if (method === 'resources/list') {
|
|
1243
|
+
assertNoModernCursor(params)
|
|
1244
|
+
return reply(id, modernResult({ resources: RESOURCE_DEFINITIONS.map((entry) => ({ ...entry })) }, MODERN_CACHE_HINTS.resourcesList))
|
|
1245
|
+
}
|
|
1246
|
+
if (method === 'tools/call') {
|
|
1247
|
+
return modernWrappedResult(await handleToolsCall(id, params))
|
|
1248
|
+
}
|
|
1249
|
+
// resources/read: modern uses -32602 for an unknown resource (legacy keeps
|
|
1250
|
+
// -32002 for pre-0.4 clients); ledger reads are private and immediately stale.
|
|
1251
|
+
const legacyResponse = await handleResourcesRead(id, params)
|
|
1252
|
+
if (legacyResponse?.error) {
|
|
1253
|
+
return legacyResponse.error.code === -32002 ? rpcError(id, -32602, 'resource not found') : legacyResponse
|
|
1254
|
+
}
|
|
1255
|
+
return modernWrappedResult(legacyResponse, MODERN_CACHE_HINTS.resourcesRead)
|
|
1256
|
+
} catch (error) {
|
|
1257
|
+
if (error instanceof RpcError) return rpcError(id, error.code, error.message)
|
|
1258
|
+
return rpcError(id, -32603, 'internal error')
|
|
1259
|
+
}
|
|
1260
|
+
}
|
|
1261
|
+
|
|
888
1262
|
/* ------------------------------ dispatcher ------------------------------ */
|
|
889
1263
|
|
|
890
1264
|
function reply(id, result) {
|
|
@@ -907,9 +1281,10 @@ export function createMcpServer({ store, ledger, loadLedger, loadStore, version
|
|
|
907
1281
|
* Legacy `initialize` result. 0.3.0 P0-T3: echo the client's version when
|
|
908
1282
|
* this server implements that legacy revision (2024-11-05 / 2025-06-18 /
|
|
909
1283
|
* 2025-11-25); a missing or unknown version keeps the 2024-11-05 default so
|
|
910
|
-
* pre-0.3 clients observe exactly the old behaviour. 2026-07-28
|
|
911
|
-
*
|
|
912
|
-
*
|
|
1284
|
+
* pre-0.3 clients observe exactly the old behaviour. 2026-07-28 is never
|
|
1285
|
+
* negotiated here: the modern revision is stateless and advertises itself
|
|
1286
|
+
* through `server/discover`, so a modern client probes that method instead of
|
|
1287
|
+
* calling initialize (dual-era; see docs/MCP.md).
|
|
913
1288
|
*/
|
|
914
1289
|
function initializeResult(requestedVersion) {
|
|
915
1290
|
const requested = typeof requestedVersion === 'string' ? requestedVersion.trim() : ''
|
|
@@ -992,6 +1367,7 @@ export function createMcpServer({ store, ledger, loadLedger, loadStore, version
|
|
|
992
1367
|
if (uri === 'advisers://today') data = todayData()
|
|
993
1368
|
else if (uri === 'advisers://commitments') data = commitListData(true)
|
|
994
1369
|
else if (uri === 'advisers://who') data = whoData()
|
|
1370
|
+
else if (uri === 'advisers://clients') data = clientsListData(false)
|
|
995
1371
|
else data = evidenceIndex()
|
|
996
1372
|
await recordAudit({ actor: 'mcp', action: 'mcp.call', allowed: true, reason: 'resource:' + uri })
|
|
997
1373
|
const bounded = boundPayload(data)
|
|
@@ -1016,6 +1392,16 @@ export function createMcpServer({ store, ledger, loadLedger, loadStore, version
|
|
|
1016
1392
|
}
|
|
1017
1393
|
const method = message.method
|
|
1018
1394
|
|
|
1395
|
+
// 0.4.0 dual-era: a request carrying the modern per-request `_meta` is
|
|
1396
|
+
// served by the stateless modern path. `initialize` is never modern (the
|
|
1397
|
+
// handshake is the legacy discriminator), and when ADVISERS_MCP_MODERN=off
|
|
1398
|
+
// no request takes this branch, so the legacy dispatcher below is byte-for-
|
|
1399
|
+
// byte the pre-0.4 surface.
|
|
1400
|
+
if (modernEnabled && method !== 'initialize') {
|
|
1401
|
+
const modernMeta = modernRequestMeta(message.params)
|
|
1402
|
+
if (modernMeta) return handleModernRequest(hasId, id, method, message.params, modernMeta)
|
|
1403
|
+
}
|
|
1404
|
+
|
|
1019
1405
|
try {
|
|
1020
1406
|
if (method === 'initialize') {
|
|
1021
1407
|
const params = paramsObject(message.params)
|