Context7 Platform -- Up-to-date docs for AI agents
CTX7-3074: Close empty MCP subscriptions instead of refusing them (#3330)
Enes Gulesafdd129df66f
diff --git a/.changeset/listen-complete-not-refused.md b/.changeset/listen-complete-not-refused.mdnew file mode 100644index 00000000..36799000--- /dev/null+++ b/.changeset/listen-complete-not-refused.md@@ -0,0 +1,5 @@+---+"@upstash/context7-mcp": patch+---++Over HTTP, answer `subscriptions/listen` with an acknowledgement and an immediate `complete` result instead of a "Subscription limit reached" error, and stop logging those refusals. Context7 has no change notifications, so the stream closes at once. Stdio subscriptions stay disabled. Removes the `MCP_MAX_SUBSCRIPTIONS` setting.diff --git a/.env.example b/.env.exampleindex 08b3b892..664c7c00 100644--- a/.env.example+++ b/.env.example@@ -17,10 +17,6 @@ VERCEL_MARKETPLACE_OIDC_AUDIENCE=# Required for hosted HTTP deployments. Must match context7.com.MCP_CLIENT_IP_ASSERTION_KEY=-# Maximum concurrent MCP notification subscriptions per process. Context7's-# advertised capabilities are static, so subscriptions are disabled by default.-MCP_MAX_SUBSCRIPTIONS=0-# Network / certificatesHTTPS_PROXY=NODE_EXTRA_CA_CERTS=diff --git a/packages/mcp/package.json b/packages/mcp/package.jsonindex 7d0f52e2..ea523503 100644--- a/packages/mcp/package.json+++ b/packages/mcp/package.json@@ -46,8 +46,8 @@},"homepage": "https://github.com/upstash/context7#readme","dependencies": {- "@modelcontextprotocol/node": "2.0.0",- "@modelcontextprotocol/server": "2.0.0",+ "@modelcontextprotocol/node": "2.1.0",+ "@modelcontextprotocol/server": "2.2.0","@opentelemetry/api": "^1.9.1","@opentelemetry/exporter-prometheus": "^0.222.0","@opentelemetry/instrumentation-runtime-node": "^0.35.0",@@ -61,7 +61,7 @@"zod": "^4.4.3"},"devDependencies": {- "@modelcontextprotocol/client": "2.0.0",+ "@modelcontextprotocol/client": "2.2.0","@opentelemetry/context-async-hooks": "^2.11.0","@opentelemetry/core": "^2.11.0","@opentelemetry/sdk-trace-base": "^2.11.0",diff --git a/packages/mcp/src/index.ts b/packages/mcp/src/index.tsindex ff82d09f..8ece8ee3 100644--- a/packages/mcp/src/index.ts+++ b/packages/mcp/src/index.ts@@ -32,7 +32,6 @@ import {import { maybeElicitAuthSignIn } from "./lib/auth/auth-prompt.js";import { QUERY_DOCS_TOOL, RESOLVE_LIBRARY_ID_TOOL } from "./lib/tool-names.js";import { installProcessShutdown } from "./lib/process-shutdown.js";-import { getMaxSubscriptions } from "./lib/subscriptions.js";import {forceFlushTelemetry,initializeTelemetry,@@ -433,7 +432,6 @@ async function main() {// go idle and the gateway reaps them at streamIdleTimeout (300s).const rawMcpHandler = createMcpHandler((mcpContext) => createMcpServer(mcpContext), {keepAliveMs: 0,- maxSubscriptions: getMaxSubscriptions(),onerror: (error) => console.error("MCP handler error:", error),});const mcpHandler = mcpInstrumentation@@ -703,7 +701,8 @@ async function main() {},{transport: stdioTransport,- maxSubscriptions: getMaxSubscriptions(),+ // The SDK's stdio router keeps a subscription that honors nothing open.+ maxSubscriptions: 0,onerror: (error) => console.error("MCP stdio error:", error),});diff --git a/packages/mcp/src/lib/subscriptions.ts b/packages/mcp/src/lib/subscriptions.tsdeleted file mode 100644index d99f4d31..00000000--- a/packages/mcp/src/lib/subscriptions.ts+++ /dev/null@@ -1,13 +0,0 @@-// Context7's tools, prompts, and resources are static for the process lifetime,-// so notification subscriptions provide no useful events.-export const DEFAULT_MAX_SUBSCRIPTIONS = 0;--export function getMaxSubscriptions(value = process.env.MCP_MAX_SUBSCRIPTIONS): number {- if (value === undefined) return DEFAULT_MAX_SUBSCRIPTIONS;-- const parsed = Number(value);- if (Number.isSafeInteger(parsed) && parsed >= 0) return parsed;-- console.warn(`Invalid MCP_MAX_SUBSCRIPTIONS; using the default of ${DEFAULT_MAX_SUBSCRIPTIONS}.`);- return DEFAULT_MAX_SUBSCRIPTIONS;-}diff --git a/packages/mcp/test/mcp-subscription-telemetry.test.ts b/packages/mcp/test/mcp-subscription-telemetry.test.tsindex a55c0244..cb9fc7f8 100644--- a/packages/mcp/test/mcp-subscription-telemetry.test.ts+++ b/packages/mcp/test/mcp-subscription-telemetry.test.ts@@ -206,7 +206,9 @@ function createInstrumentedHttpHandler(maxSubscriptions = 4) {(requestContext) =>new InstrumentedMcpServer({ name: "subscription-http-test", version: "1.0.0" },- {},+ // The SDK closes a listen stream that honors nothing, so the capacity+ // test needs a capability its streams can hold open.+ { capabilities: { tools: { listChanged: true } } },requestContext),{ keepAliveMs: 0, maxSubscriptions, onerror: () => undefined }@@ -222,7 +224,7 @@ describe("MCP v2 subscription telemetry", () => {const beforeCancelled = subscriptionDurationCount("cancelled", "anonymous");const handler = createInstrumentedHttpHandler();const abort = new AbortController();- const message = modernListenRequest(101);+ const message = modernListenRequest(101, { toolsListChanged: true });const response = await handler.fetch(modernHttpRequest("subscriptions/listen", abort.signal), {parsedBody: message,@@ -263,14 +265,14 @@ describe("MCP v2 subscription telemetry", () => {expect(await invalidResponse.json()).toMatchObject({ error: { code: -32602 }, id: 102 });const abort = new AbortController();- const accepted = modernListenRequest(103);+ const accepted = modernListenRequest(103, { toolsListChanged: true });const acceptedResponse = await handler.fetch(modernHttpRequest("subscriptions/listen", abort.signal),{ parsedBody: accepted });expect(acceptedResponse.headers.get("content-type")).toContain("text/event-stream");- const rejected = modernListenRequest(104);+ const rejected = modernListenRequest(104, { toolsListChanged: true });const rejectedResponse = await handler.fetch(modernHttpRequest("subscriptions/listen"), {parsedBody: rejected,});@@ -303,7 +305,7 @@ describe("MCP v2 subscription telemetry", () => {const handler = createInstrumentedHttpHandler();const response = await handler.fetch(modernHttpRequest("subscriptions/listen"), {- parsedBody: modernListenRequest(105),+ parsedBody: modernListenRequest(105, { toolsListChanged: true }),});expect(response.body).not.toBeNull();await metricProvider.forceFlush();@@ -315,6 +317,26 @@ describe("MCP v2 subscription telemetry", () => {expect(subscriptionDurationCount("completed", "anonymous")).toBe(beforeCompleted + 1);});+ test("completes an HTTP subscription that honors nothing right after the acknowledgement", async () => {+ await metricProvider.forceFlush();+ const beforeActive = activeSubscriptionCount("anonymous");+ const beforeCompleted = subscriptionDurationCount("completed", "anonymous");+ const handler = createInstrumentedHttpHandler();++ // Context7 advertises no listChanged, so production listens look like this one.+ const response = await handler.fetch(modernHttpRequest("subscriptions/listen"), {+ parsedBody: modernListenRequest(106, { promptsListChanged: true }),+ });+ const body = await response.text();+ expect(body).toContain('"notifications":{}');+ expect(body).toContain('"resultType":"complete"');++ await metricProvider.forceFlush();+ expect(activeSubscriptionCount("anonymous")).toBe(beforeActive);+ expect(subscriptionDurationCount("completed", "anonymous")).toBe(beforeCompleted + 1);+ await handler.close();+ });+test("delegates ordinary HTTP operations without double counting them", async () => {await metricProvider.forceFlush();const before = operationCount("tools/list");diff --git a/packages/mcp/test/subscriptions.test.ts b/packages/mcp/test/subscriptions.test.tsdeleted file mode 100644index 9127b0a5..00000000--- a/packages/mcp/test/subscriptions.test.ts+++ /dev/null@@ -1,25 +0,0 @@-import { afterEach, describe, expect, test, vi } from "vitest";-import { DEFAULT_MAX_SUBSCRIPTIONS, getMaxSubscriptions } from "../src/lib/subscriptions.js";--describe("getMaxSubscriptions", () => {- afterEach(() => {- vi.restoreAllMocks();- });-- test("disables subscriptions by default", () => {- expect(getMaxSubscriptions(undefined)).toBe(DEFAULT_MAX_SUBSCRIPTIONS);- expect(DEFAULT_MAX_SUBSCRIPTIONS).toBe(0);- });-- test("accepts non-negative integer overrides", () => {- expect(getMaxSubscriptions("0")).toBe(0);- expect(getMaxSubscriptions("8192")).toBe(8_192);- });-- test.each(["-1", "1.5", "invalid", "Infinity"])("falls back for invalid value %s", (value) => {- const warn = vi.spyOn(console, "warn").mockImplementation(() => {});-- expect(getMaxSubscriptions(value)).toBe(DEFAULT_MAX_SUBSCRIPTIONS);- expect(warn).toHaveBeenCalledOnce();- });-});diff --git a/pnpm-lock.yaml b/pnpm-lock.yamlindex 9d35d1bc..d78c4c5d 100644--- a/pnpm-lock.yaml+++ b/pnpm-lock.yaml@@ -149,11 +149,11 @@ importers:packages/mcp:dependencies:'@modelcontextprotocol/node':- specifier: 2.0.0- version: 2.0.0(@modelcontextprotocol/[email protected])([email protected])+ specifier: 2.1.0+ version: 2.1.0(@modelcontextprotocol/[email protected])([email protected])'@modelcontextprotocol/server':- specifier: 2.0.0- version: 2.0.0+ specifier: 2.2.0+ version: 2.2.0'@opentelemetry/api':specifier: ^1.9.1version: 1.9.1@@ -189,8 +189,8 @@ importers:version: 4.4.3devDependencies:'@modelcontextprotocol/client':- specifier: 2.0.0- version: 2.0.0+ specifier: 2.2.0+ version: 2.2.0'@opentelemetry/context-async-hooks':specifier: ^2.11.0version: 2.11.0(@opentelemetry/[email protected])@@ -1380,19 +1380,19 @@ packages:resolution: {integrity: sha512-ABnA53mdfkGZwOFUdZNv2S0CWGO/EIuPj8Vv9xmBFmSYg/qFc7ihO6q5FcQjvoE67kZpWkEc4AhD6B/os04yuA==}engines: {node: '>= 10'}- '@modelcontextprotocol/[email protected]':- resolution: {integrity: sha512-8f1OghQ2rjzIOfqgUCP+8GiUWqRs89njoWLNqAe8kWmDePv3s1fZXseej+QXemssEuuOvLLmLO/kqM3IQHtISw==}+ '@modelcontextprotocol/[email protected]':+ resolution: {integrity: sha512-LxCou/CSYQ6dwEnjhLZY0KnEuc8V4UJ3IEQCl/uR2yHobSQIEdJKUlKLRYRF5i5FqRGHy1eK7une3aAWDHLtig==}engines: {node: '>=20'}- '@modelcontextprotocol/[email protected]':- resolution: {integrity: sha512-pJCEwGG7Lfr/+PQp9ZTwKXNeO5wzbfKL7H3MYpCorM4oFBoQrdjnBgEoqG+RjhsvS1FKrDbKux+M1HhlnGWqcA==}+ '@modelcontextprotocol/[email protected]':+ resolution: {integrity: sha512-iLhmprRmWI8EcosOA3wVvww22z02NkgqhV4fBH6f/odQBsi7xnJc0HGmi21yWO+/iKw2yzqVE127Zhna5/+JXw==}engines: {node: '>=20'}- '@modelcontextprotocol/[email protected]':- resolution: {integrity: sha512-Y4hAC2XdGDUdDOCbLDOCA4+aL3NUldjsOWlDL/YwpAxrPhRm1xHd7lZ+mLacvZ9t3PaH28wgNoaLQGrIk1P2pg==}+ '@modelcontextprotocol/[email protected]':+ resolution: {integrity: sha512-6xg3iWVcOfiL8Y7rI6xUqXD0lI2AY5q3djsa/cOi1IJUSwx06t0gvhSwU/6mIIohrFlt40/ANUG56DQ0HLhILQ==}engines: {node: '>=20'}peerDependencies:- '@modelcontextprotocol/server': ^2.0.0+ '@modelcontextprotocol/server': ^2.1.0hono: ^4.11.4peerDependenciesMeta:hono:@@ -1408,8 +1408,8 @@ packages:'@cfworker/json-schema':optional: true- '@modelcontextprotocol/[email protected]':- resolution: {integrity: sha512-YhHWdHfpFMQfd0prsEnxKeS3Qz3ytIGmsS0sth4KDjnacIT7hxk6hXHkJ9KysxlkvTM+WZAtQbbcUhdoP4Hvtw==}+ '@modelcontextprotocol/[email protected]':+ resolution: {integrity: sha512-qFnltjus6Gk8Lx6J1w2RXmr3YPN22MNT6qD6zjAkaiWIlFIFxWClz0rXHNuJF/KSUyn5YGhkSvXkWDbQHVLFEQ==}engines: {node: '>=20'}'@msgpackr-extract/[email protected]':@@ -4965,9 +4965,9 @@ snapshots:'@mariozechner/clipboard-win32-x64-msvc': 0.3.9optional: true- '@modelcontextprotocol/[email protected]':+ '@modelcontextprotocol/[email protected]':dependencies:- '@modelcontextprotocol/core': 2.0.0+ '@modelcontextprotocol/core': 2.2.0cross-spawn: 7.0.6eventsource: 3.0.7eventsource-parser: 3.0.6@@ -4975,14 +4975,14 @@ snapshots:pkce-challenge: 5.0.0zod: 4.4.3- '@modelcontextprotocol/[email protected]':+ '@modelcontextprotocol/[email protected]':dependencies:zod: 4.4.3- '@modelcontextprotocol/[email protected](@modelcontextprotocol/[email protected])([email protected])':+ '@modelcontextprotocol/[email protected](@modelcontextprotocol/[email protected])([email protected])':dependencies:'@hono/node-server': 1.19.14([email protected])- '@modelcontextprotocol/server': 2.0.0+ '@modelcontextprotocol/server': 2.2.0optionalDependencies:hono: 4.12.23@@ -5009,9 +5009,9 @@ snapshots:- supports-coloroptional: true- '@modelcontextprotocol/[email protected]':+ '@modelcontextprotocol/[email protected]':dependencies:- '@modelcontextprotocol/core': 2.0.0+ '@modelcontextprotocol/core': 2.2.0zod: 4.4.3'@msgpackr-extract/[email protected]':