Public: anyone can read this; the crew can change it.
MCP explore answers how many results a filter leaves, in the page's own sentence #11752
Check · Review · Land · Live done — Landed and live in production.
scout/forge:scout/11496-mcp-explore-answers-how-many-results-a-f → main 2 commits fork
2 files
changed +80
−2
src/http/mcp/tools/explore.ts +5 −2
| @@ -1,10 +1,10 @@ | ||
| 1 | import { explorePage, exploreLine, exploreLineFrom, EXPLORE_PARAMS, exploreNote, exploreSuggestion, readExploreLine, rowsWithWhy } from "../../../actions/explore.ts"; | |
| 1 | import { explorePage, exploreLine, exploreCount, exploreFiltered, exploreLineFrom, EXPLORE_PARAMS, exploreNote, exploreSuggestion, readExploreLine, rowsWithWhy } from "../../../actions/explore.ts"; | |
| 2 | 2 | import { forkTreeRows } from "../../../actions/fork_tree_rows.ts"; |
| 3 | 3 | import { bad, good, type Tool, str, repoArg, isAnswer } from "../shared.ts"; |
| 4 | 4 | |
| 5 | 5 | export const tool: Tool = { |
| 6 | 6 | name: "explore", |
| 7 | doc: "`{ kind: tree, app }`: `{ app, upstream, forks, truncated }`, rows `{ repo, follows, standing, updatedAt }` for the app, the chain above it (nearest first) and its direct forks, in the words of the forks page, only what you may read, drawn on the `ui://forge/explore` card as upstream, this and fork rows; else `{ q?, cursor? }` (`query` is the same, `q` wins): the apps and people you can see, ranked, with their numbers and `why` (a short line on the ranking, absent when none); `kind:` `sort:` `updated:` `owner:` `archived:` and words in one line; each also as its own argument (`since` = `updated`); page on with `cursor`", | |
| 7 | doc: "`{ kind: tree, app }`: `{ app, upstream, forks, truncated }`, rows `{ repo, follows, standing, updatedAt }` for the app, the chain above it (nearest first) and its direct forks, in the words of the forks page, only what you may read, drawn on the `ui://forge/explore` card as upstream, this and fork rows; else `{ q?, cursor? }` (`query` is the same, `q` wins): the apps and people you can see, ranked, with `total` and `line` (how many the filter leaves, in the page's words) and, when filtered, `clear` (the call that drops it), with their numbers and `why` (a short line on the ranking, absent when none); `kind:` `sort:` `updated:` `owner:` `archived:` and words in one line; each also as its own argument (`since` = `updated`); page on with `cursor`", | |
| 8 | 8 | description: |
| 9 | 9 | "The apps, people (and, for an admin, forges) you can see, ranked.", |
| 10 | 10 | inputSchema: { |
| @@ -44,6 +44,9 @@ export const tool: Tool = { | ||
| 44 | 44 | if (page === null) return bad("invalid_cursor", "cursor is not the next_cursor of a page before"); |
| 45 | 45 | return good({ |
| 46 | 46 | query: exploreLine(query), |
| 47 | total: page.total, | |
| 48 | line: exploreCount(query, page.total), | |
| 49 | ...(exploreFiltered(query) ? { clear: { tool: "explore", arguments: {} } } : {}), | |
| 47 | 50 | items: rowsWithWhy(ctx.app, query, page.items), |
| 48 | 51 | next_cursor: page.nextCursor, |
| 49 | 52 | }); |
test/mcp-explore-count.test.ts added +75
| @@ -0,0 +1,75 @@ | ||
| 1 | /** MCP explore says how many a filter leaves in the page's sentence, and the call that clears it. */ | |
| 2 | import { afterAll, beforeAll, 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 { exploreCount, readExploreLine } from "../src/actions/explore.ts"; | |
| 8 | import { createRepo } from "../src/actions/repos.ts"; | |
| 9 | import { createApp as insertApp } from "../src/core/apps.ts"; | |
| 10 | import { createToken } from "../src/core/tokens.ts"; | |
| 11 | import { createUser, getUserByName } from "../src/core/users.ts"; | |
| 12 | import { serve, type Servers } from "../src/http/server.ts"; | |
| 13 | import { promote } from "./helpers/members.ts"; | |
| 14 | import { quietLogs } from "./helpers/globals.ts"; | |
| 15 | ||
| 16 | quietLogs(); | |
| 17 | let dir: string; | |
| 18 | let app: App; | |
| 19 | let servers: Servers; | |
| 20 | let token: string; | |
| 21 | ||
| 22 | const explore = async (args: unknown) => { | |
| 23 | const res = await fetch(`${servers.url}/mcp`, { | |
| 24 | method: "POST", | |
| 25 | headers: { authorization: `Bearer ${token}`, "content-type": "application/json", accept: "application/json, text/event-stream" }, | |
| 26 | body: JSON.stringify({ jsonrpc: "2.0", id: 1, method: "tools/call", params: { name: "explore", arguments: args } }), | |
| 27 | }); | |
| 28 | const r = (await res.json()) as any; | |
| 29 | return JSON.parse(r.result.content[0].text) as { total: number; line: string; clear?: unknown }; | |
| 30 | }; | |
| 31 | ||
| 32 | beforeAll(async () => { | |
| 33 | dir = await mkdtemp(join(tmpdir(), "forge-mcp-explore-count-")); | |
| 34 | app = createApp({ dataDir: dir, port: 0 }); | |
| 35 | servers = await serve(app); | |
| 36 | const founder = await createUser(app.db, { origin: "seed", trusted: true, username: "founder", email: "[email protected]", password: "a-long-words-passphrase", adminIfFirst: true }); | |
| 37 | if (!founder.ok) throw new Error("founder"); | |
| 38 | const made = await createUser(app.db, { origin: "seed", trusted: true, username: "alice", email: "[email protected]", password: "a-long-words-passphrase" }); | |
| 39 | if (!made.ok) throw new Error("user"); | |
| 40 | promote(app.db, made.value); | |
| 41 | const alice = getUserByName(app.db, "alice")!; | |
| 42 | const repo = await createRepo(app, alice, { name: "garden", visibility: "public" }); | |
| 43 | if (!repo.ok) throw new Error("repo"); | |
| 44 | const rec = insertApp(app.db, { repoId: repo.value.id, slug: "alice-garden", access: "public" }); | |
| 45 | if (!rec.ok) throw new Error("app"); | |
| 46 | token = createToken(app.db, alice.id, "t", "rw").token; | |
| 47 | }, 30_000); | |
| 48 | ||
| 49 | afterAll(async () => { | |
| 50 | servers.stop(); | |
| 51 | app.close(); | |
| 52 | await rm(dir, { recursive: true, force: true }); | |
| 53 | }); | |
| 54 | ||
| 55 | test("a filter with results: total, the page's sentence, and clear", async () => { | |
| 56 | const r = await explore({ q: "kind:app" }); | |
| 57 | expect(r.total).toBe(1); | |
| 58 | expect(r.line).toBe(exploreCount(readExploreLine("kind:app").query, 1)); | |
| 59 | expect(r.line).toBe("1 app"); | |
| 60 | expect(r.clear).toEqual({ tool: "explore", arguments: {} }); | |
| 61 | }); | |
| 62 | ||
| 63 | test("a filter with none: the miss sentence and clear", async () => { | |
| 64 | const r = await explore({ q: "zzzznothing" }); | |
| 65 | expect(r.total).toBe(0); | |
| 66 | expect(r.line).toBe(exploreCount(readExploreLine("zzzznothing").query, 0)); | |
| 67 | expect(r.clear).toBeDefined(); | |
| 68 | }); | |
| 69 | ||
| 70 | test("no filter: a count and no clear", async () => { | |
| 71 | const r = await explore({}); | |
| 72 | expect(r.total).toBeGreaterThan(1); | |
| 73 | expect(r.line).toBe(`${r.total} results`); | |
| 74 | expect(r.clear).toBeUndefined(); | |
| 75 | }); |
new above
offline