Skip to content

[Feature] Disposal hook for request-scoped providers #380

Description

@eddienubes

Hi!
Currently, there's a way to define a factory for an async provider, e.g.

    ClsModule.forFeatureAsync({
      provide: MCP_CLIENT_TOKEN,
      strict: true,
      inject: [McpClientConfig, CLS_REQ],
      useFactory: async (config: McpClientConfig, req: Request) => {
        const client = new McpClient(config);
        await client.connect();
        return client;
      },
    }),

However, upon request completion I'd like to disconnect to release the session.
This is a perfect place for disposal hook.
Reading through nestjs issues, I stumbled across this one
nestjs/nest#9497, seems like the official advice is to use req.res.on('finish').
However, since nestjs-cls operates outside regular request scope, I was wondering if there's a nestjs-cls-native way of doing this, and if there's not, it'd be a nice feature to have.

Use case:

MCP client to reuse across request lifecycles and to disconnect at the end.

Thank you!

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions