Registers a sending middleware on each inner server that intercepts all outgoing messages and routes them through the front session using a session-swapped request wrapper. Replaces the previous proxy client handler approach which dropped log messages in stateless mode. ## Motivation and Context Tool handlers in variant servers use req.Session (the inner session) to send notifications. Previously, log messages were silently dropped in stateless mode because the inner session is not aware of the front session. This change injects the sendingMethodHandler from the frontSession into the context and adds a sending middleware to redirect all sending requests to the frontSession handler directly. A few considerations in this implementation: * Having frontSession in the context allows notifications to be sent to the correct frontSession, when the same backend session could be used by multiple front sessions. * This solution will work for synchronous notifications that happen within the same context (progress, log, sampling, elicitation) but not working for asynchronous notifications (tool list changed, resource updated etc), because asynchronous notifications are sent with a new context. * All the operations before the sending method will still happen in the backend. Most notable impact is in stateless mode, requests from one client could change the log level for other clients. For this reason, I default the logging level to debug for now. * This implementation relies on the MCP Go SDK sendingMethodHandler taking a generic `Request` not one that tied to specific function. Based on the current SDK implementation this is unlikely to change. * The same generic method forwarding mechanism could work for receiving methods too, but with some caveat. That can be addressed in separate PR. ## How Has This Been Tested? TestNotificationDelivery — verifies progress and log messages arrive at the front clients with multiple clients making concurrent request. New tests added for context preservation that was added in https://github.com/modelcontextprotocol/experimental-ext-variants/pull/6. Context preservation is required to make the notification forwarding work anyway. Since we use the context to carry frontSession handler to the backend session. All existing tests pass ## Breaking Changes None. Tool handlers continue to use req.Session normally. ## Types of changes Bug fix (non-breaking change which fixes an issue)
149 lines
4.2 KiB
Go
149 lines
4.2 KiB
Go
package variants
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"testing"
|
|
|
|
"github.com/modelcontextprotocol/go-sdk/mcp"
|
|
"github.com/stretchr/testify/assert"
|
|
"github.com/stretchr/testify/require"
|
|
)
|
|
|
|
type contextKey string
|
|
|
|
const testCtxKey contextKey = "test-value"
|
|
|
|
type checkContextOutput struct {
|
|
ContextValue string `json:"contextValue"`
|
|
VariantID string `json:"variantId"`
|
|
HasExtra bool `json:"hasExtra"`
|
|
CustomHeader string `json:"customHeader"`
|
|
}
|
|
|
|
type emptyInput struct{}
|
|
|
|
// headerRoundTripper injects custom headers into every outgoing HTTP request.
|
|
type headerRoundTripper struct {
|
|
base http.RoundTripper
|
|
header http.Header
|
|
}
|
|
|
|
func (h *headerRoundTripper) RoundTrip(req *http.Request) (*http.Response, error) {
|
|
req = req.Clone(req.Context())
|
|
for k, v := range h.header {
|
|
req.Header[k] = v
|
|
}
|
|
return h.base.RoundTrip(req)
|
|
}
|
|
|
|
func newContextTestInnerServer() *mcp.Server {
|
|
inner := mcp.NewServer(&mcp.Implementation{Name: "ctx-test", Version: "v1.0.0"}, nil)
|
|
|
|
mcp.AddTool(inner, &mcp.Tool{
|
|
Name: "check_context",
|
|
Description: "Reports context and request metadata",
|
|
}, func(ctx context.Context, req *mcp.CallToolRequest, _ emptyInput) (*mcp.CallToolResult, checkContextOutput, error) {
|
|
out := checkContextOutput{}
|
|
|
|
if v, ok := ctx.Value(testCtxKey).(string); ok {
|
|
out.ContextValue = v
|
|
}
|
|
|
|
if req.Extra != nil {
|
|
out.HasExtra = true
|
|
if req.Extra.Header != nil {
|
|
out.CustomHeader = req.Extra.Header.Get("X-Test-Header")
|
|
}
|
|
}
|
|
|
|
if req.Params != nil {
|
|
meta := req.Params.GetMeta()
|
|
if v, ok := meta[metaKeyVariant].(string); ok {
|
|
out.VariantID = v
|
|
}
|
|
}
|
|
|
|
return nil, out, nil
|
|
})
|
|
|
|
return inner
|
|
}
|
|
|
|
func parseCheckContextOutput(t *testing.T, result *mcp.CallToolResult) checkContextOutput {
|
|
t.Helper()
|
|
require.NotEmpty(t, result.Content)
|
|
tc, ok := result.Content[0].(*mcp.TextContent)
|
|
require.True(t, ok, "expected TextContent, got %T", result.Content[0])
|
|
|
|
var out checkContextOutput
|
|
require.NoError(t, json.Unmarshal([]byte(tc.Text), &out))
|
|
return out
|
|
}
|
|
|
|
// TestContextPreservation verifies that context values set by middleware on
|
|
// the front (outer) mcp.Server and HTTP request headers are preserved through
|
|
// the variant dispatch layer and reach the inner server's tool handler.
|
|
func TestContextPreservation(t *testing.T) {
|
|
vs := NewServer(&mcp.Implementation{Name: "ctx-test", Version: "v1.0.0"}).
|
|
WithVariant(ServerVariant{
|
|
ID: "test-variant",
|
|
Description: "Context test variant",
|
|
Status: Stable,
|
|
}, newContextTestInnerServer(), 0)
|
|
|
|
frontServer, err := vs.mcpServer(false)
|
|
require.NoError(t, err)
|
|
|
|
frontServer.AddReceivingMiddleware(func(next mcp.MethodHandler) mcp.MethodHandler {
|
|
return func(ctx context.Context, method string, req mcp.Request) (mcp.Result, error) {
|
|
ctx = context.WithValue(ctx, testCtxKey, "from-middleware")
|
|
return next(ctx, method, req)
|
|
}
|
|
})
|
|
|
|
handler := mcp.NewStreamableHTTPHandler(
|
|
func(*http.Request) *mcp.Server { return frontServer },
|
|
nil,
|
|
)
|
|
httpSrv := httptest.NewServer(handler)
|
|
t.Cleanup(httpSrv.Close)
|
|
|
|
client := mcp.NewClient(
|
|
&mcp.Implementation{Name: "ctx-test-client", Version: "v0.0.1"},
|
|
nil,
|
|
)
|
|
|
|
session, err := client.Connect(context.Background(), &mcp.StreamableClientTransport{
|
|
Endpoint: httpSrv.URL,
|
|
HTTPClient: &http.Client{
|
|
Transport: &headerRoundTripper{
|
|
base: http.DefaultTransport,
|
|
header: http.Header{
|
|
"X-Test-Header": []string{"test-header-value"},
|
|
},
|
|
},
|
|
},
|
|
}, nil)
|
|
require.NoError(t, err)
|
|
t.Cleanup(func() { session.Close() })
|
|
|
|
result, err := session.CallTool(context.Background(), &mcp.CallToolParams{
|
|
Name: "check_context",
|
|
Arguments: map[string]json.RawMessage{},
|
|
})
|
|
require.NoError(t, err)
|
|
|
|
out := parseCheckContextOutput(t, result)
|
|
|
|
assert.Equal(t, "from-middleware", out.ContextValue,
|
|
"context value from front server middleware should be preserved through dispatch")
|
|
assert.Equal(t, "test-variant", out.VariantID,
|
|
"variant ID should be injected into request _meta")
|
|
assert.True(t, out.HasExtra,
|
|
"RequestExtra should be forwarded to inner tool handler")
|
|
assert.Equal(t, "test-header-value", out.CustomHeader,
|
|
"HTTP headers should be captured in RequestExtra and forwarded through dispatch")
|
|
}
|