-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest-optimization.js
More file actions
executable file
·124 lines (109 loc) · 4.52 KB
/
test-optimization.js
File metadata and controls
executable file
·124 lines (109 loc) · 4.52 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
#!/usr/bin/env node
/**
* Test token optimization on sample GitHub MCP tool
*/
const sampleTool = {
name: "create_issue",
description: "Create a new issue in a GitHub repository. This tool allows you to create issues with a title, body, labels, assignees, milestone, and project. The issue will be created in the repository specified by the owner and repo parameters. You must have write access to the repository to create issues. The body parameter supports GitHub Flavored Markdown for formatting.",
inputSchema: {
type: "object",
properties: {
owner: {
type: "string",
description: "The account owner of the repository. The name is not case sensitive and must be a valid GitHub username or organization name. This is typically your username or the organization that owns the repository.",
},
repo: {
type: "string",
description: "The name of the repository. The name is not case sensitive. This is the repository where you want to create the issue.",
},
title: {
type: "string",
description: "The title of the issue. This will be displayed at the top of the issue page and should be a concise summary of the issue.",
},
body: {
type: "string",
description: "The contents of the issue. This supports GitHub Flavored Markdown for formatting. You can include code blocks, lists, links, and other markdown formatting.",
},
labels: {
type: "array",
items: { type: "string" },
description: "Labels to associate with this issue. You must have write access to the repository to add labels.",
},
assignees: {
type: "array",
items: { type: "string" },
description: "Logins for Users to assign to this issue. You must have write access to the repository to assign users.",
},
},
required: ["owner", "repo", "title"],
},
};
// Token optimization functions (same as proxy-server.js)
function compressDescription(desc) {
if (!desc) return desc;
let compressed = desc
.replace(/This tool allows you to /gi, '')
.replace(/You must have .+ access to .+?\./gi, '')
.replace(/The .+ parameter /gi, '')
.replace(/\. The .+ is not case sensitive/gi, '')
.replace(/supports GitHub Flavored Markdown/gi, 'GFM')
.replace(/GitHub repository/gi, 'repo')
.replace(/repository/gi, 'repo')
.replace(/organization/gi, 'org')
.replace(/pull request/gi, 'PR')
.replace(/\s+/g, ' ')
.trim();
if (compressed.length > 100) {
const firstSentence = compressed.match(/^[^.!?]+[.!?]/);
if (firstSentence) {
compressed = firstSentence[0];
} else {
compressed = compressed.substring(0, 100) + '...';
}
}
return compressed;
}
function simplifySchema(schema) {
if (!schema || typeof schema !== 'object') return schema;
const simplified = { ...schema };
if (simplified.properties) {
simplified.properties = Object.entries(simplified.properties).reduce((acc, [key, prop]) => {
acc[key] = {
type: prop.type,
...(prop.enum && { enum: prop.enum }),
...(prop.items && { items: simplifySchema(prop.items) }),
...(prop.properties && { properties: simplifySchema(prop.properties).properties }),
};
if (prop.description && prop.description.length < 30) {
acc[key].description = prop.description;
}
return acc;
}, {});
}
const result = {
type: simplified.type,
...(simplified.properties && { properties: simplified.properties }),
...(simplified.required && { required: simplified.required }),
...(simplified.items && { items: simplifySchema(simplified.items) }),
};
return result;
}
// Test
const original = JSON.stringify(sampleTool, null, 2);
const optimized = JSON.stringify({
name: sampleTool.name,
description: compressDescription(sampleTool.description),
inputSchema: simplifySchema(sampleTool.inputSchema),
}, null, 2);
console.log("=== ORIGINAL TOOL (GitHub MCP) ===");
console.log(original);
console.log("\n=== OPTIMIZED TOOL (Our Proxy) ===");
console.log(optimized);
const originalBytes = original.length;
const optimizedBytes = optimized.length;
const savings = ((1 - optimizedBytes / originalBytes) * 100).toFixed(1);
console.log("\n=== TOKEN SAVINGS ===");
console.log(`Original: ${originalBytes} bytes`);
console.log(`Optimized: ${optimizedBytes} bytes`);
console.log(`Savings: ${savings}% reduction`);
console.log(`\nEstimated token reduction: ~${Math.round(originalBytes / 4)} → ~${Math.round(optimizedBytes / 4)} tokens`);