Files
wanderbooks/crates/ledger-mcp/src/opening.rs
T
swaitsandClaude Fable 5.1 996c5f40f4 feat(mcp): prompts, resource templates and report dates on resources (P5)
Agent reason: the procedures an agent follows (onboarding, reconciling a
month, closing a month, the CPA hand-off, the year's compliance, an audit)
should be one name away, written against this server's own tools, instead of
rebuilt from the handbook each session. The server now advertises the
prompts capability (also in the bridge's cold-start initialize, which uses
the same capabilities function) and serves prompts/list and prompts/get to a
token that may read: onboard, reconcile {account, month}, month_end_close
{month}, cpa_handoff {year}, annual_compliance {year}, audit {scope}. Each
text is a file in crates/ledger-mcp/prompts/ with the company's name and the
arguments filled in. migrate, process_inbox and receipts_from_email are not
built (D-46, D-28, D-17). audit points at books.verify, books.control_check
and the digest; P8 fills in its own tools.

An argument is one token (letters, digits, - _ . :, at most 40) of the shape
the prompt asks for (a month, a year, a word from a list), so what a caller
passes can never rewrite the text; an unknown prompt, a missing, extra or
malformed argument is refused. A test checks every prompt names only tools
that exist.

The five resource templates of plan 4.5 are advertised (resources/templates/
list): books://entries/{id}, books://attachments/{id}, books://journal/{seq},
books://reports/{kind}{?start,end,as_of} and
books://digest{?since,since_seq,as_of}; the reports now take their dates
from the query (they read only today's or the year to date before), and the
digest reads since_seq (what was posted since a journal line, backdated
entries included).

Tests: the_prompts_name_only_tools_that_exist,
an_argument_is_one_token_of_the_right_shape.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01HS8WZziDnyxunC23MUxrLp
2026-10-29 15:48:27 -06:00

92 lines
3.5 KiB
Rust

//! What the agents' bridge answers `initialize` with while the books are
//! still opening (plan 3.9, "The bridge and slow starts").
//!
//! ```text
//! GOAL: an agent whose session starts while the books are closed gets
//! its `initialize` answered within its client's startup timeout,
//! with capabilities that are the server's own.
//! SOURCE: plan/served.md 3.9 (the bridge answers initialize itself after
//! 5 s, "from the tool list built into the same executable and a
//! short generic text that points at books.start").
//! INVARIANTS:
//! 1. The capabilities are the server's (one function builds both).
//! 2. The protocol version is the one asked for when this build knows
//! it and it has an initialize handshake, else the newest that does.
//! 3. The text names no book and no folder: it points at books.start.
//! ```
use rmcp::model::{Implementation, ProtocolVersion, ServerCapabilities, ServerConfig};
/// The text an agent gets while the books open (INVARIANT 3).
pub const OPENING_TEXT: &str = "wanderbooks: double-entry books, opening now. Call books.start first: it returns these books' rules, conventions and open items, and every procedure is in books.handbook. Calls wait while the books open.";
/// The server's capabilities (INVARIANT 1).
pub(crate) fn capabilities() -> ServerCapabilities {
ServerCapabilities::builder()
.enable_tools()
.enable_resources()
.enable_prompts()
.build()
}
/// The `initialize` result the bridge gives while the books open, for a
/// client that asked for protocol version `requested`.
pub fn opening_initialize(requested: Option<&str>) -> serde_json::Value {
let version = requested
.and_then(|asked| {
ProtocolVersion::KNOWN_VERSIONS
.iter()
.find(|v| v.as_str() == asked && v.has_initialize())
})
.cloned()
.unwrap_or(ProtocolVersion::LATEST_WITH_INITIALIZE);
let mut info = ServerConfig::new(capabilities())
.with_instructions(OPENING_TEXT)
.with_server_info(Implementation::new(
"wanderbooks",
env!("CARGO_PKG_VERSION"),
));
info.protocol_version = version;
serde_json::to_value(info).expect("an initialize result is plain data")
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn the_version_asked_for_is_kept_when_known() {
for v in ["2024-11-05", "2025-03-26", "2025-06-18", "2025-11-25"] {
assert_eq!(opening_initialize(Some(v))["protocolVersion"], v);
}
let newest = ProtocolVersion::LATEST_WITH_INITIALIZE.as_str().to_string();
for v in [
Some("1999-01-01"),
Some("2026-07-28"),
Some("nonsense"),
None,
] {
assert_eq!(
opening_initialize(v)["protocolVersion"],
newest.as_str(),
"{v:?}"
);
}
}
#[test]
fn it_names_the_server_and_points_at_books_start() {
let r = opening_initialize(None);
assert_eq!(r["serverInfo"]["name"], "wanderbooks");
assert_eq!(r["serverInfo"]["version"], env!("CARGO_PKG_VERSION"));
assert!(r["instructions"].as_str().unwrap().contains("books.start"));
assert_eq!(
r["capabilities"],
serde_json::to_value(capabilities()).unwrap()
);
assert!(r["capabilities"]["tools"].is_object());
assert!(r["capabilities"]["resources"].is_object());
assert!(r["capabilities"]["prompts"].is_object());
}
}