Skip to content
Merged
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/fix-client-notification-descriptor-scan.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@nest-mcp/client": patch
---

Avoid invoking provider prototype accessors while wiring `@OnMcpNotification` handlers. The bootstrap scanner now inspects property descriptors and only reads method values, preventing Nest provider getters from throwing during app startup.
12 changes: 7 additions & 5 deletions packages/client/src/mcp-client.module.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -468,8 +468,11 @@ describe('McpClientBootstrap notification wiring', () => {
expect(onNotificationFn).not.toHaveBeenCalled();
});

it('skips non-function prototype own properties without throwing', async () => {
it('skips non-function prototype own properties without invoking accessors', async () => {
const onNotificationFn = vi.fn();
const accessor = vi.fn(() => {
throw new Error('getter should not be invoked while scanning handlers');
});
const clientA = {
name: 'server-a',
connect: vi.fn().mockResolvedValue(undefined),
Expand All @@ -480,14 +483,12 @@ describe('McpClientBootstrap notification wiring', () => {

// Simulates providers registered with `useValue: {}` (plain object) or
// classes whose prototype exposes non-function own properties via
// accessors — `Reflect.getMetadata` throws TypeError on non-object targets.
// accessors. The scan must not invoke getters while looking for methods.
class HandlerWithAccessor {
handleNotification() {}
}
Object.defineProperty(HandlerWithAccessor.prototype, 'nonFunctionProp', {
get() {
return null;
},
get: accessor,
enumerable: false,
configurable: true,
});
Expand Down Expand Up @@ -518,5 +519,6 @@ describe('McpClientBootstrap notification wiring', () => {
'notifications/tools/list_changed',
expect.any(Function),
);
expect(accessor).not.toHaveBeenCalled();
});
});
11 changes: 3 additions & 8 deletions packages/client/src/mcp-client.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -184,11 +184,8 @@ export class McpClientBootstrap implements OnApplicationBootstrap, OnApplication
);

for (const methodName of methodNames) {
// Prototype own-property names can include non-function values
// (e.g. `Object.prototype.__proto__` on `useValue: {}` providers, or
// accessor properties on library classes). `Reflect.getMetadata`
// throws TypeError on non-object targets, so skip non-functions.
const method = prototype[methodName];
const descriptor = Object.getOwnPropertyDescriptor(prototype, methodName);
const method = descriptor?.value;
if (typeof method !== 'function') continue;

// NestJS SetMetadata stores metadata on the descriptor.value (the method
Expand All @@ -210,9 +207,7 @@ export class McpClientBootstrap implements OnApplicationBootstrap, OnApplication
continue;
}

const boundHandler = (
instance as Record<string, (...args: unknown[]) => void | Promise<void>>
)[methodName].bind(instance);
const boundHandler = method.bind(instance);
client.onNotification(metadata.method, boundHandler);
wiredCount++;
}
Expand Down
Loading