Skip to content

feat(server): Add hooks.AddOnUnregisterSession functionality #175

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 20, 2025

Conversation

rwjblue-glean
Copy link
Collaborator

@rwjblue-glean rwjblue-glean commented Apr 18, 2025

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.

Copy link
Contributor

coderabbitai bot commented Apr 18, 2025

"""

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

Possibly related PRs

  • make context available in hooks, add OnRegisterSession hook #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.
  • Feature: Request Hooks #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 e0211e4 and 435622d.

⛔ Files ignored due to path filters (1)
  • server/internal/gen/hooks.go.tmpl is 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 call

The change correctly adds the context parameter to the UnregisterSession call, 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 good

The formatting changes in the SSEServer struct declaration improve readability with consistent indentation.


70-70: Minor formatting improvement

The addition of a blank line between field groups improves readability.


164-170: Formatting improvements in SSEServer initialization

The alignment changes in the struct initialization improve code readability and consistency.


262-262: Context parameter added correctly to UnregisterSession call

The change correctly adds the request context to the UnregisterSession call, 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 organization

The 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 @coderabbitai in 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 @coderabbitai in 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 pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file 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.

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 0

🧹 Nitpick comments (2)
server/hooks.go (1)

14-15: Consistent hook function signature for session events

The new OnUnregisterSessionHookFunc doesn't include a context parameter, unlike its counterpart OnRegisterSessionHookFunc (line 12). This creates an inconsistency in the API.

Consider adding a context parameter for consistency:

-type OnUnregisterSessionHookFunc func(session ClientSession)
+type OnUnregisterSessionHookFunc func(ctx context.Context, session ClientSession)

Or alternatively, document why context isn't needed for unregistration hooks if this is intentional.

server/server.go (1)

211-212: Improve type assertion for better error handling

The current implementation uses a type assertion without checking if the operation succeeded, which could potentially panic if the session map contained non-ClientSession values.

Consider using a more defensive approach:

-session, _ := s.sessions.LoadAndDelete(sessionID)
-s.hooks.UnregisterSession(session.(ClientSession))
+if session, ok := s.sessions.LoadAndDelete(sessionID); ok {
+    if clientSession, ok := session.(ClientSession); ok {
+        s.hooks.UnregisterSession(clientSession)
+    }
+}
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 71b910b and e0211e4.

⛔ Files ignored due to path filters (1)
  • server/internal/gen/hooks.go.tmpl is excluded by !**/gen/**
📒 Files selected for processing (3)
  • server/hooks.go (3 hunks)
  • server/server.go (1 hunks)
  • server/server_test.go (1 hunks)
🧰 Additional context used
🧬 Code Graph Analysis (1)
server/hooks.go (1)
server/server.go (1)
  • ClientSession (54-63)
🔇 Additional comments (3)
server/hooks.go (1)

228-235: LGTM! Method implementation follows established patterns.

The implementation of UnregisterSession follows the same patterns as other hook invocation methods with appropriate nil checks.

server/server_test.go (2)

1356-1398: Well-structured test for session hooks

The test thoroughly verifies that both the register and unregister session hooks are called with the correct session object when sessions are registered and unregistered.


1400-1413: Good edge case coverage with nil hooks test

This test verifies that registering and unregistering a session on a server without any hooks does not cause errors, which is an important edge case to cover.

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.
@rwjblue-glean rwjblue-glean force-pushed the rwjblue/push-lwvnsznunykv branch from e0211e4 to 435622d Compare April 19, 2025 16:26
@ezynda3 ezynda3 merged commit 7afc2e5 into mark3labs:main Apr 20, 2025
2 checks passed
@rwjblue-glean rwjblue-glean deleted the rwjblue/push-lwvnsznunykv branch April 21, 2025 18:27
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants