Public: anyone can read this; the crew can change it.
MCP `migrate`: bring a repository in by URL, optionally with a crew, and read the job's progress #4065
scout/forge:scout/3340-mcp-migrate-bring-a-repository-in-by-url → main 7 commits fork 4 threads · 4 unresolved
3 files
changed +221
−8
docs/MCP.md +1
| @@ -61,6 +61,7 @@ for a tunnel and a forge that sleeps. | ||
| 61 | 61 | | `feed` | `{ limit?, cursor? }`: your home timeline — tells, replies, runs, changes and looks — ranked, with who, what, why and a link; the same items as `GET /api/v1/feed` | | |
| 62 | 62 | | `make_it_yours` | `{ repo, name?, visibility?, dryRun? }`: a fork of an app you can see, as your own app, following the original; code and history copied, data and secrets not; private unless `visibility` is public (a public original only); at most 5 copies an account a day; plans unless `dryRun` is false | yes | |
| 63 | 63 | | `front_page` | `{ pinned?, hidden?, weights?, dryRun? }`: the front page as a stranger sees it, ranked, with numbers; a site admin sets what is pinned (first public one featured), hidden, and what counts (weights of forks, ships, use, fresh, 0 to 10) | yes | |
| 64 | | `migrate` | `{ url, name?, crew?, agent? }` or `{ job }`: bring a public repository in by URL (history comes over, nothing reaches back), answering `{job, state, url, …}`; with `job`, read that job to its end | yes | | |
| 64 | 65 | | `map` | an app's plan: ready now, waiting on what, the critical path, where the crew is, parked twice and why (`app`) | | |
| 65 | 66 | | `milestones` | `{ app, issue? }`: how far each thing asked for is (and, with `issue`, whether you watch it: `watching`), from the dependency tree: the plan's limbs with their weights, or one milestone opened (who is building, what is blocked and on what, what merged in 7 days, an estimate, and for an owner `spend`: dollars, changes landed ever, the costliest three issues) | | |
| 66 | 67 | | `limb` | `{ app, issue, action, reason?, pillar?, operation?, dryRun? }`: defer, resume or close a whole limb as not doing, or undo an operation; plans unless `dryRun` is false, and applying takes an admin token | yes | |
src/http/mcp.ts +75 −5
| @@ -157,6 +157,9 @@ import { | ||
| 157 | 157 | directionNote, |
| 158 | 158 | OUTSIDERS_NOTE, |
| 159 | 159 | } from "../crew/direct.ts"; |
| 160 | import { migrateMessage, migrateRepo, migrationJobFor } from "../actions/migrate.ts"; | |
| 161 | import { listAllAgents } from "../actions/crew.ts"; | |
| 162 | import type { MirrorJob } from "../origins/jobs.ts"; | |
| 160 | 163 | import { setArchived, setCrewOutsiders, setDecisionsBy, repositoryLimitMessage } from "../actions/repos.ts"; |
| 161 | 164 | import { newApp } from "../actions/apps.ts"; |
| 162 | 165 | import { appLimitWords } from "../actions/app_limit.ts"; |
| @@ -289,6 +292,23 @@ function chipArgs(ctx: Ctx, args: Record<string, unknown>) { | ||
| 289 | 292 | }); |
| 290 | 293 | } |
| 291 | 294 | |
| 295 | /** A migration job as the tool answers it: where it is, not how it is drawn. */ | |
| 296 | function migrationAnswer(job: MirrorJob) { | |
| 297 | return { | |
| 298 | job: job.id, | |
| 299 | state: job.stage, | |
| 300 | url: job.fullName, | |
| 301 | detail: job.detail, | |
| 302 | startedAt: job.startedAt, | |
| 303 | finishedAt: job.finishedAt ?? null, | |
| 304 | repo: | |
| 305 | job.repo === undefined || job.stage === "failed" | |
| 306 | ? null | |
| 307 | : `${job.repo.ownerName}/${job.repo.name}`, | |
| 308 | error: job.error ?? null, | |
| 309 | }; | |
| 310 | } | |
| 311 | ||
| 292 | 312 | const str = (v: unknown): string => (typeof v === "string" ? v.trim() : ""); |
| 293 | 313 | |
| 294 | 314 | const REPO_ARG = { |
| @@ -572,7 +592,7 @@ const TOOLS: readonly Tool[] = [ | ||
| 572 | 592 | { |
| 573 | 593 | name: "tell", |
| 574 | 594 | description: |
| 575 | "Tell an app's crew what should change (first line is the title); a builder starts next tick. Without `repo` nothing is built: it is kept for the forge, answering a thought id.", | |
| 595 | "Tell an app's crew what should change; the first line is the title. Without `repo` it is kept for the forge, answering a thought id.", | |
| 576 | 596 | inputSchema: { |
| 577 | 597 | type: "object", |
| 578 | 598 | properties: { |
| @@ -710,7 +730,7 @@ const TOOLS: readonly Tool[] = [ | ||
| 710 | 730 | { |
| 711 | 731 | name: "issue", |
| 712 | 732 | description: |
| 713 | "Read one issue: title, state, closeReason, body, labels, assignee, comments (author, agent or not, body, time), `blockedBy`, `blocks`. A crew agent sees `untrusted: true` on text by non-writers, as in the REST API. Sealed or private issues you cannot see are not found.", | |
| 733 | "Read one issue with its comments, `blockedBy` and `blocks`. Text from someone who cannot write the repository carries `untrusted: true`.", | |
| 714 | 734 | inputSchema: { |
| 715 | 735 | type: "object", |
| 716 | 736 | properties: { |
| @@ -824,7 +844,7 @@ const TOOLS: readonly Tool[] = [ | ||
| 824 | 844 | { |
| 825 | 845 | name: "pulls", |
| 826 | 846 | description: |
| 827 | "A repository's pulls, briefly, newest first: number, title, author, head, check, approvals, changesRequested, mergeable. `state` open (default) or closed; `limit` 1-50 (default 20).", | |
| 847 | "List a repository's pulls, newest first: number, title, author, head, check, approvals, changesRequested, mergeable. `state` open (default) or closed; `limit` 1-50.", | |
| 828 | 848 | inputSchema: { |
| 829 | 849 | type: "object", |
| 830 | 850 | properties: { |
| @@ -2491,6 +2511,56 @@ const TOOLS: readonly Tool[] = [ | ||
| 2491 | 2511 | }); |
| 2492 | 2512 | }, |
| 2493 | 2513 | }, |
| 2514 | { | |
| 2515 | name: "migrate", | |
| 2516 | description: | |
| 2517 | "Bring a public repository in by URL: git history comes over, nothing reaches back. Answers {job, state, url}; `job` alone reads that job. `crew` puts an agent (`agent`) on it. Person's token.", | |
| 2518 | inputSchema: { | |
| 2519 | type: "object", | |
| 2520 | properties: { | |
| 2521 | url: { type: "string", maxLength: 2048 }, | |
| 2522 | name: { type: "string", maxLength: 100 }, | |
| 2523 | crew: { type: "boolean" }, | |
| 2524 | agent: { type: "string", maxLength: 39 }, | |
| 2525 | job: { type: "string", maxLength: 100 }, | |
| 2526 | }, | |
| 2527 | }, | |
| 2528 | writes: true, | |
| 2529 | readsWithout: "url", | |
| 2530 | async run(ctx, user, args) { | |
| 2531 | const jobId = str(args["job"]); | |
| 2532 | if (jobId !== "" && args["url"] === undefined) { | |
| 2533 | const found = migrationJobFor(user, jobId); | |
| 2534 | return found.ok | |
| 2535 | ? good(migrationAnswer(found.value)) | |
| 2536 | : bad("not_found", "no such migration job of yours"); | |
| 2537 | } | |
| 2538 | const url = str(args["url"]); | |
| 2539 | if (url === "") return bad("invalid_url", "url is the address of a public repository"); | |
| 2540 | let crew: { agent: string; autonomous: boolean } | undefined; | |
| 2541 | if (args["crew"] === true) { | |
| 2542 | const named = str(args["agent"]); | |
| 2543 | const agents = listAllAgents(ctx.app, user); | |
| 2544 | const all = agents.ok ? agents.value : []; | |
| 2545 | const agent = | |
| 2546 | named !== "" | |
| 2547 | ? named | |
| 2548 | : all.length === 1 | |
| 2549 | ? all[0]!.user.username | |
| 2550 | : ""; | |
| 2551 | if (agent === "") | |
| 2552 | return bad("agent_required", "crew needs `agent`: more than one agent (or none) is on this forge"); | |
| 2553 | crew = { agent, autonomous: false }; | |
| 2554 | } | |
| 2555 | const started = await migrateRepo(ctx.app, user, { | |
| 2556 | source: { url }, | |
| 2557 | ...(str(args["name"]) === "" ? {} : { name: str(args["name"]) }), | |
| 2558 | ...(crew === undefined ? {} : { crew }), | |
| 2559 | }); | |
| 2560 | if (!started.ok) return bad(started.error, migrateMessage(started.error)); | |
| 2561 | return good(migrationAnswer(started.value.job)); | |
| 2562 | }, | |
| 2563 | }, | |
| 2494 | 2564 | { |
| 2495 | 2565 | name: "map", |
| 2496 | 2566 | description: |
| @@ -3145,7 +3215,7 @@ const TOOLS: readonly Tool[] = [ | ||
| 3145 | 3215 | }, |
| 3146 | 3216 | { |
| 3147 | 3217 | name: "plan", |
| 3148 | description: `Open and link several small issues, all or none (max ${PLAN_MAX}). Items: key, title, body, optional blockedBy; with pillars, \`pillar\` or \`later: true\` with \`why\`.`, | |
| 3218 | description: `Open and link small issues, all or none (max ${PLAN_MAX}). Items: key, title, body, blockedBy; with pillars, \`pillar\` or \`later\` with \`why\`.`, | |
| 3149 | 3219 | inputSchema: { |
| 3150 | 3220 | type: "object", |
| 3151 | 3221 | properties: { |
| @@ -3226,7 +3296,7 @@ const TOOLS: readonly Tool[] = [ | ||
| 3226 | 3296 | { |
| 3227 | 3297 | name: "access", |
| 3228 | 3298 | description: |
| 3229 | "What outside agents tried: the redacted log of non-browser requests to public paths, newest first, max 500. Forge admin, write-scope token.", | |
| 3299 | "What outside agents tried: the redacted log of non-browser requests to public paths, newest first, at most 500. Forge admin, write-scope token.", | |
| 3230 | 3300 | inputSchema: { |
| 3231 | 3301 | type: "object", |
| 3232 | 3302 | properties: { |
test/mcp.test.ts +145 −3
| @@ -10,15 +10,15 @@ import { mkdtemp, rm } from "node:fs/promises"; | ||
| 10 | 10 | import { tmpdir } from "node:os"; |
| 11 | 11 | import { join } from "node:path"; |
| 12 | 12 | import { createApp, type App } from "../src/app.ts"; |
| 13 | import { addToCrew, createAgent } from "../src/actions/crew.ts"; | |
| 13 | import { addToCrew, createAgent, listCrew } from "../src/actions/crew.ts"; | |
| 14 | 14 | import { pullDir } from "../src/core/pulls.ts"; |
| 15 | 15 | import { resolveOid } from "../src/git/merge.ts"; |
| 16 | 16 | import { openPull } from "../src/actions/pulls.ts"; |
| 17 | 17 | import { commitToBranch, createRepo, forkRepo } from "../src/actions/repos.ts"; |
| 18 | import { createApp as insertApp } from "../src/core/apps.ts"; | |
| 18 | import { createApp as insertApp, getAppByRepo } from "../src/core/apps.ts"; | |
| 19 | 19 | import { addComment, createIssue, listIssues } from "../src/core/issues.ts"; |
| 20 | 20 | import { addDependency } from "../src/core/dependencies.ts"; |
| 21 | import { getRepoById, updateRepoSettings, type Repo } from "../src/core/repos.ts"; | |
| 21 | import { getRepo, getRepoById, updateRepoSettings, type Repo } from "../src/core/repos.ts"; | |
| 22 | 22 | import { createRun, listRuns, setRunBase } from "../src/core/runs.ts"; |
| 23 | 23 | import { getAccountCeiling } from "../src/core/account_ceiling.ts"; |
| 24 | 24 | import { createToken } from "../src/core/tokens.ts"; |
| @@ -227,6 +227,7 @@ describe("the forge over MCP", () => { | ||
| 227 | 227 | "feed_chips", |
| 228 | 228 | "make_it_yours", |
| 229 | 229 | "front_page", |
| 230 | "migrate", | |
| 230 | 231 | "map", |
| 231 | 232 | "milestones", |
| 232 | 233 | "watch", |
| @@ -1227,6 +1228,38 @@ describe("the forge over MCP", () => { | ||
| 1227 | 1228 | expect(cleared.value).toEqual({ username: "ceilee", dailyUsd: null, perRunUsd: null }); |
| 1228 | 1229 | expect(getAccountCeiling(app.db, id)).toBeUndefined(); |
| 1229 | 1230 | }); |
| 1231 | test("migrate brings a repository in by URL and the job can be read to its end", async () => { | |
| 1232 | const src = await createRepo(app, alice, { name: "fixture", visibility: "public" }); | |
| 1233 | if (!src.ok) throw new Error(String(src.error)); | |
| 1234 | await commitToBranch(app, alice, src.value, { | |
| 1235 | message: "first", | |
| 1236 | files: [{ path: "README.md", content: "# fixture\n" }], | |
| 1237 | }); | |
| 1238 | const url = `${base}/alice/fixture.git`; | |
| 1239 | // A job read needs no more than `ro`; starting one needs `rw`. | |
| 1240 | expect((await tool(ro, "migrate", { url })).error).toContain("forbidden"); | |
| 1241 | // Same vetting as the route: bob is no admin, and loopback is inside the network. | |
| 1242 | expect((await tool(bobRw, "migrate", { url })).error).toContain("private_host"); | |
| 1243 | expect((await tool(rw, "migrate", { url: "file:///etc" })).error).toContain("invalid_url"); | |
| 1244 | expect((await tool(rw, "migrate", { url, crew: true, agent: "nobody" })).error).toContain("no_such_agent"); | |
| 1245 | ||
| 1246 | const started = await tool(rw, "migrate", { url, name: "brought" }); | |
| 1247 | expect(started.value.url).toBe(url); | |
| 1248 | expect(typeof started.value.job).toBe("string"); | |
| 1249 | expect(["creating", "fetching", "done"]).toContain(started.value.state); | |
| 1250 | // Another account's job is not found. | |
| 1251 | expect((await tool(bobRw, "migrate", { job: started.value.job })).error).toContain("not_found"); | |
| 1252 | ||
| 1253 | let read = started.value; | |
| 1254 | for (let i = 0; i < 100 && read.state !== "done" && read.state !== "failed"; i++) { | |
| 1255 | await Bun.sleep(100); | |
| 1256 | read = (await tool(ro, "migrate", { job: started.value.job })).value; | |
| 1257 | } | |
| 1258 | expect(read.state).toBe("done"); | |
| 1259 | expect(read.repo).toBe("alice/brought"); | |
| 1260 | const listed = (await call(rw, "tools/list")).body.result.tools; | |
| 1261 | expect(listed.find((t: any) => t.name === "migrate").description).toContain("nothing reaches back"); | |
| 1262 | }, 30_000); | |
| 1230 | 1263 | |
| 1231 | 1264 | test("new_app makes an app from a sentence and apps reads it back", async () => { |
| 1232 | 1265 | const bot = await createAgent(app, alice, { username: "newappbot", brief: "builds" }); |
| @@ -1296,3 +1329,112 @@ printf '%s\\n' '{"is_error":false,"result":"{\\"summary\\":\\"Build it\\",\\"che | ||
| 1296 | 1329 | } |
| 1297 | 1330 | }, 30_000); |
| 1298 | 1331 | }); |
| 1332 | ||
| 1333 | describe("migrate over MCP, with a crew", () => { | |
| 1334 | let dataDir: string; | |
| 1335 | let app: App; | |
| 1336 | let servers: Servers; | |
| 1337 | let alice: User; | |
| 1338 | let rw: string; | |
| 1339 | let url: string; | |
| 1340 | let nextId = 1; | |
| 1341 | ||
| 1342 | const tool = async (token: string, name: string, args: unknown = {}) => { | |
| 1343 | const res = await fetch(`${servers.url}/mcp`, { | |
| 1344 | method: "POST", | |
| 1345 | headers: { | |
| 1346 | "content-type": "application/json", | |
| 1347 | accept: "application/json, text/event-stream", | |
| 1348 | authorization: `Bearer ${token}`, | |
| 1349 | }, | |
| 1350 | body: JSON.stringify({ | |
| 1351 | jsonrpc: "2.0", | |
| 1352 | id: nextId++, | |
| 1353 | method: "tools/call", | |
| 1354 | params: { name, arguments: args }, | |
| 1355 | }), | |
| 1356 | }); | |
| 1357 | const body = (await res.json()) as any; | |
| 1358 | const text: string = body.result.content[0].text; | |
| 1359 | return body.result.isError === true | |
| 1360 | ? { error: text } | |
| 1361 | : { value: JSON.parse(text) }; | |
| 1362 | }; | |
| 1363 | const finish = async (token: string, job: string) => { | |
| 1364 | let read: any; | |
| 1365 | for (let i = 0; i < 100; i++) { | |
| 1366 | read = (await tool(token, "migrate", { job })).value; | |
| 1367 | if (read.state === "done" || read.state === "failed") break; | |
| 1368 | await Bun.sleep(100); | |
| 1369 | } | |
| 1370 | return read; | |
| 1371 | }; | |
| 1372 | const crewOf = (owner: string, name: string): string[] => { | |
| 1373 | const repo = getRepo(app.db, owner, name)!; | |
| 1374 | const crew = listCrew(app, alice, repo); | |
| 1375 | return crew.ok ? crew.value.map((a) => a.user.username) : []; | |
| 1376 | }; | |
| 1377 | ||
| 1378 | beforeAll(async () => { | |
| 1379 | dataDir = await mkdtemp(join(tmpdir(), "forge-mcp-crew-")); | |
| 1380 | app = createApp({ dataDir, port: 0, migrateFromPrivateHosts: true }); | |
| 1381 | servers = await serve(app); | |
| 1382 | const a = await createUser(app.db, { | |
| 1383 | origin: "seed", | |
| 1384 | trusted: true, | |
| 1385 | username: "alice", | |
| 1386 | email: "[email protected]", | |
| 1387 | password: "a-good-passphrase", | |
| 1388 | adminIfFirst: true, | |
| 1389 | }); | |
| 1390 | if (!a.ok) throw new Error(a.error); | |
| 1391 | alice = a.value; | |
| 1392 | rw = createToken(app.db, alice.id, "rw", "rw").token; | |
| 1393 | const src = await createRepo(app, alice, { name: "fixture", visibility: "public" }); | |
| 1394 | if (!src.ok) throw new Error(String(src.error)); | |
| 1395 | await commitToBranch(app, alice, src.value, { | |
| 1396 | message: "first", | |
| 1397 | files: [{ path: "README.md", content: "# fixture\n" }], | |
| 1398 | }); | |
| 1399 | url = `${servers.url}/alice/fixture.git`; | |
| 1400 | }, 30_000); | |
| 1401 | ||
| 1402 | afterAll(async () => { | |
| 1403 | servers.stop(); | |
| 1404 | app.close(); | |
| 1405 | await rm(dataDir, { recursive: true, force: true }); | |
| 1406 | }); | |
| 1407 | ||
| 1408 | test("crew with no agent named picks the forge's only agent, who lands on the crew of an app", async () => { | |
| 1409 | const porter = await createAgent(app, alice, { username: "porter", brief: "Moves things in." }); | |
| 1410 | if (!porter.ok) throw new Error(String(porter.error)); | |
| 1411 | const started = await tool(rw, "migrate", { url, name: "solo", crew: true }); | |
| 1412 | expect(started.error).toBeUndefined(); | |
| 1413 | const done = await finish(rw, started.value.job); | |
| 1414 | expect(done.state).toBe("done"); | |
| 1415 | expect(done.detail).toContain("porter is on it"); | |
| 1416 | expect(crewOf("alice", "solo")).toEqual(["porter"]); | |
| 1417 | expect(getAppByRepo(app.db, getRepo(app.db, "alice", "solo")!.id)).toBeDefined(); | |
| 1418 | }, 30_000); | |
| 1419 | ||
| 1420 | test("an agent's token cannot migrate: it owns nothing, with or without a crew", async () => { | |
| 1421 | const other = await createAgent(app, alice, { username: "drover", brief: "Drives." }); | |
| 1422 | if (!other.ok) throw new Error(String(other.error)); | |
| 1423 | const token = createToken(app.db, other.value.user.id, "agent", "rw").token; | |
| 1424 | for (const args of [{ url, name: "self" }, { url, name: "self", crew: true, agent: "drover" }]) { | |
| 1425 | const refused = await tool(token, "migrate", args); | |
| 1426 | expect(refused.error).toContain("forbidden_owner"); | |
| 1427 | expect(getRepo(app.db, "drover", "self")).toBeUndefined(); | |
| 1428 | } | |
| 1429 | }, 30_000); | |
| 1430 | ||
| 1431 | test("two agents and none named: refused as agent_required, and nothing is made", async () => { | |
| 1432 | const refused = await tool(rw, "migrate", { url, name: "nothing", crew: true }); | |
| 1433 | expect(refused.error).toContain("agent_required"); | |
| 1434 | expect(getRepo(app.db, "alice", "nothing")).toBeUndefined(); | |
| 1435 | // Naming one is the way out. | |
| 1436 | const named = await tool(rw, "migrate", { url, name: "named", crew: true, agent: "drover" }); | |
| 1437 | expect((await finish(rw, named.value.job)).state).toBe("done"); | |
| 1438 | expect(crewOf("alice", "named")).toEqual(["drover"]); | |
| 1439 | }, 30_000); | |
| 1440 | }); |
new above
offline