-
Notifications
You must be signed in to change notification settings - Fork 73
Expand file tree
/
Copy pathhttp-custom-headers.ts
More file actions
953 lines (884 loc) · 33.8 KB
/
Copy pathhttp-custom-headers.ts
File metadata and controls
953 lines (884 loc) · 33.8 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
import type { ScenarioContext } from '../../mock-server';
/**
* HTTP Custom Headers conformance test scenario for MCP clients (SEP-2243)
*
* Tests that clients correctly handle the `x-mcp-header` extension property:
* 1. Mirror annotated tool parameter values into `Mcp-Param-{Name}` headers
* 2. Apply correct value encoding (plain ASCII, Base64 for non-ASCII)
* 3. Reject tool definitions with invalid `x-mcp-header` annotations
*
* This is a Scenario (acts as a test server that inspects incoming requests
* from the client under test).
*/
import http from 'http';
import { ScenarioUrls, ConformanceCheck } from '../../types.js';
import { BaseHttpScenario } from './http-base.js';
const SPEC_REFERENCE_CUSTOM = {
id: 'SEP-2243-Custom-Headers',
url: 'https://modelcontextprotocol.io/specification/draft/basic/transports#custom-headers-from-tool-parameters'
};
const SPEC_REFERENCE_ENCODING = {
id: 'SEP-2243-Value-Encoding',
url: 'https://modelcontextprotocol.io/specification/draft/basic/transports#value-encoding'
};
const SPEC_REFERENCE_TOOL_DEF = {
id: 'SEP-2243-x-mcp-header',
url: 'https://modelcontextprotocol.io/specification/draft/server/tools#x-mcp-header'
};
/**
* Every requirement-level check ID HttpCustomHeadersScenario can emit
* (SEP-2243). Declared-but-unemitted checks are backfilled as FAILURE by
* getChecks() — same pattern as request-metadata.ts — so the emitted ID set
* is stable for traceability even when the client never exercises the
* feature.
*/
export const CUSTOM_HEADERS_DECLARED_CHECK_IDS = [
'sep-2243-client-supports-custom-headers',
'sep-2243-client-mirrors-designated-params',
'sep-2243-client-encode-values',
'sep-2243-client-base64-unsafe',
'sep-2243-client-omit-null'
] as const;
/**
* Every requirement-level check ID HttpInvalidToolHeadersScenario can emit
* (SEP-2243).
*/
export const INVALID_TOOL_DECLARED_CHECK_IDS = [
'sep-2243-client-reject-invalid-tool',
'sep-2243-x-mcp-header-not-empty',
'sep-2243-x-mcp-header-charset',
'sep-2243-x-mcp-header-unique',
'sep-2243-x-mcp-header-primitive-only'
] as const;
/**
* Invalid tool definitions served by HttpInvalidToolHeadersScenario, keyed by
* tool name, mapped to the SEP-2243 x-mcp-header constraint each one
* violates. The check for "client did not call this tool" is emitted under
* the constraint's requirement ID so each constraint traces to a check.
*/
const INVALID_TOOL_CONSTRAINT_IDS: Record<string, string> = {
invalid_empty_header: 'sep-2243-x-mcp-header-not-empty',
invalid_object_header: 'sep-2243-x-mcp-header-primitive-only',
invalid_array_header: 'sep-2243-x-mcp-header-primitive-only',
invalid_null_header: 'sep-2243-x-mcp-header-primitive-only',
invalid_duplicate_same_case: 'sep-2243-x-mcp-header-unique',
invalid_duplicate_diff_case: 'sep-2243-x-mcp-header-unique',
invalid_space_in_name: 'sep-2243-x-mcp-header-charset',
invalid_colon_in_name: 'sep-2243-x-mcp-header-charset',
invalid_non_ascii_name: 'sep-2243-x-mcp-header-charset',
invalid_control_char_name: 'sep-2243-x-mcp-header-charset'
};
/**
* Decodes a header value that may be Base64-encoded.
* Base64-encoded values use the format: =?base64?{Base64EncodedValue}?=
*/
function decodeHeaderValue(value: string): string {
const base64Match = value.match(/^=\?base64\?(.*)\?=$/);
if (base64Match) {
return Buffer.from(base64Match[1], 'base64').toString('utf-8');
}
return value;
}
/**
* Check if a value needs Base64 encoding per the spec:
* - Non-ASCII characters
* - Control characters
* - Leading/trailing whitespace
*/
function needsBase64Encoding(value: string): boolean {
// Check for non-ASCII or control characters
for (let i = 0; i < value.length; i++) {
const code = value.charCodeAt(i);
if (code < 0x20 || code > 0x7e) {
// Allow space (0x20) and tab (0x09) only inside values, not at edges
if (code === 0x09) return true; // tab always needs encoding
if (code < 0x20) return true; // other control chars
if (code > 0x7e) return true; // non-ASCII
}
}
// Check for leading/trailing whitespace
if (value !== value.trim()) return true;
return false;
}
/**
* Checks if a raw header value is properly encoded for a body value that
* needs Base64 encoding. Returns null if valid, error string if invalid.
*/
function validateEncodedHeader(
rawHeader: string,
bodyValue: string,
valueType?: string
): string | null {
if (needsBase64Encoding(bodyValue)) {
// Value requires Base64 encoding
const base64Match = rawHeader.match(/^=\?base64\?(.*)\?=$/);
if (!base64Match) {
return `Value '${bodyValue}' requires Base64 encoding but header was sent as plain: '${rawHeader}'`;
}
const decoded = Buffer.from(base64Match[1], 'base64').toString('utf-8');
if (valueType === 'number') {
return compareNumericValues(decoded, bodyValue);
}
if (decoded !== bodyValue) {
return `Base64-decoded header value '${decoded}' does not match body value '${bodyValue}'`;
}
return null;
}
// Plain ASCII - compare directly (after decoding if Base64 was used)
const decoded = decodeHeaderValue(rawHeader);
if (valueType === 'number') {
return compareNumericValues(decoded, bodyValue);
}
if (decoded !== bodyValue) {
return `Header value '${decoded}' (raw: '${rawHeader}') does not match body value '${bodyValue}'`;
}
return null;
}
/**
* Compare two string representations of numbers numerically.
* For integers, requires exact match. For decimals, allows
* a tolerance of 1e-9 to account for cross-SDK floating point
* representation differences.
*/
function compareNumericValues(
headerValue: string,
bodyValue: string
): string | null {
const headerNum = Number(headerValue);
const bodyNum = Number(bodyValue);
if (isNaN(headerNum) || isNaN(bodyNum)) {
return `Non-numeric value in number comparison: header='${headerValue}', body='${bodyValue}'`;
}
if (Number.isInteger(bodyNum)) {
// Integer: require exact numeric match (e.g. 42 === 42.0)
if (headerNum !== bodyNum) {
return `Numeric header value ${headerNum} does not match body value ${bodyNum}`;
}
} else {
// Decimal: allow tolerance of 1e-9
if (Math.abs(headerNum - bodyNum) > 1e-9) {
return `Numeric header value ${headerNum} does not match body value ${bodyNum} (difference ${Math.abs(headerNum - bodyNum)} exceeds tolerance 1e-9)`;
}
}
return null;
}
// ─────────────────────────────────────────────────────────────────────────────
// HttpCustomHeadersScenario - tests that clients mirror x-mcp-header params
// ─────────────────────────────────────────────────────────────────────────────
export class HttpCustomHeadersScenario extends BaseHttpScenario {
name = 'http-custom-headers';
description =
'Tests that client mirrors x-mcp-header tool parameters into Mcp-Param headers with correct encoding (SEP-2243)';
private toolCallReceived: boolean = false;
private nullToolCallReceived: boolean = false;
async start(_ctx: ScenarioContext): Promise<ScenarioUrls> {
const urls = await super.start(_ctx);
// Pass test values via context for encoding edge cases.
// The conformance client should use these values when calling test_custom_headers.
urls.context = {
toolCalls: [
{
name: 'test_custom_headers',
arguments: {
region: 'us-west1',
priority: 42,
verbose: false,
debug: true,
empty_val: '',
method_val: 'test-method',
float_val: 3.14159,
non_ascii_val: 'Hello, 世界',
whitespace_val: ' padded ',
leading_space_val: ' us-west1',
trailing_space_val: 'us-west1 ',
internal_space_val: 'us west 1',
control_char_val: 'line1\nline2',
crlf_val: 'line1\r\nline2',
tab_val: '\tindented',
query: 'SELECT * FROM users'
}
},
{
name: 'test_custom_headers_null',
arguments: {
region: 'us-east1',
priority: 1,
verbose: null,
query: 'SELECT 1'
}
}
]
};
return urls;
}
getChecks(): ConformanceCheck[] {
// Declared-but-unemitted -> FAILURE (request-metadata.ts pattern). Keeps
// the emitted ID set stable for traceability even when the client never
// calls the annotated tools. The `some()` guard makes this idempotent.
for (const id of CUSTOM_HEADERS_DECLARED_CHECK_IDS) {
if (this.checks.some((c) => c.id === id)) continue;
const missingNullCall =
id === 'sep-2243-client-omit-null' && !this.nullToolCallReceived;
this.checks.push({
id,
name: 'NotObserved',
description: `Declared check ${id} was never emitted`,
status: 'FAILURE',
timestamp: new Date().toISOString(),
errorMessage: missingNullCall
? 'Client did not send a tools/call request for test_custom_headers_null to test null/omitted parameter handling.'
: this.toolCallReceived
? 'Check was not observed: no tool call exercised it.'
: 'Client did not send a tools/call request for test_custom_headers.',
specReferences: [SPEC_REFERENCE_CUSTOM]
});
}
return this.checks;
}
protected handlePost(
req: http.IncomingMessage,
res: http.ServerResponse,
request: any
): void {
if (request.method === 'initialize') {
this.sendInitialize(res, request);
} else if (request.method === 'tools/list') {
this.handleToolsList(res, request);
} else if (request.method === 'tools/call') {
this.handleToolsCall(req, res, request);
} else if (request.id === undefined) {
this.sendNotificationAck(res);
} else {
this.sendGenericResult(res, request);
}
}
private handleToolsList(res: http.ServerResponse, request: any): void {
this.sendJson(res, {
jsonrpc: '2.0',
id: request.id,
result: {
resultType: 'complete',
ttlMs: 300000,
cacheScope: 'private',
tools: [
{
name: 'test_custom_headers',
description:
'A tool with x-mcp-header annotations to test custom header mirroring and encoding',
inputSchema: {
type: 'object',
properties: {
region: {
type: 'string',
description: 'Plain ASCII string value',
'x-mcp-header': 'Region'
},
priority: {
type: 'number',
description: 'Integer numeric value',
'x-mcp-header': 'Priority'
},
verbose: {
type: 'boolean',
description: 'Boolean value',
'x-mcp-header': 'Verbose'
},
debug: {
type: 'boolean',
description: 'Boolean true value',
'x-mcp-header': 'Debug'
},
empty_val: {
type: 'string',
description: 'Empty string value',
'x-mcp-header': 'EmptyVal'
},
method_val: {
type: 'string',
description:
'Value for header named "Method" — tests that x-mcp-header "Method" produces Mcp-Param-Method (not Mcp-Method)',
'x-mcp-header': 'Method'
},
float_val: {
type: 'number',
description: 'Floating point numeric value',
'x-mcp-header': 'FloatVal'
},
non_ascii_val: {
type: 'string',
description:
'Non-ASCII string value — requires Base64 encoding',
'x-mcp-header': 'NonAscii'
},
whitespace_val: {
type: 'string',
description:
'String with leading/trailing whitespace — requires Base64 encoding',
'x-mcp-header': 'Whitespace'
},
leading_space_val: {
type: 'string',
description:
'String with leading space only — requires Base64 encoding',
'x-mcp-header': 'LeadingSpace'
},
trailing_space_val: {
type: 'string',
description:
'String with trailing space only — requires Base64 encoding',
'x-mcp-header': 'TrailingSpace'
},
internal_space_val: {
type: 'string',
description:
'String with internal spaces only — plain ASCII, no Base64',
'x-mcp-header': 'InternalSpace'
},
control_char_val: {
type: 'string',
description:
'String with control characters — requires Base64 encoding',
'x-mcp-header': 'ControlChar'
},
crlf_val: {
type: 'string',
description:
'String with carriage return and line feed — requires Base64 encoding',
'x-mcp-header': 'CrLf'
},
tab_val: {
type: 'string',
description:
'String with leading tab — requires Base64 encoding',
'x-mcp-header': 'Tab'
},
query: {
type: 'string',
description:
'No x-mcp-header annotation - should not be mirrored'
}
},
required: ['region', 'priority', 'query']
}
},
{
name: 'test_custom_headers_null',
description:
'A tool for testing null/omitted x-mcp-header parameter handling',
inputSchema: {
type: 'object',
properties: {
region: {
type: 'string',
description: 'Plain ASCII string value',
'x-mcp-header': 'Region'
},
priority: {
type: 'number',
description: 'Integer numeric value',
'x-mcp-header': 'Priority'
},
verbose: {
type: 'boolean',
description: 'Boolean value — will be null to test omission',
'x-mcp-header': 'Verbose'
},
query: {
type: 'string',
description: 'No x-mcp-header annotation'
}
},
required: ['region', 'priority', 'query']
}
}
]
}
});
}
private handleToolsCall(
req: http.IncomingMessage,
res: http.ServerResponse,
request: any
): void {
const toolName = request.params?.name;
const args = request.params?.arguments || {};
if (toolName === 'test_custom_headers') {
this.toolCallReceived = true;
// SEP-2243 "MCP clients MUST support this feature": observable as the
// client calling the annotated tool with at least one mirrored
// Mcp-Param-* header. The per-parameter checks below then validate each
// mirrored value individually.
const hasAnyParamHeader = Object.keys(req.headers).some((h) =>
h.startsWith('mcp-param-')
);
this.checks.push({
id: 'sep-2243-client-supports-custom-headers',
name: 'ClientSupportsCustomHeaders',
description:
'Client supports custom headers: calls the x-mcp-header annotated tool and mirrors at least one parameter',
status: hasAnyParamHeader ? 'SUCCESS' : 'FAILURE',
timestamp: new Date().toISOString(),
errorMessage: hasAnyParamHeader
? undefined
: 'Client called test_custom_headers but sent no Mcp-Param-* headers.',
specReferences: [SPEC_REFERENCE_CUSTOM]
});
// Check Mcp-Param-Region header (plain ASCII string)
this.checkParamHeader(req, 'Region', args.region, 'string');
// Check Mcp-Param-Priority header (integer number)
this.checkParamHeader(req, 'Priority', args.priority, 'number');
// Check Mcp-Param-Verbose header (boolean value)
// checkParamHeader already FAILs on missing header, so this also covers
// "optional parameter present → client MUST include header" without a
// separate check id.
if (args.verbose !== undefined && args.verbose !== null) {
this.checkParamHeader(req, 'Verbose', args.verbose, 'boolean');
}
// Check Mcp-Param-Debug header (boolean true value)
if (args.debug !== undefined && args.debug !== null) {
this.checkParamHeader(req, 'Debug', args.debug, 'boolean');
}
// Check Mcp-Param-EmptyVal header (empty string → empty header value)
if (args.empty_val !== undefined && args.empty_val !== null) {
this.checkParamHeader(req, 'EmptyVal', args.empty_val, 'string');
}
// Check Mcp-Param-Method header (x-mcp-header "Method" → Mcp-Param-Method, NOT Mcp-Method)
if (args.method_val !== undefined && args.method_val !== null) {
this.checkParamHeader(req, 'Method', args.method_val, 'string');
}
// Check Mcp-Param-FloatVal header (floating point number)
if (args.float_val !== undefined && args.float_val !== null) {
this.checkParamHeader(req, 'FloatVal', args.float_val, 'number');
}
// Check Mcp-Param-NonAscii header (requires Base64 encoding)
if (args.non_ascii_val !== undefined && args.non_ascii_val !== null) {
this.checkParamHeader(req, 'NonAscii', args.non_ascii_val, 'string');
}
// Check Mcp-Param-Whitespace header (leading/trailing whitespace → Base64)
if (args.whitespace_val !== undefined && args.whitespace_val !== null) {
this.checkParamHeader(req, 'Whitespace', args.whitespace_val, 'string');
}
// Check Mcp-Param-LeadingSpace header (leading space only → Base64)
if (
args.leading_space_val !== undefined &&
args.leading_space_val !== null
) {
this.checkParamHeader(
req,
'LeadingSpace',
args.leading_space_val,
'string'
);
}
// Check Mcp-Param-TrailingSpace header (trailing space only → Base64)
if (
args.trailing_space_val !== undefined &&
args.trailing_space_val !== null
) {
this.checkParamHeader(
req,
'TrailingSpace',
args.trailing_space_val,
'string'
);
}
// Check Mcp-Param-InternalSpace header (internal spaces only → plain ASCII, no Base64)
if (
args.internal_space_val !== undefined &&
args.internal_space_val !== null
) {
this.checkParamHeader(
req,
'InternalSpace',
args.internal_space_val,
'string'
);
}
// Check Mcp-Param-ControlChar header (control characters → Base64)
if (
args.control_char_val !== undefined &&
args.control_char_val !== null
) {
this.checkParamHeader(
req,
'ControlChar',
args.control_char_val,
'string'
);
}
// Check Mcp-Param-CrLf header (carriage return + line feed → Base64)
if (args.crlf_val !== undefined && args.crlf_val !== null) {
this.checkParamHeader(req, 'CrLf', args.crlf_val, 'string');
}
// Check Mcp-Param-Tab header (leading tab → Base64)
if (args.tab_val !== undefined && args.tab_val !== null) {
this.checkParamHeader(req, 'Tab', args.tab_val, 'string');
}
// Check that 'query' (no x-mcp-header) is NOT mirrored — the negative
// half of "mirror the *designated* parameter values".
const queryHeader = req.headers['mcp-param-query'] as string | undefined;
this.checks.push({
id: 'sep-2243-client-mirrors-designated-params',
name: 'ClientCustomHeaderNoMirrorUnannotated',
description:
'Client MUST NOT add Mcp-Param headers for parameters without x-mcp-header',
status: queryHeader === undefined ? 'SUCCESS' : 'FAILURE',
timestamp: new Date().toISOString(),
errorMessage:
queryHeader !== undefined
? `Found unexpected Mcp-Param-Query header '${queryHeader}' for unannotated parameter`
: undefined,
specReferences: [SPEC_REFERENCE_CUSTOM]
});
} else if (toolName === 'test_custom_headers_null') {
this.nullToolCallReceived = true;
// When value is null or not provided, client MUST omit the header
const verboseHeader = req.headers['mcp-param-verbose'] as
| string
| undefined;
this.checks.push({
id: 'sep-2243-client-omit-null',
name: 'ClientCustomHeaderOmitNull',
description:
'Client MUST omit Mcp-Param header when parameter value is null or not provided',
status: verboseHeader === undefined ? 'SUCCESS' : 'FAILURE',
timestamp: new Date().toISOString(),
errorMessage:
verboseHeader !== undefined
? `Mcp-Param-Verbose should be omitted when null/undefined, but got '${verboseHeader}'`
: undefined,
specReferences: [SPEC_REFERENCE_CUSTOM]
});
}
this.sendJson(res, {
jsonrpc: '2.0',
id: request.id,
result: {
resultType: 'complete',
content: [{ type: 'text', text: 'Custom headers test completed' }]
}
});
}
private checkParamHeader(
req: http.IncomingMessage,
headerName: string,
bodyValue: any,
valueType: string
): void {
const headerKey = `mcp-param-${headerName.toLowerCase()}`;
const rawHeaderValue = req.headers[headerKey] as string | undefined;
if (bodyValue === undefined || bodyValue === null) return;
const errors: string[] = [];
if (rawHeaderValue === undefined) {
errors.push(
`Missing Mcp-Param-${headerName} header. Client MUST include headers for x-mcp-header parameters.`
);
} else {
// Convert body value to expected string representation
let expectedString: string;
switch (valueType) {
case 'number':
expectedString = String(bodyValue);
break;
case 'boolean':
expectedString = bodyValue ? 'true' : 'false';
break;
default:
expectedString = String(bodyValue);
}
// For numbers, compare numerically to allow for cross-SDK
// floating point representation differences (e.g., "42" vs "42.0").
// See SEP-2243 discussion on number precision.
const validationError = validateEncodedHeader(
rawHeaderValue,
expectedString,
valueType
);
if (validationError) {
errors.push(validationError);
}
}
// One check ID per SEP-2243 requirement, picked by what this parameter
// actually exercises: Base64 wrapping for unsafe values, primitive
// (number/boolean) string encoding, or plain mirroring. The per-parameter
// detail lives in `name`/`details`.
const needsBase64 =
typeof bodyValue === 'string' && needsBase64Encoding(String(bodyValue));
const checkId = needsBase64
? 'sep-2243-client-base64-unsafe'
: valueType === 'number' || valueType === 'boolean'
? 'sep-2243-client-encode-values'
: 'sep-2243-client-mirrors-designated-params';
this.checks.push({
id: checkId,
name: `ClientCustomHeader_${headerName}`,
description: `Client sends correct Mcp-Param-${headerName} header (${valueType} value)`,
status: errors.length === 0 ? 'SUCCESS' : 'FAILURE',
timestamp: new Date().toISOString(),
errorMessage: errors.length > 0 ? errors.join('; ') : undefined,
specReferences: [SPEC_REFERENCE_ENCODING],
details: {
headerName: `Mcp-Param-${headerName}`,
rawHeaderValue,
bodyValue,
valueType,
needsBase64
}
});
}
}
// ─────────────────────────────────────────────────────────────────────────────
// HttpInvalidToolHeadersScenario - tests that clients reject invalid tools
// ─────────────────────────────────────────────────────────────────────────────
export class HttpInvalidToolHeadersScenario extends BaseHttpScenario {
name = 'http-invalid-tool-headers';
description =
'Tests that client rejects tools with invalid x-mcp-header annotations (SEP-2243)';
allowClientError = true;
private calledTools: Set<string> = new Set();
private toolsListSent = false;
getChecks(): ConformanceCheck[] {
if (!this.toolsListSent) {
this.checks.push({
id: 'sep-2243-invalid-tool-tools-list-gate',
name: 'ClientInvalidToolHeadersToolsList',
description: 'Client requests tools/list',
status: 'FAILURE',
timestamp: new Date().toISOString(),
errorMessage: 'Client did not send a tools/list request.',
specReferences: [SPEC_REFERENCE_TOOL_DEF]
});
}
// Check that valid_tool WAS called — proves client kept valid tools
const validToolCalled = this.calledTools.has('valid_tool');
this.checks.push({
id: 'sep-2243-client-reject-invalid-tool',
name: 'ClientKeepsValidTool',
description: 'Client MUST keep valid tools while excluding invalid ones',
status: validToolCalled ? 'SUCCESS' : 'FAILURE',
timestamp: new Date().toISOString(),
errorMessage: validToolCalled
? undefined
: "Client did not call 'valid_tool'. A single malformed tool definition must not prevent other valid tools from being used.",
specReferences: [SPEC_REFERENCE_TOOL_DEF]
});
// Check that the client did NOT call any of the invalid tools. Each
// invalid tool violates one specific x-mcp-header constraint, so the
// check is emitted under that constraint's requirement ID — the client
// rejecting the tool is how the constraint is enforced on the wire.
for (const [toolName, constraintId] of Object.entries(
INVALID_TOOL_CONSTRAINT_IDS
)) {
const called = this.calledTools.has(toolName);
this.checks.push({
id: constraintId,
name: `ClientRejectsInvalidTool_${toolName}`,
description: `Client MUST NOT call tool '${toolName}' with invalid x-mcp-header`,
status: called ? 'FAILURE' : 'SUCCESS',
timestamp: new Date().toISOString(),
errorMessage: called
? `Client called '${toolName}' which has an invalid x-mcp-header. Clients MUST reject (exclude) such tools.`
: undefined,
specReferences: [SPEC_REFERENCE_TOOL_DEF]
});
}
return this.checks;
}
protected handlePost(
_req: http.IncomingMessage,
res: http.ServerResponse,
request: any
): void {
if (request.method === 'initialize') {
this.sendInitialize(res, request);
} else if (request.method === 'tools/list') {
this.handleToolsList(res, request);
} else if (request.method === 'tools/call') {
this.handleToolsCall(res, request);
} else if (request.id === undefined) {
this.sendNotificationAck(res);
} else {
this.sendGenericResult(res, request);
}
}
private handleToolsList(res: http.ServerResponse, request: any): void {
this.toolsListSent = true;
this.sendJson(res, {
jsonrpc: '2.0',
id: request.id,
result: {
resultType: 'complete',
ttlMs: 0,
cacheScope: 'private',
tools: [
// ── Valid tool (should be kept by client) ──
{
name: 'valid_tool',
description: 'A valid tool with correct x-mcp-header',
inputSchema: {
type: 'object',
properties: {
region: {
type: 'string',
'x-mcp-header': 'Region'
}
},
required: ['region']
}
},
// ── Invalid: empty x-mcp-header value ──
{
name: 'invalid_empty_header',
description:
'x-mcp-header MUST NOT be empty (MUST be rejected by client)',
inputSchema: {
type: 'object',
properties: {
value: { type: 'string', 'x-mcp-header': '' }
},
required: ['value']
}
},
// ── Invalid: x-mcp-header on object type ──
{
name: 'invalid_object_header',
description:
'x-mcp-header MUST only be on primitive types (MUST be rejected)',
inputSchema: {
type: 'object',
properties: {
data: { type: 'object', 'x-mcp-header': 'Data' }
},
required: ['data']
}
},
// ── Invalid: x-mcp-header on array type ──
{
name: 'invalid_array_header',
description:
'x-mcp-header MUST only be on primitive types (MUST be rejected)',
inputSchema: {
type: 'object',
properties: {
items: {
type: 'array',
items: { type: 'string' },
'x-mcp-header': 'Items'
}
},
required: ['items']
}
},
// ── Invalid: x-mcp-header on null type ──
{
name: 'invalid_null_header',
description:
'x-mcp-header MUST only be on primitive types (MUST be rejected)',
inputSchema: {
type: 'object',
properties: {
nil: { type: 'null', 'x-mcp-header': 'Nil' }
},
required: ['nil']
}
},
// ── Invalid: duplicate same-case x-mcp-header values ──
{
name: 'invalid_duplicate_same_case',
description:
'Duplicate x-mcp-header "Region" on two properties (MUST be rejected)',
inputSchema: {
type: 'object',
properties: {
field1: { type: 'string', 'x-mcp-header': 'Region' },
field2: { type: 'string', 'x-mcp-header': 'Region' }
},
required: ['field1', 'field2']
}
},
// ── Invalid: duplicate case-insensitive x-mcp-header values ──
{
name: 'invalid_duplicate_diff_case',
description:
'Duplicate case-insensitive x-mcp-header "MyField"/"myfield" (MUST be rejected)',
inputSchema: {
type: 'object',
properties: {
field1: { type: 'string', 'x-mcp-header': 'MyField' },
field2: { type: 'string', 'x-mcp-header': 'myfield' }
},
required: ['field1', 'field2']
}
},
// ── Invalid: space in x-mcp-header name ──
{
name: 'invalid_space_in_name',
description:
'x-mcp-header MUST NOT contain space (MUST be rejected)',
inputSchema: {
type: 'object',
properties: {
value: { type: 'string', 'x-mcp-header': 'My Region' }
},
required: ['value']
}
},
// ── Invalid: colon in x-mcp-header name ──
{
name: 'invalid_colon_in_name',
description:
'x-mcp-header MUST NOT contain colon (MUST be rejected)',
inputSchema: {
type: 'object',
properties: {
value: {
type: 'string',
'x-mcp-header': 'Region:Primary'
}
},
required: ['value']
}
},
// ── Invalid: non-ASCII in x-mcp-header name ──
{
name: 'invalid_non_ascii_name',
description:
'x-mcp-header MUST contain only ASCII chars (MUST be rejected)',
inputSchema: {
type: 'object',
properties: {
value: { type: 'string', 'x-mcp-header': 'Région' }
},
required: ['value']
}
},
// ── Invalid: control character in x-mcp-header name ──
{
name: 'invalid_control_char_name',
description:
'x-mcp-header MUST NOT contain control chars (MUST be rejected)',
inputSchema: {
type: 'object',
properties: {
value: { type: 'string', 'x-mcp-header': 'Region\t1' }
},
required: ['value']
}
}
]
}
});
}
private handleToolsCall(res: http.ServerResponse, request: any): void {
const toolName = request.params?.name;
if (toolName) this.calledTools.add(toolName);
this.sendJson(res, {
jsonrpc: '2.0',
id: request.id,
result: {
resultType: 'complete',
content: [{ type: 'text', text: 'Tool call received' }]
}
});
}
}