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 #4886
scout/forge:scout/4796-mcp-sessions-list-the-caller-s-sign-ins → main 2 commits fork
3 files
changed +120
−0
docs/MCP.md +1
| @@ -87,6 +87,7 @@ for a tunnel and a forge that sleeps. | ||
| 87 | 87 | | `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 | |
| 88 | 88 | | `plan` | `{ app, issues }`: open up to 12 linked issues, all or none (see below) | yes | |
| 89 | 89 | | `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 | |
| 90 | | `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 | | |
| 90 | 91 | | `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 | |
| 91 | 92 | |
| 92 | 93 | 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"; | ||
| 29 | 29 | import { voteVariant } from "../actions/variants.ts"; |
| 30 | 30 | import { mayView, mayViewId, sealedScope } from "../core/sealed.ts"; |
| 31 | 31 | import { listAccess } from "../actions/access_log.ts"; |
| 32 | import { accountSignIns, endAccountSession } from "../actions/signins.ts"; | |
| 32 | 33 | import { changeCapacity, listCapacity } from "../actions/capacity.ts"; |
| 33 | 34 | import { changeLimit, listLimits } from "../actions/limits.ts"; |
| 34 | 35 | import { ISSUE_TEAMS_SAY, nameTeams } from "../actions/issue_teams.ts"; |
| @@ -3834,6 +3835,35 @@ const TOOLS: readonly Tool[] = [ | ||
| 3834 | 3835 | return good({ requests: listed.value }); |
| 3835 | 3836 | }, |
| 3836 | 3837 | }, |
| 3838 | { | |
| 3839 | name: "sessions", | |
| 3840 | description: "Where you are signed in: list your sign-ins, or end one by id. Admin token.", | |
| 3841 | inputSchema: { | |
| 3842 | type: "object", | |
| 3843 | properties: { | |
| 3844 | action: { type: "string", enum: ["list", "end"] }, | |
| 3845 | id: { type: "string", maxLength: 64 }, | |
| 3846 | username: { type: "string", maxLength: 64, description: "Admin only." }, | |
| 3847 | }, | |
| 3848 | required: ["action"], | |
| 3849 | }, | |
| 3850 | writes: true, | |
| 3851 | needsAdminScope: true, | |
| 3852 | run(ctx, user, args) { | |
| 3853 | const username = typeof args["username"] === "string" ? args["username"] : user.username; | |
| 3854 | if (args["action"] === "list") { | |
| 3855 | const seen = accountSignIns(ctx.app, user, username); | |
| 3856 | if (!seen.ok) return bad("not_found", "no such account"); | |
| 3857 | return good({ tokens: seen.value.tokens, sessions: seen.value.sessions }); | |
| 3858 | } | |
| 3859 | if (args["action"] === "end") { | |
| 3860 | const id = typeof args["id"] === "string" ? args["id"] : ""; | |
| 3861 | const done = endAccountSession(ctx.app, user, username, id); | |
| 3862 | return done.ok ? good({ ended: true }) : bad("not_found", "no such sign-in"); | |
| 3863 | } | |
| 3864 | return bad("bad_action", "`action` is list or end"); | |
| 3865 | }, | |
| 3866 | }, | |
| 3837 | 3867 | { |
| 3838 | 3868 | name: "capacity", |
| 3839 | 3869 | 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 | }); |
new above
offline