-
Notifications
You must be signed in to change notification settings - Fork 151
Expand file tree
/
Copy pathtool-metadata.ts
More file actions
152 lines (128 loc) · 4.48 KB
/
tool-metadata.ts
File metadata and controls
152 lines (128 loc) · 4.48 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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
import { getToolUiResourceUri } from "@modelcontextprotocol/ext-apps/app-bridge";
import type { McpExtensionState } from "./state.ts";
import type { ToolMetadata, McpTool, McpResource, ServerEntry } from "./types.ts";
import { formatToolName, isToolExcluded } from "./types.ts";
import { resourceNameToToolName } from "./resource-tools.ts";
import { extractToolUiStreamMode } from "./utils.ts";
export function buildToolMetadata(
tools: McpTool[],
resources: McpResource[],
definition: ServerEntry,
serverName: string,
prefix: "server" | "none" | "short"
): { metadata: ToolMetadata[]; failedTools: string[] } {
const metadata: ToolMetadata[] = [];
const failedTools: string[] = [];
for (const tool of tools) {
if (!tool?.name) {
failedTools.push("(unnamed)");
continue;
}
if (isToolExcluded(tool.name, serverName, prefix, definition.excludeTools)) {
continue;
}
let uiResourceUri: string | undefined;
try {
uiResourceUri = getToolUiResourceUri({ _meta: tool._meta });
} catch {
failedTools.push(tool.name);
}
metadata.push({
name: formatToolName(tool.name, serverName, prefix),
originalName: tool.name,
description: tool.description ?? "",
inputSchema: tool.inputSchema,
uiResourceUri,
uiStreamMode: extractToolUiStreamMode(tool._meta),
});
}
if (definition.exposeResources !== false) {
for (const resource of resources) {
const baseName = `get_${resourceNameToToolName(resource.name)}`;
if (isToolExcluded(baseName, serverName, prefix, definition.excludeTools)) {
continue;
}
metadata.push({
name: formatToolName(baseName, serverName, prefix),
originalName: baseName,
description: resource.description ?? `Read resource: ${resource.uri}`,
resourceUri: resource.uri,
});
}
}
return { metadata, failedTools };
}
export function getToolNames(state: McpExtensionState, serverName: string): string[] {
return state.toolMetadata.get(serverName)?.map(m => m.name) ?? [];
}
export function totalToolCount(state: McpExtensionState): number {
let count = 0;
for (const metadata of state.toolMetadata.values()) {
count += metadata.length;
}
return count;
}
export function findToolByName(metadata: ToolMetadata[] | undefined, toolName: string): ToolMetadata | undefined {
if (!metadata) return undefined;
const exact = metadata.find(m => m.name === toolName);
if (exact) return exact;
const normalized = toolName.replace(/-/g, "_");
return metadata.find(m => m.name.replace(/-/g, "_") === normalized);
}
export function formatSchema(schema: unknown, indent = " "): string {
if (!schema || typeof schema !== "object") {
return `${indent}(no schema)`;
}
const s = schema as Record<string, unknown>;
if (s.type === "object" && s.properties && typeof s.properties === "object") {
const props = s.properties as Record<string, unknown>;
const required = Array.isArray(s.required) ? s.required as string[] : [];
if (Object.keys(props).length === 0) {
return `${indent}(no parameters)`;
}
const lines: string[] = [];
for (const [name, propSchema] of Object.entries(props)) {
const isRequired = required.includes(name);
const propLine = formatProperty(name, propSchema, isRequired, indent);
lines.push(propLine);
}
return lines.join("\n");
}
if (s.type) {
return `${indent}(${s.type})`;
}
return `${indent}(complex schema)`;
}
function formatProperty(name: string, schema: unknown, required: boolean, indent: string): string {
if (!schema || typeof schema !== "object") {
return `${indent}${name}${required ? " *required*" : ""}`;
}
const s = schema as Record<string, unknown>;
const parts: string[] = [];
let typeStr = "";
if (s.type) {
if (Array.isArray(s.type)) {
typeStr = s.type.join(" | ");
} else {
typeStr = String(s.type);
}
} else if (s.enum) {
typeStr = "enum";
} else if (s.anyOf || s.oneOf) {
typeStr = "union";
}
if (Array.isArray(s.enum)) {
const enumVals = s.enum.map(v => JSON.stringify(v)).join(", ");
typeStr = `enum: ${enumVals}`;
}
parts.push(`${indent}${name}`);
if (typeStr) parts.push(`(${typeStr})`);
if (required) parts.push("*required*");
if (s.description && typeof s.description === "string") {
parts.push(`- ${s.description}`);
}
if (s.default !== undefined) {
parts.push(`[default: ${JSON.stringify(s.default)}]`);
}
return parts.join(" ");
}