-
Notifications
You must be signed in to change notification settings - Fork 32
[BUG] Cannot import library in browser environments #62
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
This abridged code executes in the browser just fine import { loadMcpTools } from "./langchain/mcpTools"; // my fork of tools.ts
import { createReactAgent } from "@langchain/langgraph/prebuilt";
import { Client } from "@modelcontextprotocol/sdk/client/index.js";
import { SSEClientTransport } from "@modelcontextprotocol/sdk/client/sse.js";
import { StructuredToolInterface } from "@langchain/core/tools";
// ...rest
const disconnectCallbacks: (() => Promise<void>)[] = [];
const mcpServers = await Promise.all(
Object.entries(servers.servers).map(async ([name, server]) => {
const transport = new SSEClientTransport(new URL(server.url));
console.log("connecting to server", name, server.url);
const mcpClient = new Client({
name: `example`,
version: "1.0.0",
});
await mcpClient.connect(transport);
disconnectCallbacks.push(async () => {
console.log("disconnecting from server", name);
await mcpClient.close();
await transport.close();
});
return (await loadMcpTools(
name,
mcpClient,
)) as StructuredToolInterface[];
}),
);
// Create the React agent
const agent = createReactAgent({
llm: client,
tools: mcpServers.flat(),
});
// ... rest
// eventually
await Promise.all(disconnectCallbacks.map((cb) => cb())); |
@cephalization There are dozens of bundlers out there, each with dozens of versions, each with their own complex configuration options. If you need help troubleshooting this, you'll need to share a complete and executable minimal reproducible example for us to work off of. I'll push a change that drops our usage of the |
Bug Description
Library is not importable in browser environments, despite the documentation claiming so.
Reproduction Steps
Steps to reproduce the behavior:
client.ts
Expected Behavior
I should at least be able to import the contents of
tools.ts
in browser environments, as that does not depend on any node code.client.ts
could also support browsers if it were refactored to support stdio via dependency injection or config, delaying import of node:stream until necessary.I was able to successfully fork tools.ts without any changes, and connect to an sse transport per-tool, and then convert those tools and use them in the ReactAgent from a browser.
Possible Solution
Minimally, I propose adding an exports field to package.json, and allow users to directly import applicable files, treeshaking files with node dependencies out of the bundle.
Longer term, I think the multi-server Client would be very valuable in browser contexts if node libs were only used when stdio servers provided in the config.
Environment
The text was updated successfully, but these errors were encountered: