Repository navigation
Expand file tree
/
Copy pathmain.go
More file actions
1315 lines (1190 loc) · 43.4 KB
/
Copy pathmain.go
File metadata and controls
1315 lines (1190 loc) · 43.4 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
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Copyright 2025 The Go MCP SDK Authors. All rights reserved.
// Use of this source code is governed by an MIT-style
// license that can be found in the LICENSE file.
// The conformance server implements features required for MCP conformance testing.
// It mirrors the functionality of the TypeScript conformance server at
// https://github.com/modelcontextprotocol/conformance/blob/main/examples/servers/typescript/everything-server.ts
//
//lint:file-ignore SA1019 conformance server exercises deprecated SEP-2577 APIs (roots, sampling, logging).
package main
import (
"context"
"encoding/base64"
"encoding/json"
"errors"
"flag"
"fmt"
"log"
"net/http"
"os"
"strings"
"time"
"github.com/google/jsonschema-go/jsonschema"
"github.com/modelcontextprotocol/go-sdk/jsonrpc"
"github.com/modelcontextprotocol/go-sdk/mcp"
"github.com/yosida95/uritemplate/v3"
)
var (
httpAddr = flag.String("http", "", "if set, use streamable HTTP at this address, instead of stdin/stdout")
stateless = flag.Bool("stateless", true, "use stateless streamable HTTP mode")
)
const watchedResourceURI = "test://watched-resource"
func main() {
flag.Parse()
opts := &mcp.ServerOptions{
CompletionHandler: completionHandler,
SubscribeHandler: subscribeHandler,
UnsubscribeHandler: unsubscribeHandler,
}
server := mcp.NewServer(&mcp.Implementation{
Name: "mcp-conformance-test-server",
Version: "1.0.0",
}, opts)
// Register server features.
registerTools(server)
registerResources(server)
registerPrompts(server)
// Start the watched resource auto-update goroutine.
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
go watchedResourceUpdater(ctx, server)
// Serve over stdio, or streamable HTTP if -http is set.
if *httpAddr != "" {
handler := mcp.NewStreamableHTTPHandler(func(*http.Request) *mcp.Server {
return server
}, &mcp.StreamableHTTPOptions{Stateless: *stateless})
log.Printf("Conformance server listening at %s", *httpAddr)
log.Fatal(http.ListenAndServe(*httpAddr, handler))
} else {
t := &mcp.StdioTransport{}
if err := server.Run(ctx, t); err != nil {
log.Printf("Server failed: %v", err)
os.Exit(1)
}
}
}
// watchedResourceUpdater sends resource update notifications every 3 seconds.
func watchedResourceUpdater(ctx context.Context, server *mcp.Server) {
ticker := time.NewTicker(3 * time.Second)
defer ticker.Stop()
for {
select {
case <-ctx.Done():
return
case <-ticker.C:
server.ResourceUpdated(ctx, &mcp.ResourceUpdatedNotificationParams{
URI: watchedResourceURI,
})
}
}
}
// =============================================================================
// Tools
// =============================================================================
func registerTools(server *mcp.Server) {
mcp.AddTool(server, &mcp.Tool{
Name: "test_simple_text",
Description: "Tests simple text content response",
}, testSimpleTextHandler)
mcp.AddTool(server, &mcp.Tool{
Name: "test_image_content",
Description: "Tests image content response",
}, testImageContentHandler)
mcp.AddTool(server, &mcp.Tool{
Name: "test_audio_content",
Description: "Tests audio content response",
}, testAudioContentHandler)
mcp.AddTool(server, &mcp.Tool{
Name: "test_embedded_resource",
Description: "Tests embedded resource content response",
}, testEmbeddedResourceHandler)
mcp.AddTool(server, &mcp.Tool{
Name: "test_multiple_content_types",
Description: "Tests response with multiple content types (text, image, resource)",
}, testMultipleContentTypesHandler)
mcp.AddTool(server, &mcp.Tool{
Name: "test_tool_with_logging",
Description: "Tests tool that emits log messages during execution",
}, testToolWithLoggingHandler)
mcp.AddTool(server, &mcp.Tool{
Name: "test_tool_with_progress",
Description: "Tests tool that reports progress notifications",
}, testToolWithProgressHandler)
mcp.AddTool(server, &mcp.Tool{
Name: "test_error_handling",
Description: "Tests error response handling",
}, testErrorHandlingHandler)
mcp.AddTool(server, &mcp.Tool{
Name: "test_sampling",
Description: "Tests server-initiated sampling (LLM completion request)",
}, testSamplingHandler)
mcp.AddTool(server, &mcp.Tool{
Name: "test_elicitation",
Description: "Tests server-initiated elicitation (user input request)",
}, testElicitationHandler)
mcp.AddTool(server, &mcp.Tool{
Name: "test_elicitation_sep1034_defaults",
Description: "Tests elicitation with default values per SEP-1034",
}, testElicitationDefaultsHandler)
mcp.AddTool(server, &mcp.Tool{
Name: "test_elicitation_sep1330_enums",
Description: "Tests elicitation with enum schema improvements per SEP-1330",
}, testElicitationEnumsHandler)
// SEP-1613 / SEP-2106: JSON Schema 2020-12 conformance test tool.
// The scenario verifies that $schema/$defs/additionalProperties (SEP-1613)
// and the broader 2020-12 vocabulary — $anchor, allOf/anyOf, if/then/else —
// (SEP-2106) survive tools/list verbatim.
mcp.AddTool(server, &mcp.Tool{
Name: "json_schema_2020_12_tool",
Description: "Tool with JSON Schema 2020-12 features for conformance testing (SEP-1613, SEP-2106)",
InputSchema: json.RawMessage(`{
"$schema": "https://json-schema.org/draft/2020-12/schema",
"type": "object",
"$defs": {
"address": {
"$anchor": "addressDef",
"type": "object",
"properties": {
"street": { "type": "string" },
"city": { "type": "string" }
}
}
},
"properties": {
"name": { "type": "string" },
"address": { "$ref": "#/$defs/address" },
"contactMethod": { "type": "string", "enum": ["phone", "email"] },
"phone": { "type": "string" },
"email": { "type": "string" }
},
"allOf": [
{ "anyOf": [{ "required": ["phone"] }, { "required": ["email"] }] }
],
"if": {
"properties": { "contactMethod": { "const": "phone" } },
"required": ["contactMethod"]
},
"then": { "required": ["phone"] },
"else": { "required": ["email"] },
"additionalProperties": false
}`),
}, jsonSchema202012Handler)
mcp.AddTool(server, &mcp.Tool{
Name: "test_reconnection",
Description: "Tests SSE stream disconnection and client reconnection (SEP-1699). Server will close the stream mid-call and send the result after client reconnects.",
}, testReconnectionHandler)
// SEP-2243 x-mcp-header tool — arms the http-custom-header-server-validation
// conformance scenario (which skips when no tool with an x-mcp-header
// annotation is found).
mcp.AddTool(server, &mcp.Tool{
Name: "test_x_mcp_header",
Description: "Tests SEP-2243 Mcp-Param-* server-side validation",
InputSchema: json.RawMessage(`{
"type": "object",
"properties": {
"region": { "type": "string", "description": "mirrored into Mcp-Param-Region", "x-mcp-header": "Region" },
"level": { "type": "integer", "description": "non-mirrored argument" }
}
}`),
}, testXMcpHeaderHandler)
// SEP-2575 diagnostic tools used by the stateless conformance scenario.
mcp.AddTool(server, &mcp.Tool{
Name: "test_missing_capability",
Description: "Requires the sampling client capability; used to verify MissingRequiredClientCapabilityError (-32021) (SEP-2575)",
}, testMissingCapabilityHandler)
mcp.AddTool(server, &mcp.Tool{
Name: "test_streaming_elicitation",
Description: "Streams progress notifications while a call is in flight; used to verify response streams carry only notifications, never independent JSON-RPC requests (SEP-2575)",
}, testStreamingElicitationHandler)
mcp.AddTool(server, &mcp.Tool{
Name: "test_logging_tool",
Description: "Attempts to emit a log message; the framework must drop it when the client did not set _meta.../logLevel (SEP-2575)",
}, testLoggingToolHandler)
mcp.AddTool(server, &mcp.Tool{
Name: "test_trigger_tool_change",
Description: "Mutates the tool list to trigger a notifications/tools/list_changed on active subscription streams (SEP-2575)",
}, makeTriggerToolChangeHandler(server))
mcp.AddTool(server, &mcp.Tool{
Name: "test_trigger_prompt_change",
Description: "Mutates the prompt list to trigger a notifications/prompts/list_changed on active subscription streams (SEP-2575)",
}, makeTriggerPromptChangeHandler(server))
mcp.AddTool(server, &mcp.Tool{
Name: "test_input_required_result_elicitation",
Description: "MRTR (SEP-2322): asks for the caller name via an in-band elicitation request",
}, testInputRequiredElicitationHandler)
mcp.AddTool(server, &mcp.Tool{
Name: "test_input_required_result_sampling",
Description: "MRTR (SEP-2322): asks for an LLM completion via an in-band sampling request",
}, testInputRequiredSamplingHandler)
mcp.AddTool(server, &mcp.Tool{
Name: "test_input_required_result_list_roots",
Description: "MRTR (SEP-2322): asks for the client roots via an in-band roots/list request",
}, testInputRequiredListRootsHandler)
mcp.AddTool(server, &mcp.Tool{
Name: "test_input_required_result_request_state",
Description: "MRTR (SEP-2322): round-trips integrity-protected requestState alongside an elicitation request",
}, testInputRequiredRequestStateHandler)
mcp.AddTool(server, &mcp.Tool{
Name: "test_input_required_result_multiple_inputs",
Description: "MRTR (SEP-2322): asks for elicitation, sampling and roots input in a single round",
}, testInputRequiredMultipleInputsHandler)
mcp.AddTool(server, &mcp.Tool{
Name: "test_input_required_result_multi_round",
Description: "MRTR (SEP-2322): two elicitation rounds with evolving requestState before completing",
}, testInputRequiredMultiRoundHandler)
mcp.AddTool(server, &mcp.Tool{
Name: "test_input_required_result_tampered_state",
Description: "MRTR (SEP-2322): rejects retries whose requestState fails integrity verification",
}, testInputRequiredTamperedStateHandler)
mcp.AddTool(server, &mcp.Tool{
Name: "test_input_required_result_capabilities",
Description: "MRTR (SEP-2322): only requests input kinds the declared client capabilities cover",
}, testInputRequiredCapabilitiesHandler)
}
// Tool handlers
func testSimpleTextHandler(ctx context.Context, req *mcp.CallToolRequest, _ any) (*mcp.CallToolResult, any, error) {
return &mcp.CallToolResult{
Content: []mcp.Content{
&mcp.TextContent{Text: "This is a simple text response for testing."},
},
}, nil, nil
}
func testImageContentHandler(ctx context.Context, req *mcp.CallToolRequest, _ any) (*mcp.CallToolResult, any, error) {
return &mcp.CallToolResult{
Content: []mcp.Content{
&mcp.ImageContent{
Data: imageData(),
MIMEType: "image/png",
},
},
}, nil, nil
}
func testAudioContentHandler(ctx context.Context, req *mcp.CallToolRequest, _ any) (*mcp.CallToolResult, any, error) {
return &mcp.CallToolResult{
Content: []mcp.Content{
&mcp.AudioContent{
Data: audioData(),
MIMEType: "audio/wav",
},
},
}, nil, nil
}
func testEmbeddedResourceHandler(ctx context.Context, req *mcp.CallToolRequest, _ any) (*mcp.CallToolResult, any, error) {
return &mcp.CallToolResult{
Content: []mcp.Content{
&mcp.EmbeddedResource{
Resource: &mcp.ResourceContents{
URI: "test://embedded-resource",
MIMEType: "text/plain",
Text: "This is an embedded resource",
},
},
},
}, nil, nil
}
func testMultipleContentTypesHandler(ctx context.Context, req *mcp.CallToolRequest, _ any) (*mcp.CallToolResult, any, error) {
return &mcp.CallToolResult{
Content: []mcp.Content{
&mcp.TextContent{Text: "This is text content"},
&mcp.ImageContent{
Data: imageData(),
MIMEType: "image/png",
},
&mcp.EmbeddedResource{
Resource: &mcp.ResourceContents{
URI: "test://embedded-in-multiple",
MIMEType: "text/plain",
Text: "This is an embedded resource",
},
},
},
}, nil, nil
}
func testToolWithLoggingHandler(ctx context.Context, req *mcp.CallToolRequest, _ any) (*mcp.CallToolResult, any, error) {
// Emit three info-level log messages
req.Session.Log(ctx, &mcp.LoggingMessageParams{
Level: "info",
Data: "Tool execution started",
})
time.Sleep(50 * time.Millisecond)
req.Session.Log(ctx, &mcp.LoggingMessageParams{
Level: "info",
Data: "Tool processing data",
})
time.Sleep(50 * time.Millisecond)
req.Session.Log(ctx, &mcp.LoggingMessageParams{
Level: "info",
Data: "Tool execution completed",
})
return &mcp.CallToolResult{
Content: []mcp.Content{
&mcp.TextContent{Text: "Tool with logging executed successfully"},
},
}, nil, nil
}
func testToolWithProgressHandler(ctx context.Context, req *mcp.CallToolRequest, _ any) (*mcp.CallToolResult, any, error) {
// Get progress token from the request if provided
progressToken := req.Params.GetProgressToken()
// Send three progress notifications (0%, 50%, 100%)
total := 100.0
steps := []float64{0, 50, 100}
for _, progress := range steps {
req.Session.NotifyProgress(ctx, &mcp.ProgressNotificationParams{
ProgressToken: progressToken,
Progress: progress,
Total: total,
Message: fmt.Sprintf("Completed step %.0f of %.0f", progress, total),
})
time.Sleep(50 * time.Millisecond)
}
// Return the progress token value as the response (matching TypeScript behavior)
tokenStr := fmt.Sprintf("%v", progressToken)
return &mcp.CallToolResult{
Content: []mcp.Content{
&mcp.TextContent{Text: tokenStr},
},
}, nil, nil
}
func testErrorHandlingHandler(ctx context.Context, req *mcp.CallToolRequest, _ any) (*mcp.CallToolResult, any, error) {
return nil, nil, errors.New("this tool intentionally returns an error for testing")
}
type samplingInput struct {
Prompt string `json:"prompt" jsonschema:"The prompt to send to the LLM"`
}
func testSamplingHandler(ctx context.Context, req *mcp.CallToolRequest, input samplingInput) (*mcp.CallToolResult, any, error) {
// Request LLM completion from the client
result, err := req.Session.CreateMessage(ctx, &mcp.CreateMessageParams{
Messages: []*mcp.SamplingMessage{
{
Role: "user",
Content: &mcp.TextContent{
Text: input.Prompt,
},
},
},
MaxTokens: 100,
})
if err != nil {
return nil, nil, fmt.Errorf("sampling failed: %w", err)
}
// Extract the text response from the result
var responseText string
if tc, ok := result.Content.(*mcp.TextContent); ok {
responseText = tc.Text
} else {
responseText = "(non-text response)"
}
return &mcp.CallToolResult{
Content: []mcp.Content{
&mcp.TextContent{Text: fmt.Sprintf("LLM response: %s", responseText)},
},
}, nil, nil
}
type elicitationInput struct {
Message string `json:"message" jsonschema:"The message to show the user"`
}
func testElicitationHandler(ctx context.Context, req *mcp.CallToolRequest, input elicitationInput) (*mcp.CallToolResult, any, error) {
result, err := req.Session.Elicit(ctx, &mcp.ElicitParams{
Message: input.Message,
RequestedSchema: &jsonschema.Schema{
Type: "object",
Properties: map[string]*jsonschema.Schema{
"username": {
Type: "string",
Description: "Your preferred username",
},
},
Required: []string{"username"},
},
})
if err != nil {
return nil, nil, fmt.Errorf("elicitation failed: %w", err)
}
return &mcp.CallToolResult{
Content: []mcp.Content{
&mcp.TextContent{Text: fmt.Sprintf("Elicitation result: action=%s, content=%v", result.Action, result.Content)},
},
}, nil, nil
}
func testElicitationDefaultsHandler(ctx context.Context, req *mcp.CallToolRequest, _ any) (*mcp.CallToolResult, any, error) {
result, err := req.Session.Elicit(ctx, &mcp.ElicitParams{
Message: "Test defaults for primitives",
RequestedSchema: map[string]any{
"type": "object",
"properties": map[string]any{
"name": map[string]any{
"type": "string",
"description": "User name",
"default": "John Doe",
},
"age": map[string]any{
"type": "integer",
"description": "User age",
"default": 30,
},
"score": map[string]any{
"type": "number",
"description": "User score",
"default": 95.5,
},
"status": map[string]any{
"type": "string",
"description": "User status",
"enum": []string{"active", "inactive", "pending"},
"default": "active",
},
"verified": map[string]any{
"type": "boolean",
"description": "Verification status",
"default": true,
},
},
},
})
if err != nil {
return nil, nil, fmt.Errorf("elicitation failed: %w", err)
}
return &mcp.CallToolResult{
Content: []mcp.Content{
&mcp.TextContent{Text: fmt.Sprintf("Elicitation result: action=%s, content=%v", result.Action, result.Content)},
},
}, nil, nil
}
func testElicitationEnumsHandler(ctx context.Context, req *mcp.CallToolRequest, _ any) (*mcp.CallToolResult, any, error) {
result, err := req.Session.Elicit(ctx, &mcp.ElicitParams{
Message: "Test enum schemas",
RequestedSchema: map[string]any{
"type": "object",
"properties": map[string]any{
// Basic enum without titles
"untitledSingle": map[string]any{
"type": "string",
"enum": []string{"option1", "option2", "option3"},
},
// Enum with titles using oneOf
"titledSingle": map[string]any{
"type": "string",
"oneOf": []map[string]any{
{"const": "value1", "title": "First Option"},
{"const": "value2", "title": "Second Option"},
{"const": "value3", "title": "Third Option"},
},
},
// Legacy enum with enumNames
"legacyEnum": map[string]any{
"type": "string",
"enum": []string{"opt1", "opt2", "opt3"},
"enumNames": []string{"Option One", "Option Two", "Option Three"},
},
// Multi-select without titles
"untitledMulti": map[string]any{
"type": "array",
"minItems": 1,
"maxItems": 3,
"items": map[string]any{
"type": "string",
"enum": []string{"option1", "option2", "option3"},
},
},
// Multi-select with titles using anyOf
"titledMulti": map[string]any{
"type": "array",
"minItems": 1,
"maxItems": 3,
"items": map[string]any{
"anyOf": []map[string]any{
{"const": "value1", "title": "First Option"},
{"const": "value2", "title": "Second Option"},
{"const": "value3", "title": "Third Option"},
},
},
},
},
},
})
if err != nil {
return nil, nil, fmt.Errorf("elicitation failed: %w", err)
}
return &mcp.CallToolResult{
Content: []mcp.Content{
&mcp.TextContent{Text: fmt.Sprintf("Elicitation result: action=%s, content=%v", result.Action, result.Content)},
},
}, nil, nil
}
func jsonSchema202012Handler(ctx context.Context, req *mcp.CallToolRequest, input json.RawMessage) (*mcp.CallToolResult, any, error) {
return &mcp.CallToolResult{
Content: []mcp.Content{
&mcp.TextContent{
Text: fmt.Sprintf("JSON Schema 2020-12 tool called with: %s", input),
},
},
}, nil, nil
}
type testXMcpHeaderInput struct {
Region string `json:"region,omitempty"`
Level int `json:"level,omitempty"`
}
func testXMcpHeaderHandler(ctx context.Context, req *mcp.CallToolRequest, input testXMcpHeaderInput) (*mcp.CallToolResult, any, error) {
return &mcp.CallToolResult{
Content: []mcp.Content{
&mcp.TextContent{Text: fmt.Sprintf("region=%s", input.Region)},
},
}, nil, nil
}
// testMissingCapabilityHandler requires the client to have declared the
// sampling capability. When absent, it returns a MissingRequiredClientCapabilityError
// with data.requiredCapabilities = {"sampling": {}} per SEP-2575.
func testMissingCapabilityHandler(ctx context.Context, req *mcp.CallToolRequest, _ any) (*mcp.CallToolResult, any, error) {
caps := req.ClientCapabilities()
if caps != nil && caps.Sampling != nil {
return &mcp.CallToolResult{
Content: []mcp.Content{
&mcp.TextContent{Text: "Client declared the sampling capability; tool executed."},
},
}, nil, nil
}
missingCapabilityData := mcp.MissingRequiredClientCapabilityData{
RequiredCapabilities: &mcp.ClientCapabilities{
Sampling: &mcp.SamplingCapabilities{},
},
}
dataBytes, err := json.Marshal(missingCapabilityData)
if err != nil {
return nil, nil, err
}
return nil, nil, &jsonrpc.Error{
Code: mcp.CodeMissingRequiredClientCapabilities,
Message: "sampling capability required but not declared by client",
Data: dataBytes,
}
}
// testStreamingElicitationHandler returns a plain result. Per SEP-2575, the
// response stream must carry no independent top-level JSON-RPC requests; a
// plain response trivially satisfies this. The scenario declares no
// `elicitation` capability, so this handler must not call req.Session.Elicit.
func testStreamingElicitationHandler(ctx context.Context, req *mcp.CallToolRequest, _ any) (*mcp.CallToolResult, any, error) {
return &mcp.CallToolResult{
Content: []mcp.Content{
&mcp.TextContent{Text: "stream observed: result frames only, no top-level requests"},
},
}, nil, nil
}
// testLoggingToolHandler attempts to emit a log message. The SDK's
// ServerSession.Log gates on the per-request _meta.../logLevel, so this is a
// no-op notification-wise when the client did not opt in.
func testLoggingToolHandler(ctx context.Context, req *mcp.CallToolRequest, _ any) (*mcp.CallToolResult, any, error) {
req.Session.Log(ctx, &mcp.LoggingMessageParams{
Level: "info",
Data: "test_logging_tool executed",
})
return &mcp.CallToolResult{
Content: []mcp.Content{
&mcp.TextContent{Text: "Log attempted; framework gates on _meta.../logLevel."},
},
}, nil, nil
}
// makeTriggerToolChangeHandler returns a handler that (re-)registers a
// no-op transient tool. Every call to Server.AddTool dispatches a
// notifications/tools/list_changed to active subscription streams (even when
// it replaces a tool with the same name), which is what the SEP-2575
// subscription checks assert.
func makeTriggerToolChangeHandler(server *mcp.Server) mcp.ToolHandlerFor[any, any] {
return func(ctx context.Context, req *mcp.CallToolRequest, _ any) (*mcp.CallToolResult, any, error) {
mcp.AddTool(server, &mcp.Tool{
Name: "__transient_tool_for_list_changed",
Description: "Transient tool used to trigger tools/list_changed",
}, func(context.Context, *mcp.CallToolRequest, any) (*mcp.CallToolResult, any, error) {
return &mcp.CallToolResult{}, nil, nil
})
return &mcp.CallToolResult{
Content: []mcp.Content{
&mcp.TextContent{Text: "tools_list_changed published"},
},
}, nil, nil
}
}
// makeTriggerPromptChangeHandler is the prompt analogue of
// makeTriggerToolChangeHandler.
func makeTriggerPromptChangeHandler(server *mcp.Server) mcp.ToolHandlerFor[any, any] {
return func(ctx context.Context, req *mcp.CallToolRequest, _ any) (*mcp.CallToolResult, any, error) {
server.AddPrompt(&mcp.Prompt{
Name: "__transient_prompt_for_list_changed",
Description: "Transient prompt used to trigger prompts/list_changed",
}, func(context.Context, *mcp.GetPromptRequest) (*mcp.GetPromptResult, error) {
return &mcp.GetPromptResult{}, nil
})
return &mcp.CallToolResult{
Content: []mcp.Content{
&mcp.TextContent{Text: "prompts_list_changed published"},
},
}, nil, nil
}
}
// =============================================================================
// SEP-2322 Multi-Round-Trip helpers
// =============================================================================
// requestStateTamperedSuffix is what the tampered-state conformance scenario
// appends to a valid requestState. Detecting this suffix is sufficient to
// pass the check; a production server would use a real integrity mechanism
// (HMAC / AEAD, per SEP-2322 § "Server Requirements").
const requestStateTamperedSuffix = "-TAMPERED"
// requestStateValid reports whether the state looks like one this server
// minted (i.e. the harness did not append the tampered marker).
func requestStateValid(state string) bool {
return state != "" && !strings.HasSuffix(state, requestStateTamperedSuffix)
}
// acceptedContent returns the "content" field of an ElicitResult only when the
// user accepted the elicitation. Returns nil if the response is missing, of the
// wrong type, or the action is not "accept".
func acceptedContent(responses mcp.InputResponseMap, key string) map[string]any {
if responses == nil {
return nil
}
resp, ok := responses[key]
if !ok {
return nil
}
elicit, ok := resp.(*mcp.ElicitResult)
if !ok || elicit == nil || elicit.Action != "accept" {
return nil
}
return elicit.Content
}
// =============================================================================
// SEP-2322 Multi-Round-Trip tool handlers
// =============================================================================
// nameElicitSchema is the requestedSchema used by tools that elicit a name.
func nameElicitSchema() *jsonschema.Schema {
return &jsonschema.Schema{
Type: "object",
Properties: map[string]*jsonschema.Schema{
"name": {Type: "string"},
},
Required: []string{"name"},
}
}
// testInputRequiredElicitationHandler asks for the caller name via elicitation.
// Reused by the result-type, missing-input-response, ignore-extra-params, and
// validate-input scenarios: anything that does not carry an accepted
// "user_name" response is answered with a fresh InputRequiredResult.
func testInputRequiredElicitationHandler(ctx context.Context, req *mcp.CallToolRequest, _ any) (*mcp.CallToolResult, any, error) {
if content := acceptedContent(req.Params.InputResponses, "user_name"); content != nil {
name, _ := content["name"].(string)
if name != "" {
return &mcp.CallToolResult{
Content: []mcp.Content{&mcp.TextContent{Text: fmt.Sprintf("Hello, %s!", name)}},
}, nil, nil
}
}
return &mcp.CallToolResult{
InputRequests: mcp.InputRequestMap{
"user_name": &mcp.ElicitParams{
Message: "What is your name?",
RequestedSchema: nameElicitSchema(),
},
},
}, nil, nil
}
// samplingResponseText extracts the first text content from a sampling
// response. The Go SDK unmarshals responses with a "role" field into
// *CreateMessageWithToolsResult (a superset of the legacy single-content
// CreateMessageResult).
func samplingResponseText(responses mcp.InputResponseMap, key string) (string, bool) {
resp, ok := responses[key].(*mcp.CreateMessageWithToolsResult)
if !ok || resp == nil {
return "", false
}
for _, c := range resp.Content {
if tc, ok := c.(*mcp.TextContent); ok {
return tc.Text, true
}
}
return "(non-text response)", true
}
// testInputRequiredSamplingHandler asks for an LLM completion via sampling.
func testInputRequiredSamplingHandler(ctx context.Context, req *mcp.CallToolRequest, _ any) (*mcp.CallToolResult, any, error) {
if text, ok := samplingResponseText(req.Params.InputResponses, "capital_question"); ok {
return &mcp.CallToolResult{
Content: []mcp.Content{&mcp.TextContent{Text: fmt.Sprintf("Sampling response: %s", text)}},
}, nil, nil
}
return &mcp.CallToolResult{
InputRequests: mcp.InputRequestMap{
"capital_question": &mcp.CreateMessageParams{
Messages: []*mcp.SamplingMessage{
{Role: "user", Content: &mcp.TextContent{Text: "What is the capital of France?"}},
},
MaxTokens: 100,
},
},
}, nil, nil
}
// testInputRequiredListRootsHandler asks for the client roots.
func testInputRequiredListRootsHandler(ctx context.Context, req *mcp.CallToolRequest, _ any) (*mcp.CallToolResult, any, error) {
if resp, ok := req.Params.InputResponses["client_roots"].(*mcp.ListRootsResult); ok && resp != nil {
var uris []string
for _, r := range resp.Roots {
uris = append(uris, r.URI)
}
return &mcp.CallToolResult{
Content: []mcp.Content{
&mcp.TextContent{Text: fmt.Sprintf("Client exposed %d root(s): %s", len(resp.Roots), strings.Join(uris, ", "))},
},
}, nil, nil
}
return &mcp.CallToolResult{
InputRequests: mcp.InputRequestMap{
"client_roots": &mcp.ListRootsParams{},
},
}, nil, nil
}
// testInputRequiredRequestStateHandler round-trips an opaque requestState
// alongside an elicitation request.
func testInputRequiredRequestStateHandler(ctx context.Context, req *mcp.CallToolRequest, _ any) (*mcp.CallToolResult, any, error) {
if content := acceptedContent(req.Params.InputResponses, "confirm"); content != nil {
if requestStateValid(req.Params.RequestState) {
return &mcp.CallToolResult{
Content: []mcp.Content{&mcp.TextContent{Text: "state-ok: requestState received and confirmation accepted"}},
}, nil, nil
}
return nil, nil, &jsonrpc.Error{
Code: jsonrpc.CodeInvalidParams,
Message: "invalid requestState",
}
}
return &mcp.CallToolResult{
InputRequests: mcp.InputRequestMap{
"confirm": &mcp.ElicitParams{
Message: "Please confirm",
RequestedSchema: &jsonschema.Schema{
Type: "object",
Properties: map[string]*jsonschema.Schema{"ok": {Type: "boolean"}},
Required: []string{"ok"},
},
},
},
RequestState: "request_state",
}, nil, nil
}
// testInputRequiredMultipleInputsHandler asks for elicitation, sampling, and
// roots input in a single round.
func testInputRequiredMultipleInputsHandler(ctx context.Context, req *mcp.CallToolRequest, _ any) (*mcp.CallToolResult, any, error) {
nameContent := acceptedContent(req.Params.InputResponses, "user_name")
name, _ := nameContent["name"].(string)
greetingText, hasGreeting := samplingResponseText(req.Params.InputResponses, "greeting")
var rootsCount int
rootsResp, rootsOK := req.Params.InputResponses["client_roots"].(*mcp.ListRootsResult)
if rootsOK && rootsResp != nil {
rootsCount = len(rootsResp.Roots)
}
if name != "" && hasGreeting && rootsOK {
return &mcp.CallToolResult{
Content: []mcp.Content{
&mcp.TextContent{Text: fmt.Sprintf("%s %s — %d root(s) visible", greetingText, name, rootsCount)},
},
}, nil, nil
}
return &mcp.CallToolResult{
InputRequests: mcp.InputRequestMap{
"user_name": &mcp.ElicitParams{
Message: "What is your name?",
RequestedSchema: nameElicitSchema(),
},
"greeting": &mcp.CreateMessageParams{
Messages: []*mcp.SamplingMessage{
{Role: "user", Content: &mcp.TextContent{Text: "Generate a greeting"}},
},
MaxTokens: 50,
},
"client_roots": &mcp.ListRootsParams{},
},
RequestState: "multiple_inputs",
}, nil, nil
}
// testInputRequiredMultiRoundHandler runs two elicitation rounds with
// evolving requestState before completing. The round number (and the name
// elicited in round 1) lives in the state, since the stateless server keeps
// no per-session memory.
func testInputRequiredMultiRoundHandler(ctx context.Context, req *mcp.CallToolRequest, _ any) (*mcp.CallToolResult, any, error) {
state := req.Params.RequestState
// State format: "round=1" or "round=2;name=<name>".
switch {
case state == "":
return &mcp.CallToolResult{
InputRequests: mcp.InputRequestMap{
"step1": &mcp.ElicitParams{
Message: "Step 1: What is your name?",
RequestedSchema: nameElicitSchema(),
},
},
RequestState: "round=1",
}, nil, nil
case state == "round=1":
name, _ := acceptedContent(req.Params.InputResponses, "step1")["name"].(string)
if name == "" {
name = "unknown"
}
return &mcp.CallToolResult{
InputRequests: mcp.InputRequestMap{
"step2": &mcp.ElicitParams{
Message: "Step 2: What is your favorite color?",
RequestedSchema: &jsonschema.Schema{
Type: "object",
Properties: map[string]*jsonschema.Schema{"color": {Type: "string"}},
Required: []string{"color"},
},
},
},
RequestState: "round=2;name=" + name,
}, nil, nil
default:
color, _ := acceptedContent(req.Params.InputResponses, "step2")["color"].(string)
if color == "" {
color = "unknown"
}
name := strings.TrimPrefix(state, "round=2;name=")
return &mcp.CallToolResult{
Content: []mcp.Content{
&mcp.TextContent{Text: fmt.Sprintf("Multi-round complete: %s likes %s", name, color)},
},
}, nil, nil
}
}
// testInputRequiredTamperedStateHandler rejects retries whose requestState
// looks tampered. The conformance harness tampers by appending "-TAMPERED";
// production servers should use a real integrity mechanism (HMAC / AEAD).
func testInputRequiredTamperedStateHandler(ctx context.Context, req *mcp.CallToolRequest, _ any) (*mcp.CallToolResult, any, error) {
if req.Params.RequestState != "" {
if !requestStateValid(req.Params.RequestState) {
return nil, nil, &jsonrpc.Error{
Code: jsonrpc.CodeInvalidParams,
Message: "invalid requestState: tampered",
}
}
if acceptedContent(req.Params.InputResponses, "confirm") != nil {
return &mcp.CallToolResult{
Content: []mcp.Content{&mcp.TextContent{Text: "state-ok: requestState received and confirmation accepted"}},
}, nil, nil
}
}
return &mcp.CallToolResult{
InputRequests: mcp.InputRequestMap{
"confirm": &mcp.ElicitParams{
Message: "Please confirm",
RequestedSchema: &jsonschema.Schema{
Type: "object",
Properties: map[string]*jsonschema.Schema{"ok": {Type: "boolean"}},
Required: []string{"ok"},
},
},
},
RequestState: "tampered_state",
}, nil, nil
}
// testInputRequiredCapabilitiesHandler only requests input kinds the client
// declared capabilities cover. Reads capabilities from the per-request
// _meta["io.modelcontextprotocol/clientCapabilities"] envelope.
func testInputRequiredCapabilitiesHandler(ctx context.Context, req *mcp.CallToolRequest, _ any) (*mcp.CallToolResult, any, error) {
if len(req.Params.InputResponses) > 0 {
return &mcp.CallToolResult{
Content: []mcp.Content{&mcp.TextContent{Text: "Capability-aware input requests fulfilled"}},
}, nil, nil
}
caps := req.ClientCapabilities()
reqs := mcp.InputRequestMap{}
if caps != nil && caps.Elicitation != nil {
reqs["user_name"] = &mcp.ElicitParams{
Message: "What is your name?",
RequestedSchema: nameElicitSchema(),
}
}
if caps != nil && caps.Sampling != nil {
reqs["greeting"] = &mcp.CreateMessageParams{
Messages: []*mcp.SamplingMessage{
{Role: "user", Content: &mcp.TextContent{Text: "Generate a short greeting"}},
},
MaxTokens: 50,
}
}
if caps != nil && (caps.RootsV2 != nil || caps.Roots.ListChanged) {
reqs["client_roots"] = &mcp.ListRootsParams{}