-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtool-description-acquisition.ts
More file actions
135 lines (120 loc) · 3.33 KB
/
tool-description-acquisition.ts
File metadata and controls
135 lines (120 loc) · 3.33 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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
import {
fetchResourceMetadata,
type ResourceFetchResult,
type ResourceFetcherOptions,
type ResourceKind,
type ResourceRequest,
} from "./resource-fetcher.js";
export interface AcquiredToolDescription {
name: string;
description: string;
}
export interface ToolDescriptionCandidate {
serverId: string;
transport: "stdio" | "http" | "sse";
command?: string[];
url?: string;
sourceTools?: AcquiredToolDescription[];
}
export type ToolDescriptionAcquisitionStatus =
| "ok"
| "auth_failure"
| "timeout"
| "network_error"
| "command_error"
| "rejected_unsafe_stdio"
| "schema_mismatch";
export interface ToolDescriptionAcquisitionResult {
status: ToolDescriptionAcquisitionStatus;
tools: AcquiredToolDescription[];
error?: string;
}
export interface ToolDescriptionAcquisitionDeps {
fetchMetadata: (request: ResourceRequest) => Promise<ResourceFetchResult>;
}
export interface ToolDescriptionAcquisitionOptions {
fetchOptions?: ResourceFetcherOptions;
}
function defaultDeps(
options: ToolDescriptionAcquisitionOptions = {},
): ToolDescriptionAcquisitionDeps {
return {
fetchMetadata: async (request) =>
fetchResourceMetadata(request, undefined, options.fetchOptions),
};
}
function parseTools(metadata: unknown): AcquiredToolDescription[] {
if (!metadata || typeof metadata !== "object") {
return [];
}
const root = metadata as Record<string, unknown>;
if (!Array.isArray(root.tools)) {
return [];
}
return root.tools
.filter(
(entry): entry is Record<string, unknown> => typeof entry === "object" && entry !== null,
)
.map((entry) => ({
name: typeof entry.name === "string" ? entry.name : "",
description: typeof entry.description === "string" ? entry.description : "",
}))
.filter((entry) => entry.name.length > 0 && entry.description.length > 0);
}
function requestFromCandidate(candidate: ToolDescriptionCandidate): ResourceRequest | null {
if (!candidate.url) {
return null;
}
const kind: ResourceKind = candidate.transport === "sse" ? "sse" : "http";
return {
id: `${kind}:${candidate.serverId}`,
kind,
locator: candidate.url,
};
}
export async function acquireToolDescriptions(
candidate: ToolDescriptionCandidate,
customDeps: ToolDescriptionAcquisitionDeps = defaultDeps(),
): Promise<ToolDescriptionAcquisitionResult> {
if (candidate.transport === "stdio") {
if (candidate.sourceTools && candidate.sourceTools.length > 0) {
return {
status: "ok",
tools: candidate.sourceTools,
};
}
return {
status: "rejected_unsafe_stdio",
tools: [],
error: "stdio execution is not allowed for tool-description acquisition",
};
}
const request = requestFromCandidate(candidate);
if (!request) {
return {
status: "schema_mismatch",
tools: [],
error: "missing remote endpoint URL",
};
}
const response = await customDeps.fetchMetadata(request);
if (response.status !== "ok") {
return {
status: response.status,
tools: [],
error: response.error,
};
}
const tools = parseTools(response.metadata);
if (tools.length === 0) {
return {
status: "schema_mismatch",
tools: [],
error: "metadata did not include tools[] with name and description",
};
}
return {
status: "ok",
tools,
};
}