返回 AiToEarn
mcp-response.util.ts
根目录 / project / aitoearn-backend / libs / common / src / utils / mcp-response.util.ts
1 import { stringify } from 'yaml'
2
3 export interface McpTextResult {
4 content: Array<{
5 type: 'text'
6 text: string
7 }>
8 isError?: boolean
9 }
10
11 function normalizeYamlValue(value: unknown): unknown {
12 if (value instanceof Date) {
13 return value.toISOString()
14 }
15
16 if (Array.isArray(value)) {
17 return value.map(item => normalizeYamlValue(item))
18 }
19
20 if (value && typeof value === 'object') {
21 return Object.fromEntries(
22 Object.entries(value)
23 .filter(([, item]) => item !== undefined)
24 .map(([key, item]) => [key, normalizeYamlValue(item)]),
25 )
26 }
27
28 return value
29 }
30
31 export function toTextResult(text: string, isError = false): McpTextResult {
32 return {
33 content: [{
34 type: 'text',
35 text,
36 }],
37 ...(isError ? { isError: true } : {}),
38 }
39 }
40
41 export function toYamlTextResult(data: unknown): McpTextResult {
42 return toTextResult(stringify(normalizeYamlValue(data)))
43 }
44
44 lines TYPESCRIPT