Skip to content
Open
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-limit.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@modelcontextprotocol/express': minor
---

Add `limit` option to `createMcpExpressApp()` to allow overriding the default 100kb JSON body parser size limit.
13 changes: 11 additions & 2 deletions packages/middleware/express/src/express.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,15 @@ export interface CreateMcpExpressAppOptions {
* to restrict which hostnames are allowed.
*/
allowedHosts?: string[];

/**
* Controls the maximum request body size for the JSON body parser.
* Passed directly to `express.json({ limit })`.
* Defaults to Express's built-in default of '100kb' when not specified.
*
* @example '10mb'
*/
limit?: string | number;
}

/**
Expand All @@ -48,10 +57,10 @@ export interface CreateMcpExpressAppOptions {
* ```
*/
export function createMcpExpressApp(options: CreateMcpExpressAppOptions = {}): Express {
const { host = '127.0.0.1', allowedHosts } = options;
const { host = '127.0.0.1', allowedHosts, limit } = options;

const app = express();
app.use(express.json());
app.use(express.json(limit !== undefined ? { limit } : undefined));

// If allowedHosts is explicitly provided, use that for validation
if (allowedHosts) {
Expand Down
10 changes: 10 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,16 @@ describe('@modelcontextprotocol/express', () => {
warn.mockRestore();
});

test('should accept limit option for JSON body parser', () => {
const app = createMcpExpressApp({ limit: '10mb' });
expect(app).toBeDefined();
});

test('should accept numeric limit option for JSON body parser', () => {
const app = createMcpExpressApp({ limit: 1048576 });
expect(app).toBeDefined();
});

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

Expand Down