-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Description
Describe the bug
The Server class in src/server/index.ts directly imports express at the top level, which prevents the Server class from being used in browser environments. This creates an unnecessary coupling to the Express framework, even though the Server class itself doesn't directly use Express.
To Reproduce
Steps to reproduce the behavior:
- Try to import
ServerorMcpServerin a browser environment (e.g., using Vite, Webpack, or other bundlers) - The bundler will attempt to resolve the
expressdependency, which is Node.js-only - This results in build failures or runtime errors in browser environments
Example:
// This will fail in browser environments
import { Server } from '@modelcontextprotocol/sdk/server';
// or
import { McpServer } from '@modelcontextprotocol/sdk/server';Expected behavior
The Server class should be framework-agnostic and usable in browser environments. Express-related functionality (like createMcpExpressApp) should be separated into a separate module that can be optionally imported when needed.
Logs
When attempting to use Server in a browser environment, you may see errors like:
Module not found: Can't resolve 'express'express is not defined- Build failures due to Node.js-specific dependencies
Additional context
Current code structure:
// src/server/index.ts
import express, { Express } from 'express'; // ❌ Top-level import
export class Server {
// Server class implementation (doesn't use express directly)
}
export function createMcpExpressApp(options: CreateMcpExpressAppOptions = {}): Express {
const app = express();
// ...
}Dependency chain:
McpServer→Server(from./index.js) →express(imported at top level)
Impact:
- Browser incompatibility: Cannot use
Serverclass in browser environments - Unnecessary dependency: All code importing
Serveris forced to includeexpresseven if not needed - Framework coupling: Couples the SDK to Express, limiting usage with other frameworks (Fastify, Koa, Cloudflare Workers, etc.)
Proposed solution:
Move Express-related functionality to a separate file:
- Create
src/server/express.tswithcreateMcpExpressAppand related Express utilities - Remove
expressimport fromsrc/server/index.ts - Re-export
createMcpExpressAppfromindex.tsfor backward compatibility
This would allow:
Serverclass to be used in browser environments- Express functionality to remain available for Node.js users
- Framework-agnostic design for the core
Serverclass
Related files:
src/server/index.ts(line 1:import express, { Express } from 'express';)src/server/mcp.ts(depends onServerclass)package.json(express as dependency)