Skip to content

YC (S26) | Open Computer History | Continuously record your company computer work, map your workflows, help you find work worth automating, and power your agents' context

fix: authenticate HTTP MCP upstream requests (#7336)

Louis Beaumont336b419e7e63
diff --git a/packages/screenpipe-mcp/README.md b/packages/screenpipe-mcp/README.md
index a86256601..eaf88e010 100644
--- a/packages/screenpipe-mcp/README.md
+++ b/packages/screenpipe-mcp/README.md
@@ -141,6 +141,17 @@ The server exposes:
| `--listen-on-lan` | Bind `0.0.0.0` so other devices on the LAN can connect. Requires `--api-key`. | off (binds `127.0.0.1`) |
| `--api-key <secret>` | Bearer token required for non-loopback requests (`Authorization: Bearer <secret>`). Loopback always allowed. | none |
+When the Screenpipe API requires authentication, set `SCREENPIPE_LOCAL_API_KEY`
+in the HTTP server's environment (`SCREENPIPE_API_KEY` is the fallback). Obtain
+it with `screenpipe auth token`, and keep it out of client configuration and
+chat. The server uses this key for upstream API requests, including retrieval
+telemetry. `--api-key` is a separate credential for callers connecting to MCP;
+it is never forwarded to the Screenpipe API.
+
+```bash
+SCREENPIPE_LOCAL_API_KEY="$(screenpipe auth token)" npx -y screenpipe-mcp --http
+```
+
**Connecting a remote MCP client:**
Point any MCP client that supports HTTP transport at the `/mcp` endpoint:
diff --git a/packages/screenpipe-mcp/src/http-server.test.ts b/packages/screenpipe-mcp/src/http-server.test.ts
index d3420ac34..6a34a5fb2 100644
--- a/packages/screenpipe-mcp/src/http-server.test.ts
+++ b/packages/screenpipe-mcp/src/http-server.test.ts
@@ -185,6 +185,67 @@ describe("isAuthorized", () => {
});
describe("buildHttpServer", () => {
+ it.each([
+ { name: "local API key", localKey: "upstream-secret", apiKey: "other-key", succeeds: true },
+ { name: "API key fallback", localKey: "", apiKey: "upstream-secret", succeeds: true },
+ { name: "missing upstream key", localKey: "", apiKey: "", succeeds: false },
+ { name: "invalid upstream key", localKey: "wrong-key", apiKey: "", succeeds: false },
+ ])("authenticates upstream independently of the MCP bearer: $name", async ({ localKey, apiKey, succeeds }) => {
+ const requests: Array<{ path: string; authorization: string | undefined }> = [];
+ const api = createServer((request, response) => {
+ requests.push({ path: request.url!, authorization: request.headers.authorization });
+ response.setHeader("content-type", "application/json");
+ if (request.headers.authorization !== "Bearer upstream-secret") {
+ response.writeHead(403).end(JSON.stringify({ error: "unauthorized" }));
+ return;
+ }
+ response.end(JSON.stringify({
+ data: [{ type: "OCR", content: { text: "synthetic authenticated result" } }],
+ pagination: { total: 1 },
+ }));
+ });
+ await new Promise<void>(resolve => api.listen(0, "127.0.0.1", resolve));
+ const apiPort = (api.address() as { port: number }).port;
+ vi.stubEnv("SCREENPIPE_API_URL", `http://127.0.0.1:${apiPort}`);
+ vi.stubEnv("SCREENPIPE_LOCAL_API_KEY", localKey);
+ vi.stubEnv("SCREENPIPE_API_KEY", apiKey);
+ const server = buildHttpServer({
+ mcpPort: 0,
+ screenpipePort: apiPort,
+ host: "127.0.0.1",
+ apiKey: "mcp-client-secret",
+ });
+ const client = new Client({ name: "integration-test", version: "1.0.0" });
+ try {
+ await new Promise<void>(resolve => server.listen(0, "127.0.0.1", resolve));
+ const url = new URL(`http://127.0.0.1:${(server.address() as { port: number }).port}/mcp`);
+ await client.connect(new StreamableHTTPClientTransport(url, {
+ requestInit: { headers: { authorization: "Bearer mcp-client-secret" } },
+ }));
+ const call = client.callTool({ name: "search_content", arguments: { q: "fixture", limit: 1 } });
+ if (succeeds) {
+ const result = await call;
+ expect(result.isError).not.toBe(true);
+ expect(result.content).toEqual([expect.objectContaining({
+ type: "text", text: expect.stringContaining("synthetic authenticated result"),
+ })]);
+ await vi.waitFor(() => expect(requests.some(request => request.path === "/internal/telemetry/mcp-value")).toBe(true));
+ expect(requests.every(request => request.authorization === "Bearer upstream-secret")).toBe(true);
+ } else {
+ await expect(call).rejects.toThrow("HTTP error: 403");
+ expect(requests).toHaveLength(1);
+ }
+ expect(requests[0].path).toBe("/search?q=fixture&limit=1");
+ expect(requests.some(request => request.authorization === "Bearer mcp-client-secret")).toBe(false);
+ } finally {
+ await client.close();
+ server.closeAllConnections();
+ api.closeAllConnections();
+ await Promise.all([server, api].map(s => new Promise<void>(resolve => s.close(() => resolve()))));
+ vi.unstubAllEnvs();
+ }
+ });
+
it("attributes retrievals per HTTP session and excludes empty or failed searches", async () => {
const searches: Array<{ source: unknown; agent: unknown }> = [];
const outcomes: unknown[] = [];
diff --git a/packages/screenpipe-mcp/src/http-server.ts b/packages/screenpipe-mcp/src/http-server.ts
index 481d02b58..9f1ae82d2 100644
--- a/packages/screenpipe-mcp/src/http-server.ts
+++ b/packages/screenpipe-mcp/src/http-server.ts
@@ -201,11 +201,13 @@ function makeFetchAPI(screenpipePort: number, client: () => McpClient) {
/\/+$/,
"",
);
+ const apiKey = process.env.SCREENPIPE_LOCAL_API_KEY || process.env.SCREENPIPE_API_KEY;
return async (endpoint: string, options: RequestInit = {}): Promise<Response> =>
fetch(`${base}${endpoint}`, {
...options,
headers: {
"Content-Type": "application/json",
+ ...(apiKey ? { Authorization: `Bearer ${apiKey}` } : {}),
"x-screenpipe-client": "mcp",
"x-screenpipe-agent": client(),
...options.headers,