Skip to content

feat(fxmcpserver): Updated context handling #340

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 6 commits into from
May 7, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 8 additions & 10 deletions fxmcpserver/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -149,12 +149,9 @@
}

// ProvideMCPSSEServerParam allows injection of the required dependencies in ProvideMCPSSEServer.
//
//nolint:containedctx
type ProvideMCPSSEServerParam struct {
fx.In
LifeCycle fx.Lifecycle
Context context.Context
Logger *log.Logger
Config *config.Config
MCPServer *server.MCPServer
Expand All @@ -169,12 +166,14 @@
server.WithSSEContextFunc(p.MCPSSEServerContextHandler.Handle()),
)

sseServerCtx := p.Logger.WithContext(context.Background())

if p.Config.GetBool("modules.mcp.server.transport.sse.expose") {
p.LifeCycle.Append(fx.Hook{
OnStart: func(context.Context) error {
if !p.Config.IsTestEnv() {
//nolint:contextcheck,errcheck
go sseServer.Start(p.Context)
//nolint:errcheck
go sseServer.Start(sseServerCtx)

Check warning on line 176 in fxmcpserver/module.go

View check run for this annotation

Codecov / codecov/patch

fxmcpserver/module.go#L175-L176

Added lines #L175 - L176 were not covered by tests
}

return nil
Expand Down Expand Up @@ -224,12 +223,9 @@
}

// ProvideMCPStdioServerParam allows injection of the required dependencies in ProvideMCPStdioServer.
//
//nolint:containedctx
type ProvideMCPStdioServerParam struct {
fx.In
LifeCycle fx.Lifecycle
Context context.Context
Logger *log.Logger
Config *config.Config
MCPServer *server.MCPServer
Expand All @@ -244,12 +240,14 @@
server.WithStdioContextFunc(p.MCPStdioServerContextHandler.Handle()),
)

stdioServerCtx := p.Logger.WithContext(context.Background())

if p.Config.GetBool("modules.mcp.server.transport.stdio.expose") {
p.LifeCycle.Append(fx.Hook{
OnStart: func(context.Context) error {
if !p.Config.IsTestEnv() {
//nolint:contextcheck,errcheck
go stdioServer.Start(p.Context)
//nolint:errcheck
go stdioServer.Start(stdioServerCtx)

Check warning on line 250 in fxmcpserver/module.go

View check run for this annotation

Codecov / codecov/patch

fxmcpserver/module.go#L249-L250

Added lines #L249 - L250 were not covered by tests
}

return nil
Expand Down
13 changes: 8 additions & 5 deletions fxmcpserver/server/sse/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,21 +43,21 @@ func NewDefaultMCPSSEServerContextHandler(

// Handle returns the handler func.
func (h *DefaultMCPSSEServerContextHandler) Handle() server.SSEContextFunc {
return func(ctx context.Context, r *http.Request) context.Context {
return func(ctx context.Context, req *http.Request) context.Context {
// start time propagation
ctx = fsc.WithStartTime(ctx, time.Now())

// sessionId propagation
sID := r.URL.Query().Get("sessionId")
sID := req.URL.Query().Get("sessionId")

ctx = fsc.WithSessionID(ctx, sID)

// requestId propagation
rID := r.Header.Get("X-Request-Id")
rID := req.Header.Get("X-Request-Id")

if rID == "" {
rID = h.generator.Generate()
r.Header.Set("X-Request-Id", rID)
req.Header.Set("X-Request-Id", rID)
}

ctx = fsc.WithRequestID(ctx, rID)
Expand Down Expand Up @@ -89,6 +89,9 @@ func (h *DefaultMCPSSEServerContextHandler) Handle() server.SSEContextFunc {
Str("mcpRequestID", rID).
Logger()

return logger.WithContext(ctx)
ctx = logger.WithContext(ctx)

// cancellation removal
return context.WithoutCancel(ctx)
}
}
5 changes: 4 additions & 1 deletion fxmcpserver/server/stdio/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,9 @@ func (h *DefaultMCPStdioServerContextHandler) Handle() server.StdioContextFunc {
Str("mcpRequestID", rID).
Logger()

return logger.WithContext(ctx)
ctx = logger.WithContext(ctx)

// cancellation removal
return context.WithoutCancel(ctx)
}
}
Loading