@primer/mcp 0.0.3 → 0.0.4

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 CHANGED
@@ -4,6 +4,7 @@ import * as cheerio from 'cheerio'
4
4
  import {z} from 'zod'
5
5
  import TurndownService from 'turndown'
6
6
  import {listComponents, listPatterns, listIcons} from './primer'
7
+ import {tokens, serialize} from './primitives'
7
8
  import packageJson from '../package.json' with {type: 'json'}
8
9
 
9
10
  const server = new McpServer({
@@ -413,430 +414,14 @@ ${text}`,
413
414
  },
414
415
  )
415
416
 
416
- type Token = TokenCategory | string
417
- type TokenCategory = {
418
- category: string
419
- tokens: Array<Token>
420
- }
421
-
422
- const tokens: Array<Token> = [
423
- {
424
- category: 'color',
425
- tokens: [
426
- {
427
- category: 'foreground',
428
- tokens: [
429
- '--fgColor-accent',
430
- '--fgColor-attention',
431
- '--fgColor-black',
432
- '--fgColor-closed',
433
- '--fgColor-danger',
434
- '--fgColor-default',
435
- '--fgColor-disabled',
436
- '--fgColor-done',
437
- '--fgColor-link',
438
- '--fgColor-muted',
439
- '--fgColor-neutral',
440
- '--fgColor-onEmphasis',
441
- '--fgColor-onInverse',
442
- '--fgColor-open',
443
- '--fgColor-severe',
444
- '--fgColor-sponsors',
445
- '--fgColor-success',
446
- '--fgColor-upsell',
447
- '--fgColor-white',
448
- ],
449
- },
450
- {
451
- category: 'background',
452
- tokens: [
453
- '--bgColor-accent-emphasis',
454
- '--bgColor-accent-muted',
455
- '--bgColor-attention-emphasis',
456
- '--bgColor-attention-muted',
457
- '--bgColor-black',
458
- '--bgColor-closed-emphasis',
459
- '--bgColor-closed-muted',
460
- '--bgColor-danger-emphasis',
461
- '--bgColor-danger-muted',
462
- '--bgColor-default',
463
- '--bgColor-disabled',
464
- '--bgColor-done-emphasis',
465
- '--bgColor-done-muted',
466
- '--bgColor-emphasis',
467
- '--bgColor-inset',
468
- '--bgColor-inverse',
469
- '--bgColor-muted',
470
- '--bgColor-neutral-emphasis',
471
- '--bgColor-neutral-muted',
472
- '--bgColor-open-emphasis',
473
- '--bgColor-open-muted',
474
- '--bgColor-severe-emphasis',
475
- '--bgColor-severe-muted',
476
- '--bgColor-sponsors-emphasis',
477
- '--bgColor-sponsors-muted',
478
- '--bgColor-success-emphasis',
479
- '--bgColor-success-muted',
480
- '--bgColor-transparent',
481
- '--bgColor-upsell-emphasis',
482
- '--bgColor-upsell-muted',
483
- '--bgColor-white',
484
- ],
485
- },
486
- {
487
- category: 'border',
488
- tokens: [
489
- '--borderColor-accent-emphasis',
490
- '--borderColor-accent-muted',
491
- '--borderColor-attention-emphasis',
492
- '--borderColor-attention-muted',
493
- '--borderColor-closed-emphasis',
494
- '--borderColor-closed-muted',
495
- '--borderColor-danger-emphasis',
496
- '--borderColor-danger-muted',
497
- '--borderColor-default',
498
- '--borderColor-disabled',
499
- '--borderColor-done-emphasis',
500
- '--borderColor-done-muted',
501
- '--borderColor-emphasis',
502
- '--borderColor-muted',
503
- '--borderColor-neutral-emphasis',
504
- '--borderColor-neutral-muted',
505
- '--borderColor-open-emphasis',
506
- '--borderColor-open-muted',
507
- '--borderColor-severe-emphasis',
508
- '--borderColor-severe-muted',
509
- '--borderColor-sponsors-emphasis',
510
- '--borderColor-sponsors-muted',
511
- '--borderColor-success-emphasis',
512
- '--borderColor-success-muted',
513
- '--borderColor-translucent',
514
- '--borderColor-transparent',
515
- '--borderColor-upsell-emphasis',
516
- '--borderColor-upsell-muted',
517
- ],
518
- },
519
- {
520
- category: 'shadow',
521
- tokens: [
522
- '--shadow-floating-large',
523
- '--shadow-floating-legacy',
524
- '--shadow-floating-medium',
525
- '--shadow-floating-small',
526
- '--shadow-floating-xlarge',
527
- '--shadow-inset',
528
- '--shadow-resting-medium',
529
- '--shadow-resting-small',
530
- '--shadow-resting-xsmall',
531
- ],
532
- },
533
- {
534
- category: 'control',
535
- tokens: [
536
- '--control-bgColor-active',
537
- '--control-bgColor-disabled',
538
- '--control-bgColor-hover',
539
- '--control-bgColor-rest',
540
- '--control-bgColor-selected',
541
- '--control-borderColor-danger',
542
- '--control-borderColor-disabled',
543
- '--control-borderColor-emphasis',
544
- '--control-borderColor-rest',
545
- '--control-borderColor-selected',
546
- '--control-borderColor-success',
547
- '--control-borderColor-warning',
548
- '--control-checked-bgColor-active',
549
- '--control-checked-bgColor-disabled',
550
- '--control-checked-bgColor-hover',
551
- '--control-checked-bgColor-rest',
552
- '--control-checked-borderColor-active',
553
- '--control-checked-borderColor-disabled',
554
- '--control-checked-borderColor-hover',
555
- '--control-checked-borderColor-rest',
556
- '--control-checked-fgColor-disabled',
557
- '--control-checked-fgColor-rest',
558
- '--control-danger-bgColor-active',
559
- '--control-danger-bgColor-hover',
560
- '--control-danger-fgColor-hover',
561
- '--control-danger-fgColor-rest',
562
- '--control-fgColor-disabled',
563
- '--control-fgColor-placeholder',
564
- '--control-fgColor-rest',
565
- '--control-iconColor-rest',
566
- '--control-transparent-bgColor-active',
567
- '--control-transparent-bgColor-disabled',
568
- '--control-transparent-bgColor-hover',
569
- '--control-transparent-bgColor-rest',
570
- '--control-transparent-bgColor-selected',
571
- '--control-transparent-borderColor-active',
572
- '--control-transparent-borderColor-hover',
573
- '--control-transparent-borderColor-rest',
574
- ],
575
- },
576
- {
577
- category: 'focus',
578
- tokens: ['--focus-outlineColor'],
579
- },
580
- {
581
- category: 'overlay',
582
- tokens: ['--overlay-background-bgColor', '--overlay-bgColor', '--overlay-borderColor'],
583
- },
584
- ],
585
- },
586
- {
587
- category: 'size',
588
- tokens: [
589
- {
590
- category: 'base',
591
- tokens: [
592
- '--base-size-2',
593
- '--base-size-4',
594
- '--base-size-6',
595
- '--base-size-8',
596
- '--base-size-12',
597
- '--base-size-16',
598
- '--base-size-20',
599
- '--base-size-24',
600
- '--base-size-28',
601
- '--base-size-32',
602
- '--base-size-36',
603
- '--base-size-40',
604
- '--base-size-44',
605
- '--base-size-48',
606
- '--base-size-64',
607
- '--base-size-80',
608
- '--base-size-96',
609
- '--base-size-112',
610
- '--base-size-128',
611
- ],
612
- },
613
- {
614
- category: 'border',
615
- tokens: [
616
- {
617
- category: 'border-size',
618
- tokens: [
619
- '--boxShadow-thick',
620
- '--boxShadow-thicker',
621
- '--boxShadow-thin',
622
- '--borderWidth-default',
623
- '--borderWidth-thick',
624
- '--borderWidth-thicker',
625
- '--borderWidth-thin',
626
- ],
627
- },
628
- {
629
- category: 'border-radius',
630
- tokens: [
631
- '--borderRadius-default',
632
- '--borderRadius-full',
633
- '--borderRadius-large',
634
- '--borderRadius-medium',
635
- '--borderRadius-small',
636
- ],
637
- },
638
- {
639
- category: 'outline',
640
- tokens: ['--outline-focus-offset', '--outline-focus-width'],
641
- },
642
- ],
643
- },
644
- {
645
- category: 'layout',
646
- tokens: [
647
- {
648
- category: 'stack',
649
- tokens: [
650
- '--stack-gap-condensed',
651
- '--stack-gap-normal',
652
- '--stack-gap-spacious',
653
- '--stack-padding-condensed',
654
- '--stack-padding-normal',
655
- '--stack-padding-spacious',
656
- ],
657
- },
658
- {
659
- category: 'controls',
660
- tokens: [
661
- '--controlStack-large-gap-auto',
662
- '--controlStack-large-gap-condensed',
663
- '--controlStack-large-gap-spacious',
664
- '--controlStack-medium-gap-condensed',
665
- '--controlStack-medium-gap-spacious',
666
- '--controlStack-small-gap-condensed',
667
- '--controlStack-small-gap-spacious',
668
- '--controlStack-medium-gap-auto',
669
- '--controlStack-small-gap-auto',
670
-
671
- '--control-xsmall-gap',
672
- '--control-small-gap',
673
- '--control-medium-gap',
674
- '--control-large-gap',
675
- '--control-xlarge-gap',
676
- '--control-xsmall-lineBoxHeight',
677
- '--control-small-lineBoxHeight',
678
- '--control-medium-lineBoxHeight',
679
- '--control-large-lineBoxHeight',
680
- '--control-xlarge-lineBoxHeight',
681
- '--control-xsmall-paddingBlock',
682
- '--control-small-paddingBlock',
683
- '--control-medium-paddingBlock',
684
- '--control-large-paddingBlock',
685
- '--control-xlarge-paddingBlock',
686
- '--control-xsmall-paddingInline-condensed',
687
- '--control-small-paddingInline-condensed',
688
- '--control-medium-paddingInline-condensed',
689
- '--control-large-paddingInline-condensed',
690
- '--control-xlarge-paddingInline-condensed',
691
- '--control-xsmall-paddingInline-normal',
692
- '--control-small-paddingInline-normal',
693
- '--control-medium-paddingInline-normal',
694
- '--control-large-paddingInline-normal',
695
- '--control-xlarge-paddingInline-normal',
696
- '--control-xsmall-paddingInline-spacious',
697
- '--control-small-paddingInline-spacious',
698
- '--control-medium-paddingInline-spacious',
699
- '--control-large-paddingInline-spacious',
700
- '--control-xlarge-paddingInline-spacious',
701
- '--control-xsmall-size',
702
- '--control-small-size',
703
- '--control-medium-size',
704
- '--control-large-size',
705
- '--control-xlarge-size',
706
- ],
707
- },
708
- {
709
- category: 'overlay',
710
- tokens: [
711
- '--overlay-borderRadius',
712
- '--overlay-height-large',
713
- '--overlay-height-medium',
714
- '--overlay-height-small',
715
- '--overlay-height-xlarge',
716
- '--overlay-offset',
717
- '--overlay-padding-condensed',
718
- '--overlay-padding-normal',
719
- '--overlay-paddingBlock-condensed',
720
- '--overlay-paddingBlock-normal',
721
- '--overlay-width-large',
722
- '--overlay-width-medium',
723
- '--overlay-width-small',
724
- '--overlay-width-xlarge',
725
- '--overlay-width-xsmall',
726
- ],
727
- },
728
- ],
729
- },
730
- ],
731
- },
732
- {
733
- category: 'typography',
734
- tokens: [
735
- {
736
- category: 'weight',
737
- tokens: [
738
- '--base-text-weight-light',
739
- '--base-text-weight-normal',
740
- '--base-text-weight-medium',
741
- '--base-text-weight-semibold',
742
- ],
743
- },
744
- {
745
- category: 'font-family',
746
- tokens: [
747
- '--fontStack-monospace',
748
- '--fontStack-sansSerif',
749
- '--fontStack-sansSerifDisplay',
750
- '--fontStack-system',
751
- ],
752
- },
753
- {
754
- category: 'font-shorthand',
755
- tokens: [
756
- '--text-body-shorthand-large',
757
- '--text-body-shorthand-medium',
758
- '--text-body-shorthand-small',
759
- '--text-caption-shorthand',
760
- '--text-codeBlock-shorthand',
761
- '--text-codeInline-shorthand',
762
- '--text-display-shorthand',
763
- '--text-subtitle-shorthand',
764
- '--text-title-shorthand-large',
765
- '--text-title-shorthand-medium',
766
- '--text-title-shorthand-small',
767
- ],
768
- },
769
- {
770
- category: 'display',
771
- tokens: [
772
- '--text-display-lineBoxHeight',
773
- '--text-display-lineHeight',
774
- '--text-display-size',
775
- '--text-display-weight',
776
- ],
777
- },
778
- {
779
- category: 'title-large',
780
- tokens: ['--text-title-lineHeight-large', '--text-title-size-large', '--text-title-weight-large'],
781
- },
782
- {
783
- category: 'title-medium',
784
- tokens: ['--text-title-lineHeight-medium', '--text-title-size-medium', '--text-title-weight-medium'],
785
- },
786
- {
787
- category: 'title-small',
788
- tokens: ['--text-title-lineHeight-small', '--text-title-size-small', '--text-title-weight-small'],
789
- },
790
- {
791
- category: 'subtitle',
792
- tokens: ['--text-subtitle-lineHeight', '--text-subtitle-size', '--text-subtitle-weight'],
793
- },
794
- {
795
- category: 'body-large',
796
- tokens: ['--text-body-lineHeight-large', '--text-body-size-large'],
797
- },
798
- {
799
- category: 'body-medium',
800
- tokens: ['--text-body-lineHeight-medium', '--text-body-size-medium'],
801
- },
802
- {
803
- category: 'body-small',
804
- tokens: ['--text-body-lineHeight-small', '--text-body-size-small'],
805
- },
806
- {
807
- category: 'caption',
808
- tokens: ['--text-caption-lineHeight', '--text-caption-size', '--text-caption-weight'],
809
- },
810
- {
811
- category: 'code-block',
812
- tokens: ['--text-codeBlock-lineHeight', '--text-codeBlock-size', '--text-codeBlock-weight'],
813
- },
814
- {
815
- category: 'inline-code-block',
816
- tokens: ['--text-codeInline-size', '--text-codeInline-weight'],
817
- },
818
- ],
819
- },
820
- ] as const
821
-
822
- function serialize(token: Token): string {
823
- if (typeof token === 'string') {
824
- return `<token name="${token}"></token>`
825
- }
826
- return `<token-category name="${token.category}">\n${token.tokens.map(serialize).join('\n')}\n</token-category>`
827
- }
828
-
829
417
  // -----------------------------------------------------------------------------
830
418
  // Design Tokens
831
419
  // -----------------------------------------------------------------------------
832
420
  server.tool('list_tokens', 'List all of the design tokens available from Primer', async () => {
833
421
  let text =
834
- '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'
422
+ 'Below is a list of all design tokens available from Primer. Tokens are used in CSS and CSS Modules. 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'
835
423
 
836
- for (const token of tokens) {
837
- text += serialize(token)
838
- text += '\n'
839
- }
424
+ text += serialize(tokens)
840
425
 
841
426
  return {
842
427
  content: [
@@ -1005,4 +590,100 @@ ${text}`,
1005
590
  },
1006
591
  )
1007
592
 
593
+ // -----------------------------------------------------------------------------
594
+ // Coding guidelines
595
+ // -----------------------------------------------------------------------------
596
+ server.tool(
597
+ 'primer_coding_guidelines',
598
+ 'Get the guidelines when writing code that uses Primer or for UI code that you are creating',
599
+ async () => {
600
+ return {
601
+ content: [
602
+ {
603
+ type: 'text',
604
+ text: `When writing code that uses Primer, follow these guidelines:
605
+
606
+ ## Design Tokens
607
+
608
+ - Prefer design tokens over hard-coded values. For example, use \`var(--fgColor-default)\` instead of \`#24292f\`. Use the \`list_tokens\` tool to find the design token you need.
609
+ - Prefer recommending design tokens in the same group for related CSS properties. For example, when styling background and border color, use tokens from the same group/category
610
+
611
+ ## Authoring & Using Components
612
+
613
+ - Prefer re-using a component from Primer when possible over writing a new component.
614
+ - Prefer using existing props for a component for styling instead of adding styling to a component
615
+ - Prefer using icons from Primer instead of creating new icons. Use the \`list_icons\` tool to find the icon you need.
616
+ - Follow patterns from Primer when creating new components. Use the \`list_patterns\` tool to find the pattern you need, if one exists
617
+ - When using a component from Primer, make sure to follow the component's usage and accessibility guidelines
618
+
619
+ ## Coding guidelines
620
+
621
+ The following list of coding guidelines must be followed:
622
+
623
+ - Do not use the sx prop for styling components. Instead, use CSS Modules.
624
+ - Do not use the Box component for styling components. Instead, use CSS Modules.
625
+ `,
626
+ },
627
+ ],
628
+ }
629
+ },
630
+ )
631
+
632
+ // -----------------------------------------------------------------------------
633
+ // Accessibility
634
+ // -----------------------------------------------------------------------------
635
+
636
+ /**
637
+ * The `review_alt_text` tool is experimental and may be removed in future versions.
638
+ *
639
+ * The intent of this tool is to assist products like Copilot Code Review and Copilot Coding Agent
640
+ * in reviewing both user- and AI-generated alt text for images, ensuring compliance with accessibility guidelines.
641
+ * This tool is not intended to replace human-generated alt text; rather, it supports the review process
642
+ * by providing suggestions for improvement. It should be used alongside human review, not as a substitute.
643
+ *
644
+ *
645
+ **/
646
+ server.tool(
647
+ 'review_alt_text',
648
+ 'Evaluates image alt text against accessibility best practices and context relevance.',
649
+ {
650
+ surroundingText: z.string().describe('Text surrounding the image, relevant to the image.'),
651
+ alt: z.string().describe('The alt text of the image being evaluated'),
652
+ image: z
653
+ .union([
654
+ z.instanceof(File).describe('The image src file being evaluated'),
655
+ z.string().url().describe('The URL of the image src being evaluated'),
656
+ z.string().describe('The file path of the image src being evaluated'),
657
+ ])
658
+ .describe('The image file, file path, or URL being evaluated'),
659
+ },
660
+ async ({surroundingText, alt, image}) => {
661
+ // Call the LLM through MCP sampling
662
+ const response = await server.server.createMessage({
663
+ messages: [
664
+ {
665
+ role: 'user',
666
+ content: {
667
+ type: 'text',
668
+ text: `Does this alt text: '${alt}' meet accessibility guidelines and describe the image: ${image} accurately in context of this surrounding text: '${surroundingText}'?\n\n`,
669
+ },
670
+ },
671
+ ],
672
+ sampling: {temperature: 0.4},
673
+ maxTokens: 500,
674
+ })
675
+
676
+ return {
677
+ content: [
678
+ {
679
+ type: 'text',
680
+ text: response.content.type === 'text' ? response.content.text : 'Unable to generate summary',
681
+ },
682
+ ],
683
+ altTextEvaluation: response.content.type === 'text' ? response.content.text : 'Unable to generate summary',
684
+ nextSteps: `If the evaluation indicates issues with the alt text, provide more meaningful alt text based on the feedback. DO NOT run this tool repeatedly on the same image - evaluations may vary slightly with each run.`,
685
+ }
686
+ },
687
+ )
688
+
1008
689
  export {server}