@primer/mcp 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/src/server.ts ADDED
@@ -0,0 +1,875 @@
1
+ import {McpServer} from '@modelcontextprotocol/sdk/server/mcp.js'
2
+ // eslint-disable-next-line import/no-namespace
3
+ import * as cheerio from 'cheerio'
4
+ import {z} from 'zod'
5
+ import TurndownService from 'turndown'
6
+ import {listComponents, listPatterns} from './primer'
7
+ import packageJson from '../package.json' with {type: 'json'}
8
+
9
+ const server = new McpServer({
10
+ name: 'Primer',
11
+ version: packageJson.version,
12
+ })
13
+
14
+ const turndownService = new TurndownService()
15
+
16
+ // -----------------------------------------------------------------------------
17
+ // Components
18
+ // -----------------------------------------------------------------------------
19
+ server.tool('get_components', 'List all of the components available from Primer React', async () => {
20
+ const components = listComponents().map(component => {
21
+ return `- ${component.name}`
22
+ })
23
+ return {
24
+ content: [
25
+ {
26
+ type: 'text',
27
+ text: `The following components are available in the @primer/react in TypeScript projects:
28
+
29
+ ${components.join('\n')}
30
+
31
+ You can use the \`get_component\` tool to get more information about a specific component. You can use these components from the @primer/react package.`,
32
+ },
33
+ ],
34
+ }
35
+ })
36
+
37
+ server.tool(
38
+ 'get_component',
39
+ 'Get a specific component by name',
40
+ {
41
+ name: z.string().describe('The name of the component to retrieve'),
42
+ },
43
+ async ({name}) => {
44
+ const components = listComponents()
45
+ const match = components.find(component => {
46
+ return component.name === name
47
+ })
48
+ if (!match) {
49
+ return {
50
+ content: [
51
+ {
52
+ type: 'text',
53
+ text: `There is no component named \`${name}\` in the @primer/react package. For a full list of components, use the \`get_components\` tool.`,
54
+ },
55
+ ],
56
+ }
57
+ }
58
+
59
+ const url = new URL(`/product/components/${match.id}`, 'https://primer.style')
60
+ const response = await fetch(url)
61
+ if (!response.ok) {
62
+ throw new Error(`Failed to fetch ${url}: ${response.statusText}`)
63
+ }
64
+
65
+ const html = await response.text()
66
+ if (!html) {
67
+ return {
68
+ content: [],
69
+ }
70
+ }
71
+
72
+ const $ = cheerio.load(html)
73
+ const source = $('main').html()
74
+ if (!source) {
75
+ return {
76
+ content: [],
77
+ }
78
+ }
79
+
80
+ const text = turndownService.turndown(source)
81
+
82
+ return {
83
+ content: [
84
+ {
85
+ type: 'text',
86
+ text: `Here is the documentation for the \`${name}\` component from the @primer/react package:
87
+ ${text}`,
88
+ },
89
+ ],
90
+ }
91
+ },
92
+ )
93
+
94
+ server.tool(
95
+ 'get_component_examples',
96
+ 'Get examples for how to use a component from Primer React',
97
+ {
98
+ name: z.string().describe('The name of the component to retrieve'),
99
+ },
100
+ async ({name}) => {
101
+ const components = listComponents()
102
+ const match = components.find(component => {
103
+ return component.name === name
104
+ })
105
+ if (!match) {
106
+ return {
107
+ content: [
108
+ {
109
+ type: 'text',
110
+ text: `There is no component named \`${name}\` in the @primer/react package. For a full list of components, use the \`get_components\` tool.`,
111
+ },
112
+ ],
113
+ }
114
+ }
115
+
116
+ const url = new URL(`/product/components/${match.id}`, 'https://primer.style')
117
+ const response = await fetch(url)
118
+ if (!response.ok) {
119
+ throw new Error(`Failed to fetch ${url}: ${response.statusText}`)
120
+ }
121
+
122
+ const html = await response.text()
123
+ if (!html) {
124
+ return {
125
+ content: [],
126
+ }
127
+ }
128
+
129
+ const $ = cheerio.load(html)
130
+ const source = $('main').html()
131
+ if (!source) {
132
+ return {
133
+ content: [],
134
+ }
135
+ }
136
+
137
+ const text = turndownService.turndown(source)
138
+
139
+ return {
140
+ content: [
141
+ {
142
+ type: 'text',
143
+ text: `Here are some examples of how to use the \`${name}\` component from the @primer/react package:
144
+
145
+ ${text}`,
146
+ },
147
+ ],
148
+ }
149
+ },
150
+ )
151
+
152
+ server.tool(
153
+ 'get_component_usage_guidelines',
154
+ 'Get usage information for how to use a component from Primer',
155
+ {
156
+ name: z.string().describe('The name of the component to retrieve'),
157
+ },
158
+ async ({name}) => {
159
+ const components = listComponents()
160
+ const match = components.find(component => {
161
+ return component.name === name
162
+ })
163
+ if (!match) {
164
+ return {
165
+ content: [
166
+ {
167
+ type: 'text',
168
+ text: `There is no component named \`${name}\` in the @primer/react package. For a full list of components, use the \`get_components\` tool.`,
169
+ },
170
+ ],
171
+ }
172
+ }
173
+
174
+ const url = new URL(`/product/components/${match.id}/guidelines`, 'https://primer.style')
175
+ const response = await fetch(url)
176
+ if (!response.ok) {
177
+ if ((response.status >= 400 && response.status < 500) || (response.status >= 300 && response.status < 400)) {
178
+ return {
179
+ content: [
180
+ {
181
+ type: 'text',
182
+ text: `There are no accessibility guidelines for the \`${name}\` component in the @primer/react package.`,
183
+ },
184
+ ],
185
+ }
186
+ }
187
+
188
+ throw new Error(`Failed to fetch ${url}: ${response.statusText}`)
189
+ }
190
+
191
+ const html = await response.text()
192
+ if (!html) {
193
+ return {
194
+ content: [],
195
+ }
196
+ }
197
+
198
+ const $ = cheerio.load(html)
199
+ const source = $('main').html()
200
+ if (!source) {
201
+ return {
202
+ content: [],
203
+ }
204
+ }
205
+
206
+ const text = turndownService.turndown(source)
207
+
208
+ return {
209
+ content: [
210
+ {
211
+ type: 'text',
212
+ text: `Here are the usage guidelines for the \`${name}\` component from the @primer/react package:
213
+
214
+ ${text}`,
215
+ },
216
+ ],
217
+ }
218
+ },
219
+ )
220
+
221
+ server.tool(
222
+ 'get_component_accessibility_guidelines',
223
+ 'Get accessibility information for how to use a component from Primer React',
224
+ {
225
+ name: z.string().describe('The name of the component to retrieve'),
226
+ },
227
+ async ({name}) => {
228
+ const components = listComponents()
229
+ const match = components.find(component => {
230
+ return component.name === name
231
+ })
232
+ if (!match) {
233
+ return {
234
+ content: [
235
+ {
236
+ type: 'text',
237
+ text: `There is no component named \`${name}\` in the @primer/react package. For a full list of components, use the \`get_components\` tool.`,
238
+ },
239
+ ],
240
+ }
241
+ }
242
+
243
+ const url = new URL(`/product/components/${match.id}/accessibility`, 'https://primer.style')
244
+ const response = await fetch(url)
245
+ if (!response.ok) {
246
+ if ((response.status >= 400 && response.status < 500) || (response.status >= 300 && response.status < 400)) {
247
+ return {
248
+ content: [
249
+ {
250
+ type: 'text',
251
+ text: `There are no accessibility guidelines for the \`${name}\` component in the @primer/react package.`,
252
+ },
253
+ ],
254
+ }
255
+ }
256
+
257
+ throw new Error(`Failed to fetch ${url}: ${response.statusText}`)
258
+ }
259
+
260
+ const html = await response.text()
261
+ if (!html) {
262
+ return {
263
+ content: [],
264
+ }
265
+ }
266
+
267
+ const $ = cheerio.load(html)
268
+ const source = $('main').html()
269
+ if (!source) {
270
+ return {
271
+ content: [],
272
+ }
273
+ }
274
+
275
+ const text = turndownService.turndown(source)
276
+
277
+ return {
278
+ content: [
279
+ {
280
+ type: 'text',
281
+ text: `Here are the accessibility guidelines for the \`${name}\` component from the @primer/react package:
282
+
283
+ ${text}`,
284
+ },
285
+ ],
286
+ }
287
+ },
288
+ )
289
+
290
+ // -----------------------------------------------------------------------------
291
+ // Patterns
292
+ // -----------------------------------------------------------------------------
293
+ server.tool('list_patterns', 'List all of the patterns available from Primer React', async () => {
294
+ const patterns = listPatterns().map(pattern => {
295
+ return `- ${pattern.name}`
296
+ })
297
+ return {
298
+ content: [
299
+ {
300
+ type: 'text',
301
+ text: `The following patterns are available in the @primer/react in TypeScript projects:
302
+
303
+ ${patterns.join('\n')}`,
304
+ },
305
+ ],
306
+ }
307
+ })
308
+
309
+ server.tool(
310
+ 'get_pattern',
311
+ 'Get a specific pattern by name',
312
+ {
313
+ name: z.string().describe('The name of the pattern to retrieve'),
314
+ },
315
+ async ({name}) => {
316
+ const patterns = listPatterns()
317
+ const match = patterns.find(pattern => {
318
+ return pattern.name === name
319
+ })
320
+ if (!match) {
321
+ return {
322
+ content: [
323
+ {
324
+ type: 'text',
325
+ text: `There is no pattern named \`${name}\` in the @primer/react package. For a full list of patterns, use the \`list_patterns\` tool.`,
326
+ },
327
+ ],
328
+ }
329
+ }
330
+
331
+ const url = new URL(`/product/ui-patterns/${match.id}`, 'https://primer.style')
332
+ const response = await fetch(url)
333
+ if (!response.ok) {
334
+ throw new Error(`Failed to fetch ${url} - ${response.statusText}`)
335
+ }
336
+
337
+ const html = await response.text()
338
+ if (!html) {
339
+ return {
340
+ content: [],
341
+ }
342
+ }
343
+
344
+ const $ = cheerio.load(html)
345
+ const source = $('main').html()
346
+ if (!source) {
347
+ return {
348
+ content: [],
349
+ }
350
+ }
351
+
352
+ const text = turndownService.turndown(source)
353
+
354
+ return {
355
+ content: [
356
+ {
357
+ type: 'text',
358
+ text: `Here are the guidelines for the \`${name}\` pattern for Primer:
359
+
360
+ ${text}`,
361
+ },
362
+ ],
363
+ }
364
+ },
365
+ )
366
+
367
+ type Token = TokenCategory | string
368
+ type TokenCategory = {
369
+ category: string
370
+ tokens: Array<Token>
371
+ }
372
+
373
+ const tokens: Array<Token> = [
374
+ {
375
+ category: 'color',
376
+ tokens: [
377
+ {
378
+ category: 'foreground',
379
+ tokens: [
380
+ '--fgColor-accent',
381
+ '--fgColor-attention',
382
+ '--fgColor-black',
383
+ '--fgColor-closed',
384
+ '--fgColor-danger',
385
+ '--fgColor-default',
386
+ '--fgColor-disabled',
387
+ '--fgColor-done',
388
+ '--fgColor-link',
389
+ '--fgColor-muted',
390
+ '--fgColor-neutral',
391
+ '--fgColor-onEmphasis',
392
+ '--fgColor-onInverse',
393
+ '--fgColor-open',
394
+ '--fgColor-severe',
395
+ '--fgColor-sponsors',
396
+ '--fgColor-success',
397
+ '--fgColor-upsell',
398
+ '--fgColor-white',
399
+ ],
400
+ },
401
+ {
402
+ category: 'background',
403
+ tokens: [
404
+ '--bgColor-accent-emphasis',
405
+ '--bgColor-accent-muted',
406
+ '--bgColor-attention-emphasis',
407
+ '--bgColor-attention-muted',
408
+ '--bgColor-black',
409
+ '--bgColor-closed-emphasis',
410
+ '--bgColor-closed-muted',
411
+ '--bgColor-danger-emphasis',
412
+ '--bgColor-danger-muted',
413
+ '--bgColor-default',
414
+ '--bgColor-disabled',
415
+ '--bgColor-done-emphasis',
416
+ '--bgColor-done-muted',
417
+ '--bgColor-emphasis',
418
+ '--bgColor-inset',
419
+ '--bgColor-inverse',
420
+ '--bgColor-muted',
421
+ '--bgColor-neutral-emphasis',
422
+ '--bgColor-neutral-muted',
423
+ '--bgColor-open-emphasis',
424
+ '--bgColor-open-muted',
425
+ '--bgColor-severe-emphasis',
426
+ '--bgColor-severe-muted',
427
+ '--bgColor-sponsors-emphasis',
428
+ '--bgColor-sponsors-muted',
429
+ '--bgColor-success-emphasis',
430
+ '--bgColor-success-muted',
431
+ '--bgColor-transparent',
432
+ '--bgColor-upsell-emphasis',
433
+ '--bgColor-upsell-muted',
434
+ '--bgColor-white',
435
+ ],
436
+ },
437
+ {
438
+ category: 'border',
439
+ tokens: [
440
+ '--borderColor-accent-emphasis',
441
+ '--borderColor-accent-muted',
442
+ '--borderColor-attention-emphasis',
443
+ '--borderColor-attention-muted',
444
+ '--borderColor-closed-emphasis',
445
+ '--borderColor-closed-muted',
446
+ '--borderColor-danger-emphasis',
447
+ '--borderColor-danger-muted',
448
+ '--borderColor-default',
449
+ '--borderColor-disabled',
450
+ '--borderColor-done-emphasis',
451
+ '--borderColor-done-muted',
452
+ '--borderColor-emphasis',
453
+ '--borderColor-muted',
454
+ '--borderColor-neutral-emphasis',
455
+ '--borderColor-neutral-muted',
456
+ '--borderColor-open-emphasis',
457
+ '--borderColor-open-muted',
458
+ '--borderColor-severe-emphasis',
459
+ '--borderColor-severe-muted',
460
+ '--borderColor-sponsors-emphasis',
461
+ '--borderColor-sponsors-muted',
462
+ '--borderColor-success-emphasis',
463
+ '--borderColor-success-muted',
464
+ '--borderColor-translucent',
465
+ '--borderColor-transparent',
466
+ '--borderColor-upsell-emphasis',
467
+ '--borderColor-upsell-muted',
468
+ ],
469
+ },
470
+ {
471
+ category: 'shadow',
472
+ tokens: [
473
+ '--shadow-floating-large',
474
+ '--shadow-floating-legacy',
475
+ '--shadow-floating-medium',
476
+ '--shadow-floating-small',
477
+ '--shadow-floating-xlarge',
478
+ '--shadow-inset',
479
+ '--shadow-resting-medium',
480
+ '--shadow-resting-small',
481
+ '--shadow-resting-xsmall',
482
+ ],
483
+ },
484
+ {
485
+ category: 'control',
486
+ tokens: [
487
+ '--control-bgColor-active',
488
+ '--control-bgColor-disabled',
489
+ '--control-bgColor-hover',
490
+ '--control-bgColor-rest',
491
+ '--control-bgColor-selected',
492
+ '--control-borderColor-danger',
493
+ '--control-borderColor-disabled',
494
+ '--control-borderColor-emphasis',
495
+ '--control-borderColor-rest',
496
+ '--control-borderColor-selected',
497
+ '--control-borderColor-success',
498
+ '--control-borderColor-warning',
499
+ '--control-checked-bgColor-active',
500
+ '--control-checked-bgColor-disabled',
501
+ '--control-checked-bgColor-hover',
502
+ '--control-checked-bgColor-rest',
503
+ '--control-checked-borderColor-active',
504
+ '--control-checked-borderColor-disabled',
505
+ '--control-checked-borderColor-hover',
506
+ '--control-checked-borderColor-rest',
507
+ '--control-checked-fgColor-disabled',
508
+ '--control-checked-fgColor-rest',
509
+ '--control-danger-bgColor-active',
510
+ '--control-danger-bgColor-hover',
511
+ '--control-danger-fgColor-hover',
512
+ '--control-danger-fgColor-rest',
513
+ '--control-fgColor-disabled',
514
+ '--control-fgColor-placeholder',
515
+ '--control-fgColor-rest',
516
+ '--control-iconColor-rest',
517
+ '--control-transparent-bgColor-active',
518
+ '--control-transparent-bgColor-disabled',
519
+ '--control-transparent-bgColor-hover',
520
+ '--control-transparent-bgColor-rest',
521
+ '--control-transparent-bgColor-selected',
522
+ '--control-transparent-borderColor-active',
523
+ '--control-transparent-borderColor-hover',
524
+ '--control-transparent-borderColor-rest',
525
+ ],
526
+ },
527
+ {
528
+ category: 'focus',
529
+ tokens: ['--focus-outlineColor'],
530
+ },
531
+ {
532
+ category: 'overlay',
533
+ tokens: ['--overlay-background-bgColor', '--overlay-bgColor', '--overlay-borderColor'],
534
+ },
535
+ ],
536
+ },
537
+ {
538
+ category: 'size',
539
+ tokens: [
540
+ {
541
+ category: 'base',
542
+ tokens: [
543
+ '--base-size-2',
544
+ '--base-size-4',
545
+ '--base-size-6',
546
+ '--base-size-8',
547
+ '--base-size-12',
548
+ '--base-size-16',
549
+ '--base-size-20',
550
+ '--base-size-24',
551
+ '--base-size-28',
552
+ '--base-size-32',
553
+ '--base-size-36',
554
+ '--base-size-40',
555
+ '--base-size-44',
556
+ '--base-size-48',
557
+ '--base-size-64',
558
+ '--base-size-80',
559
+ '--base-size-96',
560
+ '--base-size-112',
561
+ '--base-size-128',
562
+ ],
563
+ },
564
+ {
565
+ category: 'border',
566
+ tokens: [
567
+ {
568
+ category: 'border-size',
569
+ tokens: [
570
+ '--boxShadow-thick',
571
+ '--boxShadow-thicker',
572
+ '--boxShadow-thin',
573
+ '--borderWidth-default',
574
+ '--borderWidth-thick',
575
+ '--borderWidth-thicker',
576
+ '--borderWidth-thin',
577
+ ],
578
+ },
579
+ {
580
+ category: 'border-radius',
581
+ tokens: [
582
+ '--borderRadius-default',
583
+ '--borderRadius-full',
584
+ '--borderRadius-large',
585
+ '--borderRadius-medium',
586
+ '--borderRadius-small',
587
+ ],
588
+ },
589
+ {
590
+ category: 'outline',
591
+ tokens: ['--outline-focus-offset', '--outline-focus-width'],
592
+ },
593
+ ],
594
+ },
595
+ {
596
+ category: 'layout',
597
+ tokens: [
598
+ {
599
+ category: 'stack',
600
+ tokens: [
601
+ '--stack-gap-condensed',
602
+ '--stack-gap-normal',
603
+ '--stack-gap-spacious',
604
+ '--stack-padding-condensed',
605
+ '--stack-padding-normal',
606
+ '--stack-padding-spacious',
607
+ ],
608
+ },
609
+ {
610
+ category: 'controls',
611
+ tokens: [
612
+ '--controlStack-large-gap-auto',
613
+ '--controlStack-large-gap-condensed',
614
+ '--controlStack-large-gap-spacious',
615
+ '--controlStack-medium-gap-condensed',
616
+ '--controlStack-medium-gap-spacious',
617
+ '--controlStack-small-gap-condensed',
618
+ '--controlStack-small-gap-spacious',
619
+ '--controlStack-medium-gap-auto',
620
+ '--controlStack-small-gap-auto',
621
+
622
+ '--control-xsmall-gap',
623
+ '--control-small-gap',
624
+ '--control-medium-gap',
625
+ '--control-large-gap',
626
+ '--control-xlarge-gap',
627
+ '--control-xsmall-lineBoxHeight',
628
+ '--control-small-lineBoxHeight',
629
+ '--control-medium-lineBoxHeight',
630
+ '--control-large-lineBoxHeight',
631
+ '--control-xlarge-lineBoxHeight',
632
+ '--control-xsmall-paddingBlock',
633
+ '--control-small-paddingBlock',
634
+ '--control-medium-paddingBlock',
635
+ '--control-large-paddingBlock',
636
+ '--control-xlarge-paddingBlock',
637
+ '--control-xsmall-paddingInline-condensed',
638
+ '--control-small-paddingInline-condensed',
639
+ '--control-medium-paddingInline-condensed',
640
+ '--control-large-paddingInline-condensed',
641
+ '--control-xlarge-paddingInline-condensed',
642
+ '--control-xsmall-paddingInline-normal',
643
+ '--control-small-paddingInline-normal',
644
+ '--control-medium-paddingInline-normal',
645
+ '--control-large-paddingInline-normal',
646
+ '--control-xlarge-paddingInline-normal',
647
+ '--control-xsmall-paddingInline-spacious',
648
+ '--control-small-paddingInline-spacious',
649
+ '--control-medium-paddingInline-spacious',
650
+ '--control-large-paddingInline-spacious',
651
+ '--control-xlarge-paddingInline-spacious',
652
+ '--control-xsmall-size',
653
+ '--control-small-size',
654
+ '--control-medium-size',
655
+ '--control-large-size',
656
+ '--control-xlarge-size',
657
+ ],
658
+ },
659
+ {
660
+ category: 'overlay',
661
+ tokens: [
662
+ '--overlay-borderRadius',
663
+ '--overlay-height-large',
664
+ '--overlay-height-medium',
665
+ '--overlay-height-small',
666
+ '--overlay-height-xlarge',
667
+ '--overlay-offset',
668
+ '--overlay-padding-condensed',
669
+ '--overlay-padding-normal',
670
+ '--overlay-paddingBlock-condensed',
671
+ '--overlay-paddingBlock-normal',
672
+ '--overlay-width-large',
673
+ '--overlay-width-medium',
674
+ '--overlay-width-small',
675
+ '--overlay-width-xlarge',
676
+ '--overlay-width-xsmall',
677
+ ],
678
+ },
679
+ ],
680
+ },
681
+ ],
682
+ },
683
+ {
684
+ category: 'typography',
685
+ tokens: [
686
+ {
687
+ category: 'weight',
688
+ tokens: [
689
+ '--base-text-weight-light',
690
+ '--base-text-weight-normal',
691
+ '--base-text-weight-medium',
692
+ '--base-text-weight-semibold',
693
+ ],
694
+ },
695
+ {
696
+ category: 'font-family',
697
+ tokens: [
698
+ '--fontStack-monospace',
699
+ '--fontStack-sansSerif',
700
+ '--fontStack-sansSerifDisplay',
701
+ '--fontStack-system',
702
+ ],
703
+ },
704
+ {
705
+ category: 'font-shorthand',
706
+ tokens: [
707
+ '--text-body-shorthand-large',
708
+ '--text-body-shorthand-medium',
709
+ '--text-body-shorthand-small',
710
+ '--text-caption-shorthand',
711
+ '--text-codeBlock-shorthand',
712
+ '--text-codeInline-shorthand',
713
+ '--text-display-shorthand',
714
+ '--text-subtitle-shorthand',
715
+ '--text-title-shorthand-large',
716
+ '--text-title-shorthand-medium',
717
+ '--text-title-shorthand-small',
718
+ ],
719
+ },
720
+ {
721
+ category: 'display',
722
+ tokens: [
723
+ '--text-display-lineBoxHeight',
724
+ '--text-display-lineHeight',
725
+ '--text-display-size',
726
+ '--text-display-weight',
727
+ ],
728
+ },
729
+ {
730
+ category: 'title-large',
731
+ tokens: ['--text-title-lineHeight-large', '--text-title-size-large', '--text-title-weight-large'],
732
+ },
733
+ {
734
+ category: 'title-medium',
735
+ tokens: ['--text-title-lineHeight-medium', '--text-title-size-medium', '--text-title-weight-medium'],
736
+ },
737
+ {
738
+ category: 'title-small',
739
+ tokens: ['--text-title-lineHeight-small', '--text-title-size-small', '--text-title-weight-small'],
740
+ },
741
+ {
742
+ category: 'subtitle',
743
+ tokens: ['--text-subtitle-lineHeight', '--text-subtitle-size', '--text-subtitle-weight'],
744
+ },
745
+ {
746
+ category: 'body-large',
747
+ tokens: ['--text-body-lineHeight-large', '--text-body-size-large'],
748
+ },
749
+ {
750
+ category: 'body-medium',
751
+ tokens: ['--text-body-lineHeight-medium', '--text-body-size-medium'],
752
+ },
753
+ {
754
+ category: 'body-small',
755
+ tokens: ['--text-body-lineHeight-small', '--text-body-size-small'],
756
+ },
757
+ {
758
+ category: 'caption',
759
+ tokens: ['--text-caption-lineHeight', '--text-caption-size', '--text-caption-weight'],
760
+ },
761
+ {
762
+ category: 'code-block',
763
+ tokens: ['--text-codeBlock-lineHeight', '--text-codeBlock-size', '--text-codeBlock-weight'],
764
+ },
765
+ {
766
+ category: 'inline-code-block',
767
+ tokens: ['--text-codeInline-size', '--text-codeInline-weight'],
768
+ },
769
+ ],
770
+ },
771
+ ] as const
772
+
773
+ function serialize(token: Token): string {
774
+ if (typeof token === 'string') {
775
+ // eslint-disable-next-line github/unescaped-html-literal
776
+ return `<token name="${token}"></token>`
777
+ }
778
+ // eslint-disable-next-line github/unescaped-html-literal
779
+ return `<token-category name="${token.category}">\n${token.tokens.map(serialize).join('\n')}\n</token-category>`
780
+ }
781
+
782
+ // -----------------------------------------------------------------------------
783
+ // Design Tokens
784
+ // -----------------------------------------------------------------------------
785
+ server.tool('list_tokens', 'List all of the design tokens available from Primer', async () => {
786
+ let text =
787
+ 'Below is a list of all designs tokens available from Primer. They are organized by category. Tokens are used in CSS and CSS Modules. They can also be referred to in JavaScript files using the style attribute or prop in React components. To refer to the CSS Custom Property for a design token, wrap it in var(name-of-token). To learn how to use a specific token, use a corresponding usage tool for the category of the token. For example, if a token is a color token look for the get_color_usage tool. \n\n'
788
+
789
+ for (const token of tokens) {
790
+ text += serialize(token)
791
+ text += '\n'
792
+ }
793
+
794
+ return {
795
+ content: [
796
+ {
797
+ type: 'text',
798
+ text,
799
+ },
800
+ ],
801
+ }
802
+ })
803
+
804
+ // -----------------------------------------------------------------------------
805
+ // Foundations
806
+ // -----------------------------------------------------------------------------
807
+ server.tool('get_color_usage', 'Get the guidelines for how to apply color to a user interface', async () => {
808
+ const url = new URL(`/product/getting-started/foundations/color-usage`, 'https://primer.style')
809
+ const response = await fetch(url)
810
+ if (!response.ok) {
811
+ throw new Error(`Failed to fetch ${url} - ${response.statusText}`)
812
+ }
813
+
814
+ const html = await response.text()
815
+ if (!html) {
816
+ return {
817
+ content: [],
818
+ }
819
+ }
820
+
821
+ const $ = cheerio.load(html)
822
+ const source = $('main').html()
823
+ if (!source) {
824
+ return {
825
+ content: [],
826
+ }
827
+ }
828
+
829
+ const text = turndownService.turndown(source)
830
+
831
+ return {
832
+ content: [
833
+ {
834
+ type: 'text',
835
+ text: `Here is the documentation for color usage in Primer:\n\n${text}`,
836
+ },
837
+ ],
838
+ }
839
+ })
840
+
841
+ server.tool('get_typography_usage', 'Get the guidelines for how to apply typography to a user interface', async () => {
842
+ const url = new URL(`/product/getting-started/foundations/typography`, 'https://primer.style')
843
+ const response = await fetch(url)
844
+ if (!response.ok) {
845
+ throw new Error(`Failed to fetch ${url} - ${response.statusText}`)
846
+ }
847
+
848
+ const html = await response.text()
849
+ if (!html) {
850
+ return {
851
+ content: [],
852
+ }
853
+ }
854
+
855
+ const $ = cheerio.load(html)
856
+ const source = $('main').html()
857
+ if (!source) {
858
+ return {
859
+ content: [],
860
+ }
861
+ }
862
+
863
+ const text = turndownService.turndown(source)
864
+
865
+ return {
866
+ content: [
867
+ {
868
+ type: 'text',
869
+ text: `Here is the documentation for typography usage in Primer:\n\n${text}`,
870
+ },
871
+ ],
872
+ }
873
+ })
874
+
875
+ export {server}