Skip to content

feat(example): add tool list changed notification example#2147

Open
Nishant-Chaudhary5338 wants to merge 3 commits into
modelcontextprotocol:mainfrom
Nishant-Chaudhary5338:example/tool-list-changed-notifications
Open

feat(example): add tool list changed notification example#2147
Nishant-Chaudhary5338 wants to merge 3 commits into
modelcontextprotocol:mainfrom
Nishant-Chaudhary5338:example/tool-list-changed-notifications

Conversation

@Nishant-Chaudhary5338
Copy link
Copy Markdown

Summary

Adds a self-contained, runnable example that demonstrates the complete notifications/tools/list_changed flow end-to-end.

Closes #1132


What this adds

examples/client/src/toolListChanged.ts (~115 lines)

A single file that:

  1. Creates an McpServer pre-loaded with one tool (get_weather)
  2. Creates a Client with listChanged.tools.onChanged registered
  3. Connects both via InMemoryTransport.createLinkedPair()no networking, no spawned process
  4. After 500 ms, calls server.registerTool('get_forecast', ...)McpServer internally calls sendToolListChanged() on all connected sessions
  5. Client receives notifications/tools/list_changed, re-fetches the tool list, and logs both tools

Expected output:

[server] started with 1 tool: get_weather
[client] connected. initial tools: [ 'get_weather' ]
[server] registering new tool: get_forecast
[client] tool list changed — updated tools: [ 'get_weather', 'get_forecast' ]

Run it:

pnpm --filter @modelcontextprotocol/examples-client exec tsx src/toolListChanged.ts

Why InMemoryTransport

Previous attempts at addressing #1132 added documentation. A runnable file is stronger evidence — reviewers and users can execute it and see the notification lifecycle in real time. InMemoryTransport keeps the example zero-dependency (no server process, no port binding, works anywhere the SDK's test suite runs).

Key implementation notes

  • debounceMs: 0 — the default 300 ms debounce is correct for production (coalesces rapid registrations) but makes example output non-deterministic. Setting it to 0 is documented with a comment.
  • changedOnce promise — gates teardown on the callback firing rather than a fixed sleep, so the example is deterministic regardless of host speed.
  • tsconfig.json paths — added source-level path mappings for @modelcontextprotocol/server (matching the existing pattern for @modelcontextprotocol/client) so tsgo resolves to source during the pre-push typecheck, which runs in parallel with the build job.

Checks

  • pnpm --filter @modelcontextprotocol/examples-client run check (typecheck + lint + prettier) ✅
  • pnpm -r typecheck (all 13 packages) ✅
  • Example runs and produces the expected 4-line output ✅

Note on pre-push hook: The Build job in the hook fails with SIGABRT in packages/middleware/node — reproduced on main with no changes, so it is a pre-existing environment issue unrelated to this PR. Typecheck and Lint both pass.

Nishant Chaudhary and others added 3 commits May 23, 2026 16:37
Adds a self-contained runnable example that demonstrates the complete
`notifications/tools/list_changed` flow end-to-end.

- Uses `InMemoryTransport.createLinkedPair()` so no networking is required
- Server starts with `get_weather`, client connects and lists initial tools
- After 500 ms, server registers `get_forecast`; `McpServer.registerTool()`
  auto-fires `sendToolListChanged()` on connected instances
- Client receives the notification via `listChanged.tools.onChanged`, re-
  fetches the tool list, and logs both tools
- Uses `changedOnce` promise to gate on the callback rather than a blind
  sleep, so the test is deterministic regardless of host speed
- Sets `debounceMs: 0` to disable the 300 ms default debounce and explains
  why in a comment

Also adds `@modelcontextprotocol/core` and `@modelcontextprotocol/server`
to `examples/client` dependencies (InMemoryTransport lives in core; McpServer
in server) and adds one row to the example index README.

Closes modelcontextprotocol#1132

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
…rver

The pre-push hook runs typecheck and build in parallel. Without source-level
path mappings for the server package, tsgo resolves to dist/ files that may
not exist yet when typecheck runs. Add the same path-mapping pattern that
already exists for @modelcontextprotocol/client so typecheck always resolves
to source.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
@Nishant-Chaudhary5338 Nishant-Chaudhary5338 requested a review from a team as a code owner May 23, 2026 11:14
@changeset-bot
Copy link
Copy Markdown

changeset-bot Bot commented May 23, 2026

⚠️ No Changeset found

Latest commit: 3565b3b

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

This PR includes no changesets

When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

Click here to learn what changesets are, and how to add one.

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

@pkg-pr-new
Copy link
Copy Markdown

pkg-pr-new Bot commented May 23, 2026

Open in StackBlitz

@modelcontextprotocol/client

npm i https://pkg.pr.new/modelcontextprotocol/typescript-sdk/@modelcontextprotocol/client@2147

@modelcontextprotocol/codemod

npm i https://pkg.pr.new/modelcontextprotocol/typescript-sdk/@modelcontextprotocol/codemod@2147

@modelcontextprotocol/server

npm i https://pkg.pr.new/modelcontextprotocol/typescript-sdk/@modelcontextprotocol/server@2147

@modelcontextprotocol/express

npm i https://pkg.pr.new/modelcontextprotocol/typescript-sdk/@modelcontextprotocol/express@2147

@modelcontextprotocol/fastify

npm i https://pkg.pr.new/modelcontextprotocol/typescript-sdk/@modelcontextprotocol/fastify@2147

@modelcontextprotocol/hono

npm i https://pkg.pr.new/modelcontextprotocol/typescript-sdk/@modelcontextprotocol/hono@2147

@modelcontextprotocol/node

npm i https://pkg.pr.new/modelcontextprotocol/typescript-sdk/@modelcontextprotocol/node@2147

commit: 3565b3b

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Question: Tool list changed notification

1 participant