mirror of
https://github.com/paperclipai/paperclip.git
synced 2026-11-28 21:46:15 +01:00
## Thinking Path > - Paperclip is the open source app people use to manage AI agents for work > - Each Paperclip instance serves an assistant connection (MCP) at `/mcp/paperclip`, and any instance can connect a remote MCP server by its address > - So one instance's agents can file and follow tasks on another instance, for example a colleague's > - But the other instance's tools look like the agent's own Paperclip tools: same product, similar names, and the connection prefix was a generic internal link key > - An agent can then file local work on someone else's instance, or the reverse > - This pull request recognizes a Paperclip address, names the connection and tool prefix after that instance, adds agent instructions, and refuses a connection to the same instance > - The benefit is that agents can tell their own Paperclip from a connected one ## Linked Issues or Issue Description **Subsystem affected** Connections: "Connect your own MCP server" (generic remote MCP) and the tool gateway names that agents see. **Problem or motivation** When instance A connects instance B through `/mcp/paperclip`, A's agents get B's `paperclip_create_task` and other tools. The tool prefix came from an internal key (`app-gallery🔗<uuid>`), so native runners saw `app_mcp_app_gallery_link_paperclip_create_task_…`. Nothing told the agent that the tools act on a different instance. An agent could send internal work to another organization by mistake. **Proposed solution** Recognize an address whose path is `/mcp/paperclip`. Use only the typed address, never data from the remote server. Name the connection after the host (`acme Paperclip`). Use that name as the tool prefix for these connections only. Add default agent instructions. Refuse the instance's own address with `paperclip_self_connection` (422). **Alternatives considered** Read the company name from the remote `paperclip_connection` tool. This needs an extra authenticated call during setup, and it puts remote-controlled text in names and instructions. The host is enough to identify the stack. Require approval for each write. We rejected this: actions start on, the same as other connections. **Roadmap alignment** Supports instance-to-instance work through existing MCP connections. It adds no new transport. ## What Changed - `packages/shared/src/paperclip-peer.ts`: detect a Paperclip MCP address, derive a label and name, compare with own origins (loopback hosts on the same port are equal), and build default agent instructions. - `server/src/services/tool-access.ts`: default name and default agent instructions for a new Paperclip connection. Instructions that the person supplies are kept. - `server/src/routes/tool-access.ts`: refuse the instance's own address before any network request. - `server/src/services/tool-gateway.ts`: use the prefix recorded on a Paperclip connection (`paperclipPeerToolNamespace`, derived from its address at creation). Renames never change it. Other connections, and Paperclip connections made before this change, keep their current tool names. - `ui/src/pages/apps/generic-mcp-connect.ts`: same default name in the setup flow. - `doc/connections/GENERIC-REMOTE-MCP.md`: new section "Connecting another Paperclip instance". ## Verification - `packages/shared`: `npx vitest run src/paperclip-peer.test.ts`, 20 passed. - `server`: `npx vitest run src/__tests__/generic-mcp-connection.test.ts`, 98 passed. The naming, instructions, self-connection and tool-prefix tests fail on the base revision and pass with this change (red/green). - `ui`: `npx vitest run src/pages/apps/generic-mcp-connect.test.ts`, 29 passed. - `tsc --noEmit` passes for `packages/shared` and `server`. - Live check with two local instances. Instance B was in authenticated mode on port 3201 with assistant connections on. Instance A was on port 3202. - A connected to `http://localhost:3201/mcp/paperclip`. The name was `localhost-3201 Paperclip`. A registered with B by dynamic client registration and asked for read, write, configure and offline access. - B's consent page showed "Connect Paperclip localhost-3202 to Paperclip". After approval, A had all 37 tools active, writes included. - From A, `paperclip_connection` returned B's company. `paperclip_create_task` created `ACM-1` on B. - An agent session on A listed `mcp.localhost-3201-paperclip-…:paperclip-create-task` (runner name `app_mcp_localhost_3201_p_paperclip_create_task_…`). Before the gateway change, the name was `app_mcp_app_gallery_link_paperclip_connection_…`. The run received the connection instructions. - Connecting A to `http://localhost:3202/mcp/paperclip` returned 422 `paperclip_self_connection`. ## Risks - Low risk. Only connections whose address path is exactly `/mcp/paperclip` change. Other connections keep their names, instructions and tool names. - A Paperclip connection created before this change keeps its original tool names, because only connections that record `paperclipPeerToolNamespace` at creation get the new prefix. Saved exact tool-name rules and pending approvals for those connections keep matching. They also get no default instructions; people can add them. - Native runners keep only 20 characters of the namespace (`mcp_` uses 4), so long host labels are cut. The tool part stays complete. ## Model Used - Claude Opus 5.5 (`claude-opus-5-5`) in Claude Code, with tool use (shell, file edits, browser pane). ## 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>
116 lines
5.1 KiB
TypeScript
116 lines
5.1 KiB
TypeScript
import { defaultConnectionAgentInstructions, type ConnectionAgentInstructions, type ConnectionInstructionTemplate } from "./connection-instructions.js";
|
|
|
|
/**
|
|
* Another Paperclip instance connected through "Connect your own MCP server".
|
|
*
|
|
* Every Paperclip serves its assistant connection at the same path. Recognizing
|
|
* that address lets setup name the connection after the instance it reaches and
|
|
* tell agents those tools act somewhere else. Agents already have their own
|
|
* Paperclip tools, so without this the two sets look alike: same names, same
|
|
* product in every description.
|
|
*
|
|
* Everything here is derived from the address the person typed. Nothing the
|
|
* remote server returns is used, so a remote instance cannot choose the name or
|
|
* the instructions agents read.
|
|
*/
|
|
export const PAPERCLIP_PEER_MCP_PATH = "/mcp/paperclip";
|
|
|
|
export interface PaperclipPeer {
|
|
/** `https://acme.paperclip.app` */
|
|
origin: string;
|
|
/** `acme.paperclip.app`, with a port when one is present. */
|
|
host: string;
|
|
/** Short distinctive part of the host: `acme`, `staging`, `localhost-3101`. */
|
|
label: string;
|
|
/** Default connection name. The label comes first so it reads distinctively. */
|
|
name: string;
|
|
/** Tool-name prefix agents see. Derived from the address, so renaming never changes it. */
|
|
toolNamespace: string;
|
|
}
|
|
|
|
const LOOPBACK_HOSTNAMES = new Set(["localhost", "127.0.0.1", "[::1]", "::1"]);
|
|
const GENERIC_HOST_LABELS = new Set(["www", "paperclip"]);
|
|
|
|
function isIpAddress(hostname: string) {
|
|
return /^\d{1,3}(?:\.\d{1,3}){3}$/.test(hostname) || hostname.startsWith("[");
|
|
}
|
|
|
|
function hostLabel(url: URL): string {
|
|
const hostname = url.hostname.toLowerCase();
|
|
if (LOOPBACK_HOSTNAMES.has(hostname) || isIpAddress(hostname) || !hostname.includes(".")) {
|
|
const base = hostname.replace(/[^a-z0-9]+/g, "-").replace(/^-+|-+$/g, "") || "paperclip";
|
|
return url.port ? `${base}-${url.port}` : base;
|
|
}
|
|
const parts = hostname.split(".");
|
|
// Drop the registrable domain (approximated as the last two labels) and
|
|
// generic labels, so `acme.paperclip.app` becomes `acme` and
|
|
// `paperclip.staging.paperclip.app` becomes `staging`.
|
|
const subdomain = (parts.length >= 3 ? parts.slice(0, -2) : parts.slice(0, -1))
|
|
.filter((part) => !GENERIC_HOST_LABELS.has(part));
|
|
const chosen = subdomain.length > 0 ? subdomain : parts.slice(0, -1);
|
|
const label = chosen.join("-").replace(/[^a-z0-9-]+/g, "-").replace(/^-+|-+$/g, "");
|
|
return label.slice(0, 40) || "paperclip";
|
|
}
|
|
|
|
/** The other Paperclip this MCP address points at, or null for any other server. */
|
|
export function paperclipPeerFromMcpUrl(value: string | null | undefined): PaperclipPeer | null {
|
|
if (!value) return null;
|
|
let url: URL;
|
|
try {
|
|
url = new URL(value.trim());
|
|
} catch {
|
|
return null;
|
|
}
|
|
if (url.protocol !== "https:" && url.protocol !== "http:") return null;
|
|
if (url.pathname.replace(/\/+$/, "") !== PAPERCLIP_PEER_MCP_PATH) return null;
|
|
const label = hostLabel(url);
|
|
return {
|
|
origin: url.origin,
|
|
host: url.host,
|
|
label,
|
|
name: label === "paperclip" ? `Paperclip ${url.host}` : `${label} Paperclip`,
|
|
toolNamespace: label === "paperclip" ? "paperclip" : `${label}-paperclip`,
|
|
};
|
|
}
|
|
|
|
function loopbackEquivalent(url: URL) {
|
|
const hostname = url.hostname.toLowerCase();
|
|
const port = url.port || (url.protocol === "https:" ? "443" : "80");
|
|
return LOOPBACK_HOSTNAMES.has(hostname) ? `${url.protocol}//loopback:${port}` : `${url.protocol}//${url.host.toLowerCase()}`;
|
|
}
|
|
|
|
/** True when the peer is this same instance, under any of its own origins. */
|
|
export function isSamePaperclipInstance(peer: PaperclipPeer, ownOrigins: Array<string | null | undefined>): boolean {
|
|
const target = loopbackEquivalent(new URL(peer.origin));
|
|
return ownOrigins.some((origin) => {
|
|
if (!origin) return false;
|
|
try {
|
|
return loopbackEquivalent(new URL(origin)) === target;
|
|
} catch {
|
|
return false;
|
|
}
|
|
});
|
|
}
|
|
|
|
export const PAPERCLIP_PEER_INSTRUCTIONS_TEMPLATE_ID = "paperclip.peer";
|
|
|
|
/** Default guidance for a peer connection; the connection screen edits and resets against it. */
|
|
export function paperclipPeerInstructionTemplate(peer: PaperclipPeer): ConnectionInstructionTemplate {
|
|
return {
|
|
id: PAPERCLIP_PEER_INSTRUCTIONS_TEMPLATE_ID,
|
|
version: 1,
|
|
text: [
|
|
`These tools act on a different Paperclip instance at ${peer.origin}, not the Paperclip you work in.`,
|
|
"For your own tasks, comments and documents, keep using your own Paperclip tools.",
|
|
`Use these tools only when your task asks you to send, read or update work on ${peer.host}.`,
|
|
`Whatever you send there is visible to that instance's organization, so include only what the task asks you to share.`,
|
|
`Task IDs and links from ${peer.host} do not exist in your own Paperclip: quote the full ${peer.origin} links these tools return.`,
|
|
].join(" "),
|
|
};
|
|
}
|
|
|
|
/** Standing guidance attached to a new peer connection. People can edit or turn it off. */
|
|
export function paperclipPeerAgentInstructions(peer: PaperclipPeer): ConnectionAgentInstructions {
|
|
return defaultConnectionAgentInstructions(paperclipPeerInstructionTemplate(peer))!;
|
|
}
|