@primer/mcp 0.0.5 → 0.2.0

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.
@@ -1,718 +0,0 @@
1
- import { McpServer } from '@modelcontextprotocol/sdk/server/mcp.js';
2
- import * as cheerio from 'cheerio';
3
- import { z } from 'zod';
4
- import TurndownService from 'turndown';
5
- import componentsMetadata from '@primer/react/generated/components.json' with { type: 'json' };
6
- import octicons from '@primer/octicons/build/data.json' with { type: 'json' };
7
- import baseMotion from '@primer/primitives/dist/docs/base/motion/motion.json' with { type: 'json' };
8
- import baseSize from '@primer/primitives/dist/docs/base/size/size.json' with { type: 'json' };
9
- import baseTypography from '@primer/primitives/dist/docs/base/typography/typography.json' with { type: 'json' };
10
- import functionalSizeBorder from '@primer/primitives/dist/docs/functional/size/border.json' with { type: 'json' };
11
- import functionalSizeCoarse from '@primer/primitives/dist/docs/functional/size/size-coarse.json' with { type: 'json' };
12
- import functionalSizeFine from '@primer/primitives/dist/docs/functional/size/size-fine.json' with { type: 'json' };
13
- import functionalSize from '@primer/primitives/dist/docs/functional/size/size.json' with { type: 'json' };
14
- import light from '@primer/primitives/dist/docs/functional/themes/light.json' with { type: 'json' };
15
- import functionalTypography from '@primer/primitives/dist/docs/functional/typography/typography.json' with { type: 'json' };
16
-
17
- function idToSlug(id) {
18
- if (id === 'actionbar') {
19
- return 'action-bar';
20
- }
21
- if (id === 'tooltip-v2') {
22
- return 'tooltip';
23
- }
24
- if (id === 'dialog_v2') {
25
- return 'dialog';
26
- }
27
- if (id.startsWith('skeleton')) {
28
- return 'skeleton-loaders';
29
- }
30
- return id.replaceAll('_', '-');
31
- }
32
- const components = Object.entries(componentsMetadata.components).map(([id, component]) => {
33
- return {
34
- id,
35
- name: component.name,
36
- importPath: component.importPath,
37
- slug: idToSlug(id)
38
- };
39
- });
40
- function listComponents() {
41
- return components;
42
- }
43
- const patterns = [{
44
- id: 'data-visualization',
45
- name: 'Data Visualization'
46
- }, {
47
- id: 'degraded-experiences',
48
- name: 'Degraded Experiences'
49
- }, {
50
- id: 'empty-states',
51
- name: 'Empty States'
52
- }, {
53
- id: 'feature-onboarding',
54
- name: 'Feature Onboarding'
55
- }, {
56
- id: 'forms',
57
- name: 'Forms'
58
- }, {
59
- id: 'loading',
60
- name: 'Loading'
61
- }, {
62
- id: 'navigation',
63
- name: 'Navigation'
64
- }, {
65
- id: 'notification-messaging',
66
- name: 'Notification message'
67
- }, {
68
- id: 'progressive-disclosure',
69
- name: 'Progressive disclosure'
70
- }, {
71
- id: 'saving',
72
- name: 'Saving'
73
- }];
74
- function listPatterns() {
75
- return patterns;
76
- }
77
- const icons = Object.values(octicons).map(icon => {
78
- return {
79
- name: icon.name,
80
- keywords: icon.keywords,
81
- heights: Object.keys(icon.heights)
82
- };
83
- });
84
- function listIcons() {
85
- return icons;
86
- }
87
-
88
- const categories = {
89
- base: {
90
- motion: Object.values(baseMotion).map(token => {
91
- return {
92
- name: token.name,
93
- type: token.type,
94
- value: token.value
95
- };
96
- }),
97
- size: Object.values(baseSize).map(token => {
98
- return {
99
- name: token.name,
100
- type: token.type,
101
- value: token.value
102
- };
103
- }),
104
- typography: Object.values(baseTypography).map(token => {
105
- return {
106
- name: token.name,
107
- type: token.type,
108
- value: token.value
109
- };
110
- })
111
- },
112
- functional: {
113
- border: Object.values(functionalSizeBorder).map(token => {
114
- return {
115
- name: token.name,
116
- type: token.type,
117
- value: token.value
118
- };
119
- }),
120
- sizeCoarse: Object.values(functionalSizeCoarse).map(token => {
121
- return {
122
- name: token.name,
123
- type: token.type,
124
- value: token.value
125
- };
126
- }),
127
- sizeFine: Object.values(functionalSizeFine).map(token => {
128
- return {
129
- name: token.name,
130
- type: token.type,
131
- value: token.value
132
- };
133
- }),
134
- size: Object.values(functionalSize).map(token => {
135
- return {
136
- name: token.name,
137
- type: token.type,
138
- value: token.value
139
- };
140
- }),
141
- themes: {
142
- light: Object.values(light).map(token => {
143
- return {
144
- name: token.name,
145
- type: token.type,
146
- value: token.value
147
- };
148
- })
149
- },
150
- typography: Object.values(functionalTypography).map(token => {
151
- return {
152
- name: token.name,
153
- type: token.type,
154
- value: token.value
155
- };
156
- })
157
- }
158
- };
159
- const tokens = [...categories.base.motion, ...categories.base.size, ...categories.base.typography, ...categories.functional.border, ...categories.functional.sizeCoarse, ...categories.functional.sizeFine, ...categories.functional.size, ...categories.functional.themes.light, ...categories.functional.typography];
160
- function serialize(value) {
161
- return value.map(token => {
162
- return `<token name="${token.name}" value="${token.value}" type="${token.type}"></token>`;
163
- }).join('\n');
164
- }
165
-
166
- var version = "0.0.5";
167
- var packageJson = {
168
- version: version};
169
-
170
- const server = new McpServer({
171
- name: 'Primer',
172
- version: packageJson.version
173
- });
174
- const turndownService = new TurndownService();
175
-
176
- // -----------------------------------------------------------------------------
177
- // Project setup
178
- // -----------------------------------------------------------------------------
179
- server.tool('init', 'Setup or create a project that includes Primer React', async () => {
180
- const url = new URL(`/product/getting-started/react`, 'https://primer.style');
181
- const response = await fetch(url);
182
- if (!response.ok) {
183
- throw new Error(`Failed to fetch ${url}: ${response.statusText}`);
184
- }
185
- const html = await response.text();
186
- if (!html) {
187
- return {
188
- content: []
189
- };
190
- }
191
- const $ = cheerio.load(html);
192
- const source = $('main').html();
193
- if (!source) {
194
- return {
195
- content: []
196
- };
197
- }
198
- const text = turndownService.turndown(source);
199
- return {
200
- content: [{
201
- type: 'text',
202
- text: `The getting started documentation for Primer React is included below. It's important that the project:
203
-
204
- - Is using a tool like Vite, Next.js, etc that supports TypeScript and React. If the project does not have support for that, generate an appropriate project scaffold
205
- - Installs the latest version of \`@primer/react\` from \`npm\`
206
- - Correctly adds the \`ThemeProvider\` and \`BaseStyles\` components to the root of the application
207
- - Includes an import to a theme from \`@primer/primitives\`
208
- - If the project wants to use icons, also install the \`@primer/octicons-react\` from \`npm\`
209
- - Add appropriate agent instructions (like for copilot) to the project to prefer using components, tokens, icons, and more from Primer packages
210
-
211
- ---
212
-
213
- ${text}
214
- `
215
- }]
216
- };
217
- });
218
-
219
- // -----------------------------------------------------------------------------
220
- // Components
221
- // -----------------------------------------------------------------------------
222
- server.tool('list_components', 'List all of the components available from Primer React', async () => {
223
- const components = listComponents().map(component => {
224
- return `- ${component.name}`;
225
- });
226
- return {
227
- content: [{
228
- type: 'text',
229
- text: `The following components are available in the @primer/react in TypeScript projects:
230
-
231
- ${components.join('\n')}
232
-
233
- 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.`
234
- }]
235
- };
236
- });
237
- server.tool('get_component', 'Retrieve documentation and usage details for a specific React component from the @primer/react package by its name. This tool provides the official Primer documentation for any listed component, making it easy to inspect, reuse, or integrate components in your project.', {
238
- name: z.string().describe('The name of the component to retrieve')
239
- }, async ({
240
- name
241
- }) => {
242
- const components = listComponents();
243
- const match = components.find(component => {
244
- return component.name === name || component.name.toLowerCase() === name.toLowerCase();
245
- });
246
- if (!match) {
247
- return {
248
- content: [{
249
- type: 'text',
250
- text: `There is no component named \`${name}\` in the @primer/react package. For a full list of components, use the \`list_components\` tool.`
251
- }]
252
- };
253
- }
254
- const url = new URL(`/product/components/${match.slug}`, 'https://primer.style');
255
- const response = await fetch(url);
256
- if (!response.ok) {
257
- throw new Error(`Failed to fetch ${url}: ${response.statusText}`);
258
- }
259
- const html = await response.text();
260
- if (!html) {
261
- return {
262
- content: []
263
- };
264
- }
265
- const $ = cheerio.load(html);
266
- const source = $('main').html();
267
- if (!source) {
268
- return {
269
- content: []
270
- };
271
- }
272
- const text = turndownService.turndown(source);
273
- return {
274
- content: [{
275
- type: 'text',
276
- text: `Here is the documentation for the \`${name}\` component from the @primer/react package:
277
- ${text}`
278
- }]
279
- };
280
- });
281
- server.tool('get_component_examples', 'Get examples for how to use a component from Primer React', {
282
- name: z.string().describe('The name of the component to retrieve')
283
- }, async ({
284
- name
285
- }) => {
286
- const components = listComponents();
287
- const match = components.find(component => {
288
- return component.name === name;
289
- });
290
- if (!match) {
291
- return {
292
- content: [{
293
- type: 'text',
294
- text: `There is no component named \`${name}\` in the @primer/react package. For a full list of components, use the \`get_components\` tool.`
295
- }]
296
- };
297
- }
298
- const url = new URL(`/product/components/${match.id}`, 'https://primer.style');
299
- const response = await fetch(url);
300
- if (!response.ok) {
301
- throw new Error(`Failed to fetch ${url}: ${response.statusText}`);
302
- }
303
- const html = await response.text();
304
- if (!html) {
305
- return {
306
- content: []
307
- };
308
- }
309
- const $ = cheerio.load(html);
310
- const source = $('main').html();
311
- if (!source) {
312
- return {
313
- content: []
314
- };
315
- }
316
- const text = turndownService.turndown(source);
317
- return {
318
- content: [{
319
- type: 'text',
320
- text: `Here are some examples of how to use the \`${name}\` component from the @primer/react package:
321
-
322
- ${text}`
323
- }]
324
- };
325
- });
326
- server.tool('get_component_usage_guidelines', 'Get usage information for how to use a component from Primer', {
327
- name: z.string().describe('The name of the component to retrieve')
328
- }, async ({
329
- name
330
- }) => {
331
- const components = listComponents();
332
- const match = components.find(component => {
333
- return component.name === name;
334
- });
335
- if (!match) {
336
- return {
337
- content: [{
338
- type: 'text',
339
- text: `There is no component named \`${name}\` in the @primer/react package. For a full list of components, use the \`get_components\` tool.`
340
- }]
341
- };
342
- }
343
- const url = new URL(`/product/components/${match.id}/guidelines`, 'https://primer.style');
344
- const response = await fetch(url);
345
- if (!response.ok) {
346
- if (response.status >= 400 && response.status < 500 || response.status >= 300 && response.status < 400) {
347
- return {
348
- content: [{
349
- type: 'text',
350
- text: `There are no accessibility guidelines for the \`${name}\` component in the @primer/react package.`
351
- }]
352
- };
353
- }
354
- throw new Error(`Failed to fetch ${url}: ${response.statusText}`);
355
- }
356
- const html = await response.text();
357
- if (!html) {
358
- return {
359
- content: []
360
- };
361
- }
362
- const $ = cheerio.load(html);
363
- const source = $('main').html();
364
- if (!source) {
365
- return {
366
- content: []
367
- };
368
- }
369
- const text = turndownService.turndown(source);
370
- return {
371
- content: [{
372
- type: 'text',
373
- text: `Here are the usage guidelines for the \`${name}\` component from the @primer/react package:
374
-
375
- ${text}`
376
- }]
377
- };
378
- });
379
- server.tool('get_component_accessibility_guidelines', 'Retrieve accessibility guidelines and best practices for a specific component from the @primer/react package by its name. Use this tool to get official accessibility recommendations, usage tips, and requirements to ensure your UI components are inclusive and meet accessibility standards.', {
380
- name: z.string().describe('The name of the component to retrieve')
381
- }, async ({
382
- name
383
- }) => {
384
- const components = listComponents();
385
- const match = components.find(component => {
386
- return component.name === name;
387
- });
388
- if (!match) {
389
- return {
390
- content: [{
391
- type: 'text',
392
- text: `There is no component named \`${name}\` in the @primer/react package. For a full list of components, use the \`list_components\` tool.`
393
- }]
394
- };
395
- }
396
- const url = new URL(`/product/components/${match.id}/accessibility`, 'https://primer.style');
397
- const response = await fetch(url);
398
- if (!response.ok) {
399
- if (response.status >= 400 && response.status < 500 || response.status >= 300 && response.status < 400) {
400
- return {
401
- content: [{
402
- type: 'text',
403
- text: `There are no accessibility guidelines for the \`${name}\` component in the @primer/react package.`
404
- }]
405
- };
406
- }
407
- throw new Error(`Failed to fetch ${url}: ${response.statusText}`);
408
- }
409
- const html = await response.text();
410
- if (!html) {
411
- return {
412
- content: []
413
- };
414
- }
415
- const $ = cheerio.load(html);
416
- const source = $('main').html();
417
- if (!source) {
418
- return {
419
- content: []
420
- };
421
- }
422
- const text = turndownService.turndown(source);
423
- return {
424
- content: [{
425
- type: 'text',
426
- text: `Here are the accessibility guidelines for the \`${name}\` component from the @primer/react package:
427
-
428
- ${text}`
429
- }]
430
- };
431
- });
432
-
433
- // -----------------------------------------------------------------------------
434
- // Patterns
435
- // -----------------------------------------------------------------------------
436
- server.tool('list_patterns', 'List all of the patterns available from Primer React', async () => {
437
- const patterns = listPatterns().map(pattern => {
438
- return `- ${pattern.name}`;
439
- });
440
- return {
441
- content: [{
442
- type: 'text',
443
- text: `The following patterns are available in the @primer/react in TypeScript projects:
444
-
445
- ${patterns.join('\n')}`
446
- }]
447
- };
448
- });
449
- server.tool('get_pattern', 'Get a specific pattern by name', {
450
- name: z.string().describe('The name of the pattern to retrieve')
451
- }, async ({
452
- name
453
- }) => {
454
- const patterns = listPatterns();
455
- const match = patterns.find(pattern => {
456
- return pattern.name === name;
457
- });
458
- if (!match) {
459
- return {
460
- content: [{
461
- type: 'text',
462
- text: `There is no pattern named \`${name}\` in the @primer/react package. For a full list of patterns, use the \`list_patterns\` tool.`
463
- }]
464
- };
465
- }
466
- const url = new URL(`/product/ui-patterns/${match.id}`, 'https://primer.style');
467
- const response = await fetch(url);
468
- if (!response.ok) {
469
- throw new Error(`Failed to fetch ${url} - ${response.statusText}`);
470
- }
471
- const html = await response.text();
472
- if (!html) {
473
- return {
474
- content: []
475
- };
476
- }
477
- const $ = cheerio.load(html);
478
- const source = $('main').html();
479
- if (!source) {
480
- return {
481
- content: []
482
- };
483
- }
484
- const text = turndownService.turndown(source);
485
- return {
486
- content: [{
487
- type: 'text',
488
- text: `Here are the guidelines for the \`${name}\` pattern for Primer:
489
-
490
- ${text}`
491
- }]
492
- };
493
- });
494
-
495
- // -----------------------------------------------------------------------------
496
- // Design Tokens
497
- // -----------------------------------------------------------------------------
498
- server.tool('list_tokens', 'List all of the design tokens available from Primer', async () => {
499
- let text = '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';
500
- text += serialize(tokens);
501
- return {
502
- content: [{
503
- type: 'text',
504
- text
505
- }]
506
- };
507
- });
508
-
509
- // -----------------------------------------------------------------------------
510
- // Foundations
511
- // -----------------------------------------------------------------------------
512
- server.tool('get_color_usage', 'Get the guidelines for how to apply color to a user interface', async () => {
513
- const url = new URL(`/product/getting-started/foundations/color-usage`, 'https://primer.style');
514
- const response = await fetch(url);
515
- if (!response.ok) {
516
- throw new Error(`Failed to fetch ${url} - ${response.statusText}`);
517
- }
518
- const html = await response.text();
519
- if (!html) {
520
- return {
521
- content: []
522
- };
523
- }
524
- const $ = cheerio.load(html);
525
- const source = $('main').html();
526
- if (!source) {
527
- return {
528
- content: []
529
- };
530
- }
531
- const text = turndownService.turndown(source);
532
- return {
533
- content: [{
534
- type: 'text',
535
- text: `Here is the documentation for color usage in Primer:\n\n${text}`
536
- }]
537
- };
538
- });
539
- server.tool('get_typography_usage', 'Get the guidelines for how to apply typography to a user interface', async () => {
540
- const url = new URL(`/product/getting-started/foundations/typography`, 'https://primer.style');
541
- const response = await fetch(url);
542
- if (!response.ok) {
543
- throw new Error(`Failed to fetch ${url} - ${response.statusText}`);
544
- }
545
- const html = await response.text();
546
- if (!html) {
547
- return {
548
- content: []
549
- };
550
- }
551
- const $ = cheerio.load(html);
552
- const source = $('main').html();
553
- if (!source) {
554
- return {
555
- content: []
556
- };
557
- }
558
- const text = turndownService.turndown(source);
559
- return {
560
- content: [{
561
- type: 'text',
562
- text: `Here is the documentation for typography usage in Primer:\n\n${text}`
563
- }]
564
- };
565
- });
566
-
567
- // -----------------------------------------------------------------------------
568
- // Icons
569
- // -----------------------------------------------------------------------------
570
- server.tool('list_icons', 'List all of the icons (octicons) available from Primer Octicons React', async () => {
571
- const icons = listIcons().map(icon => {
572
- const keywords = icon.keywords.map(keyword => {
573
- return `<keyword>${keyword}</keyword>`;
574
- });
575
- const sizes = icon.heights.map(height => {
576
- return `<size value="${height}"></size>`;
577
- });
578
- return [`<icon name="${icon.name}">`, ...keywords, ...sizes, `</icon>`].join('\n');
579
- });
580
- return {
581
- content: [{
582
- type: 'text',
583
- text: `The following icons are available in the @primer/octicons-react package in TypeScript projects:
584
-
585
- ${icons.join('\n')}
586
-
587
- You can use the \`get_icon\` tool to get more information about a specific icon. You can use these components from the @primer/octicons-react package.`
588
- }]
589
- };
590
- });
591
- server.tool('get_icon', 'Get a specific icon (octicon) by name from Primer', {
592
- name: z.string().describe('The name of the icon to retrieve'),
593
- size: z.string().optional().describe('The size of the icon to retrieve, e.g. "16"').default('16')
594
- }, async ({
595
- name,
596
- size
597
- }) => {
598
- const icons = listIcons();
599
- const match = icons.find(icon => {
600
- return icon.name === name || icon.name.toLowerCase() === name.toLowerCase();
601
- });
602
- if (!match) {
603
- return {
604
- content: [{
605
- type: 'text',
606
- text: `There is no icon named \`${name}\` in the @primer/octicons-react package. For a full list of icons, use the \`get_icon\` tool.`
607
- }]
608
- };
609
- }
610
- const url = new URL(`/octicons/icon/${match.name}-${size}`, 'https://primer.style');
611
- const response = await fetch(url);
612
- if (!response.ok) {
613
- throw new Error(`Failed to fetch ${url}: ${response.statusText}`);
614
- }
615
- const html = await response.text();
616
- if (!html) {
617
- return {
618
- content: []
619
- };
620
- }
621
- const $ = cheerio.load(html);
622
- const source = $('main').html();
623
- if (!source) {
624
- return {
625
- content: []
626
- };
627
- }
628
- const text = turndownService.turndown(source);
629
- return {
630
- content: [{
631
- type: 'text',
632
- text: `Here is the documentation for the \`${name}\` icon at size: \`${size}\`:
633
- ${text}`
634
- }]
635
- };
636
- });
637
-
638
- // -----------------------------------------------------------------------------
639
- // Coding guidelines
640
- // -----------------------------------------------------------------------------
641
- server.tool('primer_coding_guidelines', 'Get the guidelines when writing code that uses Primer or for UI code that you are creating', async () => {
642
- return {
643
- content: [{
644
- type: 'text',
645
- text: `When writing code that uses Primer, follow these guidelines:
646
-
647
- ## Design Tokens
648
-
649
- - 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.
650
- - 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
651
-
652
- ## Authoring & Using Components
653
-
654
- - Prefer re-using a component from Primer when possible over writing a new component.
655
- - Prefer using existing props for a component for styling instead of adding styling to a component
656
- - Prefer using icons from Primer instead of creating new icons. Use the \`list_icons\` tool to find the icon you need.
657
- - Follow patterns from Primer when creating new components. Use the \`list_patterns\` tool to find the pattern you need, if one exists
658
- - When using a component from Primer, make sure to follow the component's usage and accessibility guidelines
659
-
660
- ## Coding guidelines
661
-
662
- The following list of coding guidelines must be followed:
663
-
664
- - Do not use the sx prop for styling components. Instead, use CSS Modules.
665
- - Do not use the Box component for styling components. Instead, use CSS Modules.
666
- `
667
- }]
668
- };
669
- });
670
-
671
- // -----------------------------------------------------------------------------
672
- // Accessibility
673
- // -----------------------------------------------------------------------------
674
-
675
- /**
676
- * The `review_alt_text` tool is experimental and may be removed in future versions.
677
- *
678
- * The intent of this tool is to assist products like Copilot Code Review and Copilot Coding Agent
679
- * in reviewing both user- and AI-generated alt text for images, ensuring compliance with accessibility guidelines.
680
- * This tool is not intended to replace human-generated alt text; rather, it supports the review process
681
- * by providing suggestions for improvement. It should be used alongside human review, not as a substitute.
682
- *
683
- *
684
- **/
685
- server.tool('review_alt_text', 'Evaluates image alt text against accessibility best practices and context relevance.', {
686
- surroundingText: z.string().describe('Text surrounding the image, relevant to the image.'),
687
- alt: z.string().describe('The alt text of the image being evaluated'),
688
- image: z.union([z.instanceof(File).describe('The image src file being evaluated'), z.string().url().describe('The URL of the image src being evaluated'), z.string().describe('The file path of the image src being evaluated')]).describe('The image file, file path, or URL being evaluated')
689
- }, async ({
690
- surroundingText,
691
- alt,
692
- image
693
- }) => {
694
- // Call the LLM through MCP sampling
695
- const response = await server.server.createMessage({
696
- messages: [{
697
- role: 'user',
698
- content: {
699
- type: 'text',
700
- text: `Does this alt text: '${alt}' meet accessibility guidelines and describe the image: ${image} accurately in context of this surrounding text: '${surroundingText}'?\n\n`
701
- }
702
- }],
703
- sampling: {
704
- temperature: 0.4
705
- },
706
- maxTokens: 500
707
- });
708
- return {
709
- content: [{
710
- type: 'text',
711
- text: response.content.type === 'text' ? response.content.text : 'Unable to generate summary'
712
- }],
713
- altTextEvaluation: response.content.type === 'text' ? response.content.text : 'Unable to generate summary',
714
- 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.`
715
- };
716
- });
717
-
718
- export { server as s };