Skip to content

Pattern for per-session tool collection lifetimes? #831

@derrickaccountnumbertwo

Description

@derrickaccountnumbertwo

Hi, I'm trying to figure out the intended way to combine ListChanged notifications with per-session tool collections. Specifically, I don't know how I can become aware that a session has ended.

I'll describe my use case in more detail, so that you can check my assumptions and correct me if I'm wrong:

  1. I'm implementing an MCP server. My Tools are inheriting from Microsoft.Extensions.AI.AIFunction. During tool invocation, I need to know which user/session this is, and therefore each tool must carry the user/session state it needs, and therefore I need to create a separate tool collection per user/session. Does any of that sound wrong so far?
  2. On my MCP server program I have a way for tools to be enabled/disabled, which should send a list changed notification to clients. I have a collection of tool collections (one per session) which I can iterate over and update in order to automatically send those notifications.
  3. But here is the problem: Now a client disconnects. How can I be aware of this, so that I can forget that tool collection, and stop keeping it in memory and trying to update it?

I'm fairly inexperienced at this, so I don't know whether I should be considering things like "stateless" or whether it would help or what the tradeoffs would be.
Any advice would be appreciated, thank you.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions