-
Notifications
You must be signed in to change notification settings - Fork 223
Expand file tree
/
Copy pathsession_management_integration_test.go
More file actions
907 lines (787 loc) · 32.7 KB
/
session_management_integration_test.go
File metadata and controls
907 lines (787 loc) · 32.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
// SPDX-FileCopyrightText: Copyright 2025 Stacklok, Inc.
// SPDX-License-Identifier: Apache-2.0
package server_test
import (
"bytes"
"context"
"encoding/json"
"io"
"net/http"
"net/http/httptest"
"sync"
"sync/atomic"
"testing"
"time"
mcpmcp "github.com/mark3labs/mcp-go/mcp"
mcpsdk "github.com/mark3labs/mcp-go/server"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"go.uber.org/mock/gomock"
"github.com/stacklok/toolhive/pkg/auth"
transportsession "github.com/stacklok/toolhive/pkg/transport/session"
"github.com/stacklok/toolhive/pkg/vmcp"
"github.com/stacklok/toolhive/pkg/vmcp/aggregator"
"github.com/stacklok/toolhive/pkg/vmcp/composer"
discoveryMocks "github.com/stacklok/toolhive/pkg/vmcp/discovery/mocks"
"github.com/stacklok/toolhive/pkg/vmcp/mocks"
"github.com/stacklok/toolhive/pkg/vmcp/optimizer"
"github.com/stacklok/toolhive/pkg/vmcp/router"
"github.com/stacklok/toolhive/pkg/vmcp/server"
vmcpsession "github.com/stacklok/toolhive/pkg/vmcp/session"
sessionfactorymocks "github.com/stacklok/toolhive/pkg/vmcp/session/mocks"
sessionmocks "github.com/stacklok/toolhive/pkg/vmcp/session/types/mocks"
)
// ---------------------------------------------------------------------------
// Mock factory helpers
// ---------------------------------------------------------------------------
// newNoopMockFactory creates a MockMultiSessionFactory that permits any number
// of MakeSessionWithID calls (including zero). Each call returns a minimal
// MockMultiSession with no tools. Use for tests that construct a Server and
// may or may not trigger session creation but don't need to inspect the result.
func newNoopMockFactory(t *testing.T) *sessionfactorymocks.MockMultiSessionFactory {
t.Helper()
ctrl := gomock.NewController(t)
factory := sessionfactorymocks.NewMockMultiSessionFactory(ctrl)
factory.EXPECT().MakeSessionWithID(gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any()).
DoAndReturn(func(_ context.Context, id string, _ *auth.Identity, _ []*vmcp.Backend) (vmcpsession.MultiSession, error) {
mock := sessionmocks.NewMockMultiSession(ctrl)
mock.EXPECT().ID().Return(id).AnyTimes()
mock.EXPECT().UpdatedAt().Return(time.Time{}).AnyTimes()
mock.EXPECT().CreatedAt().Return(time.Time{}).AnyTimes()
mock.EXPECT().Type().Return(transportsession.SessionType("")).AnyTimes()
mock.EXPECT().GetData().Return(nil).AnyTimes()
mock.EXPECT().SetData(gomock.Any()).AnyTimes()
mock.EXPECT().GetMetadata().Return(map[string]string{}).AnyTimes()
mock.EXPECT().SetMetadata(gomock.Any(), gomock.Any()).AnyTimes()
mock.EXPECT().Tools().Return(nil).AnyTimes()
mock.EXPECT().AllTools().Return(nil).AnyTimes()
mock.EXPECT().Resources().Return(nil).AnyTimes()
mock.EXPECT().Prompts().Return(nil).AnyTimes()
mock.EXPECT().BackendSessions().Return(nil).AnyTimes()
mock.EXPECT().GetRoutingTable().Return(nil).AnyTimes()
mock.EXPECT().ReadResource(gomock.Any(), gomock.Any(), gomock.Any()).Return(nil, nil).AnyTimes()
mock.EXPECT().GetPrompt(gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any()).Return(nil, nil).AnyTimes()
mock.EXPECT().CallTool(gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any()).
Return(&vmcp.ToolCallResult{Content: []vmcp.Content{{Type: "text", Text: "noop"}}}, nil).AnyTimes()
mock.EXPECT().Close().Return(nil).AnyTimes()
return mock, nil
}).AnyTimes()
return factory
}
// mockFactoryState tracks observable behaviour of a mock session factory.
type mockFactoryState struct {
makeWithIDCalled atomic.Bool
callToolCalled atomic.Bool
closed atomic.Bool
mu sync.Mutex
lastSession *sessionmocks.MockMultiSession
}
// newMockFactory creates a MockMultiSessionFactory whose MakeSessionWithID returns
// a fully-configured MockMultiSession. The returned state tracks what happened.
func newMockFactory(t *testing.T, ctrl *gomock.Controller, tools []vmcp.Tool) (*sessionfactorymocks.MockMultiSessionFactory, *mockFactoryState) {
t.Helper()
state := &mockFactoryState{}
factory := sessionfactorymocks.NewMockMultiSessionFactory(ctrl)
factory.EXPECT().MakeSessionWithID(gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any()).
DoAndReturn(func(_ context.Context, id string, _ *auth.Identity, _ []*vmcp.Backend) (vmcpsession.MultiSession, error) {
state.makeWithIDCalled.Store(true)
// All sessions carry MetadataKeyIdentityBinding so Terminate takes the
// Phase 2 (storage.Delete) path. The sentinel value is sufficient for
// tests that don't validate the binding content.
mock := sessionmocks.NewMockMultiSession(ctrl)
mock.EXPECT().ID().Return(id).AnyTimes()
mock.EXPECT().UpdatedAt().Return(time.Time{}).AnyTimes()
mock.EXPECT().CreatedAt().Return(time.Time{}).AnyTimes()
mock.EXPECT().Type().Return(transportsession.SessionType("")).AnyTimes()
mock.EXPECT().GetData().Return(nil).AnyTimes()
mock.EXPECT().SetData(gomock.Any()).AnyTimes()
mock.EXPECT().GetMetadata().Return(map[string]string{
vmcpsession.MetadataKeyIdentityBinding: "unauthenticated",
}).AnyTimes()
mock.EXPECT().SetMetadata(gomock.Any(), gomock.Any()).AnyTimes()
toolsCopy := make([]vmcp.Tool, len(tools))
copy(toolsCopy, tools)
mock.EXPECT().Tools().Return(toolsCopy).AnyTimes()
mock.EXPECT().AllTools().Return(toolsCopy).AnyTimes()
mock.EXPECT().Resources().Return(nil).AnyTimes()
mock.EXPECT().Prompts().Return(nil).AnyTimes()
mock.EXPECT().BackendSessions().Return(nil).AnyTimes()
// Build a routing table from the provided tools so that
// filterWorkflowDefsForSession can check tool accessibility per session.
rt := &vmcp.RoutingTable{Tools: make(map[string]*vmcp.BackendTarget, len(tools))}
for _, tool := range tools {
rt.Tools[tool.Name] = &vmcp.BackendTarget{WorkloadID: tool.Name}
}
mock.EXPECT().GetRoutingTable().Return(rt).AnyTimes()
mock.EXPECT().ReadResource(gomock.Any(), gomock.Any(), gomock.Any()).Return(nil, nil).AnyTimes()
mock.EXPECT().GetPrompt(gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any()).Return(nil, nil).AnyTimes()
callResult := &vmcp.ToolCallResult{Content: []vmcp.Content{{Type: "text", Text: "fake result"}}}
callToolCalled := &state.callToolCalled
mock.EXPECT().CallTool(gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any()).
DoAndReturn(func(_ context.Context, _ *auth.Identity, _ string, _ map[string]any, _ map[string]any) (*vmcp.ToolCallResult, error) {
callToolCalled.Store(true)
return callResult, nil
}).AnyTimes()
closed := &state.closed
mock.EXPECT().Close().DoAndReturn(func() error {
closed.Store(true)
return nil
}).AnyTimes()
state.mu.Lock()
state.lastSession = mock
state.mu.Unlock()
return mock, nil
}).AnyTimes()
return factory, state
}
// ---------------------------------------------------------------------------
// Helpers
// ---------------------------------------------------------------------------
// serverOptions holds optional configuration extensions for buildTestServerWithOptions.
type serverOptions struct {
workflowDefs map[string]*composer.WorkflowDefinition
optimizerFactory func(context.Context, []mcpsdk.ServerTool) (optimizer.Optimizer, error)
}
// buildTestServer constructs a vMCP server with session management enabled,
// backed by mock discovery infrastructure, and returns the httptest.Server
// and the session factory so tests can inspect state.
//
// The returned httptest.Server is closed automatically via t.Cleanup.
func buildTestServer(
t *testing.T,
factory vmcpsession.MultiSessionFactory,
) *httptest.Server {
t.Helper()
return buildTestServerWithOptions(t, factory, serverOptions{})
}
// buildTestServerWithOptions is like buildTestServer but accepts optional workflow
// definitions and an optimizer factory, enabling composite tool and optimizer
// integration tests.
func buildTestServerWithOptions(
t *testing.T,
factory vmcpsession.MultiSessionFactory,
opts serverOptions,
) *httptest.Server {
t.Helper()
ctrl := gomock.NewController(t)
t.Cleanup(ctrl.Finish)
mockBackendClient := mocks.NewMockBackendClient(ctrl)
mockDiscoveryMgr := discoveryMocks.NewMockManager(ctrl)
mockBackendRegistry := mocks.NewMockBackendRegistry(ctrl)
// The discovery middleware calls List() + Discover() for every initialize request.
// Return an empty (non-nil) AggregatedCapabilities so the middleware does not
// dereference a nil pointer when logging tool/resource counts.
emptyAggCaps := &aggregator.AggregatedCapabilities{}
mockBackendRegistry.EXPECT().List(gomock.Any()).Return(nil).AnyTimes()
mockDiscoveryMgr.EXPECT().Discover(gomock.Any(), gomock.Any()).Return(emptyAggCaps, nil).AnyTimes()
// Stop is called when the server is stopped (not via httptest but via session manager cleanup).
mockDiscoveryMgr.EXPECT().Stop().AnyTimes()
rt := router.NewDefaultRouter()
srv, err := server.New(
context.Background(),
&server.Config{
Host: "127.0.0.1",
Port: 0,
SessionTTL: 5 * time.Minute,
SessionFactory: factory,
OptimizerFactory: opts.optimizerFactory,
},
rt,
mockBackendClient,
mockDiscoveryMgr,
mockBackendRegistry,
opts.workflowDefs,
)
require.NoError(t, err)
handler, err := srv.Handler(context.Background())
require.NoError(t, err)
ts := httptest.NewServer(handler)
t.Cleanup(ts.Close)
return ts
}
// postMCP sends a JSON-RPC POST to /mcp and returns the response.
func postMCP(t *testing.T, baseURL string, body map[string]any, sessionID string) *http.Response {
t.Helper()
rawBody, err := json.Marshal(body)
require.NoError(t, err)
req, err := http.NewRequestWithContext(context.Background(), http.MethodPost, baseURL+"/mcp", bytes.NewReader(rawBody))
require.NoError(t, err)
req.Header.Set("Content-Type", "application/json")
if sessionID != "" {
req.Header.Set("Mcp-Session-Id", sessionID)
}
resp, err := http.DefaultClient.Do(req)
require.NoError(t, err)
return resp
}
// ---------------------------------------------------------------------------
// Integration tests
// ---------------------------------------------------------------------------
// TestIntegration_SessionManagement_Initialize verifies the session management path end-to-end:
//
// 1. An MCP initialize request triggers handleSessionRegistration.
// 2. The fake factory's MakeSessionWithID is called (session created).
// 3. A subsequent tool call routes through the fake session's CallTool.
func TestIntegration_SessionManagement_Initialize(t *testing.T) {
t.Parallel()
ctrl := gomock.NewController(t)
testTool := vmcp.Tool{Name: "test-tool", Description: "a test tool"}
factory, state := newMockFactory(t, ctrl, []vmcp.Tool{testTool})
ts := buildTestServer(t, factory)
// Step 1: Send initialize request.
initReq := map[string]any{
"jsonrpc": "2.0",
"id": 1,
"method": "initialize",
"params": map[string]any{
"protocolVersion": "2025-06-18",
"capabilities": map[string]any{},
"clientInfo": map[string]any{
"name": "test",
"version": "1.0",
},
},
}
initResp := postMCP(t, ts.URL, initReq, "")
defer initResp.Body.Close()
require.Equal(t, http.StatusOK, initResp.StatusCode, "initialize should succeed")
sessionID := initResp.Header.Get("Mcp-Session-Id")
require.NotEmpty(t, sessionID, "session ID should be returned in Mcp-Session-Id header")
// Give the OnRegisterSession hook time to run (it may execute asynchronously
// after the response is sent, but before the next request).
require.Eventually(t, func() bool {
return state.makeWithIDCalled.Load()
}, 2*time.Second, 10*time.Millisecond,
"MakeSessionWithID should have been called after initialize")
// Step 2: Send a tool call and verify it routes through the fake session.
toolCallReq := map[string]any{
"jsonrpc": "2.0",
"id": 2,
"method": "tools/call",
"params": map[string]any{
"name": "test-tool",
"arguments": map[string]any{},
},
}
toolResp := postMCP(t, ts.URL, toolCallReq, sessionID)
defer toolResp.Body.Close()
body, err := io.ReadAll(toolResp.Body)
require.NoError(t, err)
require.Equal(t, http.StatusOK, toolResp.StatusCode,
"tool call should succeed; body: %s", string(body))
// The mock session's CallTool should have been invoked.
state.mu.Lock()
lastSession := state.lastSession
state.mu.Unlock()
require.NotNil(t, lastSession, "factory should have created a session")
assert.True(t, state.callToolCalled.Load(),
"CallTool on the mock session should have been invoked by the tool call request")
}
// deleteMCP sends a DELETE request to /mcp with the given session ID and
// returns the response. Used to exercise the session termination path.
func deleteMCP(t *testing.T, baseURL, sessionID string) *http.Response {
t.Helper()
req, err := http.NewRequestWithContext(
context.Background(), http.MethodDelete, baseURL+"/mcp", nil,
)
require.NoError(t, err)
if sessionID != "" {
req.Header.Set("Mcp-Session-Id", sessionID)
}
resp, err := http.DefaultClient.Do(req)
require.NoError(t, err)
return resp
}
// TestIntegration_SessionManagement_Termination verifies the termination path:
//
// 1. An initialize request creates a MultiSession.
// 2. A DELETE request calls Terminate(), which calls Close() on the MultiSession,
// releasing backend connections.
// 3. Subsequent requests with the terminated session ID are rejected.
func TestIntegration_SessionManagement_Termination(t *testing.T) {
t.Parallel()
ctrl := gomock.NewController(t)
testTool := vmcp.Tool{Name: "test-tool", Description: "a test tool"}
factory, state := newMockFactory(t, ctrl, []vmcp.Tool{testTool})
ts := buildTestServer(t, factory)
// Step 1: Initialize and obtain a session ID.
initReq := map[string]any{
"jsonrpc": "2.0",
"id": 1,
"method": "initialize",
"params": map[string]any{
"protocolVersion": "2025-06-18",
"capabilities": map[string]any{},
"clientInfo": map[string]any{"name": "test", "version": "1.0"},
},
}
initResp := postMCP(t, ts.URL, initReq, "")
defer initResp.Body.Close()
require.Equal(t, http.StatusOK, initResp.StatusCode)
sessionID := initResp.Header.Get("Mcp-Session-Id")
require.NotEmpty(t, sessionID)
// Wait for the OnRegisterSession hook to complete so the MultiSession exists.
require.Eventually(t, func() bool {
return state.makeWithIDCalled.Load()
}, 2*time.Second, 10*time.Millisecond,
"MakeSessionWithID should have been called after initialize")
// Step 2: Terminate the session via DELETE.
delResp := deleteMCP(t, ts.URL, sessionID)
defer delResp.Body.Close()
require.Equal(t, http.StatusOK, delResp.StatusCode, "DELETE should return 200 OK")
state.mu.Lock()
lastSession := state.lastSession
state.mu.Unlock()
require.NotNil(t, lastSession, "factory should have created a session")
// Subsequent requests with the terminated session ID are rejected.
// After Terminate() deletes the session from storage, the discovery middleware passes
// through (no session found → skip capability injection), and the SDK's Validate()
// returns HTTP 404 for the unknown session ID.
// This request also triggers the lazy eviction: GetMultiSession → checkSession →
// ErrExpired → onEvict → Close().
toolCallReq := map[string]any{
"jsonrpc": "2.0",
"id": 2,
"method": "tools/call",
"params": map[string]any{
"name": "test-tool",
"arguments": map[string]any{},
},
}
postResp := postMCP(t, ts.URL, toolCallReq, sessionID)
defer postResp.Body.Close()
assert.Equal(t, http.StatusNotFound, postResp.StatusCode,
"request with terminated session ID should be rejected")
// Close() is called lazily by onEvict when the stale cache entry is
// evicted on the first GetMultiSession call after Terminate deleted the
// session from storage (triggered by the POST above).
assert.Eventually(t, func() bool {
return state.closed.Load()
}, 2*time.Second, 10*time.Millisecond,
"Close() should have been called on the MultiSession after termination")
}
// TestIntegration_SessionManagement_TokenBinding verifies end-to-end token binding security:
//
// 1. Initialize a session with bearer token "token-A"
// 2. Make a tool call with the same token → succeeds
// 3. Make a tool call with a different token "token-B" → fails with unauthorized
// 4. Verify the session is terminated after auth failure
//
// NOTE: This test is currently skipped because the fake factory (fakeMultiSessionFactory)
// doesn't implement real token binding - it uses placeholder metadata instead of real
// HMAC-SHA256 hashes. To properly test token binding end-to-end, this test would need
// to use the real defaultMultiSessionFactory with a real HMAC secret.
//
// Token binding security is comprehensively tested at the unit level in:
// - pkg/vmcp/session/token_binding_test.go (factory behavior)
// - pkg/vmcp/session/internal/security/*_test.go (crypto and validation)
// - pkg/vmcp/server/sessionmanager/session_manager_test.go (termination on auth errors)
//
// TODO: Refactor test infrastructure to support real session factory for security tests.
func TestIntegration_SessionManagement_TokenBinding(t *testing.T) {
t.Skip("Fake factory doesn't implement real token binding - see test comment for details")
t.Parallel()
ctrl := gomock.NewController(t)
testTool := vmcp.Tool{Name: "echo", Description: "echoes input"}
factory, state := newMockFactory(t, ctrl, []vmcp.Tool{testTool})
ts := buildTestServer(t, factory)
tokenA := "bearer-token-A"
tokenB := "bearer-token-B"
// Step 1: Initialize with token A
initReq := map[string]any{
"jsonrpc": "2.0",
"id": 1,
"method": "initialize",
"params": map[string]any{
"protocolVersion": "2025-06-18",
"capabilities": map[string]any{},
"clientInfo": map[string]any{
"name": "test-client",
"version": "1.0",
},
},
}
req, err := http.NewRequestWithContext(context.Background(), http.MethodPost, ts.URL+"/mcp", nil)
require.NoError(t, err)
req.Header.Set("Content-Type", "application/json")
req.Header.Set("Authorization", "Bearer "+tokenA) // Set token A
reqBody, err := json.Marshal(initReq)
require.NoError(t, err)
req.Body = io.NopCloser(bytes.NewReader(reqBody))
initResp, err := http.DefaultClient.Do(req)
require.NoError(t, err)
defer initResp.Body.Close()
require.Equal(t, http.StatusOK, initResp.StatusCode)
sessionID := initResp.Header.Get("Mcp-Session-Id")
require.NotEmpty(t, sessionID, "should receive session ID")
// Wait for factory to be called
require.Eventually(t,
func() bool { return state.makeWithIDCalled.Load() },
1*time.Second,
10*time.Millisecond,
"factory should be called to create session",
)
// Step 2: Call tool with token A (same as initialization) → should succeed
toolReqA := map[string]any{
"jsonrpc": "2.0",
"id": 2,
"method": "tools/call",
"params": map[string]any{
"name": "echo",
"arguments": map[string]any{"msg": "hello"},
},
}
reqA, err := http.NewRequestWithContext(context.Background(), http.MethodPost, ts.URL+"/mcp", nil)
require.NoError(t, err)
reqA.Header.Set("Content-Type", "application/json")
reqA.Header.Set("Mcp-Session-Id", sessionID)
reqA.Header.Set("Authorization", "Bearer "+tokenA) // Same token
reqBodyA, err := json.Marshal(toolReqA)
require.NoError(t, err)
reqA.Body = io.NopCloser(bytes.NewReader(reqBodyA))
respA, err := http.DefaultClient.Do(reqA)
require.NoError(t, err)
defer respA.Body.Close()
assert.Equal(t, http.StatusOK, respA.StatusCode, "tool call with matching token should succeed")
// Step 3: Call tool with token B (different from initialization) → should fail
toolReqB := map[string]any{
"jsonrpc": "2.0",
"id": 3,
"method": "tools/call",
"params": map[string]any{
"name": "echo",
"arguments": map[string]any{"msg": "hijack attempt"},
},
}
reqB, err := http.NewRequestWithContext(context.Background(), http.MethodPost, ts.URL+"/mcp", nil)
require.NoError(t, err)
reqB.Header.Set("Content-Type", "application/json")
reqB.Header.Set("Mcp-Session-Id", sessionID)
reqB.Header.Set("Authorization", "Bearer "+tokenB) // Different token!
reqBodyB, err := json.Marshal(toolReqB)
require.NoError(t, err)
reqB.Body = io.NopCloser(bytes.NewReader(reqBodyB))
respB, err := http.DefaultClient.Do(reqB)
require.NoError(t, err)
defer respB.Body.Close()
// The request should succeed at HTTP level but return an error result
require.Equal(t, http.StatusOK, respB.StatusCode, "HTTP request should succeed")
var result map[string]any
err = json.NewDecoder(respB.Body).Decode(&result)
require.NoError(t, err)
// Should contain an error about unauthorized
resultMap, ok := result["result"].(map[string]any)
require.True(t, ok, "result should be an object")
isError, ok := resultMap["isError"].(bool)
require.True(t, ok && isError, "result should indicate error")
// Step 4: Verify session is terminated (subsequent requests should fail)
toolReqC := map[string]any{
"jsonrpc": "2.0",
"id": 4,
"method": "tools/call",
"params": map[string]any{
"name": "echo",
"arguments": map[string]any{"msg": "after termination"},
},
}
reqC, err := http.NewRequestWithContext(context.Background(), http.MethodPost, ts.URL+"/mcp", nil)
require.NoError(t, err)
reqC.Header.Set("Content-Type", "application/json")
reqC.Header.Set("Mcp-Session-Id", sessionID)
reqC.Header.Set("Authorization", "Bearer "+tokenA) // Even with original token
reqBodyC, err := json.Marshal(toolReqC)
require.NoError(t, err)
reqC.Body = io.NopCloser(bytes.NewReader(reqBodyC))
respC, err := http.DefaultClient.Do(reqC)
require.NoError(t, err)
defer respC.Body.Close()
// Session should be terminated, so this should fail
assert.Equal(t, http.StatusInternalServerError, respC.StatusCode,
"request should fail after session termination due to auth failure")
}
// ---------------------------------------------------------------------------
// Helpers for composite tool and optimizer mode tests
// ---------------------------------------------------------------------------
// listToolNames sends a tools/list request and returns the tool names from the
// response. Returns nil when the request fails or the response cannot be parsed.
func listToolNames(t *testing.T, baseURL, sessionID string) []string {
t.Helper()
resp := postMCP(t, baseURL, map[string]any{
"jsonrpc": "2.0",
"id": 99,
"method": "tools/list",
"params": map[string]any{},
}, sessionID)
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
return nil
}
var body struct {
Result struct {
Tools []struct {
Name string `json:"name"`
} `json:"tools"`
} `json:"result"`
}
if err := json.NewDecoder(resp.Body).Decode(&body); err != nil {
return nil
}
names := make([]string, 0, len(body.Result.Tools))
for _, tool := range body.Result.Tools {
names = append(names, tool.Name)
}
return names
}
// fakeOptimizer is a minimal optimizer.Optimizer for testing optimizer mode.
// It returns empty results and does not require an embedding store.
type fakeOptimizer struct{}
func (*fakeOptimizer) FindTool(_ context.Context, _ optimizer.FindToolInput) (*optimizer.FindToolOutput, error) {
return &optimizer.FindToolOutput{}, nil
}
func (*fakeOptimizer) CallTool(_ context.Context, _ optimizer.CallToolInput) (*mcpmcp.CallToolResult, error) {
return &mcpmcp.CallToolResult{}, nil
}
// ---------------------------------------------------------------------------
// Composite tool and optimizer integration tests
// ---------------------------------------------------------------------------
// TestIntegration_SessionManagement_CompositeTools verifies that composite tools
// (workflow definitions) appear in tools/list alongside backend tools when
// session management is enabled.
func TestIntegration_SessionManagement_CompositeTools(t *testing.T) {
t.Parallel()
ctrl := gomock.NewController(t)
backendTool := vmcp.Tool{Name: "backend-tool", Description: "a backend tool"}
factory, _ := newMockFactory(t, ctrl, []vmcp.Tool{backendTool})
workflowDef := &composer.WorkflowDefinition{
Name: "composite-tool",
Description: "a composite workflow tool",
Steps: []composer.WorkflowStep{
{
ID: "step1",
Type: composer.StepTypeTool,
Tool: "backend-tool",
},
},
}
ts := buildTestServerWithOptions(t, factory, serverOptions{
workflowDefs: map[string]*composer.WorkflowDefinition{
"composite-tool": workflowDef,
},
})
initResp := postMCP(t, ts.URL, map[string]any{
"jsonrpc": "2.0",
"id": 1,
"method": "initialize",
"params": map[string]any{
"protocolVersion": "2025-06-18",
"capabilities": map[string]any{},
"clientInfo": map[string]any{"name": "test", "version": "1.0"},
},
}, "")
defer initResp.Body.Close()
require.Equal(t, http.StatusOK, initResp.StatusCode)
sessionID := initResp.Header.Get("Mcp-Session-Id")
require.NotEmpty(t, sessionID)
// Poll tools/list until the composite tool appears — confirms registration
// and tool injection have both completed.
require.Eventually(t, func() bool {
for _, n := range listToolNames(t, ts.URL, sessionID) {
if n == "composite-tool" {
return true
}
}
return false
}, 2*time.Second, 20*time.Millisecond,
"composite-tool should appear in tools/list after session registration")
toolNames := listToolNames(t, ts.URL, sessionID)
assert.Contains(t, toolNames, "backend-tool", "backend tool should be in tools/list")
assert.Contains(t, toolNames, "composite-tool", "composite tool should be in tools/list")
}
// TestIntegration_SessionManagement_CompositeToolConflict verifies that when a
// composite tool name collides with a backend tool name, the composite tool is
// silently skipped and the backend tool remains registered and callable.
func TestIntegration_SessionManagement_CompositeToolConflict(t *testing.T) {
t.Parallel()
// Both the backend and the workflow definition use the same name — a collision.
const sharedName = "shared-tool"
ctrl := gomock.NewController(t)
factory, _ := newMockFactory(t, ctrl, []vmcp.Tool{{Name: sharedName, Description: "backend version"}})
workflowDef := &composer.WorkflowDefinition{
Name: sharedName, // conflicts with the backend tool
Description: "composite version — should be skipped due to name conflict",
Steps: []composer.WorkflowStep{
{
ID: "step1",
Type: composer.StepTypeTool,
Tool: "other-tool",
},
},
}
ts := buildTestServerWithOptions(t, factory, serverOptions{
workflowDefs: map[string]*composer.WorkflowDefinition{
sharedName: workflowDef,
},
})
initResp := postMCP(t, ts.URL, map[string]any{
"jsonrpc": "2.0",
"id": 1,
"method": "initialize",
"params": map[string]any{
"protocolVersion": "2025-06-18",
"capabilities": map[string]any{},
"clientInfo": map[string]any{"name": "test", "version": "1.0"},
},
}, "")
defer initResp.Body.Close()
require.Equal(t, http.StatusOK, initResp.StatusCode)
sessionID := initResp.Header.Get("Mcp-Session-Id")
require.NotEmpty(t, sessionID)
// Wait for the backend tool to appear in tools/list (confirms injection completed).
require.Eventually(t, func() bool {
for _, n := range listToolNames(t, ts.URL, sessionID) {
if n == sharedName {
return true
}
}
return false
}, 2*time.Second, 20*time.Millisecond,
"backend tool should appear in tools/list")
toolNames := listToolNames(t, ts.URL, sessionID)
assert.Contains(t, toolNames, sharedName,
"backend tool should still be registered despite the name conflict")
// Exactly one tool should have the shared name — the composite was skipped.
count := 0
for _, n := range toolNames {
if n == sharedName {
count++
}
}
assert.Equal(t, 1, count,
"only the backend tool should be registered; the conflicting composite tool must be skipped")
// Backend tool must remain callable after conflict detection.
toolResp := postMCP(t, ts.URL, map[string]any{
"jsonrpc": "2.0",
"id": 2,
"method": "tools/call",
"params": map[string]any{
"name": sharedName,
"arguments": map[string]any{},
},
}, sessionID)
defer toolResp.Body.Close()
respBody, err := io.ReadAll(toolResp.Body)
require.NoError(t, err)
assert.Equal(t, http.StatusOK, toolResp.StatusCode,
"backend tool call should succeed after conflict detection; body: %s", string(respBody))
}
// TestIntegration_SessionManagement_CompositeToolsFilteredForSession verifies that
// composite tools whose underlying backend tools are not routable in a session are
// excluded from that session's tools/list. This enforces per-session authorization:
// a session that cannot access a backend tool also cannot access composite tools
// that depend on it.
func TestIntegration_SessionManagement_CompositeToolsFilteredForSession(t *testing.T) {
t.Parallel()
ctrl := gomock.NewController(t)
// The session only has "allowed-tool"; it does NOT have "restricted-tool".
allowedTool := vmcp.Tool{Name: "allowed-tool", Description: "accessible backend tool"}
factory, _ := newMockFactory(t, ctrl, []vmcp.Tool{allowedTool})
// accessible-workflow only uses allowed-tool → should appear for this session.
accessibleDef := &composer.WorkflowDefinition{
Name: "accessible-workflow",
Description: "uses only allowed backend tools",
Steps: []composer.WorkflowStep{
{ID: "s1", Type: composer.StepTypeTool, Tool: "allowed-tool"},
},
}
// restricted-workflow uses restricted-tool which is absent from this session's
// routing table → must NOT appear for this session.
restrictedDef := &composer.WorkflowDefinition{
Name: "restricted-workflow",
Description: "uses a backend tool not accessible in this session",
Steps: []composer.WorkflowStep{
{ID: "s1", Type: composer.StepTypeTool, Tool: "allowed-tool"},
{ID: "s2", Type: composer.StepTypeTool, Tool: "restricted-tool"},
},
}
ts := buildTestServerWithOptions(t, factory, serverOptions{
workflowDefs: map[string]*composer.WorkflowDefinition{
"accessible-workflow": accessibleDef,
"restricted-workflow": restrictedDef,
},
})
initResp := postMCP(t, ts.URL, map[string]any{
"jsonrpc": "2.0",
"id": 1,
"method": "initialize",
"params": map[string]any{
"protocolVersion": "2025-06-18",
"capabilities": map[string]any{},
"clientInfo": map[string]any{"name": "test", "version": "1.0"},
},
}, "")
defer initResp.Body.Close()
require.Equal(t, http.StatusOK, initResp.StatusCode)
sessionID := initResp.Header.Get("Mcp-Session-Id")
require.NotEmpty(t, sessionID)
// Wait until accessible-workflow appears, then verify restricted-workflow does not.
require.Eventually(t, func() bool {
for _, n := range listToolNames(t, ts.URL, sessionID) {
if n == "accessible-workflow" {
return true
}
}
return false
}, 2*time.Second, 20*time.Millisecond,
"accessible-workflow should appear in tools/list")
toolNames := listToolNames(t, ts.URL, sessionID)
assert.Contains(t, toolNames, "accessible-workflow",
"composite tool whose backend tools are all accessible must be visible")
assert.NotContains(t, toolNames, "restricted-workflow",
"composite tool that depends on an inaccessible backend tool must be hidden")
}
// TestIntegration_SessionManagement_OptimizerMode verifies that when an optimizer
// factory is configured with session management, tools/list exposes only
// find_tool and call_tool (the optimizer wraps all backend tools).
func TestIntegration_SessionManagement_OptimizerMode(t *testing.T) {
t.Parallel()
ctrl := gomock.NewController(t)
testTool := vmcp.Tool{Name: "test-tool", Description: "a test tool"}
factory, _ := newMockFactory(t, ctrl, []vmcp.Tool{testTool})
ts := buildTestServerWithOptions(t, factory, serverOptions{
optimizerFactory: func(_ context.Context, _ []mcpsdk.ServerTool) (optimizer.Optimizer, error) {
return &fakeOptimizer{}, nil
},
})
initResp := postMCP(t, ts.URL, map[string]any{
"jsonrpc": "2.0",
"id": 1,
"method": "initialize",
"params": map[string]any{
"protocolVersion": "2025-06-18",
"capabilities": map[string]any{},
"clientInfo": map[string]any{"name": "test", "version": "1.0"},
},
}, "")
defer initResp.Body.Close()
require.Equal(t, http.StatusOK, initResp.StatusCode)
sessionID := initResp.Header.Get("Mcp-Session-Id")
require.NotEmpty(t, sessionID)
// Poll until find_tool appears, confirming optimizer tools were injected.
require.Eventually(t, func() bool {
for _, n := range listToolNames(t, ts.URL, sessionID) {
if n == "find_tool" {
return true
}
}
return false
}, 2*time.Second, 20*time.Millisecond,
"find_tool should appear in tools/list when optimizer is configured")
toolNames := listToolNames(t, ts.URL, sessionID)
assert.Contains(t, toolNames, "find_tool", "find_tool must be exposed in optimizer mode")
assert.Contains(t, toolNames, "call_tool", "call_tool must be exposed in optimizer mode")
// The raw backend tool must not be directly visible — the optimizer wraps it.
assert.NotContains(t, toolNames, "test-tool",
"backend tools must not be directly exposed in optimizer mode")
assert.Len(t, toolNames, 2,
"only find_tool and call_tool should be exposed in optimizer mode")
}