Skip to content

Build your personal knowledge base with Trilium Notes

feat(mcp): tell MCP clients which note tools only read

Elian Doran651911154e98
diff --git a/apps/server/src/assets/doc_notes/en/User Guide/User Guide/AI/MCP.html b/apps/server/src/assets/doc_notes/en/User Guide/User Guide/AI/MCP.html
index 20af2ccd55..29f68f46da 100644
--- a/apps/server/src/assets/doc_notes/en/User Guide/User Guide/AI/MCP.html
+++ b/apps/server/src/assets/doc_notes/en/User Guide/User Guide/AI/MCP.html
@@ -13,6 +13,14 @@
listed below.</p>
<p>The tools exposed to the MCP are the same tools that are supported by
the internal chat (see the <em>Note access</em> section).</p>
+<h3>Approving tool calls</h3>
+<p>Trilium does not ask for approval of the calls an external assistant makes;
+ that is left to the assistant's own permission settings. To help it decide,
+ each tool tells the assistant whether it only reads your notes, only adds
+ to them (creating a note, cloning it or appending to it), or can overwrite
+ or remove something (editing, renaming, moving or deleting). An assistant
+ that supports these hints can then run reads on their own and ask before
+ changes, the way <em>Ask before changes</em> does in Trilium's chat.</p>
<h3>Authentication</h3>
<p>To protect your notes from unauthorized access, the MCP server requires
authentication. Generally MCP servers need to support OAuth for authentication,
diff --git a/apps/server/src/services/mcp/mcp_server.spec.ts b/apps/server/src/services/mcp/mcp_server.spec.ts
index 90d82402a4..9d4960c134 100644
--- a/apps/server/src/services/mcp/mcp_server.spec.ts
+++ b/apps/server/src/services/mcp/mcp_server.spec.ts
@@ -52,7 +52,8 @@ const readRegistry: any = [
["fail_tool", { description: "f", inputSchema: {}, execute: failExecute }]
];
const mutateRegistry: any = [
- ["mutate_tool", { description: "m", inputSchema: {}, mutates: true, execute: mutateExecute }]
+ ["mutate_tool", { description: "m", inputSchema: {}, mutates: true, execute: mutateExecute }],
+ ["add_tool", { description: "a", inputSchema: {}, mutates: true, additive: true, execute: mutateExecute }]
];
const resolveToolRegistriesMock = vi.fn(async () => {});
@@ -79,7 +80,7 @@ describe("createMcpServer", () => {
expect(server).toBeInstanceOf(FakeMcpServer);
expect((server as any).info).toMatchObject({ name: "trilium-notes", version: "9.9.9-test" });
- expect(registered.map((r) => r.name)).toEqual(["read_tool", "fail_tool", "mutate_tool"]);
+ expect(registered.map((r) => r.name)).toEqual(["read_tool", "fail_tool", "mutate_tool", "add_tool"]);
expect(resolveToolRegistriesMock).toHaveBeenCalled();
});
@@ -117,3 +118,14 @@ describe("createMcpServer", () => {
});
});
});
+
+describe("tool annotations", () => {
+ it("tells the client which tools only read, which only add and which overwrite or remove", async () => {
+ await createMcpServer();
+ const annotationsOf = (name: string) => registered.find((r) => r.name === name)?.config.annotations;
+
+ expect(annotationsOf("read_tool")).toEqual({ readOnlyHint: true, openWorldHint: false });
+ expect(annotationsOf("add_tool")).toEqual({ readOnlyHint: false, destructiveHint: false, openWorldHint: false });
+ expect(annotationsOf("mutate_tool")).toEqual({ readOnlyHint: false, destructiveHint: true, openWorldHint: false });
+ });
+});
diff --git a/apps/server/src/services/mcp/mcp_server.ts b/apps/server/src/services/mcp/mcp_server.ts
index 2ab3b60f2d..cf4eca73ab 100644
--- a/apps/server/src/services/mcp/mcp_server.ts
+++ b/apps/server/src/services/mcp/mcp_server.ts
@@ -20,12 +20,17 @@ import sql from "../sql.js";
* Register a tool definition on the MCP server.
*
* Write operations are wrapped in CLS + transaction context so that
- * Becca entity tracking works correctly.
+ * Becca entity tracking works correctly. The annotations tell the client which
+ * tools only read and which overwrite or remove, so it can decide what to ask
+ * the user about.
*/
function registerTool(server: McpServer, name: string, def: ToolDefinition) {
server.registerTool(name, {
description: def.description,
- inputSchema: def.inputSchema
+ inputSchema: def.inputSchema,
+ annotations: def.mutates
+ ? { readOnlyHint: false, destructiveHint: def.additive !== true, openWorldHint: false }
+ : { readOnlyHint: true, openWorldHint: false }
}, (args: any): CallToolResult => {
const result = cls.init(() => {
cls.set("componentId", "mcp");
diff --git a/docs/User Guide/User Guide/AI/MCP.md b/docs/User Guide/User Guide/AI/MCP.md
index ed48981f19..405b6d1fd8 100644
--- a/docs/User Guide/User Guide/AI/MCP.md
+++ b/docs/User Guide/User Guide/AI/MCP.md
@@ -11,6 +11,10 @@ Only the HTTP transport is supported, the `stdio` method is not supported. If th
The tools exposed to the MCP are the same tools that are supported by the internal chat (see the _Note access_ section).
+### Approving tool calls
+
+Trilium does not ask for approval of the calls an external assistant makes; that is left to the assistant's own permission settings. To help it decide, each tool tells the assistant whether it only reads your notes, only adds to them (creating a note, cloning it or appending to it), or can overwrite or remove something (editing, renaming, moving or deleting). An assistant that supports these hints can then run reads on their own and ask before changes, the way _Ask before changes_ does in Trilium's chat.
+
### Authentication
To protect your notes from unauthorized access, the MCP server requires authentication. Generally MCP servers need to support OAuth for authentication, but Trilium uses a simpler mechanism using Bearer tokens.
diff --git a/packages/trilium-core/src/services/llm/tools/hierarchy_tools.ts b/packages/trilium-core/src/services/llm/tools/hierarchy_tools.ts
index a600bff2a6..af314b0bcd 100644
--- a/packages/trilium-core/src/services/llm/tools/hierarchy_tools.ts
+++ b/packages/trilium-core/src/services/llm/tools/hierarchy_tools.ts
@@ -163,6 +163,7 @@ export const hierarchyTools = defineTools({
prefix: z.string().optional().describe("Optional branch prefix (displayed before the note title in the tree)")
}),
mutates: true,
+ additive: true,
execute: ({ noteId, parentNoteId, prefix }) => {
const note = becca.getNote(noteId);
if (!note) {
diff --git a/packages/trilium-core/src/services/llm/tools/note_tools.ts b/packages/trilium-core/src/services/llm/tools/note_tools.ts
index 1dd02f9b04..c6e90ce723 100644
--- a/packages/trilium-core/src/services/llm/tools/note_tools.ts
+++ b/packages/trilium-core/src/services/llm/tools/note_tools.ts
@@ -166,6 +166,7 @@ export const noteTools = defineTools({
content: z.string().describe("The content to append (Markdown for text notes, plain text for code notes)")
}),
mutates: true,
+ additive: true,
execute: ({ noteId, content }) => {
const note = becca.getNote(noteId);
if (!note) {
@@ -299,6 +300,7 @@ export const noteTools = defineTools({
mime: z.string().optional().describe("MIME type, REQUIRED for code notes (e.g. 'application/javascript;env=backend', 'text/jsx'). Ignored for other types.")
}),
mutates: true,
+ additive: true,
execute: ({ parentNoteId, title, content, type, mime }) => {
if (type === "code" && !mime) {
return { error: "mime is required when creating code notes" };
diff --git a/packages/trilium-core/src/services/llm/tools/tool_registry.ts b/packages/trilium-core/src/services/llm/tools/tool_registry.ts
index 4c97093326..32aa6050c5 100644
--- a/packages/trilium-core/src/services/llm/tools/tool_registry.ts
+++ b/packages/trilium-core/src/services/llm/tools/tool_registry.ts
@@ -25,6 +25,8 @@ interface MutatingToolDefinition {
inputSchema: z.ZodType;
/** Marks this tool as modifying data (needs CLS + transaction wrapping). */
mutates: true;
+ /** Marks a tool that only adds to the notes (a note, a clone, text at the end), never overwriting or removing. */
+ additive?: true;
/**
* Execute the tool synchronously. Must NOT be async because better-sqlite3
* transactions are synchronous and would commit before awaits complete.