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 #3350
scout/forge:scout/3340-mcp-migrate-bring-a-repository-in-by-url → main 5 commits fork 7 threads · 7 unresolved
3 files
changed +216
−3
docs/MCP.md +1
| @@ -60,6 +60,7 @@ for a tunnel and a forge that sleeps. | ||
| 60 | 60 | | `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` | | |
| 61 | 61 | | `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 | |
| 62 | 62 | | `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 | |
| 63 | | `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 | | |
| 63 | 64 | | `map` | an app's plan: ready now, waiting on what, the critical path, where the crew is, parked twice and why (`app`) | | |
| 64 | 65 | | `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) | | |
| 65 | 66 | | `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 +70
| @@ -154,6 +154,9 @@ import { | ||
| 154 | 154 | directionNote, |
| 155 | 155 | OUTSIDERS_NOTE, |
| 156 | 156 | } from "../crew/direct.ts"; |
| 157 | import { migrateMessage, migrateRepo, migrationJobFor } from "../actions/migrate.ts"; | |
| 158 | import { listAllAgents } from "../actions/crew.ts"; | |
| 159 | import type { MirrorJob } from "../origins/jobs.ts"; | |
| 157 | 160 | import { setArchived, setCrewOutsiders, setDecisionsBy, repositoryLimitMessage } from "../actions/repos.ts"; |
| 158 | 161 | import { newApp } from "../actions/apps.ts"; |
| 159 | 162 | import { appLimitWords } from "../actions/app_limit.ts"; |
| @@ -286,6 +289,23 @@ function chipArgs(ctx: Ctx, args: Record<string, unknown>) { | ||
| 286 | 289 | }); |
| 287 | 290 | } |
| 288 | 291 | |
| 292 | /** A migration job as the tool answers it: where it is, not how it is drawn. */ | |
| 293 | function migrationAnswer(job: MirrorJob) { | |
| 294 | return { | |
| 295 | job: job.id, | |
| 296 | state: job.stage, | |
| 297 | url: job.fullName, | |
| 298 | detail: job.detail, | |
| 299 | startedAt: job.startedAt, | |
| 300 | finishedAt: job.finishedAt ?? null, | |
| 301 | repo: | |
| 302 | job.repo === undefined || job.stage === "failed" | |
| 303 | ? null | |
| 304 | : `${job.repo.ownerName}/${job.repo.name}`, | |
| 305 | error: job.error ?? null, | |
| 306 | }; | |
| 307 | } | |
| 308 | ||
| 289 | 309 | const str = (v: unknown): string => (typeof v === "string" ? v.trim() : ""); |
| 290 | 310 | |
| 291 | 311 | const REPO_ARG = { |
| @@ -2431,6 +2451,56 @@ const TOOLS: readonly Tool[] = [ | ||
| 2431 | 2451 | }); |
| 2432 | 2452 | }, |
| 2433 | 2453 | }, |
| 2454 | { | |
| 2455 | name: "migrate", | |
| 2456 | description: | |
| 2457 | "Bring a public repository in by URL, for good. Git history comes over (every branch and tag) and nothing reaches back: the copy here is native, with no origin and no write-through. Starts a clone job and answers {job, state, url} at once; with `job` (an id from an earlier call) instead of `url`, reads that job: its state (creating, fetching, crew, done, failed), the detail of the step it is on, the new repository when done, the reason when failed. `name` defaults to the URL's last segment. `crew` true puts an agent on it (`agent` names one; otherwise the forge's only agent), makes it an app, and tells the crew to make it run here. Needs a person's token: an agent owns no repositories. The URL goes through the same vetting as POST /api/v1/migrate: https, http or git only, no password, no address inside the network (a forge admin excepted). The same as /migrate and /api/v1/migrate.", | |
| 2458 | inputSchema: { | |
| 2459 | type: "object", | |
| 2460 | properties: { | |
| 2461 | url: { type: "string", maxLength: 2048, description: "Address of a public repository." }, | |
| 2462 | name: { type: "string", maxLength: 100, description: "What to call it here." }, | |
| 2463 | crew: { type: "boolean", description: "Put an agent on it to make it run here." }, | |
| 2464 | agent: { type: "string", maxLength: 39, description: "Which agent, with crew." }, | |
| 2465 | job: { type: "string", maxLength: 100, description: "Read this job instead of starting one." }, | |
| 2466 | }, | |
| 2467 | }, | |
| 2468 | writes: true, | |
| 2469 | readsWithout: "url", | |
| 2470 | async run(ctx, user, args) { | |
| 2471 | const jobId = str(args["job"]); | |
| 2472 | if (jobId !== "" && args["url"] === undefined) { | |
| 2473 | const found = migrationJobFor(user, jobId); | |
| 2474 | return found.ok | |
| 2475 | ? good(migrationAnswer(found.value)) | |
| 2476 | : bad("not_found", "no such migration job of yours"); | |
| 2477 | } | |
| 2478 | const url = str(args["url"]); | |
| 2479 | if (url === "") return bad("invalid_url", "url is the address of a public repository"); | |
| 2480 | let crew: { agent: string; autonomous: boolean } | undefined; | |
| 2481 | if (args["crew"] === true) { | |
| 2482 | const named = str(args["agent"]); | |
| 2483 | const agents = listAllAgents(ctx.app, user); | |
| 2484 | const all = agents.ok ? agents.value : []; | |
| 2485 | const agent = | |
| 2486 | named !== "" | |
| 2487 | ? named | |
| 2488 | : all.length === 1 | |
| 2489 | ? all[0]!.user.username | |
| 2490 | : ""; | |
| 2491 | if (agent === "") | |
| 2492 | return bad("agent_required", "crew needs `agent`: more than one agent (or none) is on this forge"); | |
| 2493 | crew = { agent, autonomous: false }; | |
| 2494 | } | |
| 2495 | const started = await migrateRepo(ctx.app, user, { | |
| 2496 | source: { url }, | |
| 2497 | ...(str(args["name"]) === "" ? {} : { name: str(args["name"]) }), | |
| 2498 | ...(crew === undefined ? {} : { crew }), | |
| 2499 | }); | |
| 2500 | if (!started.ok) return bad(started.error, migrateMessage(started.error)); | |
| 2501 | return good(migrationAnswer(started.value.job)); | |
| 2502 | }, | |
| 2503 | }, | |
| 2434 | 2504 | { |
| 2435 | 2505 | name: "map", |
| 2436 | 2506 | description: |
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"; |
| @@ -226,6 +226,7 @@ describe("the forge over MCP", () => { | ||
| 226 | 226 | "feed_chips", |
| 227 | 227 | "make_it_yours", |
| 228 | 228 | "front_page", |
| 229 | "migrate", | |
| 229 | 230 | "map", |
| 230 | 231 | "milestones", |
| 231 | 232 | "watch", |
| @@ -1226,6 +1227,38 @@ describe("the forge over MCP", () => { | ||
| 1226 | 1227 | expect(cleared.value).toEqual({ username: "ceilee", dailyUsd: null, perRunUsd: null }); |
| 1227 | 1228 | expect(getAccountCeiling(app.db, id)).toBeUndefined(); |
| 1228 | 1229 | }); |
| 1230 | test("migrate brings a repository in by URL and the job can be read to its end", async () => { | |
| 1231 | const src = await createRepo(app, alice, { name: "fixture", visibility: "public" }); | |
| 1232 | if (!src.ok) throw new Error(String(src.error)); | |
| 1233 | await commitToBranch(app, alice, src.value, { | |
| 1234 | message: "first", | |
| 1235 | files: [{ path: "README.md", content: "# fixture\n" }], | |
| 1236 | }); | |
| 1237 | const url = `${base}/alice/fixture.git`; | |
| 1238 | // A job read needs no more than `ro`; starting one needs `rw`. | |
| 1239 | expect((await tool(ro, "migrate", { url })).error).toContain("forbidden"); | |
| 1240 | // Same vetting as the route: bob is no admin, and loopback is inside the network. | |
| 1241 | expect((await tool(bobRw, "migrate", { url })).error).toContain("private_host"); | |
| 1242 | expect((await tool(rw, "migrate", { url: "file:///etc" })).error).toContain("invalid_url"); | |
| 1243 | expect((await tool(rw, "migrate", { url, crew: true, agent: "nobody" })).error).toContain("no_such_agent"); | |
| 1244 | ||
| 1245 | const started = await tool(rw, "migrate", { url, name: "brought" }); | |
| 1246 | expect(started.value.url).toBe(url); | |
| 1247 | expect(typeof started.value.job).toBe("string"); | |
| 1248 | expect(["creating", "fetching", "done"]).toContain(started.value.state); | |
| 1249 | // Another account's job is not found. | |
| 1250 | expect((await tool(bobRw, "migrate", { job: started.value.job })).error).toContain("not_found"); | |
| 1251 | ||
| 1252 | let read = started.value; | |
| 1253 | for (let i = 0; i < 100 && read.state !== "done" && read.state !== "failed"; i++) { | |
| 1254 | await Bun.sleep(100); | |
| 1255 | read = (await tool(ro, "migrate", { job: started.value.job })).value; | |
| 1256 | } | |
| 1257 | expect(read.state).toBe("done"); | |
| 1258 | expect(read.repo).toBe("alice/brought"); | |
| 1259 | const listed = (await call(rw, "tools/list")).body.result.tools; | |
| 1260 | expect(listed.find((t: any) => t.name === "migrate").description).toContain("nothing reaches back"); | |
| 1261 | }, 30_000); | |
| 1229 | 1262 | |
| 1230 | 1263 | test("new_app makes an app from a sentence and apps reads it back", async () => { |
| 1231 | 1264 | const bot = await createAgent(app, alice, { username: "newappbot", brief: "builds" }); |
| @@ -1295,3 +1328,112 @@ printf '%s\\n' '{"is_error":false,"result":"{\\"summary\\":\\"Build it\\",\\"che | ||
| 1295 | 1328 | } |
| 1296 | 1329 | }, 30_000); |
| 1297 | 1330 | }); |
| 1331 | ||
| 1332 | describe("migrate over MCP, with a crew", () => { | |
| 1333 | let dataDir: string; | |
| 1334 | let app: App; | |
| 1335 | let servers: Servers; | |
| 1336 | let alice: User; | |
| 1337 | let rw: string; | |
| 1338 | let url: string; | |
| 1339 | let nextId = 1; | |
| 1340 | ||
| 1341 | const tool = async (token: string, name: string, args: unknown = {}) => { | |
| 1342 | const res = await fetch(`${servers.url}/mcp`, { | |
| 1343 | method: "POST", | |
| 1344 | headers: { | |
| 1345 | "content-type": "application/json", | |
| 1346 | accept: "application/json, text/event-stream", | |
| 1347 | authorization: `Bearer ${token}`, | |
| 1348 | }, | |
| 1349 | body: JSON.stringify({ | |
| 1350 | jsonrpc: "2.0", | |
| 1351 | id: nextId++, | |
| 1352 | method: "tools/call", | |
| 1353 | params: { name, arguments: args }, | |
| 1354 | }), | |
| 1355 | }); | |
| 1356 | const body = (await res.json()) as any; | |
| 1357 | const text: string = body.result.content[0].text; | |
| 1358 | return body.result.isError === true | |
| 1359 | ? { error: text } | |
| 1360 | : { value: JSON.parse(text) }; | |
| 1361 | }; | |
| 1362 | const finish = async (token: string, job: string) => { | |
| 1363 | let read: any; | |
| 1364 | for (let i = 0; i < 100; i++) { | |
| 1365 | read = (await tool(token, "migrate", { job })).value; | |
| 1366 | if (read.state === "done" || read.state === "failed") break; | |
| 1367 | await Bun.sleep(100); | |
| 1368 | } | |
| 1369 | return read; | |
| 1370 | }; | |
| 1371 | const crewOf = (owner: string, name: string): string[] => { | |
| 1372 | const repo = getRepo(app.db, owner, name)!; | |
| 1373 | const crew = listCrew(app, alice, repo); | |
| 1374 | return crew.ok ? crew.value.map((a) => a.user.username) : []; | |
| 1375 | }; | |
| 1376 | ||
| 1377 | beforeAll(async () => { | |
| 1378 | dataDir = await mkdtemp(join(tmpdir(), "forge-mcp-crew-")); | |
| 1379 | app = createApp({ dataDir, port: 0, migrateFromPrivateHosts: true }); | |
| 1380 | servers = await serve(app); | |
| 1381 | const a = await createUser(app.db, { | |
| 1382 | origin: "seed", | |
| 1383 | trusted: true, | |
| 1384 | username: "alice", | |
| 1385 | email: "[email protected]", | |
| 1386 | password: "a-good-passphrase", | |
| 1387 | adminIfFirst: true, | |
| 1388 | }); | |
| 1389 | if (!a.ok) throw new Error(a.error); | |
| 1390 | alice = a.value; | |
| 1391 | rw = createToken(app.db, alice.id, "rw", "rw").token; | |
| 1392 | const src = await createRepo(app, alice, { name: "fixture", visibility: "public" }); | |
| 1393 | if (!src.ok) throw new Error(String(src.error)); | |
| 1394 | await commitToBranch(app, alice, src.value, { | |
| 1395 | message: "first", | |
| 1396 | files: [{ path: "README.md", content: "# fixture\n" }], | |
| 1397 | }); | |
| 1398 | url = `${servers.url}/alice/fixture.git`; | |
| 1399 | }, 30_000); | |
| 1400 | ||
| 1401 | afterAll(async () => { | |
| 1402 | servers.stop(); | |
| 1403 | app.close(); | |
| 1404 | await rm(dataDir, { recursive: true, force: true }); | |
| 1405 | }); | |
| 1406 | ||
| 1407 | test("crew with no agent named picks the forge's only agent, who lands on the crew of an app", async () => { | |
| 1408 | const porter = await createAgent(app, alice, { username: "porter", brief: "Moves things in." }); | |
| 1409 | if (!porter.ok) throw new Error(String(porter.error)); | |
| 1410 | const started = await tool(rw, "migrate", { url, name: "solo", crew: true }); | |
| 1411 | expect(started.error).toBeUndefined(); | |
| 1412 | const done = await finish(rw, started.value.job); | |
| 1413 | expect(done.state).toBe("done"); | |
| 1414 | expect(done.detail).toContain("porter is on it"); | |
| 1415 | expect(crewOf("alice", "solo")).toEqual(["porter"]); | |
| 1416 | expect(getAppByRepo(app.db, getRepo(app.db, "alice", "solo")!.id)).toBeDefined(); | |
| 1417 | }, 30_000); | |
| 1418 | ||
| 1419 | test("an agent's token cannot migrate: it owns nothing, with or without a crew", async () => { | |
| 1420 | const other = await createAgent(app, alice, { username: "drover", brief: "Drives." }); | |
| 1421 | if (!other.ok) throw new Error(String(other.error)); | |
| 1422 | const token = createToken(app.db, other.value.user.id, "agent", "rw").token; | |
| 1423 | for (const args of [{ url, name: "self" }, { url, name: "self", crew: true, agent: "drover" }]) { | |
| 1424 | const refused = await tool(token, "migrate", args); | |
| 1425 | expect(refused.error).toContain("forbidden_owner"); | |
| 1426 | expect(getRepo(app.db, "drover", "self")).toBeUndefined(); | |
| 1427 | } | |
| 1428 | }, 30_000); | |
| 1429 | ||
| 1430 | test("two agents and none named: refused as agent_required, and nothing is made", async () => { | |
| 1431 | const refused = await tool(rw, "migrate", { url, name: "nothing", crew: true }); | |
| 1432 | expect(refused.error).toContain("agent_required"); | |
| 1433 | expect(getRepo(app.db, "alice", "nothing")).toBeUndefined(); | |
| 1434 | // Naming one is the way out. | |
| 1435 | const named = await tool(rw, "migrate", { url, name: "named", crew: true, agent: "drover" }); | |
| 1436 | expect((await finish(rw, named.value.job)).state).toBe("done"); | |
| 1437 | expect(crewOf("alice", "named")).toEqual(["drover"]); | |
| 1438 | }, 30_000); | |
| 1439 | }); |
new above
offline