Skip to content

Commit 46b415c

Browse files
committed
fix(mcp-server): align tool responses and templates
1 parent 4769c92 commit 46b415c

1 file changed

Lines changed: 42 additions & 26 deletions

File tree

packages/mcp-server/src/server.ts

Lines changed: 42 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
import { McpServer } from '@modelcontextprotocol/sdk/server/mcp.js'
2-
import type { LintAssemblyInstructionsResult } from '@transloadit/node'
2+
import type { CallToolResult, TextContent } from '@modelcontextprotocol/sdk/types.js'
3+
import type {
4+
CreateAssemblyParams,
5+
LintAssemblyInstructionsResult,
6+
TemplateContent,
7+
} from '@transloadit/node'
38
import {
49
getRobotHelp,
510
goldenTemplates,
@@ -50,6 +55,16 @@ type ToolExtra = {
5055
}
5156

5257
const maxBase64Bytes = 512_000
58+
type GoldenTemplate = {
59+
slug: string
60+
version: string
61+
description: string
62+
steps: Record<string, unknown>
63+
}
64+
65+
type LintAssemblyInstructionsInput = Parameters<Transloadit['lintAssemblyInstructions']>[0]
66+
67+
const goldenTemplatesMap = goldenTemplates as Record<string, GoldenTemplate>
5368

5469
const lintIssueSchema = z.object({
5570
path: z.string(),
@@ -240,15 +255,17 @@ const safeJsonParse = (value: string): unknown => {
240255
}
241256
}
242257

243-
const buildToolResponse = (payload: Record<string, unknown>) => ({
244-
content: [
245-
{
246-
type: 'text',
247-
text: JSON.stringify(payload),
248-
},
249-
],
250-
structuredContent: payload,
251-
})
258+
const buildToolResponse = (payload: Record<string, unknown>): CallToolResult => {
259+
const content: TextContent = {
260+
type: 'text',
261+
text: JSON.stringify(payload),
262+
}
263+
264+
return {
265+
content: [content],
266+
structuredContent: payload,
267+
}
268+
}
252269

253270
const buildToolError = (
254271
code: string,
@@ -385,35 +402,32 @@ const resolveAssemblyAccess = (
385402
}
386403
}
387404

388-
const resolveGoldenTemplate = (
389-
slug: string,
390-
version?: string,
391-
): (typeof goldenTemplates)[string] | undefined => {
405+
const resolveGoldenTemplate = (slug: string, version?: string): GoldenTemplate | undefined => {
392406
if (slug.includes('@')) {
393-
return goldenTemplates[slug]
407+
return goldenTemplatesMap[slug]
394408
}
395409

396410
if (version) {
397-
return goldenTemplates[`${slug}@${version}`]
411+
return goldenTemplatesMap[`${slug}@${version}`]
398412
}
399413

400-
const matches = Object.keys(goldenTemplates).filter((key) => key.startsWith(`${slug}@`))
414+
const matches = Object.keys(goldenTemplatesMap).filter((key) => key.startsWith(`${slug}@`))
401415
if (matches.length === 0) return undefined
402416
const latest = matches.sort().at(-1)
403-
return latest ? goldenTemplates[latest] : undefined
417+
return latest ? goldenTemplatesMap[latest] : undefined
404418
}
405419

406-
const parseInstructions = (input: unknown): Record<string, unknown> | undefined => {
420+
const parseInstructions = (input: unknown): CreateAssemblyParams | undefined => {
407421
if (input == null) return undefined
408422
if (typeof input === 'string') {
409423
const parsed = safeJsonParse(input)
410-
return isRecord(parsed) ? parsed : undefined
424+
return isRecord(parsed) ? (parsed as CreateAssemblyParams) : undefined
411425
}
412426
if (isRecord(input)) {
413427
if ('steps' in input) {
414-
return input as Record<string, unknown>
428+
return input as CreateAssemblyParams
415429
}
416-
return { steps: input }
430+
return { steps: input } as CreateAssemblyParams
417431
}
418432
return undefined
419433
}
@@ -437,8 +451,10 @@ export const createTransloaditMcpServer = (
437451
},
438452
async ({ instructions, strict, return_fixed }) => {
439453
const client = createLintClient(options)
454+
const assemblyInstructions =
455+
instructions as LintAssemblyInstructionsInput['assemblyInstructions']
440456
const result = await client.lintAssemblyInstructions({
441-
assemblyInstructions: instructions,
457+
assemblyInstructions,
442458
fix: return_fixed ?? false,
443459
fatal: strict ? 'warning' : 'error',
444460
})
@@ -497,7 +513,7 @@ export const createTransloaditMcpServer = (
497513

498514
try {
499515
const fileInputs = files ?? []
500-
let params = parseInstructions(instructions) ?? {}
516+
let params = parseInstructions(instructions) ?? ({} as CreateAssemblyParams)
501517

502518
if (golden_template) {
503519
const template = resolveGoldenTemplate(golden_template.slug, golden_template.version)
@@ -513,7 +529,7 @@ export const createTransloaditMcpServer = (
513529
const overrides = golden_template.overrides
514530
const templateContent = {
515531
steps: template.steps,
516-
}
532+
} as TemplateContent
517533
params = mergeTemplateContent(
518534
templateContent,
519535
overrides && isRecord(overrides) ? (overrides as Record<string, unknown>) : undefined,
@@ -721,7 +737,7 @@ export const createTransloaditMcpServer = (
721737
() => {
722738
return buildToolResponse({
723739
status: 'ok',
724-
templates: Object.values(goldenTemplates),
740+
templates: Object.values(goldenTemplatesMap),
725741
})
726742
},
727743
)

0 commit comments

Comments
 (0)