Skip to content

Commit 3d8d82d

Browse files
committed
feat: enhance token entry structure and improve resolved value handling
1 parent e2ec93d commit 3d8d82d

File tree

3 files changed

+21
-6
lines changed

3 files changed

+21
-6
lines changed

mcp-server/src/tools.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ export type GetTokenDefsParametersInput = z.input<typeof GetTokenDefsParametersS
6868
export type TokenEntry = {
6969
kind: 'color' | 'number' | 'string' | 'boolean'
7070
value: string | Record<string, string> // single mode -> string; multi-mode -> map (mode name -> literal or alias)
71-
resolvedValue: string // value for the active mode (or the single mode)
71+
resolvedValue?: string // value for the active mode when multi-mode
7272
activeMode?: string // only present when multi-mode map is returned
7373
}
7474

mcp/tools/code/index.ts

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,16 @@ function filterTokensByNames(input: GetTokenDefsResult, names: Set<string>): Get
6060
return { tokens }
6161
}
6262

63+
function primaryTokenValue(entry: GetTokenDefsResult['tokens'][string]): string | undefined {
64+
if (typeof entry.value === 'string') return entry.value
65+
if (typeof entry.resolvedValue === 'string') return entry.resolvedValue
66+
if (entry.activeMode && typeof entry.value[entry.activeMode] === 'string') {
67+
return entry.value[entry.activeMode]
68+
}
69+
const first = Object.values(entry.value)[0]
70+
return typeof first === 'string' ? first : undefined
71+
}
72+
6373
export async function handleGetCode(
6474
nodes: SceneNode[],
6575
preferredLang?: CodeLanguage,
@@ -140,7 +150,7 @@ export async function handleGetCode(
140150

141151
if (resolveTokens) {
142152
const tokenMap = new Map(
143-
Object.entries(usedTokens.tokens).map(([name, entry]) => [name, entry.resolvedValue])
153+
Object.entries(usedTokens.tokens).map(([name, entry]) => [name, primaryTokenValue(entry)])
144154
)
145155
const resolvedMarkup = markup.replace(/var\((--[a-zA-Z0-9-_]+)\)/g, (match, name) => {
146156
const val = tokenMap.get(name)

mcp/tools/token-defs.ts

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -136,12 +136,17 @@ async function buildTokenEntries(
136136

137137
const value: string | Record<string, string> = modeIds.length <= 1 ? resolvedValue : valueMap
138138

139-
tokens[canonicalName] = {
139+
const entry: TokenEntry = {
140140
kind: mapResolvedType(variable.resolvedType),
141-
value,
142-
resolvedValue,
143-
...(modeIds.length > 1 && primaryModeName ? { activeMode: primaryModeName } : {})
141+
value
144142
}
143+
144+
if (modeIds.length > 1 && primaryModeName) {
145+
entry.resolvedValue = resolvedValue
146+
entry.activeMode = primaryModeName
147+
}
148+
149+
tokens[canonicalName] = entry
145150
}
146151

147152
return { tokens }

0 commit comments

Comments
 (0)