scorecard-ai-mcp 1.0.0-alpha.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +201 -0
- package/README.md +228 -0
- package/compat.d.mts +53 -0
- package/compat.d.mts.map +1 -0
- package/compat.d.ts +53 -0
- package/compat.d.ts.map +1 -0
- package/compat.js +351 -0
- package/compat.js.map +1 -0
- package/compat.mjs +338 -0
- package/compat.mjs.map +1 -0
- package/compat.test.d.mts +2 -0
- package/compat.test.d.mts.map +1 -0
- package/compat.test.d.ts +2 -0
- package/compat.test.d.ts.map +1 -0
- package/compat.test.js +950 -0
- package/compat.test.js.map +1 -0
- package/compat.test.mjs +948 -0
- package/compat.test.mjs.map +1 -0
- package/index.d.mts +2 -0
- package/index.d.mts.map +1 -0
- package/index.d.ts +2 -0
- package/index.d.ts.map +1 -0
- package/index.js +89 -0
- package/index.js.map +1 -0
- package/index.mjs +86 -0
- package/index.mjs.map +1 -0
- package/options.d.mts +9 -0
- package/options.d.mts.map +1 -0
- package/options.d.ts +9 -0
- package/options.d.ts.map +1 -0
- package/options.js +321 -0
- package/options.js.map +1 -0
- package/options.mjs +314 -0
- package/options.mjs.map +1 -0
- package/options.test.d.mts +2 -0
- package/options.test.d.mts.map +1 -0
- package/options.test.d.ts +2 -0
- package/options.test.d.ts.map +1 -0
- package/options.test.js +154 -0
- package/options.test.js.map +1 -0
- package/options.test.mjs +152 -0
- package/options.test.mjs.map +1 -0
- package/package.json +129 -0
- package/server.d.mts +33 -0
- package/server.d.mts.map +1 -0
- package/server.d.ts +33 -0
- package/server.d.ts.map +1 -0
- package/server.js +85 -0
- package/server.js.map +1 -0
- package/server.mjs +74 -0
- package/server.mjs.map +1 -0
- package/src/compat.test.ts +1068 -0
- package/src/compat.ts +437 -0
- package/src/index.ts +106 -0
- package/src/options.test.ts +193 -0
- package/src/options.ts +339 -0
- package/src/server.ts +98 -0
- package/src/tools/index.ts +129 -0
- package/src/tools/projects/list-projects.ts +39 -0
- package/src/tools/records/create-records.ts +47 -0
- package/src/tools/runs/create-runs.ts +46 -0
- package/src/tools/runs/update-runs.ts +44 -0
- package/src/tools/system-configs/create-system-configs.ts +59 -0
- package/src/tools/system-configs/get-system-configs.ts +34 -0
- package/src/tools/system-configs/list-system-configs.ts +42 -0
- package/src/tools/systems/create-systems.ts +52 -0
- package/src/tools/systems/delete-systems.ts +31 -0
- package/src/tools/systems/get-systems.ts +31 -0
- package/src/tools/systems/list-systems.ts +41 -0
- package/src/tools/systems/update-systems.ts +52 -0
- package/src/tools/testcases/create-testcases.ts +47 -0
- package/src/tools/testcases/delete-testcases.ts +35 -0
- package/src/tools/testcases/get-testcases.ts +31 -0
- package/src/tools/testcases/list-testcases.ts +41 -0
- package/src/tools/testcases/update-testcases.ts +35 -0
- package/src/tools/testsets/create-testsets.ts +73 -0
- package/src/tools/testsets/delete-testsets.ts +31 -0
- package/src/tools/testsets/get-testsets.ts +31 -0
- package/src/tools/testsets/list-testsets.ts +41 -0
- package/src/tools/testsets/update-testsets.ts +73 -0
- package/src/tools.ts +1 -0
- package/src/tsconfig.json +11 -0
- package/tools/index.d.mts +21 -0
- package/tools/index.d.mts.map +1 -0
- package/tools/index.d.ts +21 -0
- package/tools/index.d.ts.map +1 -0
- package/tools/index.js +99 -0
- package/tools/index.js.map +1 -0
- package/tools/index.mjs +92 -0
- package/tools/index.mjs.map +1 -0
- package/tools/projects/list-projects.d.mts +24 -0
- package/tools/projects/list-projects.d.mts.map +1 -0
- package/tools/projects/list-projects.d.ts +24 -0
- package/tools/projects/list-projects.d.ts.map +1 -0
- package/tools/projects/list-projects.js +33 -0
- package/tools/projects/list-projects.js.map +1 -0
- package/tools/projects/list-projects.mjs +29 -0
- package/tools/projects/list-projects.mjs.map +1 -0
- package/tools/records/create-records.d.mts +24 -0
- package/tools/records/create-records.d.mts.map +1 -0
- package/tools/records/create-records.d.ts +24 -0
- package/tools/records/create-records.d.ts.map +1 -0
- package/tools/records/create-records.js +44 -0
- package/tools/records/create-records.js.map +1 -0
- package/tools/records/create-records.mjs +40 -0
- package/tools/records/create-records.mjs.map +1 -0
- package/tools/runs/create-runs.d.mts +24 -0
- package/tools/runs/create-runs.d.mts.map +1 -0
- package/tools/runs/create-runs.d.ts +24 -0
- package/tools/runs/create-runs.d.ts.map +1 -0
- package/tools/runs/create-runs.js +43 -0
- package/tools/runs/create-runs.js.map +1 -0
- package/tools/runs/create-runs.mjs +39 -0
- package/tools/runs/create-runs.mjs.map +1 -0
- package/tools/runs/update-runs.d.mts +24 -0
- package/tools/runs/update-runs.d.mts.map +1 -0
- package/tools/runs/update-runs.d.ts +24 -0
- package/tools/runs/update-runs.d.ts.map +1 -0
- package/tools/runs/update-runs.js +41 -0
- package/tools/runs/update-runs.js.map +1 -0
- package/tools/runs/update-runs.mjs +37 -0
- package/tools/runs/update-runs.mjs.map +1 -0
- package/tools/system-configs/create-system-configs.d.mts +24 -0
- package/tools/system-configs/create-system-configs.d.mts.map +1 -0
- package/tools/system-configs/create-system-configs.d.ts +24 -0
- package/tools/system-configs/create-system-configs.d.ts.map +1 -0
- package/tools/system-configs/create-system-configs.js +54 -0
- package/tools/system-configs/create-system-configs.js.map +1 -0
- package/tools/system-configs/create-system-configs.mjs +50 -0
- package/tools/system-configs/create-system-configs.mjs.map +1 -0
- package/tools/system-configs/get-system-configs.d.mts +24 -0
- package/tools/system-configs/get-system-configs.d.mts.map +1 -0
- package/tools/system-configs/get-system-configs.d.ts +24 -0
- package/tools/system-configs/get-system-configs.d.ts.map +1 -0
- package/tools/system-configs/get-system-configs.js +31 -0
- package/tools/system-configs/get-system-configs.js.map +1 -0
- package/tools/system-configs/get-system-configs.mjs +27 -0
- package/tools/system-configs/get-system-configs.mjs.map +1 -0
- package/tools/system-configs/list-system-configs.d.mts +24 -0
- package/tools/system-configs/list-system-configs.d.mts.map +1 -0
- package/tools/system-configs/list-system-configs.d.ts +24 -0
- package/tools/system-configs/list-system-configs.d.ts.map +1 -0
- package/tools/system-configs/list-system-configs.js +36 -0
- package/tools/system-configs/list-system-configs.js.map +1 -0
- package/tools/system-configs/list-system-configs.mjs +32 -0
- package/tools/system-configs/list-system-configs.mjs.map +1 -0
- package/tools/systems/create-systems.d.mts +24 -0
- package/tools/systems/create-systems.d.mts.map +1 -0
- package/tools/systems/create-systems.d.ts +24 -0
- package/tools/systems/create-systems.d.ts.map +1 -0
- package/tools/systems/create-systems.js +48 -0
- package/tools/systems/create-systems.js.map +1 -0
- package/tools/systems/create-systems.mjs +44 -0
- package/tools/systems/create-systems.mjs.map +1 -0
- package/tools/systems/delete-systems.d.mts +24 -0
- package/tools/systems/delete-systems.d.mts.map +1 -0
- package/tools/systems/delete-systems.d.ts +24 -0
- package/tools/systems/delete-systems.d.ts.map +1 -0
- package/tools/systems/delete-systems.js +28 -0
- package/tools/systems/delete-systems.js.map +1 -0
- package/tools/systems/delete-systems.mjs +24 -0
- package/tools/systems/delete-systems.mjs.map +1 -0
- package/tools/systems/get-systems.d.mts +24 -0
- package/tools/systems/get-systems.d.mts.map +1 -0
- package/tools/systems/get-systems.d.ts +24 -0
- package/tools/systems/get-systems.d.ts.map +1 -0
- package/tools/systems/get-systems.js +28 -0
- package/tools/systems/get-systems.js.map +1 -0
- package/tools/systems/get-systems.mjs +24 -0
- package/tools/systems/get-systems.mjs.map +1 -0
- package/tools/systems/list-systems.d.mts +24 -0
- package/tools/systems/list-systems.d.mts.map +1 -0
- package/tools/systems/list-systems.d.ts +24 -0
- package/tools/systems/list-systems.d.ts.map +1 -0
- package/tools/systems/list-systems.js +36 -0
- package/tools/systems/list-systems.js.map +1 -0
- package/tools/systems/list-systems.mjs +32 -0
- package/tools/systems/list-systems.mjs.map +1 -0
- package/tools/systems/update-systems.d.mts +24 -0
- package/tools/systems/update-systems.d.mts.map +1 -0
- package/tools/systems/update-systems.d.ts +24 -0
- package/tools/systems/update-systems.d.ts.map +1 -0
- package/tools/systems/update-systems.js +48 -0
- package/tools/systems/update-systems.js.map +1 -0
- package/tools/systems/update-systems.mjs +44 -0
- package/tools/systems/update-systems.mjs.map +1 -0
- package/tools/testcases/create-testcases.d.mts +24 -0
- package/tools/testcases/create-testcases.d.mts.map +1 -0
- package/tools/testcases/create-testcases.d.ts +24 -0
- package/tools/testcases/create-testcases.d.ts.map +1 -0
- package/tools/testcases/create-testcases.js +43 -0
- package/tools/testcases/create-testcases.js.map +1 -0
- package/tools/testcases/create-testcases.mjs +39 -0
- package/tools/testcases/create-testcases.mjs.map +1 -0
- package/tools/testcases/delete-testcases.d.mts +24 -0
- package/tools/testcases/delete-testcases.d.mts.map +1 -0
- package/tools/testcases/delete-testcases.d.ts +24 -0
- package/tools/testcases/delete-testcases.d.ts.map +1 -0
- package/tools/testcases/delete-testcases.js +32 -0
- package/tools/testcases/delete-testcases.js.map +1 -0
- package/tools/testcases/delete-testcases.mjs +28 -0
- package/tools/testcases/delete-testcases.mjs.map +1 -0
- package/tools/testcases/get-testcases.d.mts +24 -0
- package/tools/testcases/get-testcases.d.mts.map +1 -0
- package/tools/testcases/get-testcases.d.ts +24 -0
- package/tools/testcases/get-testcases.d.ts.map +1 -0
- package/tools/testcases/get-testcases.js +28 -0
- package/tools/testcases/get-testcases.js.map +1 -0
- package/tools/testcases/get-testcases.mjs +24 -0
- package/tools/testcases/get-testcases.mjs.map +1 -0
- package/tools/testcases/list-testcases.d.mts +24 -0
- package/tools/testcases/list-testcases.d.mts.map +1 -0
- package/tools/testcases/list-testcases.d.ts +24 -0
- package/tools/testcases/list-testcases.d.ts.map +1 -0
- package/tools/testcases/list-testcases.js +36 -0
- package/tools/testcases/list-testcases.js.map +1 -0
- package/tools/testcases/list-testcases.mjs +32 -0
- package/tools/testcases/list-testcases.mjs.map +1 -0
- package/tools/testcases/update-testcases.d.mts +24 -0
- package/tools/testcases/update-testcases.d.mts.map +1 -0
- package/tools/testcases/update-testcases.d.ts +24 -0
- package/tools/testcases/update-testcases.d.ts.map +1 -0
- package/tools/testcases/update-testcases.js +32 -0
- package/tools/testcases/update-testcases.js.map +1 -0
- package/tools/testcases/update-testcases.mjs +28 -0
- package/tools/testcases/update-testcases.mjs.map +1 -0
- package/tools/testsets/create-testsets.d.mts +24 -0
- package/tools/testsets/create-testsets.d.mts.map +1 -0
- package/tools/testsets/create-testsets.d.ts +24 -0
- package/tools/testsets/create-testsets.d.ts.map +1 -0
- package/tools/testsets/create-testsets.js +68 -0
- package/tools/testsets/create-testsets.js.map +1 -0
- package/tools/testsets/create-testsets.mjs +64 -0
- package/tools/testsets/create-testsets.mjs.map +1 -0
- package/tools/testsets/delete-testsets.d.mts +24 -0
- package/tools/testsets/delete-testsets.d.mts.map +1 -0
- package/tools/testsets/delete-testsets.d.ts +24 -0
- package/tools/testsets/delete-testsets.d.ts.map +1 -0
- package/tools/testsets/delete-testsets.js +28 -0
- package/tools/testsets/delete-testsets.js.map +1 -0
- package/tools/testsets/delete-testsets.mjs +24 -0
- package/tools/testsets/delete-testsets.mjs.map +1 -0
- package/tools/testsets/get-testsets.d.mts +24 -0
- package/tools/testsets/get-testsets.d.mts.map +1 -0
- package/tools/testsets/get-testsets.d.ts +24 -0
- package/tools/testsets/get-testsets.d.ts.map +1 -0
- package/tools/testsets/get-testsets.js +28 -0
- package/tools/testsets/get-testsets.js.map +1 -0
- package/tools/testsets/get-testsets.mjs +24 -0
- package/tools/testsets/get-testsets.mjs.map +1 -0
- package/tools/testsets/list-testsets.d.mts +24 -0
- package/tools/testsets/list-testsets.d.mts.map +1 -0
- package/tools/testsets/list-testsets.d.ts +24 -0
- package/tools/testsets/list-testsets.d.ts.map +1 -0
- package/tools/testsets/list-testsets.js +36 -0
- package/tools/testsets/list-testsets.js.map +1 -0
- package/tools/testsets/list-testsets.mjs +32 -0
- package/tools/testsets/list-testsets.mjs.map +1 -0
- package/tools/testsets/update-testsets.d.mts +24 -0
- package/tools/testsets/update-testsets.d.mts.map +1 -0
- package/tools/testsets/update-testsets.d.ts +24 -0
- package/tools/testsets/update-testsets.d.ts.map +1 -0
- package/tools/testsets/update-testsets.js +68 -0
- package/tools/testsets/update-testsets.js.map +1 -0
- package/tools/testsets/update-testsets.mjs +64 -0
- package/tools/testsets/update-testsets.mjs.map +1 -0
- package/tools.d.mts +2 -0
- package/tools.d.mts.map +1 -0
- package/tools.d.ts +2 -0
- package/tools.d.ts.map +1 -0
- package/tools.js +18 -0
- package/tools.js.map +1 -0
- package/tools.mjs +2 -0
- package/tools.mjs.map +1 -0
package/src/compat.ts
ADDED
|
@@ -0,0 +1,437 @@
|
|
|
1
|
+
import { Tool } from '@modelcontextprotocol/sdk/types.js';
|
|
2
|
+
import { Endpoint } from './tools';
|
|
3
|
+
import Ajv from 'ajv';
|
|
4
|
+
|
|
5
|
+
const ajv = new Ajv();
|
|
6
|
+
|
|
7
|
+
export interface ClientCapabilities {
|
|
8
|
+
topLevelUnions: boolean;
|
|
9
|
+
validJson: boolean;
|
|
10
|
+
refs: boolean;
|
|
11
|
+
unions: boolean;
|
|
12
|
+
formats: boolean;
|
|
13
|
+
toolNameLength: number | undefined;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
export const defaultClientCapabilities: ClientCapabilities = {
|
|
17
|
+
topLevelUnions: true,
|
|
18
|
+
validJson: true,
|
|
19
|
+
refs: true,
|
|
20
|
+
unions: true,
|
|
21
|
+
formats: true,
|
|
22
|
+
toolNameLength: undefined,
|
|
23
|
+
};
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* Attempts to parse strings into JSON objects if initial schema
|
|
27
|
+
* validation fails.
|
|
28
|
+
*/
|
|
29
|
+
export function parseEmbeddedJSON(args: Record<string, unknown>, schema: Record<string, unknown>) {
|
|
30
|
+
if (ajv.validate(schema, args)) {
|
|
31
|
+
return args;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
let updated = false;
|
|
35
|
+
const newArgs: Record<string, unknown> = Object.assign({}, args);
|
|
36
|
+
|
|
37
|
+
for (const [key, value] of Object.entries(newArgs)) {
|
|
38
|
+
if (typeof value === 'string') {
|
|
39
|
+
try {
|
|
40
|
+
const parsed = JSON.parse(value);
|
|
41
|
+
newArgs[key] = parsed;
|
|
42
|
+
updated = true;
|
|
43
|
+
} catch (e) {
|
|
44
|
+
// Not valid JSON, leave as is
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
if (updated) {
|
|
50
|
+
return newArgs;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
return args;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
export type JSONSchema = {
|
|
57
|
+
type?: string;
|
|
58
|
+
properties?: Record<string, JSONSchema>;
|
|
59
|
+
required?: string[];
|
|
60
|
+
anyOf?: JSONSchema[];
|
|
61
|
+
$ref?: string;
|
|
62
|
+
$defs?: Record<string, JSONSchema>;
|
|
63
|
+
[key: string]: any;
|
|
64
|
+
};
|
|
65
|
+
|
|
66
|
+
/**
|
|
67
|
+
* Truncates tool names to the specified length while ensuring uniqueness.
|
|
68
|
+
* If truncation would cause duplicate names, appends a number to make them unique.
|
|
69
|
+
*/
|
|
70
|
+
export function truncateToolNames(names: string[], maxLength: number): Map<string, string> {
|
|
71
|
+
if (maxLength <= 0) {
|
|
72
|
+
return new Map();
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
const renameMap = new Map<string, string>();
|
|
76
|
+
const usedNames = new Set<string>();
|
|
77
|
+
|
|
78
|
+
const toTruncate = names.filter((name) => name.length > maxLength);
|
|
79
|
+
|
|
80
|
+
if (toTruncate.length === 0) {
|
|
81
|
+
return renameMap;
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
const willCollide =
|
|
85
|
+
new Set(toTruncate.map((name) => name.slice(0, maxLength - 1))).size < toTruncate.length;
|
|
86
|
+
|
|
87
|
+
if (!willCollide) {
|
|
88
|
+
for (const name of toTruncate) {
|
|
89
|
+
const truncatedName = name.slice(0, maxLength);
|
|
90
|
+
renameMap.set(name, truncatedName);
|
|
91
|
+
}
|
|
92
|
+
} else {
|
|
93
|
+
const baseLength = maxLength - 1;
|
|
94
|
+
|
|
95
|
+
for (const name of toTruncate) {
|
|
96
|
+
const baseName = name.slice(0, baseLength);
|
|
97
|
+
let counter = 1;
|
|
98
|
+
|
|
99
|
+
while (usedNames.has(baseName + counter)) {
|
|
100
|
+
counter++;
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
const finalName = baseName + counter;
|
|
104
|
+
renameMap.set(name, finalName);
|
|
105
|
+
usedNames.add(finalName);
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
return renameMap;
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
/**
|
|
113
|
+
* Removes top-level unions from a tool by splitting it into multiple tools,
|
|
114
|
+
* one for each variant in the union.
|
|
115
|
+
*/
|
|
116
|
+
export function removeTopLevelUnions(tool: Tool): Tool[] {
|
|
117
|
+
const inputSchema = tool.inputSchema as JSONSchema;
|
|
118
|
+
const variants = inputSchema.anyOf;
|
|
119
|
+
|
|
120
|
+
if (!variants || !Array.isArray(variants) || variants.length === 0) {
|
|
121
|
+
return [tool];
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
const defs = inputSchema.$defs || {};
|
|
125
|
+
|
|
126
|
+
return variants.map((variant, index) => {
|
|
127
|
+
const variantSchema: JSONSchema = {
|
|
128
|
+
...inputSchema,
|
|
129
|
+
...variant,
|
|
130
|
+
type: 'object',
|
|
131
|
+
properties: {
|
|
132
|
+
...(inputSchema.properties || {}),
|
|
133
|
+
...(variant.properties || {}),
|
|
134
|
+
},
|
|
135
|
+
};
|
|
136
|
+
|
|
137
|
+
delete variantSchema.anyOf;
|
|
138
|
+
|
|
139
|
+
if (!variantSchema['description']) {
|
|
140
|
+
variantSchema['description'] = tool.description;
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
const usedDefs = findUsedDefs(variant, defs);
|
|
144
|
+
if (Object.keys(usedDefs).length > 0) {
|
|
145
|
+
variantSchema.$defs = usedDefs;
|
|
146
|
+
} else {
|
|
147
|
+
delete variantSchema.$defs;
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
return {
|
|
151
|
+
...tool,
|
|
152
|
+
name: `${tool.name}_${toSnakeCase(variant['title'] || `variant${index + 1}`)}`,
|
|
153
|
+
description: variant['description'] || tool.description,
|
|
154
|
+
inputSchema: variantSchema,
|
|
155
|
+
} as Tool;
|
|
156
|
+
});
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
function findUsedDefs(schema: JSONSchema, defs: Record<string, JSONSchema>): Record<string, JSONSchema> {
|
|
160
|
+
const usedDefs: Record<string, JSONSchema> = {};
|
|
161
|
+
|
|
162
|
+
if (typeof schema !== 'object' || schema === null) {
|
|
163
|
+
return usedDefs;
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
if (schema.$ref) {
|
|
167
|
+
const refParts = schema.$ref.split('/');
|
|
168
|
+
if (refParts[0] === '#' && refParts[1] === '$defs' && refParts[2]) {
|
|
169
|
+
const defName = refParts[2];
|
|
170
|
+
const def = defs[defName];
|
|
171
|
+
if (def) {
|
|
172
|
+
usedDefs[defName] = def;
|
|
173
|
+
Object.assign(usedDefs, findUsedDefs(def, defs));
|
|
174
|
+
}
|
|
175
|
+
}
|
|
176
|
+
return usedDefs;
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
for (const key in schema) {
|
|
180
|
+
if (key !== '$defs' && typeof schema[key] === 'object' && schema[key] !== null) {
|
|
181
|
+
Object.assign(usedDefs, findUsedDefs(schema[key] as JSONSchema, defs));
|
|
182
|
+
}
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
return usedDefs;
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
/**
|
|
189
|
+
* Inlines all $refs in a schema, eliminating $defs.
|
|
190
|
+
* If a circular reference is detected, the circular property is removed.
|
|
191
|
+
*/
|
|
192
|
+
export function inlineRefs(schema: JSONSchema): JSONSchema {
|
|
193
|
+
if (!schema || typeof schema !== 'object') {
|
|
194
|
+
return schema;
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
const clonedSchema = { ...schema };
|
|
198
|
+
const defs: Record<string, JSONSchema> = schema.$defs || {};
|
|
199
|
+
|
|
200
|
+
delete clonedSchema.$defs;
|
|
201
|
+
|
|
202
|
+
const result = inlineRefsRecursive(clonedSchema, defs, new Set<string>());
|
|
203
|
+
// The top level can never be null
|
|
204
|
+
return result === null ? {} : result;
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
function inlineRefsRecursive(
|
|
208
|
+
schema: JSONSchema,
|
|
209
|
+
defs: Record<string, JSONSchema>,
|
|
210
|
+
refPath: Set<string>,
|
|
211
|
+
): JSONSchema | null {
|
|
212
|
+
if (!schema || typeof schema !== 'object') {
|
|
213
|
+
return schema;
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
if (Array.isArray(schema)) {
|
|
217
|
+
return schema.map((item) => {
|
|
218
|
+
const processed = inlineRefsRecursive(item, defs, refPath);
|
|
219
|
+
return processed === null ? {} : processed;
|
|
220
|
+
}) as JSONSchema;
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
const result = { ...schema };
|
|
224
|
+
|
|
225
|
+
if ('$ref' in result && typeof result.$ref === 'string') {
|
|
226
|
+
if (result.$ref.startsWith('#/$defs/')) {
|
|
227
|
+
const refName = result.$ref.split('/').pop() as string;
|
|
228
|
+
const def = defs[refName];
|
|
229
|
+
|
|
230
|
+
// If we've already seen this ref in our path, we have a circular reference
|
|
231
|
+
if (refPath.has(result.$ref)) {
|
|
232
|
+
// For circular references, we completely remove the property
|
|
233
|
+
// by returning null. The parent will remove it.
|
|
234
|
+
return null;
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
if (def) {
|
|
238
|
+
const newRefPath = new Set(refPath);
|
|
239
|
+
newRefPath.add(result.$ref);
|
|
240
|
+
|
|
241
|
+
const inlinedDef = inlineRefsRecursive({ ...def }, defs, newRefPath);
|
|
242
|
+
|
|
243
|
+
if (inlinedDef === null) {
|
|
244
|
+
return { ...result };
|
|
245
|
+
}
|
|
246
|
+
|
|
247
|
+
// Merge the inlined definition with the original schema's properties
|
|
248
|
+
// but preserve things like description, etc.
|
|
249
|
+
const { $ref, ...rest } = result;
|
|
250
|
+
return { ...inlinedDef, ...rest };
|
|
251
|
+
}
|
|
252
|
+
}
|
|
253
|
+
|
|
254
|
+
// Keep external refs as-is
|
|
255
|
+
return result;
|
|
256
|
+
}
|
|
257
|
+
|
|
258
|
+
for (const key in result) {
|
|
259
|
+
if (result[key] && typeof result[key] === 'object') {
|
|
260
|
+
const processed = inlineRefsRecursive(result[key] as JSONSchema, defs, refPath);
|
|
261
|
+
if (processed === null) {
|
|
262
|
+
// Remove properties that would cause circular references
|
|
263
|
+
delete result[key];
|
|
264
|
+
} else {
|
|
265
|
+
result[key] = processed;
|
|
266
|
+
}
|
|
267
|
+
}
|
|
268
|
+
}
|
|
269
|
+
|
|
270
|
+
return result;
|
|
271
|
+
}
|
|
272
|
+
|
|
273
|
+
/**
|
|
274
|
+
* Removes anyOf fields from a schema, using only the first variant.
|
|
275
|
+
*/
|
|
276
|
+
export function removeAnyOf(schema: JSONSchema): JSONSchema {
|
|
277
|
+
if (!schema || typeof schema !== 'object') {
|
|
278
|
+
return schema;
|
|
279
|
+
}
|
|
280
|
+
|
|
281
|
+
if (Array.isArray(schema)) {
|
|
282
|
+
return schema.map((item) => removeAnyOf(item)) as JSONSchema;
|
|
283
|
+
}
|
|
284
|
+
|
|
285
|
+
const result = { ...schema };
|
|
286
|
+
|
|
287
|
+
if ('anyOf' in result && Array.isArray(result.anyOf) && result.anyOf.length > 0) {
|
|
288
|
+
const firstVariant = result.anyOf[0];
|
|
289
|
+
|
|
290
|
+
if (firstVariant && typeof firstVariant === 'object') {
|
|
291
|
+
// Special handling for properties to ensure deep merge
|
|
292
|
+
if (firstVariant.properties && result.properties) {
|
|
293
|
+
result.properties = {
|
|
294
|
+
...result.properties,
|
|
295
|
+
...(firstVariant.properties as Record<string, JSONSchema>),
|
|
296
|
+
};
|
|
297
|
+
} else if (firstVariant.properties) {
|
|
298
|
+
result.properties = { ...firstVariant.properties };
|
|
299
|
+
}
|
|
300
|
+
|
|
301
|
+
for (const key in firstVariant) {
|
|
302
|
+
if (key !== 'properties') {
|
|
303
|
+
result[key] = firstVariant[key];
|
|
304
|
+
}
|
|
305
|
+
}
|
|
306
|
+
}
|
|
307
|
+
|
|
308
|
+
delete result.anyOf;
|
|
309
|
+
}
|
|
310
|
+
|
|
311
|
+
for (const key in result) {
|
|
312
|
+
if (result[key] && typeof result[key] === 'object') {
|
|
313
|
+
result[key] = removeAnyOf(result[key] as JSONSchema);
|
|
314
|
+
}
|
|
315
|
+
}
|
|
316
|
+
|
|
317
|
+
return result;
|
|
318
|
+
}
|
|
319
|
+
|
|
320
|
+
/**
|
|
321
|
+
* Removes format fields from a schema and appends them to the description.
|
|
322
|
+
*/
|
|
323
|
+
export function removeFormats(schema: JSONSchema, formatsCapability: boolean): JSONSchema {
|
|
324
|
+
if (formatsCapability) {
|
|
325
|
+
return schema;
|
|
326
|
+
}
|
|
327
|
+
|
|
328
|
+
if (!schema || typeof schema !== 'object') {
|
|
329
|
+
return schema;
|
|
330
|
+
}
|
|
331
|
+
|
|
332
|
+
if (Array.isArray(schema)) {
|
|
333
|
+
return schema.map((item) => removeFormats(item, formatsCapability)) as JSONSchema;
|
|
334
|
+
}
|
|
335
|
+
|
|
336
|
+
const result = { ...schema };
|
|
337
|
+
|
|
338
|
+
if ('format' in result && typeof result['format'] === 'string') {
|
|
339
|
+
const formatStr = `(format: "${result['format']}")`;
|
|
340
|
+
|
|
341
|
+
if ('description' in result && typeof result['description'] === 'string') {
|
|
342
|
+
result['description'] = `${result['description']} ${formatStr}`;
|
|
343
|
+
} else {
|
|
344
|
+
result['description'] = formatStr;
|
|
345
|
+
}
|
|
346
|
+
|
|
347
|
+
delete result['format'];
|
|
348
|
+
}
|
|
349
|
+
|
|
350
|
+
for (const key in result) {
|
|
351
|
+
if (result[key] && typeof result[key] === 'object') {
|
|
352
|
+
result[key] = removeFormats(result[key] as JSONSchema, formatsCapability);
|
|
353
|
+
}
|
|
354
|
+
}
|
|
355
|
+
|
|
356
|
+
return result;
|
|
357
|
+
}
|
|
358
|
+
|
|
359
|
+
/**
|
|
360
|
+
* Applies all compatibility transformations to the endpoints based on the provided capabilities.
|
|
361
|
+
*/
|
|
362
|
+
export function applyCompatibilityTransformations(
|
|
363
|
+
endpoints: Endpoint[],
|
|
364
|
+
capabilities: ClientCapabilities,
|
|
365
|
+
): Endpoint[] {
|
|
366
|
+
let transformedEndpoints = [...endpoints];
|
|
367
|
+
|
|
368
|
+
// Handle top-level unions first as this changes tool names
|
|
369
|
+
if (!capabilities.topLevelUnions) {
|
|
370
|
+
const newEndpoints: Endpoint[] = [];
|
|
371
|
+
|
|
372
|
+
for (const endpoint of transformedEndpoints) {
|
|
373
|
+
const variantTools = removeTopLevelUnions(endpoint.tool);
|
|
374
|
+
|
|
375
|
+
if (variantTools.length === 1) {
|
|
376
|
+
newEndpoints.push(endpoint);
|
|
377
|
+
} else {
|
|
378
|
+
for (const variantTool of variantTools) {
|
|
379
|
+
newEndpoints.push({
|
|
380
|
+
...endpoint,
|
|
381
|
+
tool: variantTool,
|
|
382
|
+
});
|
|
383
|
+
}
|
|
384
|
+
}
|
|
385
|
+
}
|
|
386
|
+
|
|
387
|
+
transformedEndpoints = newEndpoints;
|
|
388
|
+
}
|
|
389
|
+
|
|
390
|
+
if (capabilities.toolNameLength) {
|
|
391
|
+
const toolNames = transformedEndpoints.map((endpoint) => endpoint.tool.name);
|
|
392
|
+
const renameMap = truncateToolNames(toolNames, capabilities.toolNameLength);
|
|
393
|
+
|
|
394
|
+
transformedEndpoints = transformedEndpoints.map((endpoint) => ({
|
|
395
|
+
...endpoint,
|
|
396
|
+
tool: {
|
|
397
|
+
...endpoint.tool,
|
|
398
|
+
name: renameMap.get(endpoint.tool.name) ?? endpoint.tool.name,
|
|
399
|
+
},
|
|
400
|
+
}));
|
|
401
|
+
}
|
|
402
|
+
|
|
403
|
+
if (!capabilities.refs || !capabilities.unions || !capabilities.formats) {
|
|
404
|
+
transformedEndpoints = transformedEndpoints.map((endpoint) => {
|
|
405
|
+
let schema = endpoint.tool.inputSchema as JSONSchema;
|
|
406
|
+
|
|
407
|
+
if (!capabilities.refs) {
|
|
408
|
+
schema = inlineRefs(schema);
|
|
409
|
+
}
|
|
410
|
+
|
|
411
|
+
if (!capabilities.unions) {
|
|
412
|
+
schema = removeAnyOf(schema);
|
|
413
|
+
}
|
|
414
|
+
|
|
415
|
+
if (!capabilities.formats) {
|
|
416
|
+
schema = removeFormats(schema, capabilities.formats);
|
|
417
|
+
}
|
|
418
|
+
|
|
419
|
+
return {
|
|
420
|
+
...endpoint,
|
|
421
|
+
tool: {
|
|
422
|
+
...endpoint.tool,
|
|
423
|
+
inputSchema: schema as typeof endpoint.tool.inputSchema,
|
|
424
|
+
},
|
|
425
|
+
};
|
|
426
|
+
});
|
|
427
|
+
}
|
|
428
|
+
|
|
429
|
+
return transformedEndpoints;
|
|
430
|
+
}
|
|
431
|
+
|
|
432
|
+
function toSnakeCase(str: string): string {
|
|
433
|
+
return str
|
|
434
|
+
.replace(/\s+/g, '_')
|
|
435
|
+
.replace(/([a-z])([A-Z])/g, '$1_$2')
|
|
436
|
+
.toLowerCase();
|
|
437
|
+
}
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,106 @@
|
|
|
1
|
+
import { StdioServerTransport } from '@modelcontextprotocol/sdk/server/stdio.js';
|
|
2
|
+
import { init, server } from './server';
|
|
3
|
+
import { Endpoint, endpoints, Filter, query } from './tools';
|
|
4
|
+
import { applyCompatibilityTransformations } from './compat';
|
|
5
|
+
import { parseOptions } from './options';
|
|
6
|
+
|
|
7
|
+
async function main() {
|
|
8
|
+
const { filters, capabilities, list } = parseOptionsOrError();
|
|
9
|
+
|
|
10
|
+
if (list) {
|
|
11
|
+
listAllTools();
|
|
12
|
+
return;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
const filteredEndpoints = filterEndpointsOrError(filters, endpoints);
|
|
16
|
+
|
|
17
|
+
// Apply compatibility transformations
|
|
18
|
+
const transformedEndpoints = applyCompatibilityTransformations(filteredEndpoints, capabilities);
|
|
19
|
+
|
|
20
|
+
console.error(
|
|
21
|
+
`MCP Server starting with ${transformedEndpoints.length} tools:`,
|
|
22
|
+
transformedEndpoints.map((e) => e.tool.name),
|
|
23
|
+
);
|
|
24
|
+
|
|
25
|
+
init({ server, endpoints: transformedEndpoints });
|
|
26
|
+
|
|
27
|
+
const transport = new StdioServerTransport();
|
|
28
|
+
await server.connect(transport);
|
|
29
|
+
console.error('MCP Server running on stdio');
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
if (require.main === module) {
|
|
33
|
+
main().catch((error) => {
|
|
34
|
+
console.error('Fatal error in main():', error);
|
|
35
|
+
process.exit(1);
|
|
36
|
+
});
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
function parseOptionsOrError() {
|
|
40
|
+
try {
|
|
41
|
+
return parseOptions();
|
|
42
|
+
} catch (error) {
|
|
43
|
+
console.error('Error parsing options:', error);
|
|
44
|
+
process.exit(1);
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
function filterEndpointsOrError(filters: Filter[], endpoints: Endpoint[]) {
|
|
49
|
+
try {
|
|
50
|
+
const filteredEndpoints = query(filters, endpoints);
|
|
51
|
+
if (filteredEndpoints.length === 0) {
|
|
52
|
+
console.error('No tools match the provided filters.');
|
|
53
|
+
process.exit(1);
|
|
54
|
+
}
|
|
55
|
+
return filteredEndpoints;
|
|
56
|
+
} catch (error) {
|
|
57
|
+
if (error instanceof Error) {
|
|
58
|
+
console.error('Error filtering tools:', error.message);
|
|
59
|
+
} else {
|
|
60
|
+
console.error('Error filtering tools:', error);
|
|
61
|
+
}
|
|
62
|
+
process.exit(1);
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
function listAllTools() {
|
|
67
|
+
if (endpoints.length === 0) {
|
|
68
|
+
console.error('No tools available.');
|
|
69
|
+
return;
|
|
70
|
+
}
|
|
71
|
+
console.error('Available tools:\n');
|
|
72
|
+
|
|
73
|
+
// Group endpoints by resource
|
|
74
|
+
const resourceGroups = new Map<string, typeof endpoints>();
|
|
75
|
+
|
|
76
|
+
for (const endpoint of endpoints) {
|
|
77
|
+
const resource = endpoint.metadata.resource;
|
|
78
|
+
if (!resourceGroups.has(resource)) {
|
|
79
|
+
resourceGroups.set(resource, []);
|
|
80
|
+
}
|
|
81
|
+
resourceGroups.get(resource)!.push(endpoint);
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
// Sort resources alphabetically
|
|
85
|
+
const sortedResources = Array.from(resourceGroups.keys()).sort();
|
|
86
|
+
|
|
87
|
+
// Display hierarchically by resource
|
|
88
|
+
for (const resource of sortedResources) {
|
|
89
|
+
console.error(`Resource: ${resource}`);
|
|
90
|
+
|
|
91
|
+
const resourceEndpoints = resourceGroups.get(resource)!;
|
|
92
|
+
// Sort endpoints by tool name
|
|
93
|
+
resourceEndpoints.sort((a, b) => a.tool.name.localeCompare(b.tool.name));
|
|
94
|
+
|
|
95
|
+
for (const endpoint of resourceEndpoints) {
|
|
96
|
+
const {
|
|
97
|
+
tool,
|
|
98
|
+
metadata: { operation, tags },
|
|
99
|
+
} = endpoint;
|
|
100
|
+
|
|
101
|
+
console.error(` - ${tool.name} (${operation}) ${tags.length > 0 ? `tags: ${tags.join(', ')}` : ''}`);
|
|
102
|
+
console.error(` Description: ${tool.description}`);
|
|
103
|
+
}
|
|
104
|
+
console.error('');
|
|
105
|
+
}
|
|
106
|
+
}
|