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
29 changes: 17 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -221,17 +221,21 @@ interface. `write` is a draft: a reversible change that publishes nothing, which
an agent makes on its own. `publish` is a publication: an irreversible step or
one others see (an approval, a signature, a sending, a deletion, a movement of
money, the publication of a draft or of data to everyone). An agent calls it
only after the person's explicit "Publikuj". A publication is a tool of its own,
on the person's "Publikuj". That word is an agreement between the person and
the agent, not a mechanism the App checks (the Owner, 2026-11-22), so **a
production application offers its publications to agents** as a development
version does, on `/mcp` and on the page. A publication is a tool of its own,
never a mode of a draft tool, and the registry gives it `destructiveHint: true`
itself. A draft and a publication are offered only while `dataWriter` allows
writing (`LAZURIO_RUNTIME_DATA_WRITER=true`; unset, never in a runtime that
states `LAZURIO_RUNTIME_DEPLOYMENT=production`, else only to a local or
Environment entry). **Until the Owner decides how the App knows that the person
said "Publikuj", a production application offers no publication**, on `/mcp`
or on the page, and a development version does
(`AGENT_PUBLICATION_IN_PRODUCTION`, the one place that decision changes). A
0.4 declaration of a publication (`effect: "write"` with `publishesData:
true`, or `publicationConsent`) is refused at registration.
Environment entry). The App records who published and which way with the
change itself: the person is `context.access.person.subject` and the way is
`context.channel` (`mcp` or `webmcp`), as the Standard's "Author and channel"
says; the registry's audit line names them too, with the effect.
`AGENT_PUBLICATION_IN_PRODUCTION` is `true` and deprecated. A 0.4 declaration
of a publication (`effect: "write"` with `publishesData: true`, or
`publicationConsent`) is refused at registration.

**Answers and refusals.** A tool returns `{ ok: true, data, files? }` or
`{ ok: false, reason, message, current_revision?, details? }` and never throws
Expand All @@ -240,8 +244,8 @@ for an expected outcome. The common reasons are `identity_unavailable`,
(with `current_revision`), `data_read_only`, `unavailable` and
`internal_error`; a Module adds domain reasons of its own. On `/mcp` a refusal
is a tool result with `isError` and `structuredContent`; a file is an embedded
resource. Every call writes one audit line (channel, tool, entry, subject,
role, outcome, duration), never the input or the answer.
resource. Every call writes one audit line (channel, tool, effect, entry,
subject, role, outcome, duration), never the input or the answer.

**A data layer that decides by the method.** Every tool call is a `POST`.
An App whose data layer chooses a read or a write path by the HTTP method asks
Expand All @@ -252,9 +256,10 @@ readable.

**Tests.** `@lazurio/module-kit/tools/testing` builds requests as the gateway
states them (`gatewayRequest`, `hostedEnvironment`, `localEnvironment`) and
calls the registry (`mcpCall`, `mcpToolNames`, `mcpTool`, `pageTool`), so a
Module tests the list per role, the refusal without a role, writes in the
read-only mode and `record_changed` on both transports.
calls the registry (`mcpCall`, `mcpToolNames`, `mcpTools`, `mcpTool`,
`pageTool`), so a Module tests the list per role, the refusal without a role,
writes in the read-only mode, `record_changed` on both transports, and that
each publication is listed with `destructiveHint: true` (`mcpTools`).

## Bun.serve

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@lazurio/module-kit",
"version": "0.5.0",
"version": "0.6.0",
"description": "Lazurio Module Standard runtime helpers: listener, health, shutdown, app access, tools for agents",
"license": "MIT",
"type": "module",
Expand Down
23 changes: 23 additions & 0 deletions src/tools-testing.ts
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,29 @@ export async function mcpToolNames(
return (result?.tools ?? []).map((tool) => tool.name).sort();
}

/** The tools `tools/list` returns for a caller, with their annotations, so
* a Module checks that every publication carries `destructiveHint`. */
export async function mcpTools(
registry: ToolRegistry,
caller: GatewayCaller,
appOrigin?: string,
): Promise<Array<{ name: string; annotations: Record<string, unknown> }>> {
const { body } = await mcpCall(
registry,
"tools/list",
{},
{ caller, ...(appOrigin === undefined ? {} : { appOrigin }) },
);
const result = body.result as
| {
tools?: Array<{ name: string; annotations?: Record<string, unknown> }>;
}
| undefined;
return (result?.tools ?? [])
.map((tool) => ({ name: tool.name, annotations: tool.annotations ?? {} }))
.sort((a, b) => a.name.localeCompare(b.name));
}

/** Calls a tool through `/mcp` and returns its structured answer. */
export async function mcpTool(
registry: ToolRegistry,
Expand Down
40 changes: 17 additions & 23 deletions src/tools.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,16 +54,18 @@ export type RefusalCode = (typeof REFUSAL_CODES)[number];
* makes it on its own;
* - `publish`: a publication, an irreversible step or one others see (an
* approval, a signature, a sending, a deletion, a movement of money, the
* publication of a draft or of data to everyone). An agent calls it only
* after the person's explicit "Publikuj". It always carries
* `destructiveHint`, and until the Owner decides how the App knows that the
* person said "Publikuj", a production application does not offer it. */
* publication of a draft or of data to everyone). An agent calls it on the
* person's "Publikuj", which is an agreement between the person and the
* agent, not a mechanism the App checks (the Owner, 2026-11-22). It is a
* tool of its own, always carries `destructiveHint`, and is offered in a
* production application as in a development version. */
export type ToolEffect = "read" | "prepare" | "write" | "publish";

/** Whether an agent may publish in a production application. False until the
* Owner decides how the App knows the person's "Publikuj" (LZD-0037, D1);
* the one place that decision changes. */
export const AGENT_PUBLICATION_IN_PRODUCTION = false;
/** Whether an agent may publish in a production application: yes, since the
* Owner decided on 2026-11-22 that "Publikuj" is an agreement between the
* person and the agent, not a mechanism the App checks (LZD-0037, D1).
* @deprecated Always true; kept so that 0.5 imports still compile. */
export const AGENT_PUBLICATION_IN_PRODUCTION = true;

/** The application role a tool needs at least. */
export type ToolRole = "user" | "admin";
Expand Down Expand Up @@ -224,6 +226,9 @@ export interface ToolAuditEvent {
readonly entry: AppAccess["entry"];
readonly subject: string | null;
readonly appRole: AppAccess["appRole"];
/** The tool's effect; null for a tool this App does not have. With
* `subject` and `channel` it tells who published and which way. */
readonly effect: ToolEffect | null;
readonly outcome: "ok" | RefusalCode | (string & {});
readonly durationMs: number;
}
Expand Down Expand Up @@ -524,7 +529,6 @@ export function createToolRegistry(options: ToolRegistryOptions): ToolRegistry {
tool: ToolDefinition<unknown>,
access: KnownAccess,
canWrite: boolean,
where: Deployment,
): ToolRefusal | null => {
if (tool.minRole === "admin" && access.appRole !== "admin")
return refusal(
Expand All @@ -538,15 +542,6 @@ export function createToolRegistry(options: ToolRegistryOptions): ToolRegistry {
"data_read_only",
"This App does not change data now (read-only mode).",
);
if (
tool.effect === "publish" &&
where === "production" &&
!AGENT_PUBLICATION_IN_PRODUCTION
)
return refusal(
"forbidden",
`${tool.name} publishes; a production application offers agents no publication until the Owner decides how it knows the person's "Publikuj" (LZD-0037, D1). A person publishes in the interface.`,
);
return null;
};

Expand All @@ -556,9 +551,8 @@ export function createToolRegistry(options: ToolRegistryOptions): ToolRegistry {
const list = (access: AppAccess): readonly ToolDescriptor[] => {
if (!known(access)) return [];
const canWrite = dataWriter(access, env);
const where = deployment(env);
return [...tools.values()]
.filter((tool) => barrier(tool, access, canWrite, where) === null)
.filter((tool) => barrier(tool, access, canWrite) === null)
.map(descriptor);
};

Expand All @@ -575,6 +569,7 @@ export function createToolRegistry(options: ToolRegistryOptions): ToolRegistry {
event: "agent-tool",
channel,
tool: tools.has(name) ? name : "(unknown)",
effect: tools.get(name)?.effect ?? null,
entry: access.entry,
subject: access.person?.subject ?? null,
appRole: access.appRole,
Expand All @@ -587,8 +582,7 @@ export function createToolRegistry(options: ToolRegistryOptions): ToolRegistry {
if (tool === undefined)
return finish(refusal("unknown_tool", "This App has no such tool."));
const canWrite = dataWriter(access, env);
const where = deployment(env);
const blocked = barrier(tool, access, canWrite, where);
const blocked = barrier(tool, access, canWrite);
if (blocked !== null) return finish(blocked);
let parsed: Awaited<
ReturnType<(typeof tool.input)["~standard"]["validate"]>
Expand Down Expand Up @@ -616,7 +610,7 @@ export function createToolRegistry(options: ToolRegistryOptions): ToolRegistry {
const result = await tool.run(parsed.value, {
access,
channel,
deployment: where,
deployment: deployment(env),
canWrite,
request,
});
Expand Down
69 changes: 56 additions & 13 deletions test/tools.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import {
mcpCall,
mcpTool,
mcpToolNames,
mcpTools,
pageTool,
TEST_ORIGIN,
TEST_SUBJECT,
Expand Down Expand Up @@ -334,6 +335,7 @@ describe("/mcp transport", () => {
"export_record_pdf",
"find_records",
"get_app_context",
"publish_record",
"save_record_draft",
]);
expect(await mcpToolNames(writer, { role: "admin" })).toContain(
Expand Down Expand Up @@ -366,15 +368,28 @@ describe("/mcp transport", () => {
expect(call.structured.reason).toBe("data_read_only");
});

test("a publication is not offered in production until D1, and is in a development version", async () => {
expect(await mcpToolNames(writer, { role: "admin" })).not.toContain(
test("a publication is offered in production and in a development version, always destructive (D1, 2026-11-22)", async () => {
// "Publikuj" is an agreement between the person and the agent: the App
// offers the publication and the agent calls it on the person's word.
expect(await mcpToolNames(writer, { role: "user" })).toContain(
"publish_record",
);
const call = await mcpTool(writer, "publish_record", { id: "r1" });
expect(call.structured.reason).toBe("forbidden");
expect(String(call.structured.message)).toContain("LZD-0037, D1");
expect(call.isError).toBe(false);
expect(call.structured).toEqual({ id: "r1" });
const viaPage = await pageTool(writer, "publish_record", { id: "r1" });
expect([viaPage.status, viaPage.body.reason]).toEqual([403, "forbidden"]);
expect(viaPage.status).toBe(200);
const destructive = {
readOnlyHint: false,
destructiveHint: true,
idempotentHint: false,
openWorldHint: false,
};
const hosted = (await mcpTools(writer, { role: "user" })).find(
(tool) => tool.name === "publish_record",
);
expect(hosted?.annotations).toEqual(destructive);
// A development version in an Environment offers it the same way.
const local = registry(localEnvironment());
const response = await local.handleMcp(
new Request("http://127.0.0.1:43100/mcp", {
Expand All @@ -393,18 +408,44 @@ describe("/mcp transport", () => {
};
}
).result.tools;
const published = listed.find((tool) => tool.name === "publish_record");
expect(published?.annotations).toEqual({
readOnlyHint: false,
destructiveHint: true,
idempotentHint: false,
openWorldHint: false,
});
expect(
listed.find((tool) => tool.name === "publish_record")?.annotations,
).toEqual(destructive);
// The same writer gate as a draft: not in a read-only copy.
expect(await mcpToolNames(reader, { role: "admin" })).not.toContain(
"publish_record",
);
expect(AGENT_PUBLICATION_IN_PRODUCTION).toBe(false);
const refused = await mcpTool(reader, "publish_record", { id: "r1" });
expect(refused.structured.reason).toBe("data_read_only");
expect(AGENT_PUBLICATION_IN_PRODUCTION).toBe(true);
});

test("a publication is audited with who published, which way and its effect", async () => {
const audit: ToolAuditEvent[] = [];
const audited = registry(hostedEnvironment({ writer: true }), audit);
await mcpTool(audited, "publish_record", { id: "r1" });
await pageTool(audited, "publish_record", { id: "r1" });
expect(
audit.map(({ channel, subject, effect, outcome }) => ({
channel,
subject,
effect,
outcome,
})),
).toEqual([
{
channel: "mcp",
subject: TEST_SUBJECT,
effect: "publish",
outcome: "ok",
},
{
channel: "webmcp",
subject: TEST_SUBJECT,
effect: "publish",
outcome: "ok",
},
]);
});

test("a publication declared the 0.4 way is refused at registration", () => {
Expand Down Expand Up @@ -545,6 +586,7 @@ describe("/mcp transport", () => {
event: "agent-tool",
channel: "mcp",
tool: "find_records",
effect: "read",
entry: "app",
subject: TEST_SUBJECT,
appRole: "user",
Expand All @@ -554,6 +596,7 @@ describe("/mcp transport", () => {
event: "agent-tool",
channel: "mcp",
tool: "prepare_record_approval",
effect: "prepare",
entry: "app",
subject: TEST_SUBJECT,
appRole: "user",
Expand Down
Loading