feat(mcp-tasks): add a demo MCP server that uses Tasks, test against it, and keep audit records out of agent errors (#21464)

## Thinking Path

> - Paperclip is the open source app people use to manage AI agents for
work
> - Agents call tools in connected apps through the tool gateway, which
speaks MCP to each app
> - MCP Tasks (#21263) lets an app answer a long tool call with a
durable task. It shipped behind the experimental setting
`enableMcpTasks`, off by default
> - No public app returns tasks without credentials, so nobody had seen
the feature work against a real server, and it was hard to understand
> - This pull request adds a small demo app that uses Tasks, tests the
gateway against it over HTTP, and records a live end-to-end run
> - The live run found one bug: every task stop sent the call's audit
record, with a server stack trace, to the agent. This pull request fixes
it
> - The benefit is a repeatable way to try and test Tasks,
plain-language docs, and evidence for the decision to turn the setting
on

## Linked Issues or Issue Description

Refs #21389
Refs #21263

## What Changed

- Add an MCP Tasks demo server to `packages/kv-demo-mcp-server` (new bin
`paperclip-tasks-demo-mcp-server`; run `node
packages/kv-demo-mcp-server/dist/tasks/main.js` after a build). It is
stateless MCP 2026-07-28 with `server/discover`, and it declares
`io.modelcontextprotocol/tasks`. It has six tools: `slow_report`,
`needs_approval` (form question), `will_fail`, `cancellable`, `expires`,
and `quick_echo`. It saves task state to a file, so it can restart
mid-task. It needs no credentials. It has a status page at `/` and JSON
at `/api/state`.
- I put it in the existing demo package, so there is no new workspace
package and no `pnpm-lock.yaml` change.
- Add `server/src/__tests__/mcp-tasks-demo-server.test.ts`. It runs the
real gateway against the real demo server over HTTP, with one test per
tool plus a restart.
- Fix: `server/src/routes/tool-gateway.ts` no longer sends
`details.execution` to callers. That record has request IDs and the
server-side error stack. The audit still keeps it. Add
`server/src/__tests__/tool-gateway-agent-errors.test.ts` (red before the
fix, green after).
- Docs: `doc/connections/MCP-TASKS.md` now has "In plain words", "What a
person sees", "Try it yourself", the live-run results, and a
recommendation. The package README documents the demo server.
- `enableMcpTasks` stays off by default.

## Verification

- `packages/kv-demo-mcp-server`: `npx vitest run` gives 25/25 pass (13
new demo-server tests). `tsc -p tsconfig.test.json` is clean.
- `server`: `mcp-tasks-demo-server.test.ts` gives 8/8 pass.
`tool-gateway-agent-errors.test.ts` gives 2/2 pass, and it failed 2/2
before the fix.
- Existing suites that cover the changed route and Tasks:
`mcp-tasks.test.ts` and `tool-gateway-discovery-http.test.ts` (results
in a comment below).
- `pnpm verify` exits 2, because the doc is a global CI input. Required
CI gives the broad coverage.
- Live end-to-end run on a local `test-drive` instance with
`enableMcpTasks` on. The agent was Codex `gpt-6-luna` at low effort. The
demo was connected through "Connect your own MCP server":

| Behaviour | Result | `tools/call` count at the app |
| --- | --- | --- |
| Polling (`slow_report`, 45 s) | Delivered after 22 polls | 1 |
| Question (`needs_approval`) | The card was answered in the UI.
`tasks/update` was sent, and the task finished | 1 |
| Failure (`will_fail`) | `provider_task_failed` with the app's message
| 1 |
| Cancel (`cancellable`) | Stop on the run sent `tasks/cancel` | 1 |
| Expiry (`expires`, 15 s) | `provider_task_expired`, no hang | 1 |
| Restart (`slow_report`, 120 s) | The server was down for 2 min 23 s.
The result was delivered once, and the agent was woken once | 1 |

After the run, all 6 `tool_mcp_tasks` rows were settled and none had a
next poll.

## Risks

- Low risk. The demo server is new code in a demo package, and the bin
is additive.
- The route change removes `execution` from tool-gateway error
responses. No UI code or test reads that field from these responses. The
audit and tool-call events still record it.

## Model Used

- Claude Opus 5.5 (`claude-opus-5-5`), in Claude Code with tool use and
code execution.

## Checklist

- [x] I have included a thinking path that traces from project context
to this change
- [x] I have specified the model used (with version and capability
details)
- [x] I have checked ROADMAP.md and confirmed this PR does not duplicate
planned core work
- [x] I have searched GitHub for duplicate or related PRs and linked
them above
- [x] I have either (a) linked existing issues with `Fixes: #` / `Closes
#` / `Refs #` OR (b) described the issue in-PR following the relevant
issue template
- [x] I have not referenced internal/instance-local Paperclip issues or
links (only public GitHub `#NNN` / `github.com/paperclipai/paperclip`
URLs)
- [x] My branch name describes the change (e.g. `docs/...`, `fix/...`)
and contains no internal Paperclip ticket id or instance-derived details
- [x] I have run tests locally and they pass
- [x] I have added or updated tests where applicable
- [x] I have updated relevant documentation to reflect my changes
- [x] I have considered and documented any risks above
- [ ] All Paperclip CI gates are green
- [ ] Greptile is 5/5 with no open P2s, recommendations, or follow-ups
- [x] I will address all Greptile and reviewer comments before
requesting merge

🤖 Generated with [Claude Code](https://claude.com/claude-code)

---------

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
DottaandClaude Opus 5.5 authored and GitHub committed 2026-11-13 19:47:58 -06:00
1 parent 1f388ed385
commit 81d06cfc97
13 files changed
+1375 -4

No files matched your search

@@ -0,0 +1,197 @@
import { randomUUID } from "node:crypto";
import { mkdtemp, rm } from "node:fs/promises";
import { tmpdir } from "node:os";
import { join } from "node:path";
import { afterAll, beforeAll, describe, expect, it, vi } from "vitest";
import {
agents,
companies,
companyMemberships,
createDb,
heartbeatRuns,
issueThreadInteractions,
issues,
projects,
toolMcpTasks,
} from "@paperclipai/db";
import { and, eq, lte, ne } from "drizzle-orm";
import { forgetMcpHttpSessions } from "../services/mcp-http.js";
import { createToolGatewayService } from "../services/tool-gateway.js";
import { toolAccessService } from "../services/tool-access.js";
import { instanceSettingsService } from "../services/instance-settings.js";
import { adapterExecutionControls, createAdapterExecutionControl } from "../services/adapter-execution-control.js";
import {
createTasksDemoHttpServer,
DemoTaskStore,
type TasksDemoHttpServer,
} from "../../../packages/kv-demo-mcp-server/src/index.js";
import { getEmbeddedPostgresTestSupport, startEmbeddedPostgresTestDatabase } from "./helpers/embedded-postgres.js";
/**
* MCP Tasks end to end against the real demo server
* (`packages/kv-demo-mcp-server/src/tasks`) over HTTP. The demo's clock is
* moved by hand, so a "30-second" job finishes the moment the test says so.
*/
const support = await getEmbeddedPostgresTestSupport();
(support.supported ? describe : describe.skip)("MCP Tasks with the demo server", () => {
const actor = { actorType: "user" as const, actorId: "mcp-tasks-demo-user", actorSource: "local_implicit" as const };
let fixture: Awaited<ReturnType<typeof startEmbeddedPostgresTestDatabase>>;
let db: ReturnType<typeof createDb>;
let keyDir: string;
let demo: TasksDemoHttpServer;
let demoUrl: string;
let clock = Date.now();
beforeAll(async () => {
keyDir = await mkdtemp(join(tmpdir(), "mcp-tasks-demo-secrets-"));
vi.stubEnv("PAPERCLIP_SECRETS_MASTER_KEY_FILE", join(keyDir, "key"));
fixture = await startEmbeddedPostgresTestDatabase("mcp-tasks-demo-");
db = createDb(fixture.connectionString);
await instanceSettingsService(db).updateExperimental({ enableMcpTasks: true });
demo = createTasksDemoHttpServer({ store: new DemoTaskStore({ now: () => clock }) });
demoUrl = `http://127.0.0.1:${await demo.listen(0)}/mcp`;
}, 30_000);
afterAll(async () => {
await demo?.close();
await fixture?.cleanup();
vi.unstubAllEnvs();
if (keyDir) await rm(keyDir, { recursive: true, force: true });
});
const starts = (tool: string) => demo.store.snapshot().toolCalls[tool] ?? 0;
const demoTask = (tool: string) => demo.store.snapshot().tasks.filter((task) => task.tool === tool).at(-1);
const polled = (tool: string) => demoTask(tool)?.polls ?? 0;
async function setup() {
const [org] = await db.insert(companies).values({ name: `Demo ${randomUUID()}`, issuePrefix: `D${randomUUID().slice(0, 5)}` }).returning();
await db.insert(companyMemberships).values({ companyId: org.id, principalType: "user", principalId: actor.actorId, status: "active", membershipRole: "admin" });
const [agent] = await db.insert(agents).values({ companyId: org.id, name: "Demo agent", role: "engineer", adapterType: "process", adapterConfig: {}, runtimeConfig: {} }).returning();
const [project] = await db.insert(projects).values({ companyId: org.id, name: "Demo project" }).returning();
const [issue] = await db.insert(issues).values({ companyId: org.id, projectId: project.id, title: "Try the demo", status: "in_progress", assigneeAgentId: agent.id }).returning();
const [run] = await db.insert(heartbeatRuns).values({ companyId: org.id, agentId: agent.id, invocationSource: "assignment", status: "running", contextSnapshot: { issueId: issue.id, projectId: project.id } }).returning();
// A public-looking link, forwarded to the local demo server.
const options = { deploymentMode: "local_trusted" as const, deploymentExposure: "private" as const,
remoteHttpEndpointLookup: async () => [{ address: "8.8.8.8", family: 4 as const }],
remoteHttpRequest: (_url: string, init: RequestInit) => fetch(demoUrl, init) };
const service = toolAccessService(db, options);
const connected = await service.connectGalleryApp(org.id, { link: `https://tasks-demo-${randomUUID().slice(0, 8)}.example.com/mcp`, name: "Tasks demo" }, actor);
await service.finishGalleryAppConnection(org.id, connected.connectionId, {
enabledCatalogEntryIds: connected.catalog.map((entry) => entry.id), askFirstCatalogEntryIds: [], access: "all_agents",
}, actor);
const gatewayOptions = { ...options, toolActionSigningSecret: "tasks-demo-signing-key" };
let gateway = createToolGatewayService(db, gatewayOptions);
const session = await gateway.createSession({ companyId: org.id, agentId: agent.id, runId: run.id });
const names = (await gateway.listToolsForSession(session.token)).map((tool) => tool.name);
const toolName = (name: string) => names.find((candidate) => candidate.endsWith(`:${name.replace(/_/g, "-")}`))!;
const call = (name: string, parameters: Record<string, unknown> = {}, timeoutMs = 10_000, sessionToken = session.token) =>
gateway.executeTool({ sessionToken, tool: toolName(name), parameters, timeoutMs });
const rows = () => db.select().from(toolMcpTasks).where(eq(toolMcpTasks.companyId, org.id));
return {
org, agent, issue, run, call, rows, names,
gateway: () => gateway,
restart: () => { forgetMcpHttpSessions(connected.connectionId); gateway = createToolGatewayService(db, gatewayOptions); return gateway; },
/** Background poll for this company only. */
poll: async (wake: (input: unknown) => Promise<void>) => {
await db.update(toolMcpTasks).set({ nextPollAt: new Date(Date.now() + 60 * 60_000) })
.where(and(ne(toolMcpTasks.companyId, org.id), lte(toolMcpTasks.nextPollAt, new Date())));
return gateway.pollDueMcpTasks({ wake });
},
};
}
it("lists every demo tool", async () => {
const { names } = await setup();
expect(names.map((name) => name.split(":")[1]).sort()).toEqual(["cancellable", "expires", "needs-approval", "quick-echo", "slow-report", "will-fail"]);
});
it("slow_report: polls the task and returns the report like a direct result", async () => {
const { call, rows } = await setup();
const before = starts("slow_report");
const pending = call("slow_report", { topic: "uptime", seconds: 30 });
await vi.waitFor(() => expect(polled("slow_report")).toBeGreaterThan(0), { timeout: 5_000 });
clock += 30_000;
const result = await pending;
expect(result).toMatchObject({ status: "completed", result: { content: expect.stringContaining("Report on uptime"), data: { structuredContent: { topic: "uptime", seconds: 30 } } } });
expect(starts("slow_report") - before).toBe(1);
expect((await rows())[0]).toMatchObject({ status: "completed" });
});
it("slow_report: outlives the timeout and a Paperclip restart, then is delivered once without starting again", async () => {
const ctx = await setup();
const before = starts("slow_report");
await expect(ctx.call("slow_report", { topic: "restart", seconds: 60 }, 400)).rejects.toMatchObject({ reasonCode: "provider_task_pending" });
const fresh = ctx.restart();
await db.update(heartbeatRuns).set({ status: "failed" }).where(eq(heartbeatRuns.id, ctx.run.id));
const [row] = await ctx.rows();
await db.update(toolMcpTasks).set({ nextPollAt: new Date(Date.now() - 1_000), leaseExpiresAt: null, leaseOwner: null }).where(eq(toolMcpTasks.id, row!.id));
clock += 60_000;
const wake = vi.fn(async () => undefined);
await ctx.poll(wake);
expect(wake).toHaveBeenCalledTimes(1);
const [recovered] = await db.insert(heartbeatRuns).values({ companyId: ctx.org.id, agentId: ctx.agent.id, invocationSource: "assignment", status: "running", contextSnapshot: { issueId: ctx.issue.id } }).returning();
const session = await fresh.createSession({ companyId: ctx.org.id, agentId: ctx.agent.id, runId: recovered!.id });
expect(await ctx.call("slow_report", { topic: "restart", seconds: 60 }, 5_000, session.token))
.toMatchObject({ status: "completed", result: { content: expect.stringContaining("Report on restart") } });
expect(starts("slow_report") - before).toBe(1);
// Asking again is new work: the first result was delivered exactly once.
const [delivered] = await ctx.rows();
expect(delivered!.deliveredAt).toBeInstanceOf(Date);
});
it("needs_approval: the question becomes a card, and the person's choice is sent with tasks/update", async () => {
const { call, org, issue } = await setup();
const first = await call("needs_approval", { action: "rotate the demo key" }).catch((error) => error);
expect(first).toMatchObject({ reasonCode: "elicitation_required" });
const [card] = await db.select().from(issueThreadInteractions).where(and(eq(issueThreadInteractions.companyId, org.id), eq(issueThreadInteractions.issueId, issue.id)));
expect(card).toMatchObject({ kind: "ask_user_questions", status: "pending" });
await db.update(issueThreadInteractions).set({ status: "answered", result: { version: 1, answers: [
{ questionId: "decision", optionIds: ["approve"] },
{ questionId: "note", optionIds: [], otherText: "go ahead" },
] } }).where(eq(issueThreadInteractions.id, card!.id));
const pending = call("needs_approval", { action: "rotate the demo key" });
await vi.waitFor(() => expect(demoTask("needs_approval")?.status).toBe("working"), { timeout: 5_000 });
clock += 3_000;
expect(await pending).toMatchObject({ status: "completed", result: { content: "Approved: rotate the demo key was carried out. Note: go ahead" } });
});
it("will_fail: becomes a tool error with the app's message", async () => {
const { call } = await setup();
const pending = call("will_fail", { seconds: 5 }).catch((error) => error);
await vi.waitFor(() => expect(polled("will_fail")).toBeGreaterThan(0), { timeout: 5_000 });
clock += 5_000;
const failure = await pending;
expect(failure).toMatchObject({ reasonCode: "provider_task_failed" });
expect(failure.message).toContain("crashed on purpose");
});
it("cancellable: stopping the run cancels the task in the app", async () => {
const { call, run, rows } = await setup();
const control = createAdapterExecutionControl();
adapterExecutionControls.set(run.id, control);
try {
const pending = call("cancellable").catch((error) => error);
await vi.waitFor(() => expect(polled("cancellable")).toBeGreaterThan(0), { timeout: 5_000 });
control.controller.abort();
expect(await pending).toMatchObject({ reasonCode: "provider_task_cancelled" });
} finally {
adapterExecutionControls.delete(run.id);
}
expect(demoTask("cancellable")?.status).toBe("cancelled");
expect((await rows())[0]).toMatchObject({ status: "cancelled", errorCode: "run_cancelled" });
});
it("expires: a task the app forgets ends with a clear error, never a hang", async () => {
const { call, rows } = await setup();
const pending = call("expires", { ttlSeconds: 15 }).catch((error) => error);
await vi.waitFor(() => expect(polled("expires")).toBeGreaterThan(0), { timeout: 5_000 });
clock += 15_000;
expect(await pending).toMatchObject({ reasonCode: "provider_task_expired" });
expect((await rows())[0]).toMatchObject({ status: "expired" });
});
it("quick_echo: an ordinary call stays synchronous", async () => {
const { call, rows } = await setup();
expect(await call("quick_echo", { text: "hi" })).toMatchObject({ status: "completed", result: { content: "echo: hi" } });
expect(await rows()).toEqual([]);
});
});
@@ -0,0 +1,51 @@
import express from "express";
import request from "supertest";
import { describe, expect, it } from "vitest";
import type { Db } from "@paperclipai/db";
import { mcpGatewayProtocolRoutes, toolGatewayRoutes } from "../routes/tool-gateway.js";
import { ToolGatewayHttpError, type ToolGatewayService } from "../services/tool-gateway.js";
/**
* A tool error carries the call's execution record (request IDs, timings, and
* the server-side stack of the error) so the audit can store it. Agents get
* the message and reason, never that record: they paste errors into task
* comments, where a person would read Paperclip's internals.
*/
function failingGateway() {
const error = new ToolGatewayHttpError(410,
"The app no longer has this action's result: it expired or was removed. Run the action again to start it over.",
"provider_task_expired",
{
connectionId: "connection-1",
execution: {
transport: "mcp_remote",
request: { requestId: "paperclip-tool-123" },
error: { name: "Error", message: "expired", code: null, stack: "Error: expired\n at mcpTaskExpiredError (/srv/paperclip/server/src/services/tool-gateway.ts:6823:12)", cause: null },
},
});
return { executeTool: async () => { throw error; } } as unknown as ToolGatewayService;
}
describe("tool errors returned to agents", () => {
it("leave out the execution record on the MCP gateway", async () => {
const app = express().use(express.json()).use(mcpGatewayProtocolRoutes(failingGateway()));
const response = await request(app).post("/mcp/gateways/gateway-1").set("Authorization", "Bearer token")
.send({ jsonrpc: "2.0", id: 7, method: "tools/call", params: { name: "mcp.demo:expires", arguments: {} } });
expect(response.body.error).toMatchObject({
message: expect.stringContaining("Run the action again"),
data: { reasonCode: "provider_task_expired", connectionId: "connection-1" },
});
expect(response.body.error.data).not.toHaveProperty("execution");
expect(JSON.stringify(response.body)).not.toContain("tool-gateway.ts");
});
it("leave out the execution record on the session tool call route", async () => {
const app = express().use(express.json()).use(toolGatewayRoutes({} as Db, failingGateway()));
const response = await request(app).post("/tool-gateway/tools/call").set("x-paperclip-tool-gateway-token", "token")
.send({ tool: "mcp.demo:expires", parameters: {} });
expect(response.status).toBe(410);
expect(response.body).toMatchObject({ reasonCode: "provider_task_expired", connectionId: "connection-1" });
expect(response.body).not.toHaveProperty("execution");
expect(JSON.stringify(response.body)).not.toContain("tool-gateway.ts");
});
});
+12 -2
View File
@@ -56,6 +56,16 @@ function callerHeaders(req: { headers: Record<string, string | string[] | undefi
return headers;
}
/**
* Error details an HTTP caller may see. `execution` is the call's audit record
* (request IDs, timings, the server-side stack of an error); the audit keeps
* it, and callers never need it. Agents paste errors into task comments.
*/
function callerErrorDetails(details: Record<string, unknown>): Record<string, unknown> {
const { execution: _auditOnly, ...rest } = details;
return rest;
}
async function cancellableRequest<T>(req: Request, res: Response, work: (signal: AbortSignal) => Promise<T>) {
const controller = new AbortController();
const aborted = () => controller.abort();
@@ -244,7 +254,7 @@ async function handleMcpGatewayProtocol(
res.status(err.status).json({
jsonrpc: "2.0",
id,
error: { code: err.status >= 500 ? -32603 : -32000, message: err.message, data: { reasonCode: err.reasonCode, ...err.details } },
error: { code: err.status >= 500 ? -32603 : -32000, message: err.message, data: { reasonCode: err.reasonCode, ...callerErrorDetails(err.details) } },
});
return;
}
@@ -340,7 +350,7 @@ function sendGatewayError(res: import("express").Response, err: unknown) {
res.status(err.status).json({
error: err.message,
reasonCode: err.reasonCode,
...err.details,
...callerErrorDetails(err.details),
});
return;
}