Skip to content

Build your personal knowledge base with Trilium Notes

8.2 KB
Raw Blame
import { becca, cls, options as optionService } from "@triliumnext/core";
import { Application } from "express";
import supertest from "supertest";
import { beforeAll, describe, expect, it } from "vitest";

import config from "../../src/services/config.js";
import etapiTokenService from "../../src/services/etapi_tokens.js";
import { createNote, login } from "./utils.js";

let app: Application;
let token: string;

const USER = "etapi";
const MCP_ACCEPT = "application/json, text/event-stream";

/** Builds a JSON-RPC 2.0 request body for MCP. */
function jsonRpc(method: string, params?: Record<string, unknown>, id: number = 1) {
    return { jsonrpc: "2.0", id, method, params };
}

/** Parses the JSON-RPC response from an SSE response text. */
function parseSseResponse(text: string) {
    const dataLine = text.split("\n").find(line => line.startsWith("data: "));
    if (!dataLine) {
        throw new Error(`No SSE data line found in response: ${text}`);
    }
    return JSON.parse(dataLine.slice("data: ".length));
}

/** An MCP request carrying no credentials at all. */
function mcpPostAnonymous(app: Application) {
    return supertest(app)
        .post("/mcp")
        .set("Host", "localhost")
        .set("Accept", MCP_ACCEPT)
        .set("Content-Type", "application/json");
}

/** An MCP request authenticated with the suite's token — what every real client sends. */
function mcpPost(app: Application) {
    return mcpPostAnonymous(app).set("Authorization", token);
}

function setOption(name: Parameters<typeof optionService.setOption>[0], value: string) {
    cls.init(() => optionService.setOption(name, value));
}

describe("mcp", () => {
    beforeAll(async () => {
        config.General.noAuthentication = false;
        const buildApp = (await (import("../../src/app.js"))).default;
        app = await buildApp();
        token = await login(app);
    });

    describe("option gate", () => {
        it("rejects requests when mcpEnabled is false", async () => {
            setOption("mcpEnabled", "false");

            const response = await mcpPost(app)
                .send(jsonRpc("initialize"))
                .expect(403);

            expect(response.body.error).toContain("disabled");
        });

        it("rejects requests when mcpEnabled option does not exist", async () => {
            const saved = becca.options["mcpEnabled"];
            delete becca.options["mcpEnabled"];

            try {
                const response = await mcpPost(app)
                    .send(jsonRpc("initialize"))
                    .expect(403);

                expect(response.body.error).toContain("disabled");
            } finally {
                becca.options["mcpEnabled"] = saved;
            }
        });

        it("accepts requests when mcpEnabled is true", async () => {
            setOption("mcpEnabled", "true");

            const response = await mcpPost(app)
                .send(jsonRpc("initialize", {
                    protocolVersion: "2025-03-26",
                    capabilities: {},
                    clientInfo: { name: "test", version: "1.0.0" }
                }));

            expect(response.status).toBe(200);
        });
    });

    // Loopback is not a credential: any local process can reach the listener, so a token is
    // required on every request. Without these, the rest of the suite could keep passing
    // while the endpoint silently rejected or silently admitted everyone.
    describe("authentication", () => {
        beforeAll(() => {
            setOption("mcpEnabled", "true");
        });

        it("rejects a request that carries no token", async () => {
            const response = await mcpPostAnonymous(app)
                .send(jsonRpc("initialize"))
                .expect(401);

            expect(response.body.error).toContain("ETAPI token");
        });

        it("rejects a token that was never issued", async () => {
            const response = await mcpPostAnonymous(app)
                .set("Authorization", "Bearer nSFHqNzUUAtCHTIm_not_a_real_token")
                .send(jsonRpc("initialize"))
                .expect(401);

            expect(response.body.error).toContain("ETAPI token");
        });

        it("stops accepting a token once it is revoked", async () => {
            const name = "mcp revocation spec";
            const { authToken } = cls.init(() => etapiTokenService.createToken(name));

            await mcpPostAnonymous(app)
                .set("Authorization", `Bearer ${authToken}`)
                .send(jsonRpc("initialize"))
                .expect(200);

            const issued = becca.getEtapiTokens().find(t => t.name === name);
            expect(issued).toBeDefined();
            cls.init(() => etapiTokenService.deleteToken(issued?.etapiTokenId ?? ""));

            await mcpPostAnonymous(app)
                .set("Authorization", `Bearer ${authToken}`)
                .send(jsonRpc("initialize"))
                .expect(401);
        });
    });

    describe("protocol", () => {
        beforeAll(() => {
            setOption("mcpEnabled", "true");
        });

        it("initializes and returns server capabilities", async () => {
            const response = await mcpPost(app)
                .send(jsonRpc("initialize", {
                    protocolVersion: "2025-03-26",
                    capabilities: {},
                    clientInfo: { name: "test", version: "1.0.0" }
                }))
                .expect(200);

            const body = parseSseResponse(response.text);
            expect(body.result.serverInfo.name).toBe("trilium-notes");
            expect(body.result.capabilities.tools).toBeDefined();
        });

        it("lists available tools", async () => {
            const response = await mcpPost(app)
                .send(jsonRpc("tools/list"))
                .expect(200);

            const body = parseSseResponse(response.text);
            const toolNames: string[] = body.result.tools.map((t: { name: string }) => t.name);
            expect(toolNames).toContain("search_notes");
            expect(toolNames).toContain("get_note");
            expect(toolNames).toContain("get_note_content");
            expect(toolNames).toContain("create_note");
            expect(toolNames).not.toContain("get_current_note");
        });
    });

    describe("tools", () => {
        let noteId: string;

        beforeAll(async () => {
            setOption("mcpEnabled", "true");
            noteId = await createNote(app, token, "MCP test note content");
        });

        it("searches for notes", async () => {
            const response = await mcpPost(app)
                .send(jsonRpc("tools/call", {
                    name: "search_notes",
                    arguments: { query: "MCP test note content" }
                }))
                .expect(200);

            const body = parseSseResponse(response.text);
            expect(body.result).toBeDefined();
            const content = body.result.content;
            expect(content.length).toBeGreaterThan(0);
            expect(content[0].text).toContain(noteId);
        });

        it("gets note metadata by ID", async () => {
            const response = await mcpPost(app)
                .send(jsonRpc("tools/call", {
                    name: "get_note",
                    arguments: { noteId }
                }))
                .expect(200);

            const body = parseSseResponse(response.text);
            expect(body.result).toBeDefined();
            const parsed = JSON.parse(body.result.content[0].text);
            expect(parsed.noteId).toBe(noteId);
            expect(parsed.type).toBeDefined();
            expect(parsed.attributes).toBeDefined();
        });

        it("reads note content by ID", async () => {
            const response = await mcpPost(app)
                .send(jsonRpc("tools/call", {
                    name: "get_note_content",
                    arguments: { noteId }
                }))
                .expect(200);

            const body = parseSseResponse(response.text);
            expect(body.result).toBeDefined();
            const parsed = JSON.parse(body.result.content[0].text);
            expect(parsed.noteId).toBe(noteId);
            expect(parsed.content).toContain("MCP test note content");
        });
    });
});