-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathoutput.utils.ts
More file actions
97 lines (90 loc) · 2.2 KB
/
Copy pathoutput.utils.ts
File metadata and controls
97 lines (90 loc) · 2.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
import type { JsonObjectSchema, ToolResult } from "./tool-registry.js";
export interface ToolOutput {
summary: string;
data: unknown;
suggestions?: string[];
warnings?: string[];
}
/**
* Standard result envelope. Returns both `structuredContent` (typed, for
* clients that consume it) and an equivalent `text` block (backward
* compatibility, as recommended by the MCP spec).
*/
export function formatToolOutput(payload: ToolOutput): ToolResult {
const structured: Record<string, unknown> = {
summary: payload.summary,
data: payload.data
};
if (payload.suggestions !== undefined) {
structured.suggestions = payload.suggestions;
}
if (payload.warnings !== undefined) {
structured.warnings = payload.warnings;
}
return {
content: [
{
type: "text",
text: JSON.stringify(structured, null, 2)
}
],
structuredContent: structured
};
}
export function formatData(
summary: string,
data: unknown,
suggestions?: string[],
warnings?: string[]
): ToolResult {
return formatToolOutput({
summary,
data,
suggestions,
warnings
});
}
export function formatList(
entity: string,
items: unknown[],
suggestions?: string[],
warnings?: string[]
): ToolResult {
return formatToolOutput({
summary: `Retrieved ${items.length} ${entity}.`,
data: {
total: items.length,
items
},
suggestions,
warnings
});
}
/**
* Wrap a tool-specific `data` schema in the standard output envelope so every
* tool advertises the same `summary`/`data`/`suggestions`/`warnings` contract.
*/
export function buildOutputSchema(dataSchema: Record<string, unknown>): JsonObjectSchema {
return {
type: "object",
properties: {
summary: {
type: "string",
description: "One-line human-readable result"
},
data: dataSchema,
suggestions: {
type: "array",
items: { type: "string" },
description: "Optional follow-up actions for the agent"
},
warnings: {
type: "array",
items: { type: "string" },
description: "Optional caveats about the result"
}
},
required: ["summary", "data"],
additionalProperties: false
};
}