feat(example): add tool list changed notification example#2147
Open
Nishant-Chaudhary5338 wants to merge 3 commits into
Open
Conversation
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>
|
@modelcontextprotocol/client
@modelcontextprotocol/codemod
@modelcontextprotocol/server
@modelcontextprotocol/express
@modelcontextprotocol/fastify
@modelcontextprotocol/hono
@modelcontextprotocol/node
commit: |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Adds a self-contained, runnable example that demonstrates the complete
notifications/tools/list_changedflow end-to-end.Closes #1132
What this adds
examples/client/src/toolListChanged.ts(~115 lines)A single file that:
McpServerpre-loaded with one tool (get_weather)ClientwithlistChanged.tools.onChangedregisteredInMemoryTransport.createLinkedPair()— no networking, no spawned processserver.registerTool('get_forecast', ...)—McpServerinternally callssendToolListChanged()on all connected sessionsnotifications/tools/list_changed, re-fetches the tool list, and logs both toolsExpected output:
Run it:
pnpm --filter @modelcontextprotocol/examples-client exec tsx src/toolListChanged.tsWhy
InMemoryTransportPrevious 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.
InMemoryTransportkeeps 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 to0is documented with a comment.changedOncepromise — gates teardown on the callback firing rather than a fixed sleep, so the example is deterministic regardless of host speed.tsconfig.jsonpaths — added source-level path mappings for@modelcontextprotocol/server(matching the existing pattern for@modelcontextprotocol/client) sotsgoresolves 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) ✅