Skip to content

Commit f610565

Browse files
sidharth aroraclaude
authored andcommitted
feat(express): add jsonOptions to createMcpExpressApp()
Allow users to configure express.json() body parser options (e.g. request body size limit) via a new jsonOptions option, fixing 413 errors for large MCP payloads. Closes modelcontextprotocol#1354 Co-Authored-By: Claude Opus 4.6 <[email protected]>
1 parent 1a78b01 commit f610565

4 files changed

Lines changed: 41 additions & 2 deletions

File tree

‎.changeset/express-json-options.md‎

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 `jsonOptions` option to `createMcpExpressApp()` to allow configuring `express.json()` body parser options (e.g. request body size limit).

‎packages/middleware/express/src/express.examples.ts‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,3 +39,13 @@ function createMcpExpressApp_allowedHosts() {
3939
//#endregion createMcpExpressApp_allowedHosts
4040
return app;
4141
}
42+
43+
/**
44+
* Example: Custom JSON body parser options.
45+
*/
46+
function createMcpExpressApp_jsonOptions() {
47+
//#region createMcpExpressApp_jsonOptions
48+
const app = createMcpExpressApp({ jsonOptions: { limit: '5mb' } });
49+
//#endregion createMcpExpressApp_jsonOptions
50+
return app;
51+
}

‎packages/middleware/express/src/express.ts‎

Lines changed: 14 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+
* Options passed directly to `express.json()` body parser.
28+
* Use this to configure the request body size limit, e.g. `{ limit: '5mb' }`.
29+
* @see https://expressjs.com/en/api.html#express.json
30+
*/
31+
jsonOptions?: Parameters<typeof express.json>[0];
2532
}
2633

2734
/**
@@ -49,12 +56,17 @@ export interface CreateMcpExpressAppOptions {
4956
* ```ts source="./express.examples.ts#createMcpExpressApp_allowedHosts"
5057
* const app = createMcpExpressApp({ host: '0.0.0.0', allowedHosts: ['myapp.local', 'localhost'] });
5158
* ```
59+
*
60+
* @example Custom JSON body parser options
61+
* ```ts source="./express.examples.ts#createMcpExpressApp_jsonOptions"
62+
* const app = createMcpExpressApp({ jsonOptions: { limit: '5mb' } });
63+
* ```
5264
*/
5365
export function createMcpExpressApp(options: CreateMcpExpressAppOptions = {}): Express {
54-
const { host = '127.0.0.1', allowedHosts } = options;
66+
const { host = '127.0.0.1', allowedHosts, jsonOptions } = options;
5567

5668
const app = express();
57-
app.use(express.json());
69+
app.use(express.json(jsonOptions));
5870

5971
// If allowedHosts is explicitly provided, use that for validation
6072
if (allowedHosts) {

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

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

170+
test('should pass jsonOptions to express.json()', () => {
171+
const app = createMcpExpressApp({ jsonOptions: { limit: '5mb' } });
172+
expect(app).toBeDefined();
173+
expect(typeof app.use).toBe('function');
174+
});
175+
176+
test('should use default json options when jsonOptions not provided', () => {
177+
const app = createMcpExpressApp();
178+
expect(app).toBeDefined();
179+
expect(typeof app.use).toBe('function');
180+
});
181+
170182
test('should not apply host validation for non-localhost hosts without allowedHosts', () => {
171183
const warn = vi.spyOn(console, 'warn').mockImplementation(() => {});
172184

0 commit comments

Comments
 (0)