Skip to content

Commit 33d2e76

Browse files
committed
feat(express): add jsonLimit option to createMcpExpressApp
Add jsonLimit option to configure the maximum request body size for the JSON body parser. Accepts a string (e.g., '5mb') or number (bytes). Defaults to Express's default of '100kb'. Fixes #1354
1 parent b0ef89f commit 33d2e76

File tree

3 files changed

+24
-2
lines changed

3 files changed

+24
-2
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@modelcontextprotocol/express': patch
3+
---
4+
5+
Add `jsonLimit` option to `createMcpExpressApp()` to allow overriding the default JSON body parser limit of 100kb. Accepts a string (e.g., '5mb') or number (bytes).

packages/middleware/express/src/express.ts

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,13 @@ export interface CreateMcpExpressAppOptions {
2222
* to restrict which hostnames are allowed.
2323
*/
2424
allowedHosts?: string[];
25+
26+
/**
27+
* Maximum request body size for the JSON body parser.
28+
* Accepts a number (bytes) or a string with units (e.g., '1mb', '100kb').
29+
* Defaults to Express's default of '100kb'.
30+
*/
31+
jsonLimit?: string | number;
2532
}
2633

2734
/**
@@ -48,10 +55,10 @@ export interface CreateMcpExpressAppOptions {
4855
* ```
4956
*/
5057
export function createMcpExpressApp(options: CreateMcpExpressAppOptions = {}): Express {
51-
const { host = '127.0.0.1', allowedHosts } = options;
58+
const { host = '127.0.0.1', allowedHosts, jsonLimit } = options;
5259

5360
const app = express();
54-
app.use(express.json());
61+
app.use(express.json(jsonLimit === undefined ? {} : { limit: jsonLimit }));
5562

5663
// If allowedHosts is explicitly provided, use that for validation
5764
if (allowedHosts) {

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,5 +178,15 @@ describe('@modelcontextprotocol/express', () => {
178178

179179
warn.mockRestore();
180180
});
181+
182+
test('should accept jsonLimit option', () => {
183+
const app = createMcpExpressApp({ jsonLimit: '5mb' });
184+
expect(app).toBeDefined();
185+
});
186+
187+
test('should accept numeric jsonLimit option', () => {
188+
const app = createMcpExpressApp({ jsonLimit: 1024 * 1024 });
189+
expect(app).toBeDefined();
190+
});
181191
});
182192
});

0 commit comments

Comments
 (0)