Skip to content

feat(examples): multi-server chatbot with tool routing (closes #740)#2148

Open
Nishant-Chaudhary5338 wants to merge 1 commit into
modelcontextprotocol:mainfrom
Nishant-Chaudhary5338:example/multi-server-chatbot
Open

feat(examples): multi-server chatbot with tool routing (closes #740)#2148
Nishant-Chaudhary5338 wants to merge 1 commit into
modelcontextprotocol:mainfrom
Nishant-Chaudhary5338:example/multi-server-chatbot

Conversation

@Nishant-Chaudhary5338
Copy link
Copy Markdown

Summary

Adds a runnable multi-server chatbot example showing how one MCP client connects to multiple servers simultaneously and routes Claude's tool calls to the correct server.

Closes #740

What this adds

  • examples/server/src/weatherServer.ts — stateless Streamable HTTP server on :3001 with get_weather and get_forecast tools
  • examples/server/src/mathServer.ts — stateless Streamable HTTP server on :3002 with add, multiply, and convert_temperature tools
  • examples/client/src/multiServerChatbot.ts — connects to both servers, builds a Map<toolName, Client> routing table, runs an interactive Anthropic SDK agentic chat loop, and dispatches parallel tool calls to the correct server

How it works

Client ──► Map<toolName, Client> ──► weather-server (:3001)
                                  └► math-server   (:3002)
  1. On startup the client calls listTools() on each server and populates the routing table
  2. For every Anthropic tool_use block, the client looks up the owning server in the map and dispatches the call — no hardcoded routing, no tool-name prefixes needed
  3. Multiple tool calls in a single response are dispatched in parallel via Promise.all

How to run

Terminal 1:

pnpm --filter @modelcontextprotocol/examples-server exec tsx src/weatherServer.ts

Terminal 2:

pnpm --filter @modelcontextprotocol/examples-server exec tsx src/mathServer.ts

Terminal 3:

ANTHROPIC_API_KEY=sk-... \
  pnpm --filter @modelcontextprotocol/examples-client exec tsx src/multiServerChatbot.ts

Try these prompts to exercise both servers in one turn:

What's the weather in Tokyo?
What is 17 x 19?
Convert 100 C to Fahrenheit and give me a 3-day forecast for Paris.

Testing

  • pnpm --filter @modelcontextprotocol/examples-client run check (typecheck + lint + prettier) passed
  • pnpm --filter @modelcontextprotocol/examples-server run check passed
  • All 5 tools verified via direct JSON-RPC calls
  • All 6 temperature conversion paths correct
  • Boundary values (days=1, days=7), negative numbers, floats, multiply-by-zero
  • Validation errors (days>7, invalid enum) return isError: true
  • Client connection failure prints a clear actionable error message

…protocol#740)

Adds three files demonstrating how one Anthropic-powered chatbot can
connect to multiple MCP servers simultaneously and route tool calls to
the correct server using a Map<toolName, Client> routing table.

- examples/server/src/weatherServer.ts — stateless Streamable HTTP
  server on :3001 with get_weather and get_forecast tools
- examples/server/src/mathServer.ts — stateless Streamable HTTP server
  on :3002 with add, multiply, and convert_temperature tools
- examples/client/src/multiServerChatbot.ts — connects to both servers,
  builds the routing table, runs an interactive Anthropic SDK chat loop,
  and dispatches parallel tool calls to the correct server

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 18:23
@changeset-bot
Copy link
Copy Markdown

changeset-bot Bot commented May 23, 2026

⚠️ No Changeset found

Latest commit: 3eacbab

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@2148

@modelcontextprotocol/codemod

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

@modelcontextprotocol/server

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

@modelcontextprotocol/express

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

@modelcontextprotocol/fastify

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

@modelcontextprotocol/hono

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

@modelcontextprotocol/node

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

commit: 3eacbab

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.

Can we have a chatbot example that connected to multiple remote servers

1 participant