Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/express-json-options.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@modelcontextprotocol/express': patch
---

Add `jsonOptions` option to `createMcpExpressApp()` to allow configuring `express.json()` body parser options (e.g. request body size limit).
10 changes: 10 additions & 0 deletions packages/middleware/express/src/express.examples.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,3 +39,13 @@ function createMcpExpressApp_allowedHosts() {
//#endregion createMcpExpressApp_allowedHosts
return app;
}

/**
* Example: Custom JSON body parser options.
*/
function createMcpExpressApp_jsonOptions() {
//#region createMcpExpressApp_jsonOptions
const app = createMcpExpressApp({ jsonOptions: { limit: '5mb' } });
//#endregion createMcpExpressApp_jsonOptions
return app;
}
16 changes: 14 additions & 2 deletions packages/middleware/express/src/express.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,13 @@ export interface CreateMcpExpressAppOptions {
* to restrict which hostnames are allowed.
*/
allowedHosts?: string[];

/**
* Options passed directly to `express.json()` body parser.
* Use this to configure the request body size limit, e.g. `{ limit: '5mb' }`.
* @see https://expressjs.com/en/api.html#express.json
*/
jsonOptions?: Parameters<typeof express.json>[0];
}

/**
Expand Down Expand Up @@ -49,12 +56,17 @@ export interface CreateMcpExpressAppOptions {
* ```ts source="./express.examples.ts#createMcpExpressApp_allowedHosts"
* const app = createMcpExpressApp({ host: '0.0.0.0', allowedHosts: ['myapp.local', 'localhost'] });
* ```
*
* @example Custom JSON body parser options
* ```ts source="./express.examples.ts#createMcpExpressApp_jsonOptions"
* const app = createMcpExpressApp({ jsonOptions: { limit: '5mb' } });
* ```
*/
export function createMcpExpressApp(options: CreateMcpExpressAppOptions = {}): Express {
const { host = '127.0.0.1', allowedHosts } = options;
const { host = '127.0.0.1', allowedHosts, jsonOptions } = options;

const app = express();
app.use(express.json());
app.use(express.json(jsonOptions));

// If allowedHosts is explicitly provided, use that for validation
if (allowedHosts) {
Expand Down
12 changes: 12 additions & 0 deletions packages/middleware/express/test/express.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,18 @@ describe('@modelcontextprotocol/express', () => {
warn.mockRestore();
});

test('should pass jsonOptions to express.json()', () => {
const app = createMcpExpressApp({ jsonOptions: { limit: '5mb' } });
expect(app).toBeDefined();
expect(typeof app.use).toBe('function');
});

test('should use default json options when jsonOptions not provided', () => {
const app = createMcpExpressApp();
expect(app).toBeDefined();
expect(typeof app.use).toBe('function');
});

test('should not apply host validation for non-localhost hosts without allowedHosts', () => {
const warn = vi.spyOn(console, 'warn').mockImplementation(() => {});

Expand Down
Loading