@contractkit/plugin-typescript 0.33.1 → 0.33.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@contractkit/plugin-typescript",
3
- "version": "0.33.1",
3
+ "version": "0.33.3",
4
4
  "description": "ContractKit built-in plugin: TypeScript codegen (SDK clients, Koa routers, Zod schemas, plain types)",
5
5
  "license": "MIT",
6
6
  "author": {
@@ -27,7 +27,7 @@
27
27
  ".": "./dist/index.js"
28
28
  },
29
29
  "dependencies": {
30
- "@contractkit/core": "0.28.1"
30
+ "@contractkit/core": "0.28.2"
31
31
  },
32
32
  "devDependencies": {
33
33
  "@repo/config-eslint": "0.3.1",
@@ -490,23 +490,24 @@ export function generateMcpAggregator(entries: McpAggregatorEntry[]): string {
490
490
  /** Generate the optional `mcp.router.ts` — the standard ServerKit route wiring for the dispatcher. */
491
491
  export function generateMcpRouter(options: { path?: string } = {}): string {
492
492
  const path = options.path ?? '/mcp';
493
- return `import { ServerKitRouter, requireSignature } from '@maroonedsoftware/koa';
493
+ return `import { ServerKitRouter, bodyParserMiddleware, requireSignature } from '@maroonedsoftware/koa';
494
494
  import { McpDispatcher, createMcpRequestContext, MCP_AUTH_POLICY } from '@maroonedsoftware/mcp';
495
495
 
496
496
  /** Mount the MCP endpoint onto a ServerKit router. Call \`registerMcpTools(container)\` at startup. */
497
497
  export function mountMcp(router: ReturnType<typeof ServerKitRouter>): void {
498
- router.post('${path}', requireSignature('mcp', { policy: MCP_AUTH_POLICY }), async (ctx) => {
498
+ router.post('${path}', bodyParserMiddleware(['json']), requireSignature('mcp', { policy: MCP_AUTH_POLICY }), async (ctx) => {
499
499
  const dispatcher = ctx.container.get(McpDispatcher);
500
500
  const context = createMcpRequestContext({ requestId: ctx.requestId, logger: ctx.logger });
501
501
  if (dispatcher.sessionMode === 'stateful') {
502
502
  ctx.respond = false;
503
503
  await dispatcher.dispatchStateful(
504
- { req: ctx.req, res: ctx.res, body: ctx.request.body, sessionId: ctx.get('mcp-session-id') },
504
+ { req: ctx.req, res: ctx.res, body: ctx.parsedBody, sessionId: ctx.get('mcp-session-id') },
505
505
  context,
506
506
  );
507
507
  } else {
508
- const response = await dispatcher.dispatch(JSON.parse(ctx.rawBody), context);
508
+ const response = await dispatcher.dispatch(JSON.parse(String(ctx.rawBody)), context);
509
509
  if (response) ctx.body = response;
510
+ else ctx.status = 202; // a notification — nothing to return
510
511
  }
511
512
  });
512
513
  }
@@ -233,6 +233,37 @@ describe('generateMcpRouter', () => {
233
233
  expect(out).toContain("dispatcher.sessionMode === 'stateful'");
234
234
  });
235
235
 
236
+ it('parses the body before verifying the signature', () => {
237
+ const out = generateMcpRouter({ path: '/mcp' });
238
+ // `requireSignature` HMACs `ctx.rawBody`, which only `bodyParserMiddleware` populates,
239
+ // so the parser has to run first. Asserting the whole route line pins the order too.
240
+ expect(out).toContain("import { ServerKitRouter, bodyParserMiddleware, requireSignature } from '@maroonedsoftware/koa';");
241
+ expect(out).toContain(
242
+ "router.post('/mcp', bodyParserMiddleware(['json']), requireSignature('mcp', { policy: MCP_AUTH_POLICY }), async (ctx) => {",
243
+ );
244
+ });
245
+
246
+ it('answers notifications with 202 rather than falling through to a 404', () => {
247
+ const out = generateMcpRouter();
248
+ // `dispatch` resolves undefined for a notification; an unset ctx.body 404s in errorMiddleware.
249
+ expect(out).toContain('if (response) ctx.body = response;');
250
+ expect(out).toContain('else ctx.status = 202;');
251
+ });
252
+
253
+ it('stringifies rawBody before JSON.parse', () => {
254
+ const out = generateMcpRouter();
255
+ // ctx.rawBody is BinaryLike; JSON.parse takes string.
256
+ expect(out).toContain('JSON.parse(String(ctx.rawBody))');
257
+ expect(out).not.toContain('JSON.parse(ctx.rawBody)');
258
+ });
259
+
260
+ it('hands the stateful transport the parsed body', () => {
261
+ const out = generateMcpRouter();
262
+ // The parser drains the stream and writes ctx.parsedBody; ctx.request.body is never set.
263
+ expect(out).toContain('body: ctx.parsedBody');
264
+ expect(out).not.toContain('ctx.request.body');
265
+ });
266
+
236
267
  it('defaults the mount path to /mcp', () => {
237
268
  expect(generateMcpRouter()).toContain("router.post('/mcp'");
238
269
  });