Public: anyone can read this; the crew can change it.
MCP `sessions` lists the caller's sign-ins #5159
scout/forge:scout/5147-mcp-sessions-lists-the-caller-s-sign-ins → main 1 commit fork
4 files
changed +101
−0
docs/MCP.md +1
| @@ -88,6 +88,7 @@ for a tunnel and a forge that sleeps. | ||
| 88 | 88 | | `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 | |
| 89 | 89 | | `plan` | `{ app, issues }`: open up to 12 linked issues, all or none (see below) | yes | |
| 90 | 90 | | `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` | `{ username? }`: where you (or, as the forge's owner, a person) are signed in: tokens and browsers, as `GET /api/v1/users/:username/signins` lists them, never a secret; an admin-scope token, the route's refusals | | | |
| 91 | 92 | | `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 | |
| 92 | 93 | |
| 93 | 94 | ### Room for a new tool |
src/http/mcp.ts +23
| @@ -256,6 +256,7 @@ import { listPinned as listPinnedPanels, movePinned, pinPanel, unpinPanel } from | ||
| 256 | 256 | import { deleteBoard, listAllBoards, showBoard, writeBoard, type BoardError } from "../actions/boards.ts"; |
| 257 | 257 | import { panelData, parseWindow } from "../boards/panel_data.ts"; |
| 258 | 258 | import { fileReport } from "../actions/reports.ts"; |
| 259 | import { accountSignIns } from "../actions/signins.ts"; | |
| 259 | 260 | import { BODY_MAX, REQUEST_MAX, TITLE_MAX } from "../core/reports.ts"; |
| 260 | 261 | import { placementWords } from "../checks/placement.ts"; |
| 261 | 262 | import { everydayOf } from "../core/everyday.ts"; |
| @@ -3870,6 +3871,28 @@ const TOOLS: readonly Tool[] = [ | ||
| 3870 | 3871 | return good({ requests: listed.value }); |
| 3871 | 3872 | }, |
| 3872 | 3873 | }, |
| 3874 | { | |
| 3875 | name: "sessions", | |
| 3876 | description: "Where you are signed in: your tokens and browsers, never a secret; `username` for a person as owner.", | |
| 3877 | inputSchema: { | |
| 3878 | type: "object", | |
| 3879 | properties: { username: { type: "string", maxLength: 64 } }, | |
| 3880 | }, | |
| 3881 | writes: false, | |
| 3882 | run(ctx, user, args) { | |
| 3883 | if (!scopeAllows(ctx.auth.scope ?? "ro", "admin")) | |
| 3884 | return bad("forbidden", "listing sign-ins needs an admin token"); | |
| 3885 | const name = typeof args["username"] === "string" && args["username"] !== "" ? args["username"].slice(0, 64) : user.username; | |
| 3886 | const seen = accountSignIns(ctx.app, user, name); | |
| 3887 | if (!seen.ok) return bad("not_found", "no such account"); | |
| 3888 | const { tokens, sessions } = seen.value; | |
| 3889 | return good({ | |
| 3890 | tokens, | |
| 3891 | sessions, | |
| 3892 | says: `${seen.value.user.username} is signed in with ${sessions.length} browser${sessions.length === 1 ? "" : "s"} and ${tokens.length} token${tokens.length === 1 ? "" : "s"}.`, | |
| 3893 | }); | |
| 3894 | }, | |
| 3895 | }, | |
| 3873 | 3896 | { |
| 3874 | 3897 | name: "capacity", |
| 3875 | 3898 | description: |
test/mcp-sessions.test.ts added +76
| @@ -0,0 +1,76 @@ | ||
| 1 | /** MCP `sessions`: the sign-ins API, from a harness. */ | |
| 2 | ||
| 3 | import { afterAll, beforeAll, describe, expect, test } from "bun:test"; | |
| 4 | import { mkdtemp, rm } from "node:fs/promises"; | |
| 5 | import { tmpdir } from "node:os"; | |
| 6 | import { join } from "node:path"; | |
| 7 | import { createApp, type App } from "../src/app.ts"; | |
| 8 | import { createSession } from "../src/core/sessions.ts"; | |
| 9 | import { createToken } from "../src/core/tokens.ts"; | |
| 10 | import { createUser, getUserByName } from "../src/core/users.ts"; | |
| 11 | import { serve, type Servers } from "../src/http/server.ts"; | |
| 12 | import { quietLogs } from "./helpers/globals.ts"; | |
| 13 | ||
| 14 | quietLogs(); | |
| 15 | ||
| 16 | describe("MCP sessions", () => { | |
| 17 | let dataDir = ""; | |
| 18 | let app: App; | |
| 19 | let servers: Servers; | |
| 20 | const id = (name: string) => getUserByName(app.db, name)!.id; | |
| 21 | const mcp = async (token: string, args: unknown) => { | |
| 22 | const res = await fetch(`${servers.url}/mcp`, { | |
| 23 | method: "POST", | |
| 24 | headers: { "content-type": "application/json", accept: "application/json, text/event-stream", authorization: `Bearer ${token}` }, | |
| 25 | body: JSON.stringify({ jsonrpc: "2.0", id: 1, method: "tools/call", params: { name: "sessions", arguments: args } }), | |
| 26 | }); | |
| 27 | const body = (await res.json()) as any; | |
| 28 | return { error: body.result.isError === true, text: body.result.content[0].text as string }; | |
| 29 | }; | |
| 30 | ||
| 31 | beforeAll(async () => { | |
| 32 | dataDir = await mkdtemp(join(tmpdir(), "forge-mcp-sessions-")); | |
| 33 | app = createApp({ dataDir, port: 0 }); | |
| 34 | servers = await serve(app); | |
| 35 | for (const [username, isAdmin] of [["alice", true], ["bob", false], ["carol", false]] as const) { | |
| 36 | const made = await createUser(app.db, { origin: "seed", trusted: true, username, email: `${username}@x.test`, password: "a-long-sessions-passphrase", isAdmin }); | |
| 37 | if (!made.ok) throw new Error(username); | |
| 38 | } | |
| 39 | }, 30_000); | |
| 40 | ||
| 41 | afterAll(async () => { | |
| 42 | servers.stop(); | |
| 43 | app.close(); | |
| 44 | await rm(dataDir, { recursive: true, force: true }); | |
| 45 | }); | |
| 46 | ||
| 47 | test("a person with two sign-ins sees both, and no secret", async () => { | |
| 48 | const a = createSession(app.db, id("bob")).token; | |
| 49 | const b = createSession(app.db, id("bob")).token; | |
| 50 | const tok = createToken(app.db, id("bob"), "t", "admin").token; | |
| 51 | const got = await mcp(tok, {}); | |
| 52 | expect(got.error).toBe(false); | |
| 53 | const out = JSON.parse(got.text); | |
| 54 | expect(out.sessions).toHaveLength(2); | |
| 55 | expect(out.says).toContain("bob"); | |
| 56 | for (const secret of [a, b, tok]) expect(got.text).not.toContain(secret); | |
| 57 | }); | |
| 58 | ||
| 59 | test("a stranger naming another person is refused as the route refuses", async () => { | |
| 60 | const tok = createToken(app.db, id("carol"), "t", "admin").token; | |
| 61 | const got = await mcp(tok, { username: "bob" }); | |
| 62 | expect(got.error).toBe(true); | |
| 63 | expect(got.text).toContain("not_found"); | |
| 64 | const route = await fetch(`${servers.url}/api/v1/users/bob/signins`, { headers: { authorization: `Bearer ${tok}` } }); | |
| 65 | expect(route.status).toBe(404); | |
| 66 | }); | |
| 67 | ||
| 68 | test("the owner sees a person's; a token below admin scope is refused", async () => { | |
| 69 | const owner = createToken(app.db, id("alice"), "t", "admin").token; | |
| 70 | expect(JSON.parse((await mcp(owner, { username: "bob" })).text).sessions).toHaveLength(2); | |
| 71 | const rw = createToken(app.db, id("bob"), "t2", "rw").token; | |
| 72 | const got = await mcp(rw, {}); | |
| 73 | expect(got.error).toBe(true); | |
| 74 | expect(got.text).toContain("forbidden"); | |
| 75 | }); | |
| 76 | }); |
test/mcp.test.ts +1
| @@ -255,6 +255,7 @@ describe("the forge over MCP", () => { | ||
| 255 | 255 | "hold", |
| 256 | 256 | "plan", |
| 257 | 257 | "access", |
| 258 | "sessions", | |
| 258 | 259 | "capacity", |
| 259 | 260 | "scorecard", |
| 260 | 261 | "flow", |
new above
offline