@patine-ui/mcp 0.0.0-stage → 0.4.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.js ADDED
@@ -0,0 +1,152 @@
1
+ // The MCP protocol over JSON-RPC 2.0: initialize, the tool list, and the four tools. Pure: a request in, a response out.
2
+ const { findPage, searchPages, summarize } = require('./docs');
3
+ const { searchIcons } = require('./icons');
4
+ const { PRO_NOTE, searchProIcons, searchProPages } = require('./pro');
5
+
6
+ const PROTOCOL_VERSION = '2025-06-18';
7
+
8
+ const TOOLS = [
9
+ {
10
+ name: 'list_components',
11
+ description:
12
+ 'List every page of the Patine UI (patine-ui) docs: components, guides and recipes, each with a one-line summary. Call this first to see what exists.',
13
+ inputSchema: { type: 'object', properties: {} },
14
+ },
15
+ {
16
+ name: 'get_component_docs',
17
+ description:
18
+ 'Get the full docs page for one Patine UI component or topic, as Markdown: props table, examples, accessibility and platform notes. E.g. "Button", "DatePicker", "BottomSheet", "Theming".',
19
+ inputSchema: {
20
+ type: 'object',
21
+ properties: { name: { type: 'string', description: 'Component or page name, e.g. "Select"' } },
22
+ required: ['name'],
23
+ },
24
+ },
25
+ {
26
+ name: 'search_docs',
27
+ description:
28
+ 'Search the Patine UI docs for a prop, behaviour or task, e.g. "min nights date range" or "dark mode theme". Returns the best free pages with a snippet, then matching Patine Pro items (charts, blocks, app templates), tagged Pro.',
29
+ inputSchema: {
30
+ type: 'object',
31
+ properties: {
32
+ query: { type: 'string' },
33
+ limit: { type: 'number', description: 'Default: 5' },
34
+ },
35
+ required: ['query'],
36
+ },
37
+ },
38
+ {
39
+ name: 'search_icons',
40
+ description:
41
+ 'Find icons in the free Patine icon set (@patine-ui/icons) by name or meaning, e.g. "notification", "arrow left", "delete". Returns the names to pass to <Icon name="…" />, so you never guess one that does not exist; then matching Patine Pro icons and brand logos, tagged Pro.',
42
+ inputSchema: {
43
+ type: 'object',
44
+ properties: {
45
+ query: { type: 'string' },
46
+ limit: { type: 'number', description: 'Default: 20' },
47
+ },
48
+ required: ['query'],
49
+ },
50
+ },
51
+ ];
52
+
53
+ const ICON_USAGE =
54
+ "Use: `import { Icon } from '@patine-ui/icons'` then `<Icon name=\"bell\" />`, or `import { Glyph, iconBell } from '@patine-ui/icons'` to ship only that icon. Patine Pro's Icon takes the same names, plus more icons and solid, duotone and color variants.";
55
+
56
+ const text = (value, isError = false) => ({
57
+ content: [{ type: 'text', text: value }],
58
+ isError,
59
+ });
60
+
61
+ const callTool = (pages, name, args = {}) => {
62
+ switch (name) {
63
+ case 'list_components':
64
+ return text(pages.map((p) => `- ${p.name}: ${summarize(p)}`).join('\n'));
65
+ case 'get_component_docs': {
66
+ const page = findPage(pages, String(args.name ?? ''));
67
+ return page
68
+ ? text(`# ${page.name}\n\n${page.body}`)
69
+ : text(`No page named "${args.name}". Call list_components to see the names.`, true);
70
+ }
71
+ case 'search_docs': {
72
+ const query = String(args.query ?? '');
73
+ const results = searchPages(pages, query, Number(args.limit) || 5);
74
+ // Free pages first; Pro's own docs pages (titled "(Pro)") and the rest of Pro after them, tagged.
75
+ const isPro = (r) => r.page.title.endsWith('(Pro)');
76
+ const free = results.filter((r) => !isPro(r)).map((r) => `## ${r.page.name}\n${r.snippet}`);
77
+ const proDocs = results.filter(isPro);
78
+ const pro = [
79
+ ...proDocs.map((r) => `- [Pro] ${r.page.name.replace(/ Pro$/, '')}: ${r.snippet.replace(/\s+/g, ' ')}`),
80
+ ...searchProPages(
81
+ query,
82
+ 3,
83
+ proDocs.map((r) => r.page.name.replace(/ Pro$/, ''))
84
+ ),
85
+ ];
86
+ const sections = [...free, ...(pro.length > 0 ? [`## Patine Pro\n${pro.join('\n')}\n\n${PRO_NOTE}`] : [])];
87
+ return sections.length > 0 ? text(sections.join('\n\n')) : text(`Nothing matched "${args.query}".`);
88
+ }
89
+ case 'search_icons': {
90
+ const query = String(args.query ?? '');
91
+ const limit = Number(args.limit) || 20;
92
+ const icons = searchIcons(query, limit);
93
+ const pro = searchProIcons(query, Math.min(limit, 10));
94
+ const sections = [
95
+ ...(icons.length > 0 ? [`${icons.map((i) => `- ${i.name} (${i.category}): ${i.tags}`).join('\n')}\n\n${ICON_USAGE}`] : []),
96
+ ...(pro.length > 0 ? [`## Patine Pro\n${pro.join('\n')}\n\n${PRO_NOTE}`] : []),
97
+ ];
98
+ return sections.length > 0
99
+ ? text(sections.join('\n\n'))
100
+ : text(`No icon matched "${args.query}". Try a simpler word, e.g. "trash" for delete.`);
101
+ }
102
+ default:
103
+ return null;
104
+ }
105
+ };
106
+
107
+ /** The response to one JSON-RPC message, or `null` for a notification. */
108
+ const handle = (pages, version, message) => {
109
+ const { id, method, params } = message;
110
+ if (id === undefined || id === null) return null;
111
+ const reply = (result) => ({
112
+ jsonrpc: '2.0',
113
+ id,
114
+ result,
115
+ });
116
+ const fail = (code, msg) => ({
117
+ jsonrpc: '2.0',
118
+ id,
119
+ error: {
120
+ code,
121
+ message: msg,
122
+ },
123
+ });
124
+ switch (method) {
125
+ case 'initialize':
126
+ return reply({
127
+ protocolVersion: params?.protocolVersion ?? PROTOCOL_VERSION,
128
+ capabilities: { tools: {} },
129
+ serverInfo: {
130
+ name: 'patine',
131
+ version,
132
+ },
133
+ instructions:
134
+ 'Patine UI (patine-ui) is a React Native component library for iOS, Android and the web. Before writing UI code with it, look the component up here instead of guessing props.',
135
+ });
136
+ case 'ping':
137
+ return reply({});
138
+ case 'tools/list':
139
+ return reply({ tools: TOOLS });
140
+ case 'tools/call': {
141
+ const result = callTool(pages, params?.name, params?.arguments);
142
+ return result ? reply(result) : fail(-32602, `Unknown tool: ${params?.name}`);
143
+ }
144
+ default:
145
+ return fail(-32601, `Method not found: ${method}`);
146
+ }
147
+ };
148
+
149
+ module.exports = {
150
+ TOOLS,
151
+ handle,
152
+ };