Skip to content

Server class should be framework-agnostic and not import express #1222

@cytle

Description

@cytle

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:

  1. Try to import Server or McpServer in a browser environment (e.g., using Vite, Webpack, or other bundlers)
  2. The bundler will attempt to resolve the express dependency, which is Node.js-only
  3. 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:

  • McpServerServer (from ./index.js) → express (imported at top level)

Impact:

  1. Browser incompatibility: Cannot use Server class in browser environments
  2. Unnecessary dependency: All code importing Server is forced to include express even if not needed
  3. 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:

  1. Create src/server/express.ts with createMcpExpressApp and related Express utilities
  2. Remove express import from src/server/index.ts
  3. Re-export createMcpExpressApp from index.ts for backward compatibility

This would allow:

  • Server class to be used in browser environments
  • Express functionality to remain available for Node.js users
  • Framework-agnostic design for the core Server class

Related files:

  • src/server/index.ts (line 1: import express, { Express } from 'express';)
  • src/server/mcp.ts (depends on Server class)
  • package.json (express as dependency)

Metadata

Metadata

Assignees

Labels

bugSomething isn't working

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions