feat(server): Add hooks.AddOnUnregisterSession functionality
Add OnUnregisterSession hook functionality to complement the existing OnRegisterSession hooks, allowing code to run when a client session is being removed from the server. In some cases, the server may want to do additional work when a session has been closed. For example, in the SSE server case where you may end up managing various logs for the duration of the session -- you would want to indicate that the session was finished.
Summary by CodeRabbit
-
New Features
- Added support for hooks that trigger when a client session is unregistered.
-
Tests
- Introduced new tests to verify that session registration and unregistration hooks are called as expected, and to ensure correct behavior when no hooks are present.
"""
Walkthrough
This change introduces a new hook mechanism for session unregistration events in the server. A new hook type and associated methods are added to the hooks system, enabling registration and invocation of functions when a client session is unregistered. The server's session unregistration logic is updated to trigger these hooks upon session removal. Additionally, tests are added to verify that both registration and unregistration hooks are correctly called, and to ensure that the absence of hooks does not cause errors during session lifecycle operations. Minor updates were made to propagate context parameters in session unregistration calls across several server components.
Changes
| Files / Areas Changed | Summary of Changes |
|---|---|
| server/hooks.go | Added OnUnregisterSessionHookFunc type, new OnUnregisterSession slice field, and methods to add and invoke unregister hooks. |
| server/server.go | Updated UnregisterSession method to accept context.Context, use atomic session removal, and invoke unregister hooks. |
| server/server_test.go | Added tests verifying session registration and unregistration hooks, including behavior with nil hooks; simplified error variable declarations in an existing test. |
| server/sse.go | Updated calls to UnregisterSession to include context.Context parameter; formatting and alignment improvements only. |
| server/stdio.go | Updated deferred call to UnregisterSession in Listen method to pass context.Context as first argument. |
Possibly related issues
- mark3labs/mcp-go#173: Implements explicit hooks and calls for session unregistration events, directly addressing the feature requested in this issue.
Possibly related PRs
- mark3labs/mcp-go#92: Adds context-aware hooks for session registration and complements this PR’s addition of unregister session hooks, both modifying the hooks system and MCPServer session lifecycle handling.
- mark3labs/mcp-go#62: Introduces the initial hooks framework for request lifecycle events; this PR builds on that foundation by adding session unregistration hooks and integrating them into session management.
[!TIP]
⚡💬 Agentic Chat (Pro Plan, General Availability)
- We're introducing multi-step agentic chat in review comments and issue comments, within and outside of PR's. This feature enhances review and issue discussions with the CodeRabbit agentic chat by enabling advanced interactions, including the ability to create pull requests directly from comments and add commits to existing pull requests.
📜 Recent review details
Configuration used: CodeRabbit UI Review profile: CHILL Plan: Pro
📥 Commits
Reviewing files that changed from the base of the PR and between e0211e4236c733942c372600bae119f75861f088 and 435622d1b3f1194924350e999c330c50073cd6f4.
⛔ Files ignored due to path filters (1)
server/internal/gen/hooks.go.tmplis excluded by!**/gen/**
📒 Files selected for processing (5)
server/hooks.go(3 hunks)server/server.go(1 hunks)server/server_test.go(2 hunks)server/sse.go(6 hunks)server/stdio.go(1 hunks)
🚧 Files skipped from review as they are similar to previous changes (3)
- server/server.go
- server/hooks.go
- server/server_test.go
🔇 Additional comments (6)
server/stdio.go (1)
207-207: Context parameter added correctly to UnregisterSession callThe change correctly adds the context parameter to the
UnregisterSessioncall, enabling context propagation for the new unregister session hooks. This aligns with the PR objective of adding hooks functionality for when sessions end.server/sse.go (5)
57-66: Code formatting improvements look goodThe formatting changes in the SSEServer struct declaration improve readability with consistent indentation.
70-70: Minor formatting improvementThe addition of a blank line between field groups improves readability.
164-170: Formatting improvements in SSEServer initializationThe alignment changes in the struct initialization improve code readability and consistency.
262-262: Context parameter added correctly to UnregisterSession callThe change correctly adds the request context to the
UnregisterSessioncall, which is essential for the new session unregistration hooks functionality. This allows hooks to access the request context when a session is unregistered.
441-441: Whitespace improvements for better code organizationThe addition of blank lines between function definitions improves code readability and organization.
Also applies to: 453-453, 465-465
✨ Finishing Touches
- [ ] 📝 Generate Docstrings
🪧 Tips
Chat
There are 3 ways to chat with CodeRabbit:
- Review comments: Directly reply to a review comment made by CodeRabbit. Example:
I pushed a fix in commit <commit_id>, please review it.Generate unit testing code for this file.Open a follow-up GitHub issue for this discussion.
- Files and specific lines of code (under the "Files changed" tab): Tag
@coderabbitaiin a new review comment at the desired location with your query. Examples:@coderabbitai generate unit testing code for this file.@coderabbitai modularize this function.
- PR comments: Tag
@coderabbitaiin a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:@coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.@coderabbitai read src/utils.ts and generate unit testing code.@coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.@coderabbitai help me debug CodeRabbit configuration file.
Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.
CodeRabbit Commands (Invoked using PR comments)
@coderabbitai pauseto pause the reviews on a PR.@coderabbitai resumeto resume the paused reviews.@coderabbitai reviewto trigger an incremental review. This is useful when automatic reviews are disabled for the repository.@coderabbitai full reviewto do a full review from scratch and review all the files again.@coderabbitai summaryto regenerate the summary of the PR.@coderabbitai generate docstringsto generate docstrings for this PR.@coderabbitai resolveresolve all the CodeRabbit review comments.@coderabbitai configurationto show the current CodeRabbit configuration for the repository.@coderabbitai helpto get help.
Other keywords and placeholders
- Add
@coderabbitai ignoreanywhere in the PR description to prevent this PR from being reviewed. - Add
@coderabbitai summaryto generate the high-level summary at a specific location in the PR description. - Add
@coderabbitaianywhere in the PR title to generate the title automatically.
CodeRabbit Configuration File (.coderabbit.yaml)
- You can programmatically configure CodeRabbit by adding a
.coderabbit.yamlfile to the root of your repository. - Please see the configuration documentation for more information.
- If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation:
# yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json
Documentation and Community
- Visit our Documentation for detailed information on how to use CodeRabbit.
- Join our Discord Community to get help, request features, and share feedback.
- Follow us on X/Twitter for updates and announcements.