fix: update sessionIdGenerator#320
Conversation
## Walkthrough
The changes update the type signature of the `sessionIdGenerator` property in the MCP configuration to require a `Context` parameter, modify its usage to pass the current context when generating session IDs, add a new configuration implementation that uses request headers or UUID fallback, and update tests to verify custom session ID propagation.
## Changes
| File(s) | Change Summary |
|-----------------------------------------------------------------------------------------|---------------------------------------------------------------------------------------------------------------|
| plugin/controller/lib/impl/mcp/MCPConfig.ts | Changed `sessionIdGenerator` type in `MCPConfigOptions` and `MCPConfig` from `() => string` to `(ctx: Context) => string`. |
| plugin/controller/lib/impl/mcp/MCPControllerRegister.ts | Updated call to `sessionIdGenerator` to pass current context `ctx` as argument. |
| plugin/controller/test/fixtures/apps/mcp-app/config/config.default.js | Added `mcp` config with `sessionIdGenerator(ctx)` that uses request header `'custom-session-id'` or falls back to UUID. |
| plugin/controller/test/mcp/mcp.test.ts | Modified test to send custom header `'custom-session-id'` and assert `sessionId` matches this custom session ID. |
| plugin/controller/test/mcp/mcpCluster.test.ts | Modified test to send custom header `'custom-session-id'` and assert `sessionId` matches this custom session ID. |
## Sequence Diagram(s)
```mermaid
sequenceDiagram
participant Client
participant MCPControllerRegister
participant MCPConfig
participant SessionIdGenerator
Client->>MCPControllerRegister: HTTP request received
MCPControllerRegister->>MCPConfig: Access sessionIdGenerator
MCPControllerRegister->>SessionIdGenerator: sessionIdGenerator(ctx)
SessionIdGenerator-->>MCPControllerRegister: Returns session ID
MCPControllerRegister-->>Client: Continue processing with session IDSuggested reviewers
Poem
|
There was a problem hiding this comment.
Actionable comments posted: 0
🧹 Nitpick comments (1)
plugin/controller/lib/impl/mcp/MCPControllerRegister.ts (1)
223-245: Improved request body parsing with proper error handling.The code now consistently parses the request body upfront with robust error handling, which is a good practice. However, there's a minor grammar issue in the error message.
- message: `Bad Request: body should is json, ${e.toString()}`, + message: `Bad Request: body should be json, ${e.toString()}`,
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (2)
plugin/controller/lib/impl/mcp/MCPConfig.ts(3 hunks)plugin/controller/lib/impl/mcp/MCPControllerRegister.ts(3 hunks)
🧰 Additional context used
🧬 Code Graph Analysis (1)
plugin/controller/lib/impl/mcp/MCPControllerRegister.ts (1)
plugin/controller/lib/impl/mcp/MCPConfig.ts (1)
eventStore(55-57)
⏰ Context from checks skipped due to timeout of 90000ms (9)
- GitHub Check: Runner-macos (20)
- GitHub Check: Analyze (typescript)
- GitHub Check: Analyze (javascript)
- GitHub Check: Runner-macos (16)
- GitHub Check: Runner-ubuntu (22)
- GitHub Check: Runner-ubuntu (16)
- GitHub Check: Runner-macos (18)
- GitHub Check: Runner-ubuntu (18)
- GitHub Check: Runner-ubuntu (20)
🔇 Additional comments (7)
plugin/controller/lib/impl/mcp/MCPConfig.ts (3)
1-1: Appropriate import addition for updated type definition.The addition of the
Contextimport from 'egg' properly supports the type expansion of thesessionIdGeneratorfunction.
11-11: Good type expansion for more flexible session ID generation.The expanded type definition allows the
sessionIdGeneratorto optionally accept a request context parameter, providing more flexibility while maintaining backward compatibility with existing implementations.
21-21: Consistent type updating in class property.The private property's type definition is properly updated to match the interface, ensuring type consistency throughout the code.
plugin/controller/lib/impl/mcp/MCPControllerRegister.ts (4)
246-274: Well-structured initialization request handling.The initialization request handling is now more clearly structured. The refactored code extracts the event store, creates a transport with the correct session ID generator, and properly handles the request with the parsed body.
290-290: Consistent request body handling.The transport handler now correctly uses the pre-parsed body, ensuring consistent request processing across different conditions.
304-315: Improved error handling for missing session ID.Adding explicit error handling for requests without a valid session ID improves the robustness of the code and provides clearer feedback to clients.
251-251:Details
✅ Verification successful
Context-aware session ID generation.
The updated code now properly passes the context to the session ID generator, aligning with the expanded type definition in MCPConfig.ts.
🏁 Script executed:
#!/bin/bash # Check if there are any custom implementations of sessionIdGenerator that might break with the new signature # Search for custom implementations of sessionIdGenerator rg -A 5 -B 5 "sessionIdGenerator\s*:" --type tsLength of output: 5214
Approved: Context-aware session ID Generation
The
MCPControllerRegisternow correctly passesctxtosessionIdGenerator, matching the expanded((ctx?: Context) => string)signature inMCPConfig.ts. We verified there are no custom implementations ofsessionIdGeneratorthat would break with this change.
- plugin/controller/lib/impl/mcp/MCPControllerRegister.ts:251
dc6aec7 to
2d77a39
Compare
4ba612d to
410d745
Compare
410d745 to
deba55a
Compare
Checklist
npm testpassesAffected core subsystem(s)
Description of change
Summary by CodeRabbit