Public: anyone can read this; the crew can change it.

MCP `sessions`: list the caller's sign-ins and end one, so a lost device is signed out from anywhere #5135

Parked opened this
Closed without merging

scout/forge:scout/4796-mcp-sessions-list-the-caller-s-sign-ins → main 3 commits fork

  • Closed without merging. Nothing was applied to main.

Review

Sign in to review this change.
3 files changed +120 −0
docs/MCP.md +1
@@ -88,6 +88,7 @@ for a tunnel and a forge that sleeps.
8888 | `email_verify` | no input: send the caller a new verification email, with the API's rate limit and errors; the answer never carries the link | yes |
8989 | `plan` | `{ app, issues }`: open up to 12 linked issues, all or none (see below) | yes |
9090 | `prompts` | `{ repo, action, kind?, token?, at?, id? }`: list, add, run or remove an app's prompt sources (Instagram), as the `…/prompts` API does; the token goes in once and is never shown; listed only for an admin-scope token, and only the repository's admin may use it | yes |
91 | `sessions` | `{ action, id?, username? }`: `list` where you are signed in (browsers: id, created, last seen, expires; tokens by name, never a secret) or `end` one browser by `id`; only your own, an owner may name another person; an admin-scope token | yes |
9192 | `account_suspend` | `{ username, suspended }`: suspend a person's account, or unsuspend it with `false`; their sessions and tokens stop at once; the API's rules (not yourself, another admin or an agent; not the only owner of an organization); an admin token | yes |
9293
9394 Answers are JSON in a text block; most carry a `says` sentence short enough
src/http/mcp.ts +30
@@ -29,6 +29,7 @@ import { NOT_A_MEMBER } from "../core/roles.ts";
2929 import { voteVariant } from "../actions/variants.ts";
3030 import { mayView, mayViewId, sealedScope } from "../core/sealed.ts";
3131 import { listAccess } from "../actions/access_log.ts";
32 import { accountSignIns, endAccountSession } from "../actions/signins.ts";
3233 import { changeCapacity, listCapacity } from "../actions/capacity.ts";
3334 import { changeLimit, listLimits } from "../actions/limits.ts";
3435 import { ISSUE_TEAMS_SAY, nameTeams } from "../actions/issue_teams.ts";
@@ -3870,6 +3871,35 @@ const TOOLS: readonly Tool[] = [
38703871 return good({ requests: listed.value });
38713872 },
38723873 },
3874 {
3875 name: "sessions",
3876 description: "Where you are signed in: list your sign-ins, or end one by id. Admin token.",
3877 inputSchema: {
3878 type: "object",
3879 properties: {
3880 action: { type: "string", enum: ["list", "end"] },
3881 id: { type: "string", maxLength: 64 },
3882 username: { type: "string", maxLength: 64, description: "Admin only." },
3883 },
3884 required: ["action"],
3885 },
3886 writes: true,
3887 needsAdminScope: true,
3888 run(ctx, user, args) {
3889 const username = typeof args["username"] === "string" ? args["username"] : user.username;
3890 if (args["action"] === "list") {
3891 const seen = accountSignIns(ctx.app, user, username);
3892 if (!seen.ok) return bad("not_found", "no such account");
3893 return good({ tokens: seen.value.tokens, sessions: seen.value.sessions });
3894 }
3895 if (args["action"] === "end") {
3896 const id = typeof args["id"] === "string" ? args["id"] : "";
3897 const done = endAccountSession(ctx.app, user, username, id);
3898 return done.ok ? good({ ended: true }) : bad("not_found", "no such sign-in");
3899 }
3900 return bad("bad_action", "`action` is list or end");
3901 },
3902 },
38733903 {
38743904 name: "capacity",
38753905 description:
test/mcp-sessions.test.ts added +89
@@ -0,0 +1,89 @@
1 /** The `sessions` tool is the sign-ins API over MCP: the caller's own, ended for good. */
2 import { afterAll, beforeAll, describe, expect, test } from "bun:test";
3 import { mkdtemp, rm } from "node:fs/promises";
4 import { tmpdir } from "node:os";
5 import { join } from "node:path";
6 import { createApp, type App } from "../src/app.ts";
7 import { createSession, sessionOf } from "../src/core/sessions.ts";
8 import { createToken } from "../src/core/tokens.ts";
9 import { createUser } from "../src/core/users.ts";
10 import { serve, type Servers } from "../src/http/server.ts";
11 import { quietLogs } from "./helpers/globals.ts";
12
13 quietLogs();
14
15 describe("MCP sessions", () => {
16 let dataDir: string;
17 let app: App;
18 let servers: Servers;
19 let alice = "";
20 let bob = "";
21 let aliceId = "";
22 let id = 1;
23
24 const tool = async (token: string, args: unknown) => {
25 const res = await fetch(`${servers.url}/mcp`, {
26 method: "POST",
27 headers: {
28 "content-type": "application/json",
29 accept: "application/json, text/event-stream",
30 authorization: `Bearer ${token}`,
31 },
32 body: JSON.stringify({ jsonrpc: "2.0", id: id++, method: "tools/call", params: { name: "sessions", arguments: args } }),
33 });
34 const body = (await res.json()) as any;
35 const text: string = body.result.content[0].text;
36 return { text, error: body.result.isError === true, value: body.result.isError ? undefined : JSON.parse(text) };
37 };
38
39 beforeAll(async () => {
40 dataDir = await mkdtemp(join(tmpdir(), "forge-mcp-sessions-"));
41 app = createApp({ dataDir, port: 0 });
42 servers = await serve(app);
43 const a = await createUser(app.db, { origin: "seed", trusted: true,
44 username: "alice", email: "[email protected]", password: "a-good-passphrase", adminIfFirst: true });
45 const b = await createUser(app.db, { origin: "seed", trusted: true,
46 username: "bob", email: "[email protected]", password: "a-good-passphrase" });
47 if (!a.ok || !b.ok) throw new Error("users");
48 aliceId = a.value.id;
49 alice = createToken(app.db, a.value.id, "admin", "admin").token;
50 bob = createToken(app.db, b.value.id, "admin", "admin").token;
51 });
52
53 afterAll(async () => {
54 servers.stop();
55 app.close();
56 await rm(dataDir, { recursive: true, force: true });
57 });
58
59 test("a person lists two sessions, ends one, and its cookie is signed out", async () => {
60 const laptop = createSession(app.db, aliceId);
61 const phone = createSession(app.db, aliceId);
62 const listed = await tool(bob, { action: "list" });
63 expect(listed.value.sessions).toHaveLength(0);
64 const mine = await tool(alice, { action: "list" });
65 expect(mine.value.sessions).toHaveLength(2);
66 expect(mine.text).not.toContain(laptop.token);
67 expect(mine.text).not.toContain(phone.token);
68 const lost = sessionOf(app.db, laptop.token);
69 if (!lost.ok) throw new Error("no session");
70 const ended = await tool(alice, { action: "end", id: lost.value.id });
71 expect(ended.value).toEqual({ ended: true });
72 expect(sessionOf(app.db, laptop.token).ok).toBe(false);
73 expect(sessionOf(app.db, phone.token).ok).toBe(true);
74 expect((await tool(alice, { action: "list" })).value.sessions).toHaveLength(1);
75 });
76
77 test("a person cannot end another's session, and an unknown id is refused", async () => {
78 const other = createSession(app.db, aliceId);
79 const theirs = sessionOf(app.db, other.token);
80 if (!theirs.ok) throw new Error("no session");
81 const stolen = await tool(bob, { action: "end", id: theirs.value.id });
82 expect(stolen.error).toBe(true);
83 const named = await tool(bob, { action: "end", id: theirs.value.id, username: "alice" });
84 expect(named.error).toBe(true);
85 expect(sessionOf(app.db, other.token).ok).toBe(true);
86 const unknown = await tool(alice, { action: "end", id: "nope" });
87 expect(unknown.error).toBe(true);
88 });
89 });

↑↓ navigate ↵ open ⌘↵ its app esc close

Keyboard shortcuts

Docs