mirror of
https://github.com/paperclipai/paperclip.git
synced 2026-10-23 00:53:38 +02:00
## Thinking Path > - Paperclip is the open source app people use to manage AI agents for work. > - Its tool gateway applies company access rules and approval controls to connected apps. > - MCP aggregators expose many apps through one provider endpoint. > - Each aggregator needs its own credential, catalog, grants, and lifecycle in Paperclip. > - This pull request adds independent Zapier, Arcade, Composio Connect, and Executor setup with a common Access → Connect layout. > - A default-off MCP aggregators flag lets operators opt in while we complete provider acceptance tests. > - Agents use the normal Paperclip permissions, Test screen, and gateway after setup. ## Linked Issues or Issue Description **Subsystem affected** Apps, connection setup, shared contracts, and the remote MCP gateway. **Problem or motivation** Aggregator endpoints need clear provider setup and correct MCP sessions. Generic setup does not explain each provider's authentication or broad execution tools. Provider approval must preserve the original execution instead of replaying a write. **Proposed solution** Add four separate connectors behind Settings → Experimental → MCP aggregators. Start with human and agent access, then connect the endpoint and read its tools. Enable tools by default. Use the existing Permissions and Test screens after setup. Keep legacy Composio API-key and child connections intact. **Alternatives considered** A shared connection for all providers would mix credentials and access rules. Separate provider-specific permission and test screens would duplicate existing controls. Vercel Connect is outside this change. **Roadmap alignment** Extends the existing MCP Tool Gateway & Apps capability and the Connected Apps roadmap area. This work was requested and reviewed by the maintainer. Related work: #11894, #12630, #12632, #12634, and #12906 concern the legacy Composio broker. #13102 also covers remote MCP pagination. This change preserves the broker path and adds initialized sessions, response matching, and provider resume handling alongside pagination. ## What Changed - Add branded setup and interactive Storybooks for Zapier, Arcade, Composio Connect, and Executor. Use the existing access controls and normal action tests. Do not request a connection name or action choices during setup. - Add the default-off `enableMcpAggregators` flag to settings, managed feature metadata, the catalog, and setup guards. Hidden connections keep running. Legacy Composio connections remain unchanged. - Reuse the vault, grants, policy, and catalog models. Support OAuth discovery, bearer tokens, custom headers, and credential-bearing URLs. Add no database tables or migrations. - Initialize and retain Streamable HTTP sessions by connection and effective credentials. Read paginated catalogs and match streaming responses to request IDs. - Classify unfamiliar aggregator tools as writes despite upstream read-only hints; only exact reviewed read capabilities enter the read-only allowlist. Legacy Composio child behavior is preserved. - Preserve provider authorization links and execution IDs. Support Executor approve/resume, decline, and cancel without automatic replay of uncertain writes. - Preserve Off and Ask first choices during refresh and reconnect. Allow new tools and retire removed tools. Keep agent access updates atomic and preserve an empty agent selection. - Document connector UX rules, provider branding sources, and live acceptance results. - Stabilize the existing Sentry release fixture after its repeated CI failure by reusing one module mock; production Sentry behavior is unchanged. ## Verification - Final head `d11781970`: [CI run](https://github.com/paperclipai/paperclip/actions/runs/35633534900) passed, including broad typecheck, test shards, build, and E2E. All 54 checks pass; 2 optional checks are skipped. Greptile is 5/5, Security Scan passes, and all review threads are resolved. - Passed 27 focused connector Vitest checks and 18 connector-only Storybook browser checks before the flag change. All 85 stories rendered at desktop and narrow widths. - Passed 5 connector lifecycle/server checks and 7 selected flag checks after adding the flag. The latter cover settings, managed defaults, cached catalog visibility, and all four setup routes. - Review fixes passed 13 risk/handoff/lifecycle checks, dedicated session-expiration and transport regressions, 13 selected connector/gateway CI cases, and 10 selected setup/reconnect UI cases. A real Composio connection-list call also succeeded through the refreshed UI on `9ab115f71`. - UI and server TypeScript checks passed. UI build, Storybook build, token gates, and diff whitespace checks passed during implementation. - Real browser and real Paperclip agent tests passed for Arcade, Composio, and Executor. Tested action permissions, denied agent access, reconnect, disconnect, and isolation. Tested Arcade catalog additions/removal and Executor provider approve/resume, decline, and cancel. - Zapier live acceptance is incomplete. Its dedicated provider server is configured, but its credential-copy dialog returned an empty clipboard through browser automation. No live Zapier action is claimed. - The three isolated Sentry release cases pass after the CI fixture fix. - Local verification is deliberately narrow at the maintainer's request. The full local suite, recursive typecheck, and repository-wide build were not run. CI provides the broader checks. ## Risks - Shared MCP transport changes affect other remote MCP servers. Protocol fixtures cover initialized sessions, streaming response matching, pagination, and isolation. - Broad execution tools remain broad permissions. The provider governs actions inside those tools. - Provider handoff links are retained briefly in memory. After a server restart, a one-time link may require reopening the provider dashboard. Paperclip does not replay the original call. - Zapier remains unproven live. Custom-header imports and self-hosted endpoints have fixture coverage rather than a separate live account for every variant. - Turning the experimental flag off hides setup; it does not revoke existing credentials or stop existing connections. ## Model Used OpenAI GPT-6 through Codex, with reasoning, repository tools, shell execution, and browser automation. The exact runtime model ID and context-window size are not exposed in this session. A separate Anthropic-backed Paperclip agent performed live gateway acceptance tasks. ## 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 - [x] All Paperclip CI gates are green - [x] Greptile is 5/5 with no open P2s, recommendations, or follow-ups - [x] I will address all Greptile and reviewer comments before requesting merge --------- Co-authored-by: Paperclip <noreply@paperclip.ing>
270 lines
11 KiB
TypeScript
270 lines
11 KiB
TypeScript
import { createHash } from "node:crypto";
|
|
// Helpers for talking to remote MCP servers over the Streamable HTTP transport.
|
|
//
|
|
// The MCP Streamable HTTP spec requires the client to advertise that it accepts
|
|
// BOTH a single JSON response and an SSE stream on every POST:
|
|
//
|
|
// Accept: application/json, text/event-stream
|
|
//
|
|
// Spec-compliant servers reject requests missing this header with 406 Not
|
|
// Acceptable, and when the header is present they are free to answer with an
|
|
// SSE stream (`event: message\ndata: {…}`) instead of a bare JSON body. So any
|
|
// code path that POSTs JSON-RPC to a remote `/mcp` endpoint must (a) send the
|
|
// Accept header and (b) be able to read an SSE-framed response.
|
|
|
|
/** The Accept header value required by the MCP Streamable HTTP transport. */
|
|
export const MCP_HTTP_ACCEPT = "application/json, text/event-stream";
|
|
export const MCP_PROTOCOL_VERSION = "2025-06-18";
|
|
|
|
export class McpHttpResponseError extends Error {
|
|
constructor(readonly reason: "invalid_json" | "malformed_response" | "too_large", message: string) {
|
|
super(message);
|
|
this.name = "McpHttpResponseError";
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Default headers for an MCP Streamable HTTP JSON-RPC POST. Caller-supplied
|
|
* headers (e.g. resolved credentials) are preserved, while the required
|
|
* Streamable HTTP Accept value is kept authoritative.
|
|
*/
|
|
export function mcpHttpRequestHeaders(extra?: Record<string, string>): Record<string, string> {
|
|
return {
|
|
"content-type": "application/json",
|
|
...extra,
|
|
accept: MCP_HTTP_ACCEPT,
|
|
};
|
|
}
|
|
|
|
export class McpHttpInitializationError extends Error {
|
|
constructor(
|
|
message: string,
|
|
readonly stage: "initialize" | "initialized_notification",
|
|
readonly status: number | null,
|
|
readonly response?: Response,
|
|
) {
|
|
super(message);
|
|
this.name = "McpHttpInitializationError";
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Establish a Streamable HTTP session. Callers may retain protocol headers in
|
|
* the in-memory cache scoped to the connection and effective credential identity.
|
|
*/
|
|
export async function initializeMcpHttpSession(input: {
|
|
send: (init: RequestInit) => Promise<Response>;
|
|
headers?: Record<string, string>;
|
|
requestId: string;
|
|
}): Promise<Record<string, string>> {
|
|
const initializeResponse = await input.send({
|
|
method: "POST",
|
|
headers: mcpHttpRequestHeaders(input.headers),
|
|
body: JSON.stringify({
|
|
jsonrpc: "2.0",
|
|
id: `${input.requestId}-initialize`,
|
|
method: "initialize",
|
|
params: {
|
|
protocolVersion: MCP_PROTOCOL_VERSION,
|
|
capabilities: {},
|
|
clientInfo: { name: "paperclip", version: "1" },
|
|
},
|
|
}),
|
|
});
|
|
if (!initializeResponse.ok) {
|
|
throw new McpHttpInitializationError(
|
|
`Remote MCP initialization returned HTTP ${initializeResponse.status}`,
|
|
"initialize",
|
|
initializeResponse.status,
|
|
initializeResponse,
|
|
);
|
|
}
|
|
let payload: unknown;
|
|
try {
|
|
payload = await readMcpHttpResponse(initializeResponse, `${input.requestId}-initialize`);
|
|
} catch {
|
|
throw new McpHttpInitializationError("Remote MCP initialization returned an invalid response", "initialize", null);
|
|
}
|
|
const result = payload && typeof payload === "object" && "result" in payload
|
|
? (payload as { result?: unknown }).result
|
|
: null;
|
|
const resultRecord = result && typeof result === "object" ? result as Record<string, unknown> : null;
|
|
if (!resultRecord) throw new McpHttpInitializationError("Remote MCP initialization failed", "initialize", null);
|
|
const protocolVersion = typeof resultRecord?.protocolVersion === "string" && resultRecord.protocolVersion
|
|
? resultRecord.protocolVersion
|
|
: MCP_PROTOCOL_VERSION;
|
|
const sessionId = initializeResponse.headers.get("mcp-session-id");
|
|
const sessionHeaders: Record<string, string> = {
|
|
...(input.headers ?? {}),
|
|
"MCP-Protocol-Version": protocolVersion,
|
|
...(sessionId ? { "Mcp-Session-Id": sessionId } : {}),
|
|
};
|
|
const initializedResponse = await input.send({
|
|
method: "POST",
|
|
headers: mcpHttpRequestHeaders(sessionHeaders),
|
|
body: JSON.stringify({
|
|
jsonrpc: "2.0",
|
|
method: "notifications/initialized",
|
|
params: {},
|
|
}),
|
|
});
|
|
if (!initializedResponse.ok) {
|
|
throw new McpHttpInitializationError(
|
|
`Remote MCP initialized notification returned HTTP ${initializedResponse.status}`,
|
|
"initialized_notification",
|
|
initializedResponse.status,
|
|
);
|
|
}
|
|
return sessionHeaders;
|
|
}
|
|
|
|
function looksLikeJsonRpcMessage(value: unknown): boolean {
|
|
if (typeof value !== "object" || value === null) return false;
|
|
const record = value as Record<string, unknown>;
|
|
return "result" in record || "error" in record || "method" in record || "id" in record;
|
|
}
|
|
|
|
/**
|
|
* Parse the body of an MCP Streamable HTTP response into its JSON-RPC payload.
|
|
*
|
|
* Handles both response shapes the transport allows:
|
|
* - `application/json`: the body is the JSON-RPC message directly.
|
|
* - `text/event-stream`: one or more SSE events; we return the JSON payload of
|
|
* the first `data:` event that parses as a JSON-RPC message.
|
|
*
|
|
* Falls back to a plain JSON parse when the content type is unknown so we stay
|
|
* compatible with non-compliant servers that ignore the Accept header.
|
|
*/
|
|
export function parseMcpHttpResponseBody(bodyText: string, contentType: string | null): unknown {
|
|
const isEventStream = (contentType ?? "").toLowerCase().includes("text/event-stream");
|
|
if (!isEventStream) {
|
|
return JSON.parse(bodyText) as unknown;
|
|
}
|
|
|
|
// Split the SSE stream into events on blank lines, then collect each event's
|
|
// `data:` lines (which may span multiple lines per the SSE spec).
|
|
const events = bodyText.replace(/\r\n/g, "\n").split(/\n\n+/);
|
|
let lastError: unknown = null;
|
|
let firstParsed: unknown;
|
|
let sawData = false;
|
|
for (const event of events) {
|
|
const dataLines = event
|
|
.split("\n")
|
|
.filter((line) => line.startsWith("data:"))
|
|
.map((line) => line.slice("data:".length).replace(/^ /, ""));
|
|
if (dataLines.length === 0) continue;
|
|
const data = dataLines.join("\n");
|
|
let parsed: unknown;
|
|
try {
|
|
parsed = JSON.parse(data) as unknown;
|
|
} catch (error) {
|
|
lastError = error;
|
|
continue;
|
|
}
|
|
if (!sawData) {
|
|
firstParsed = parsed;
|
|
sawData = true;
|
|
}
|
|
if (looksLikeJsonRpcMessage(parsed)) {
|
|
return parsed;
|
|
}
|
|
}
|
|
if (sawData) return firstParsed;
|
|
if (lastError) throw lastError;
|
|
throw new SyntaxError("MCP SSE response contained no data events");
|
|
}
|
|
|
|
/** Read until the response for this request arrives, without waiting for an SSE
|
|
* connection to close. Notifications and responses for other IDs are ignored. */
|
|
export async function readMcpHttpResponse(
|
|
response: Response,
|
|
requestId: string | number,
|
|
options: { maxBytes?: number; onRequest?: (message: Record<string, unknown>) => Promise<void> } = {},
|
|
): Promise<unknown> {
|
|
const maxBytes = options.maxBytes ?? 8 * 1024 * 1024;
|
|
const isStream = response.headers.get("content-type")?.toLowerCase().includes("text/event-stream");
|
|
const reader = response.body?.getReader();
|
|
// Injected HTTP transports can expose a buffered text response rather than a
|
|
// Web ReadableStream. Keep the same size and message-ID checks for both forms.
|
|
if (!reader) {
|
|
const body = await response.text();
|
|
if (Buffer.byteLength(body, "utf8") > maxBytes) throw new McpHttpResponseError("too_large", "MCP response exceeded the size limit");
|
|
return readMcpHttpResponse(new Response(body, {
|
|
headers: { "content-type": response.headers.get("content-type") ?? "application/json" },
|
|
}), requestId, options);
|
|
}
|
|
const decoder = new TextDecoder();
|
|
let buffer = "";
|
|
let bytes = 0;
|
|
const parse = (text: string): unknown => {
|
|
try { return JSON.parse(text); }
|
|
catch { throw new McpHttpResponseError("invalid_json", "MCP response contained invalid JSON"); }
|
|
};
|
|
const inspect = async (message: unknown): Promise<unknown | undefined> => {
|
|
if (!message || typeof message !== "object") return undefined;
|
|
const record = message as Record<string, unknown>;
|
|
if (record.id === requestId && ("result" in record || "error" in record)) return record;
|
|
if ("method" in record && "id" in record) await options.onRequest?.(record);
|
|
return undefined;
|
|
};
|
|
const event = async (value: string) => {
|
|
const data = value.split("\n").filter((line) => line.startsWith("data:")).map((line) => line.slice(5).replace(/^ /, "")).join("\n");
|
|
if (!data) return undefined;
|
|
return inspect(parse(data));
|
|
};
|
|
try {
|
|
while (true) {
|
|
const { value, done } = await reader.read();
|
|
bytes += value?.byteLength ?? 0;
|
|
if (bytes > maxBytes) throw new McpHttpResponseError("too_large", "MCP response exceeded the size limit");
|
|
buffer += decoder.decode(value, { stream: !done });
|
|
if (isStream) {
|
|
// Normalize CRLF after concatenating chunks, including split CR/LF pairs.
|
|
buffer = buffer.replace(/\r\n/g, "\n");
|
|
let boundary: number;
|
|
while ((boundary = buffer.indexOf("\n\n")) >= 0) {
|
|
const result = await event(buffer.slice(0, boundary));
|
|
buffer = buffer.slice(boundary + 2);
|
|
if (result !== undefined) return result;
|
|
}
|
|
}
|
|
if (done) break;
|
|
}
|
|
const result = isStream ? await event(buffer) : await inspect(parse(buffer));
|
|
if (result !== undefined) return result;
|
|
throw new McpHttpResponseError("malformed_response", "MCP response did not contain the requested message ID");
|
|
} finally {
|
|
await reader.cancel().catch(() => undefined);
|
|
reader.releaseLock();
|
|
}
|
|
}
|
|
|
|
const sessions = new Map<string, { headers: Record<string, string>; expiresAt: number }>();
|
|
const initializing = new Map<string, Promise<Record<string, string>>>();
|
|
const SESSION_TTL_MS = 30 * 60_000;
|
|
|
|
/** Cache only protocol headers; credential hashes and scope separate every
|
|
* connection and effective identity. Never cache a tool call or replay a write. */
|
|
export async function getMcpHttpSession(input: Parameters<typeof initializeMcpHttpSession>[0] & { scope: string }) {
|
|
const key = `${input.scope}:${createHash("sha256").update(JSON.stringify(Object.entries(input.headers ?? {}).sort())).digest("hex")}`;
|
|
const cached = sessions.get(key);
|
|
if (cached && cached.expiresAt > Date.now()) return { ...input.headers, ...cached.headers };
|
|
const pending = initializing.get(key);
|
|
if (pending) return pending;
|
|
const promise = initializeMcpHttpSession(input).then((headers) => {
|
|
if (initializing.get(key) !== promise) throw new Error("MCP connection changed while initializing; reconnect before calling tools");
|
|
for (const [id, value] of sessions) if (value.expiresAt <= Date.now()) sessions.delete(id);
|
|
if (sessions.size >= 1000) sessions.delete(sessions.keys().next().value!);
|
|
const protocolHeaders = Object.fromEntries(Object.entries(headers).filter(([name]) => ["mcp-session-id", "mcp-protocol-version"].includes(name.toLowerCase())));
|
|
sessions.set(key, { headers: protocolHeaders, expiresAt: Date.now() + SESSION_TTL_MS });
|
|
return headers;
|
|
}).finally(() => { if (initializing.get(key) === promise) initializing.delete(key); });
|
|
initializing.set(key, promise);
|
|
return promise;
|
|
}
|
|
|
|
export function forgetMcpHttpSessions(connectionId: string) {
|
|
for (const key of sessions.keys()) if (key.startsWith(`${connectionId}:`)) sessions.delete(key);
|
|
for (const key of initializing.keys()) if (key.startsWith(`${connectionId}:`)) initializing.delete(key);
|
|
}
|