refactor: make Server class framework-agnostic by moving express to separate module #1223
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
refactor: make Server class framework-agnostic by moving express to separate module
This allows Server class to be used without forcing express dependency, making it framework-agnostic and browser-compatible.
Summary
This PR refactors the
Serverclass to be framework-agnostic by moving Express-related functionality to a separate module. ThecreateMcpExpressAppfunction andCreateMcpExpressAppOptionsinterface have been moved fromsrc/server/index.tstosrc/server/express.ts, and theexpressimport has been removed from the main server module.Motivation and Context
The
Serverclass was previously importingexpressat the top level ofsrc/server/index.ts, which created an unnecessary coupling to the Express framework. This prevented theServerclass from being used in browser environments, as bundlers would attempt to resolve the Node.js-onlyexpressdependency.Problem:
Serverclass cannot be used in browser environments due toexpressdependencyServerwas forced to includeexpresseven if not neededSolution:
src/server/express.tsexpressimport fromsrc/server/index.tsserver/express.jsforcreateMcpExpressAppServerclass framework-agnostic and browser-compatibleHow Has This Been Tested?
createMcpExpressAppfrom the new locationServerclass no longer importsexpressdirectlyFiles updated:
src/examples/server/src/server/index.test.ts)src/server/sse.ts,src/server/streamableHttp.ts)docs/server.md)Breaking Changes
Yes, this is a breaking change for users importing
createMcpExpressApp.Users who were importing
createMcpExpressAppfrom@modelcontextprotocol/sdk/serveror@modelcontextprotocol/sdk/server/index.jsneed to update their imports to:cript
// Before
import { createMcpExpressApp } from '@modelcontextprotocol/sdk/server';
// or
import { createMcpExpressApp } from '@modelcontextprotocol/sdk/server/index.js';
// After
import { createMcpExpressApp } from '@modelcontextprotocol/sdk/server/express.js';Migration guide:
Serverclass itself remains unchanged and can still be imported from@modelcontextprotocol/sdk/servercreateMcpExpressApp,CreateMcpExpressAppOptions) has movedTypes of changes
Checklist
Additional context
Design decisions:
Separate module approach: Instead of using conditional imports or making
expressoptional, we chose to completely separate Express functionality into its own module. This provides the cleanest separation of concerns and makes it explicit when Express is being used.No re-exports: We intentionally did not re-export
createMcpExpressAppfromindex.tsto avoid any possibility of theexpressdependency being pulled in when importingServer. Users must explicitly import fromserver/express.jsif they need Express functionality.Backward compatibility: While this is a breaking change, the migration is straightforward - users just need to update their import path. The functionality itself remains identical.
Benefits:
Serverclass can now be used in browser environmentsexpressdependency when usingServerclassRelated issue: This addresses the concern raised in the GitHub issue about making
Serverclass framework-agnostic.