diff --git a/.changeset/express-json-options.md b/.changeset/express-json-options.md new file mode 100644 index 000000000..945ad0836 --- /dev/null +++ b/.changeset/express-json-options.md @@ -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). diff --git a/packages/middleware/express/src/express.examples.ts b/packages/middleware/express/src/express.examples.ts index 8d3f8e2ff..86be13bfa 100644 --- a/packages/middleware/express/src/express.examples.ts +++ b/packages/middleware/express/src/express.examples.ts @@ -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; +} diff --git a/packages/middleware/express/src/express.ts b/packages/middleware/express/src/express.ts index af156a229..297ae4135 100644 --- a/packages/middleware/express/src/express.ts +++ b/packages/middleware/express/src/express.ts @@ -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[0]; } /** @@ -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) { diff --git a/packages/middleware/express/test/express.test.ts b/packages/middleware/express/test/express.test.ts index 64cf533bc..4484e9b47 100644 --- a/packages/middleware/express/test/express.test.ts +++ b/packages/middleware/express/test/express.test.ts @@ -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(() => {});