Skip to content
This repository was archived by the owner on May 16, 2025. It is now read-only.

Commit 0f42f28

Browse files
fixes, add Streaming HTTP example w/ SSE fallback, update REAMDE
1 parent b33a2de commit 0f42f28

File tree

6 files changed

+325
-127
lines changed

6 files changed

+325
-127
lines changed

README.md

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@ This library provides a lightweight wrapper that makes [Anthropic Model Context
99

1010
- 🔌 **Transport Options**
1111

12-
- Connect to MCP servers via stdio (local) or SSE (remote)
12+
- Connect to MCP servers via stdio (local) or Streamable HTTP (remote)
13+
- Streamable HTTP automatically falls back to SSE for compatibility with legacy MCP server implementations
1314
- Support for custom headers in SSE connections for authentication
1415
- Configurable reconnection strategies for both transport types
1516

@@ -38,13 +39,13 @@ npm install @langchain/mcp-adapters
3839

3940
### Optional Dependencies
4041

41-
For SSE connections with custom headers in Node.js:
42+
For SSE connections with custom headers in Node.js (does not apply to Streamable HTTP):
4243

4344
```bash
4445
npm install eventsource
4546
```
4647

47-
For enhanced SSE header support:
48+
For enhanced SSE header support (does not apply to Streamable HTTP):
4849

4950
```bash
5051
npm install extended-eventsource
@@ -155,26 +156,25 @@ const client = new MultiServerMCPClient({
155156
args: ["-y", "@modelcontextprotocol/server-filesystem"],
156157
},
157158

158-
// SSE transport example with reconnection configuration
159+
// Sreamable HTTP transport example, with auth headers and automatic SSE fallback disabled (defaults to enabled)
159160
weather: {
160-
transport: "sse",
161-
url: "https://example.com/mcp-weather",
161+
url: "https://example.com/weather/mcp",
162162
headers: {
163163
Authorization: "Bearer token123",
164-
},
165-
useNodeEventSource: true,
164+
}
165+
automaticSSEFallback: false
166+
},
167+
168+
// how to force SSE, for old servers that are known to only support SSE (streamable HTTP falls back automatically if unsure)
169+
github: {
170+
transport: "sse", // also works with "type" field instead of "transport"
171+
url: "https://example.com/mcp",
166172
reconnect: {
167173
enabled: true,
168174
maxAttempts: 5,
169175
delayMs: 2000,
170176
},
171177
},
172-
173-
// Streamable HTTP transport example
174-
github: {
175-
transport: "streamable",
176-
url: "https://example.com/mcp",
177-
},
178178
},
179179
});
180180

@@ -218,8 +218,8 @@ When loading MCP tools either directly through `loadMcpTools` or via `MultiServe
218218
| Option | Type | Default | Description |
219219
| ------------------------------ | ------- | ------- | ------------------------------------------------------------------------------------ |
220220
| `throwOnLoadError` | boolean | `true` | Whether to throw an error if a tool fails to load |
221-
| `prefixToolNameWithServerName` | boolean | `false` | If true, prefixes all tool names with the server name (e.g., `serverName__toolName`) |
222-
| `additionalToolNamePrefix` | string | `""` | Additional prefix to add to tool names (e.g., `prefix__serverName__toolName`) |
221+
| `prefixToolNameWithServerName` | boolean | `true` | If true, prefixes all tool names with the server name (e.g., `serverName__toolName`) |
222+
| `additionalToolNamePrefix` | string | `mcp` | Additional prefix to add to tool names (e.g., `prefix__serverName__toolName`) |
223223

224224
## Response Handling
225225

@@ -367,8 +367,8 @@ Example Zod error for an invalid SSE URL:
367367

368368
When using in browsers:
369369

370-
- Native EventSource API doesn't support custom headers
371-
- Consider using a proxy or pass authentication via query parameters
370+
- EventSource API doesn't support custom headers for SSE
371+
- Consider using a proxy or pass authentication via query parameters to avoid leaking credentials to client
372372
- May require CORS configuration on the server side
373373

374374
## Troubleshooting

__tests__/client.basic.test.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,7 @@ describe("MultiServerMCPClient", () => {
107107
command: "python",
108108
args: ["./script.py"],
109109
env: undefined,
110+
stderr: "inherit",
110111
});
111112

112113
expect(Client).toHaveBeenCalled();

__tests__/client.comprehensive.test.ts

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
import { vi, describe, test, expect, beforeEach, type Mock } from "vitest";
22
import { ZodError } from "zod";
3-
import { Connection } from "../src/client.js";
3+
import type {
4+
ClientConfig,
5+
Connection,
6+
StdioConnection,
7+
} from "../src/client.js";
48

59
import "./mocks.js";
610

@@ -344,6 +348,10 @@ describe("MultiServerMCPClient", () => {
344348
},
345349
});
346350

351+
const conf = client.config;
352+
expect(conf.additionalToolNamePrefix).toBe("mcp");
353+
expect(conf.prefixToolNameWithServerName).toBe(true);
354+
347355
await client.initializeConnections();
348356
const tools = await client.getTools();
349357

@@ -542,7 +550,7 @@ describe("MultiServerMCPClient", () => {
542550
// Should not have created a client
543551
expect(Client).not.toHaveBeenCalled();
544552
});
545-
553+
546554
test("should throw on streamable HTTP transport creation errors", async () => {
547555
// Force an error when creating transport
548556
(StreamableHTTPClientTransport as Mock).mockImplementationOnce(() => {

examples/calculator_sse_shttp_example.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,6 @@ import {
2626
isHumanMessage,
2727
} from "@langchain/core/messages";
2828
import dotenv from "dotenv";
29-
import fs from "fs";
30-
import path from "path";
3129

3230
import { main as calculatorServerMain } from "./calculator_server_shttp_sse.js";
3331

examples/langgraph_example.ts

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -57,10 +57,6 @@ async function runExample() {
5757
command: "npx",
5858
args: ["-y", "@modelcontextprotocol/server-everything"],
5959
},
60-
streamable_http: {
61-
transport: "streamable",
62-
url: "http://0.0.0.0/mcp",
63-
},
6460
});
6561

6662
// Get the tools (flattened array is the default now)

0 commit comments

Comments
 (0)