Skip to content

fix(client): forward listTools/listPrompts/listResources when not connected - #3198

Open
Muzaqil555 wants to merge 3 commits into
modelcontextprotocol:mainfrom
Muzaqil555:fix/issue-3195
Open

Muzaqil555 wants to merge 3 commits into
modelcontextprotocol:mainfrom
Muzaqil555:fix/issue-3195

Conversation

@Muzaqil555

Copy link
Copy Markdown

Summary

Fixes #3195.

Previously, Client.listPrompts, Client.listResources, Client.listResourceTemplates, and Client.listTools in packages/client/src/client/client.ts checked !this._serverCapabilities?.prompts / .resources / .tools (and checked this.responseCacheStore) before verifying this.transport. When connect() had not been called or after close() cleared this.transport, _serverCapabilities is undefined, causing those methods to resolve to empty lists ({ tools: [] }, etc.) instead of rejecting with Error('Not connected').

Changes

  • packages/client/src/client/client.ts: Added an early if (!this.transport) check in listPrompts, listResources, listResourceTemplates, and listTools that delegates to this.request(...) (which rejects with Not connected) ahead of capability short-circuits and response caching.
  • packages/client/test/client/listNotConnected.test.ts: Added 4 regression tests covering disconnected calls before connect(), after close(), connected calls when capabilities omit those features (preserving empty-list behavior), and disconnected calls with a configured InMemoryResponseCacheStore.
  • test/e2e/requirements.ts: Removed 'typescript:protocol:error:not-connected' from knownFailures.

Testing

  • pnpm --filter @modelcontextprotocol/client run typecheck — passed
  • pnpm --filter @modelcontextprotocol/client run lint — passed
  • pnpm --filter @modelcontextprotocol/client run build — passed
  • pnpm --filter @modelcontextprotocol/client run test — 45 test files passed (45), 1082 tests passed (1082)
  • test/e2e: pnpm exec vitest run scenarios/protocol.test.ts -t "typescript:protocol:error:not-connected" — 12 passed | 396 skipped (408)
  • pnpm exec prettier --check on all 3 touched files — passed

@Muzaqil555
Muzaqil555 requested a review from a team as a code owner November 18, 2026 05:31
@changeset-bot

changeset-bot Bot commented Nov 18, 2026 •

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: 7405500

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 6 packages
Name Type
@modelcontextprotocol/client Patch
@modelcontextprotocol/codemod Patch
@modelcontextprotocol/core Patch
@modelcontextprotocol/server-legacy Patch
@modelcontextprotocol/server Patch
@modelcontextprotocol/core-internal Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@pkg-pr-new

pkg-pr-new Bot commented Nov 18, 2026 •

Copy link
Copy Markdown

Open in StackBlitz

@modelcontextprotocol/client

npm i https://pkg.pr.new/@modelcontextprotocol/client@3198

@modelcontextprotocol/codemod

npm i https://pkg.pr.new/@modelcontextprotocol/codemod@3198

@modelcontextprotocol/core

npm i https://pkg.pr.new/@modelcontextprotocol/core@3198

@modelcontextprotocol/server

npm i https://pkg.pr.new/@modelcontextprotocol/server@3198

@modelcontextprotocol/server-legacy

npm i https://pkg.pr.new/@modelcontextprotocol/server-legacy@3198

@modelcontextprotocol/express

npm i https://pkg.pr.new/@modelcontextprotocol/express@3198

@modelcontextprotocol/fastify

npm i https://pkg.pr.new/@modelcontextprotocol/fastify@3198

@modelcontextprotocol/hono

npm i https://pkg.pr.new/@modelcontextprotocol/hono@3198

@modelcontextprotocol/node

npm i https://pkg.pr.new/@modelcontextprotocol/node@3198

commit: 7405500

@claude claude Bot added the v2 Ideas, requests and plans for v2 of the SDK which will incorporate major changes and fixes label Nov 18, 2026
@prkomb

prkomb commented Nov 18, 2026

Copy link
Copy Markdown

Thanks for picking this up. Going through request() when there is no transport gives the same Not connected rejection as the other request methods, and it skips the cache read as well.

The build check fails on the client typecheck:

test/client/listNotConnected.test.ts(2,27): error TS2307: Cannot find module '@modelcontextprotocol/server' or its corresponding type declarations.

The client package doesn't depend on @modelcontextprotocol/server, and the build job runs check:all (which typechecks) before build:all. Locally the import only resolves through the workspace root's link once packages/server/dist exists; with that folder moved aside I get the same TS2307. The other client tests avoid this with a scripted server on a raw InMemoryTransport pair (connectedPair() in responseCacheCodec.test.ts: "the client package's tests must not depend on @modelcontextprotocol/server"), so these tests need one too, answering with the capabilities each test needs.

Two smaller points:

  • It needs a changeset for @modelcontextprotocol/client. A caller that awaited an empty list before connect() or after close() now gets a rejection, and point 4 of the repo's CLAUDE.md asks the changeset to say what breaks in a case like this.
  • The cache test passes without the early return too. close() resets the cache's server identity (resetForReconnect() sets it to ''), so the entry written while connected is not read in this sequence. I replaced the early return in listTools with if (this.transport && !this._serverCapabilities?.tools && ...) on the capability check, so a closed client reaches the cache lookup before request() rejects, and all four tests still passed. It still checks the after-close rejection with a store configured, but it doesn't pin the ordering its name suggests.

With the tests off the server package and a changeset, the fix itself looks right to me.

@Muzaqil555

Copy link
Copy Markdown
Author

Thanks @prkomb for the detailed review! Addressed all three points in commit 367b706:

  • Removed @modelcontextprotocol/server from client tests: Rewrote packages/client/test/client/listNotConnected.test.ts to use a lightweight scripted InMemoryTransport pair (connectedPair()), and verified pnpm -r typecheck passes across all 54 workspace projects with no packages/*/dist build artifacts present, followed by pnpm run build:all.
  • Pinned cache-lookup ordering: Updated the disconnect/cache tests with a store.get spy and seeded pre-connect/post-reset (['', '']) plus remote server.close() cases across listTools, listPrompts, listResources, and listResourceTemplates so the tests verify Not connected is rejected before consulting ResponseCacheStore.
  • Added changeset: Added .changeset/list-not-connected-rejects.md for @modelcontextprotocol/client documenting the breaking change per CLAUDE.md.

Verified locally after final edit: @modelcontextprotocol/client (45 files, 1084 tests passed), listNotConnected.test.ts (6/6), not-connected e2e (12 passed), eslint & prettier --check, pnpm changeset status, and pnpm run build:all.

@prkomb

prkomb commented Nov 19, 2026

Copy link
Copy Markdown

Checked 367b706 on top of current main (5ee54ee3): the client tests no longer import the server package, there's a changeset, and the ordering is pinned now. With the four early returns removed and the capability checks changed to if (this.transport && ...), all three cache tests fail at their first listTools() assertion, which resolves from the cache instead of rejecting. The new tests and the client suite pass on the merge.

One wording point in the changeset. When the server closes the connection, _resetConnectionState() doesn't run, so the default store keeps its entries. In that state readResource() for a URI read earlier with a positive ttlMs still resolves from the cache with no request sent, on main and with this PR, so "matching all other Client request methods" isn't quite right. The "Previously" sentence is a bit broad for the same reason: before this PR, listTools() after a server-side close served a still-fresh cached list or rejected with Not connected; the empty list was the before-connect() and after-close() case. (Checked with an in-memory pair and the default store.) Whether readResource() should also reject there is a separate question, so I'd just narrow the wording here.

@Muzaqil555

Copy link
Copy Markdown
Author

Updated .changeset/list-not-connected-rejects.md\ per your note to clarify the before-\connect()/after-\close()\ vs. server-side-disconnect behavior and drop the overly broad comparison to all other \Client\ request methods.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

v2 Ideas, requests and plans for v2 of the SDK which will incorporate major changes and fixes

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[v2] After client.close(), listTools() and the other list methods resolve empty instead of rejecting "Not connected"

2 participants