phation 0.0.0-stage → 0.1.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/bin/server.mjs ADDED
@@ -0,0 +1,147 @@
1
+ #!/usr/bin/env node
2
+ // Phation MCP server (stdio). Tools: list_motions, search_motions, get_motion, add_motion, get_standard, install_standard, audit_motion, animate_ui.
3
+ // Also exposes the standard as a resource (phation://standard) and a prompt (motion_review).
4
+ import { McpServer } from '@modelcontextprotocol/sdk/server/mcp.js'
5
+ import { StdioServerTransport } from '@modelcontextprotocol/sdk/server/stdio.js'
6
+ import { z } from 'zod'
7
+ import { ENTRIES, CATEGORIES, search, get, summary, suggest, add, STANDARD, CORE, STANDARD_VERSION, standardSection, install, TOOLS, getCode, motionBlock, requirePro } from '../lib.mjs'
8
+
9
+ const server = new McpServer(
10
+ { name: 'phation', version: '0.1.0' },
11
+ {
12
+ instructions:
13
+ 'Phation: tested React + Motion interactions, a motion standard and a frame-by-frame audit. Work token-lean. ' +
14
+ 'For any motion task: search_motions → add_motion (writes the file to disk; never retype it) → edit only its MOTION object, copy and styles → audit_motion, fix blockers, re-run. ' +
15
+ 'A Studio link (…/m/<slug>?…) goes in add_motion `from`. get_motion returns the spec and MOTION values; pass code: true only if you must read the source. ' +
16
+ 'Free: list_motions, search_motions, get_motion (spec + MOTION values), get_standard, install_standard. Pro (license key via `npx phation login <key>` or PHATION_KEY): add_motion, get_motion code, audit_motion, animate_ui. If a Pro tool answers with the Pro message, tell the user once and continue with the free tools (use the spec values by hand). ' +
17
+ 'Rules: get_standard returns the ~600-token core; ask for one section ("springs", "recipes", "accessibility") when you need more, "all" only if asked. ' +
18
+ 'To animate the user\'s existing screens: animate_ui (plan) → confirm roles → animate_ui apply → audit_motion. Report the values you used and any rule you broke on purpose.',
19
+ },
20
+ )
21
+ const text = v => ({ content: [{ type: 'text', text: typeof v === 'string' ? v : JSON.stringify(v, null, 2) }] })
22
+ // one line per entry: what the model needs to choose, nothing more
23
+ const line = e => `${e.slug} · ${e.name} · ${e.category}${(e.tags || []).length ? ' · ' + e.tags.join(', ') : ''}`
24
+ const fail = m => ({ content: [{ type: 'text', text: m }], isError: true })
25
+
26
+ server.registerTool('list_motions', {
27
+ title: 'List motions',
28
+ description: `Every entry as one line (slug · name · category · tags). Optional category: ${CATEGORIES.join(', ')}.`,
29
+ inputSchema: { category: z.enum(CATEGORIES).optional() },
30
+ annotations: { readOnlyHint: true },
31
+ }, async ({ category }) => text(ENTRIES.filter(e => !category || e.category === category).map(line).join('\n')))
32
+
33
+ server.registerTool('search_motions', {
34
+ title: 'Search motions',
35
+ description: 'Find entries by what the motion is for or how it should feel, e.g. "tactile quantity stepper", "calm bottom sheet".',
36
+ inputSchema: { query: z.string().min(1).describe('What the motion is for or how it should feel'), category: z.enum(CATEGORIES).optional(), limit: z.number().int().min(1).max(20).optional() },
37
+ annotations: { readOnlyHint: true },
38
+ }, async ({ query, category, limit }) => {
39
+ const hits = search(query, { category, limit: limit ?? 4 })
40
+ return hits.length ? text(hits.map(h => `${h.slug} · ${h.name} · ${h.category}\n ${String(h.description).split(/(?<=\.)\s/)[0]}`).join('\n')) : text(`Nothing matched "${query}". Try list_motions, or describe the UI element (button, sheet, list, price, cursor).`)
41
+ })
42
+
43
+ server.registerTool('get_motion', {
44
+ title: 'Get a motion',
45
+ description: 'Spec (curves, springs, timings), its MOTION values, dependencies and media. The source is left out: add_motion writes it to disk. Pass code: true only to read it.',
46
+ inputSchema: { slug: z.string().describe('Entry slug'), code: z.boolean().optional().describe('Include the full React source (~1–2k tokens)') },
47
+ annotations: { readOnlyHint: true },
48
+ }, async ({ slug, code }) => {
49
+ const e = get(slug)
50
+ if (!e) return fail(`No entry "${slug}".${suggest(slug).length ? ' Did you mean: ' + suggest(slug).join(', ') + '?' : ''}`)
51
+ const head = [`${e.slug} · ${e.name} · ${e.file}`, e.description, `spec: ${Object.entries(e.spec).map(([k, v]) => `${k} ${v}`).join(' · ')}`,
52
+ `deps: ${(e.dependencies || []).join(', ')}${(e.assets || []).length ? ` · media: ${e.assets.join(', ')}` : ''}`, '', motionBlock(e)].join('\n')
53
+ if (!code) return text(`${head}\n\n(add_motion writes the file; pass code: true to read the source. Both are Pro.)`)
54
+ try { return text(`${head}\n\n// ${e.file}\n${await getCode(e)}`) } catch (err) { return fail(`${head}\n\n${err.message}`) }
55
+ })
56
+
57
+ server.registerTool('add_motion', {
58
+ title: 'Add a motion to the project',
59
+ description: 'Write an entry\'s file (and its media) into the project, so you never retype it. Will not overwrite unless force. A Studio link (…/m/<slug>?…) in `from` writes its tuned values into MOTION.',
60
+ inputSchema: {
61
+ slug: z.string(),
62
+ projectRoot: z.string().optional().describe('Project path (default: cwd)'),
63
+ dir: z.string().optional().describe('default src/motion'),
64
+ publicDir: z.string().optional().describe('default public'),
65
+ force: z.boolean().optional(),
66
+ from: z.string().optional().describe('Studio share link'),
67
+ },
68
+ annotations: { destructiveHint: false, idempotentHint: true },
69
+ }, async args => {
70
+ try { return text(await add(args.slug, args)) } catch (e) { return fail(e.message) }
71
+ })
72
+
73
+ server.registerTool('get_standard', {
74
+ title: 'Get the Phation Standard',
75
+ description: 'The motion rules. No section: the ~600-token core (tokens, never-list, reduced motion, workflow). A section ("springs", "recipes", "accessibility", "code") for detail; "all" for everything.',
76
+ inputSchema: { section: z.string().optional().describe('e.g. "springs", "durations", "recipes", "accessibility", "code", "all"') },
77
+ annotations: { readOnlyHint: true },
78
+ }, async ({ section }) => {
79
+ const s = standardSection(section)
80
+ return s ? text(s) : fail(`No section "${section}". Try: principles, tokens, curves, springs, durations, stagger, recipes, never, ui, accessibility, code, workflow.`)
81
+ })
82
+
83
+ server.registerTool('install_standard', {
84
+ title: 'Install the standard into the project',
85
+ description: 'Write the standard as project rules for one tool (~600-token core) plus .phation/STANDARD.md (read on demand). AGENTS.md and CLAUDE.md are merged, never replaced.',
86
+ inputSchema: {
87
+ tool: z.enum([...Object.keys(TOOLS).filter(k => !TOOLS[k].hidden), 'all']),
88
+ projectRoot: z.string().optional().describe('Project path (default: cwd)'),
89
+ force: z.boolean().optional(),
90
+ },
91
+ annotations: { destructiveHint: false, idempotentHint: true },
92
+ }, async args => { try { return text(install(args.tool, args)) } catch (e) { return fail(e.message) } })
93
+
94
+ server.registerTool('audit_motion', {
95
+ title: 'Audit the motion of a page',
96
+ description: 'Play a page in headless Chrome with the given actions, trace every element per frame and score the motion (PASS ≥ 85, no blockers). Returns each issue with time, element and the fix. 15–40 s; the page must be running.',
97
+ inputSchema: {
98
+ url: z.string().url(),
99
+ actions: z.array(z.string()).optional().describe('click:<sel> hover:<sel> focus:<sel> scroll:<px> drag:<sel>:dx,dy hold:<sel>:ms key:<Key> type:<sel>:text wait:<ms>'),
100
+ root: z.string().optional().describe('Selector to trace (default body)'),
101
+ mobile: z.boolean().optional(),
102
+ dark: z.boolean().optional(),
103
+ reduced: z.boolean().optional().describe('Check reduced motion (default true)'),
104
+ json: z.boolean().optional().describe('Add raw JSON'),
105
+ },
106
+ annotations: { readOnlyHint: true, openWorldHint: true },
107
+ }, async args => {
108
+ try {
109
+ await requirePro()
110
+ const { audit, formatReport } = await import('../audit.mjs')
111
+ const r = await audit(args.url, args)
112
+ if (r.status === 'ERROR') return fail(formatReport(r))
113
+ return { content: [{ type: 'text', text: formatReport(r) }, ...(args.json ? [{ type: 'text', text: JSON.stringify(r) }] : [])] }
114
+ } catch (e) { return fail(e.message) }
115
+ })
116
+
117
+ server.registerTool('animate_ui', {
118
+ title: 'Animate the user\'s own UI',
119
+ description: 'Read a .jsx/.tsx file or folder, name each part (button, card, sheet, modal, tabs, toggle, list, number…) and map it to a Phation recipe under one Feel. ' +
120
+ 'Default: plan only (~15 tokens a part). apply: true writes <file>.animated.tsx (write: true edits in place), adds one MOTION object and data-phation hooks, and returns only the changed lines; then run audit_motion with the listed actions. ' +
121
+ 'Parts marked "⚠ check" are guesses: confirm or fix them with roles: { "<line>": "<role>" }.',
122
+ inputSchema: {
123
+ path: z.string().describe('File or folder, relative to projectRoot'),
124
+ feel: z.enum(['calm', 'fluid', 'snappy', 'playful', 'heavy', 'mechanical']).optional().describe('One Feel for the screen (default fluid)'),
125
+ only: z.array(z.string()).optional().describe('Limit to these recipe ids, e.g. ["press", "sheet-open"]'),
126
+ roles: z.record(z.string(), z.string()).optional().describe('Fix detected roles: { "<line>": "button" }'),
127
+ apply: z.boolean().optional(),
128
+ write: z.boolean().optional().describe('Edit the file in place instead of writing <file>.animated.tsx'),
129
+ projectRoot: z.string().optional(),
130
+ },
131
+ annotations: { destructiveHint: false },
132
+ }, async args => {
133
+ try { await requirePro(); const { animate } = await import('../animate/index.mjs'); return text(animate(args.path, args).text) } catch (e) { return fail(e.message) }
134
+ })
135
+
136
+ server.registerResource('standard', 'phation://standard', { title: 'Phation Standard', description: 'Rules for product UI motion by Pham Huy', mimeType: 'text/markdown' },
137
+ async uri => ({ contents: [{ uri: uri.href, mimeType: 'text/markdown', text: STANDARD }] }))
138
+
139
+ server.registerPrompt('motion_review', {
140
+ title: 'Review motion against the Phation Standard',
141
+ description: 'Audit the motion in a file or component and propose fixes with exact values.',
142
+ argsSchema: { target: z.string().describe('File, component or screen to review') },
143
+ }, ({ target }) => ({ messages: [{ role: 'user', content: { type: 'text', text:
144
+ `Review every animation and transition in ${target} against the Phation Standard below. For each one list: what moves, its current curve/spring and duration, ` +
145
+ `which rules it breaks, and the fix with exact values from the tokens. Then apply the fixes. Call get_standard with a section if you need more detail.\n\n${CORE}` } }] }))
146
+
147
+ await server.connect(new StdioServerTransport())