Skip to content

Commit 45d7713

Browse files
committed
update RequestSession
1 parent 51fbd1d commit 45d7713

File tree

2 files changed

+15
-15
lines changed

2 files changed

+15
-15
lines changed

server/request_session.go

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,11 @@ type requestIDKey struct{}
1111
// RequestSession represents an exchange with MCP client, and provides
1212
// methods to interact with the client and query its capabilities.
1313
type RequestSession struct {
14-
mcpServer *MCPServer
15-
1614
progressToken *mcp.ProgressToken
1715
}
1816

19-
func NewRequestSession(mcpServer *MCPServer, requestParamMeta *mcp.Meta) RequestSession {
20-
requestSession := RequestSession{
21-
mcpServer: mcpServer,
22-
}
17+
func NewRequestSession(requestParamMeta *mcp.Meta) RequestSession {
18+
requestSession := RequestSession{}
2319

2420
// server should send progress notification if request metadata includes a progressToken
2521
if requestParamMeta != nil && requestParamMeta.ProgressToken != nil {
@@ -30,14 +26,15 @@ func NewRequestSession(mcpServer *MCPServer, requestParamMeta *mcp.Meta) Request
3026
}
3127

3228
// IsLoggingNotificationSupported returns true if server supports logging notification
33-
func (exchange *RequestSession) IsLoggingNotificationSupported() bool {
34-
return exchange.mcpServer != nil && exchange.mcpServer.capabilities.logging != nil && *exchange.mcpServer.capabilities.logging
29+
func (exchange *RequestSession) IsLoggingNotificationSupported(ctx context.Context) bool {
30+
mcpServer := ServerFromContext(ctx)
31+
return mcpServer != nil && mcpServer.capabilities.logging != nil && *mcpServer.capabilities.logging
3532
}
3633

3734
// SendLoggingNotification send logging notification to client.
3835
// If server does not support logging notification, this method will do nothing.
3936
func (exchange *RequestSession) SendLoggingNotification(ctx context.Context, level mcp.LoggingLevel, message map[string]any) error {
40-
if !exchange.IsLoggingNotificationSupported() {
37+
if !exchange.IsLoggingNotificationSupported(ctx) {
4138
return nil
4239
}
4340

@@ -58,7 +55,8 @@ func (exchange *RequestSession) SendLoggingNotification(ctx context.Context, lev
5855
params["logger"] = *ClientSessionFromContext(ctx).GetLoggerName()
5956
}
6057

61-
return exchange.mcpServer.SendNotificationToClient(
58+
mcpServer := ServerFromContext(ctx)
59+
return mcpServer.SendNotificationToClient(
6260
ctx,
6361
string(mcp.MethodNotificationMessage),
6462
params,
@@ -82,7 +80,8 @@ func (exchange *RequestSession) SendProgressNotification(ctx context.Context, pr
8280
params["message"] = *message
8381
}
8482

85-
return exchange.mcpServer.SendNotificationToClient(
83+
mcpServer := ServerFromContext(ctx)
84+
return mcpServer.SendNotificationToClient(
8685
ctx,
8786
string(mcp.MethodNotificationProgress),
8887
params,
@@ -108,7 +107,8 @@ func (exchange *RequestSession) SendCancellationNotification(ctx context.Context
108107
params["reason"] = reason
109108
}
110109

111-
return exchange.mcpServer.SendNotificationToClient(
110+
mcpServer := ServerFromContext(ctx)
111+
return mcpServer.SendNotificationToClient(
112112
ctx,
113113
string(mcp.MethodNotificationCancellation),
114114
params,

server/server.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -728,7 +728,7 @@ func (s *MCPServer) handleReadResource(
728728
request mcp.ReadResourceRequest,
729729
) (*mcp.ReadResourceResult, *requestError) {
730730

731-
requestSession := NewRequestSession(s, request.Params.Meta)
731+
requestSession := NewRequestSession(request.Params.Meta)
732732

733733
s.resourcesMu.RLock()
734734
// First try direct resource handlers
@@ -848,7 +848,7 @@ func (s *MCPServer) handleGetPrompt(
848848
}
849849
}
850850

851-
requestSession := NewRequestSession(s, request.Params.Meta)
851+
requestSession := NewRequestSession(request.Params.Meta)
852852
result, err := handler(ctx, requestSession, request)
853853
if err != nil {
854854
return nil, &requestError{
@@ -999,7 +999,7 @@ func (s *MCPServer) handleToolCall(
999999
finalHandler = mw[i](finalHandler)
10001000
}
10011001

1002-
requestSession := NewRequestSession(s, request.Params.Meta)
1002+
requestSession := NewRequestSession(request.Params.Meta)
10031003
result, err := finalHandler(ctx, requestSession, request)
10041004
if err != nil {
10051005
return nil, &requestError{

0 commit comments

Comments
 (0)