fix session WIP - #1
Conversation
WalkthroughThis set of changes refactors session and server management throughout the codebase, removing the concept of a default browser session and introducing explicit session scoping via unique MCP session IDs. The Estimated code review effort🎯 4 (Complex) | ⏱️ ~45 minutes
Note ⚡️ Unit Test Generation is now available in beta!Learn more here, or try it out under "Finishing Touches" below. ✨ Finishing Touches
🧪 Generate unit tests
🪧 TipsChatThere are 3 ways to chat with CodeRabbit:
SupportNeed help? Create a ticket on our support page for assistance with any issues or questions. CodeRabbit Commands (Invoked using PR comments)
Other keywords and placeholders
CodeRabbit Configuration File (
|
There was a problem hiding this comment.
Actionable comments posted: 0
🧹 Nitpick comments (2)
src/transport.ts (1)
39-50: Consider adding collision check for MCP session IDWhile UUID collision is extremely unlikely, consider adding a check to ensure the generated
mcpSessionIddoesn't already exist in your session tracking system for complete robustness.src/sessionManager.ts (1)
20-27: Consider consistent logging approachThe function mixes
console.logfor info messages andprocess.stderr.writefor warnings. Consider using a consistent logging approach throughout the module.
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
⛔ Files ignored due to path filters (1)
package-lock.jsonis excluded by!**/package-lock.json
📒 Files selected for processing (10)
.gitignore(1 hunks)src/context.ts(4 hunks)src/index.ts(2 hunks)src/program.ts(2 hunks)src/server.ts(1 hunks)src/sessionManager.ts(8 hunks)src/tools/index.ts(0 hunks)src/tools/multiSession.ts(1 hunks)src/tools/session.ts(0 hunks)src/transport.ts(2 hunks)
💤 Files with no reviewable changes (2)
- src/tools/index.ts
- src/tools/session.ts
🧰 Additional context used
🧬 Code Graph Analysis (4)
src/program.ts (2)
src/server.ts (1)
ServerList(3-55)src/transport.ts (1)
startHttpTransport(76-127)
src/transport.ts (1)
src/server.ts (1)
ServerList(3-55)
src/context.ts (3)
config.d.ts (1)
Config(4-101)src/sessionManager.ts (2)
getSession(257-305)closeSession(360-377)src/types/types.ts (2)
BrowserSession(25-30)MCPTool(38-38)
src/index.ts (1)
src/context.ts (1)
Context(9-162)
🔇 Additional comments (26)
.gitignore (1)
296-297: Addition of.ideadirectory looks goodIgnoring IDE-specific metadata keeps the repository clean. No further action required.
src/program.ts (3)
8-8: LGTM: Stdio transport removal is consistent with the refactoring.The removal of
startStdioTransportfrom the import aligns with the broader architectural change to HTTP-only transport.
71-76: LGTM: Port validation enforces HTTP-only mode correctly.The mandatory port requirement with clear error messaging properly enforces the new HTTP-only transport mode.
82-93: LGTM: Server factory integrates well with session-aware architecture.The server factory properly accepts and logs the
mcpSessionIdparameter, which integrates correctly with the updatedServerListclass that manages servers by session ID. The unconditional call tostartHttpTransportis appropriate given the removal of stdio transport.src/index.ts (3)
118-124: LGTM: Function signature correctly supports session-aware architecture.Adding the optional
mcpSessionIdparameter maintains flexibility while enabling the session-aware server creation pattern.
132-136: LGTM: Session ID validation is necessary and well-implemented.The validation correctly enforces the requirement for
mcpSessionIdin HTTP mode, which is necessary for the session-aware Context class integration.
157-158: LGTM: Context integration with session ID is correct.The Context constructor call properly passes the
mcpSessionId, which aligns with the updated Context class that requires this parameter for session-scoped browser management.src/tools/multiSession.ts (1)
67-81: LGTM: Context wrapper refactoring improves clarity and aligns with Context class changes.The shift from
Object.create()to object spreading with explicit method overrides is cleaner and more maintainable. ThecurrentSessionIdgetter correctly aligns with the updated Context class design where this property is read-only.src/server.ts (4)
4-9: LGTM: Map-based server storage enables proper session management.Changing from array to
Map<string, Server>allows for efficient session-keyed server management, and the updated factory signature supports session-aware server creation.
11-28: LGTM: Create method properly handles session-aware server management.The method correctly uses the
mcpSessionIdas a key when provided and falls back to generating a unique timestamp-based key for legacy scenarios. The enhanced logging provides good visibility into server lifecycle management.
30-44: LGTM: Close method correctly handles Map-based server removal.The method properly finds the server by reference in the Map and removes it by key. The enhanced logging provides good traceability for server lifecycle management.
46-55: LGTM: CloseAll method efficiently handles bulk server closure.The method correctly converts Map values to an array for concurrent closure and properly clears the Map afterward. The logging provides good visibility into the bulk closure process.
src/context.ts (6)
6-6: LGTM: Import change aligns with explicit session management.Replacing
defaultSessionIdwithcloseSessionis consistent with the removal of default session concepts in favor of explicit session lifecycle management.
11-24: LGTM: Constructor properly establishes session-aware context.The required
mcpSessionIdparameter and generatedbrowserSessionIdcreate a clear hierarchy between MCP sessions and browser sessions. The logging provides excellent traceability for debugging session-related issues.
26-29: LGTM: Read-only getter prevents accidental session ID modification.Making
currentSessionIda read-only getter that returnsbrowserSessionIdprovides clear access to the session identifier while preventing accidental modifications.
38-48: LGTM: Enhanced getStagehand method supports flexible session targeting.The optional
sessionIdparameter enables both default and explicit session targeting, while the enhanced logging and improved error messages provide better debugging support.
52-68: LGTM: Consistent logging pattern improves debugging capability.The enhanced logging with both MCP and browser session IDs provides excellent context for debugging session-related issues across all browser access methods.
80-96: LGTM: Cleanup method provides proper session lifecycle management.The
cleanupmethod correctly uses thecloseSessionfunction for browser session cleanup, with appropriate error handling and comprehensive logging to ensure reliable resource management during MCP session termination.src/transport.ts (4)
8-12: Good interface design for session trackingThe
TransportSessioninterface effectively groups related session data, improving code organization and type safety.
22-36: Comprehensive session lookup with appropriate loggingThe session lookup logic properly handles missing sessions and provides detailed logging for debugging.
52-63: Robust cleanup logic with detailed loggingThe cleanup handler properly checks for
transport.sessionIdexistence before attempting deletion and provides comprehensive logging for debugging session lifecycle.
81-84: Clear startup logging and proper type usageThe addition of startup logging and the updated map type properly align with the new session tracking approach.
src/sessionManager.ts (4)
10-14: Appropriate state management improvementsThe nullable
activeSessionIdbetter represents session state, and the initialization logging aids debugging.
113-123: Clean session disconnect handlingThe simplified disconnect handler properly manages the active session state without the complexity of default session handling.
256-305: Excellent session management refactoringThe refactored
getSessionfunction provides clear session validation, proper cleanup of stale sessions, and comprehensive logging. The removal of default session logic simplifies the code while maintaining robustness.
356-377: Well-implemented session closure functionThe new
closeSessionfunction follows established patterns, handles edge cases properly, and maintains consistency with other session management functions.
Summary by CodeRabbit
New Features
Bug Fixes
Refactor
Chores
.gitignoreto exclude.ideadirectory.