Skip to content

Commit 152e1b6

Browse files
feat(express): add limit option for JSON body parser
Add a `limit` option to `CreateMcpExpressAppOptions` that is passed through to `express.json({ limit })`, allowing users to override the default 100kb request body size limit. Closes #1354
1 parent 0da5d3f commit 152e1b6

File tree

2 files changed

+21
-2
lines changed

2 files changed

+21
-2
lines changed

packages/middleware/express/src/express.ts

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,15 @@ export interface CreateMcpExpressAppOptions {
2222
* to restrict which hostnames are allowed.
2323
*/
2424
allowedHosts?: string[];
25+
26+
/**
27+
* Controls the maximum request body size for the JSON body parser.
28+
* Passed directly to `express.json({ limit })`.
29+
* Defaults to Express's built-in default of '100kb' when not specified.
30+
*
31+
* @example '10mb'
32+
*/
33+
limit?: string | number;
2534
}
2635

2736
/**
@@ -48,10 +57,10 @@ export interface CreateMcpExpressAppOptions {
4857
* ```
4958
*/
5059
export function createMcpExpressApp(options: CreateMcpExpressAppOptions = {}): Express {
51-
const { host = '127.0.0.1', allowedHosts } = options;
60+
const { host = '127.0.0.1', allowedHosts, limit } = options;
5261

5362
const app = express();
54-
app.use(express.json());
63+
app.use(express.json(limit !== undefined ? { limit } : undefined));
5564

5665
// If allowedHosts is explicitly provided, use that for validation
5766
if (allowedHosts) {

packages/middleware/express/test/express.test.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,16 @@ describe('@modelcontextprotocol/express', () => {
167167
warn.mockRestore();
168168
});
169169

170+
test('should accept limit option for JSON body parser', () => {
171+
const app = createMcpExpressApp({ limit: '10mb' });
172+
expect(app).toBeDefined();
173+
});
174+
175+
test('should accept numeric limit option for JSON body parser', () => {
176+
const app = createMcpExpressApp({ limit: 1048576 });
177+
expect(app).toBeDefined();
178+
});
179+
170180
test('should not apply host validation for non-localhost hosts without allowedHosts', () => {
171181
const warn = vi.spyOn(console, 'warn').mockImplementation(() => {});
172182

0 commit comments

Comments
 (0)