forked from tanaikech/ToolsForMCPServer
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain_library_side.js
More file actions
434 lines (400 loc) · 13 KB
/
main_library_side.js
File metadata and controls
434 lines (400 loc) · 13 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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
/**
* Tools for MCP server built by MCPApp.
* Author: Kanshi Tanaike
* https://github.com/tanaikech/ToolsForMCPServer
*
* Updated on 20250822 16:03
* version 1.0.27
*/
const ToolsForMCPServerVersion = "1.0.27";
const ProtocolVersion = "2025-06-18";
const ServerName = "gas_web_apps";
/**
* This is an API key for using Gemini API.
* When the tools of `use_gemini` are used, this API key is required to be used.
*/
var apiKey = "";
/**
* This is a calendar ID. When this is not set, the primary calendar is used.
* This calendar ID is used for searching and creating events as the default calendar.
*/
var defaultCalendarId = null;
/**
* When this is false, the base tools are not included in the tools of the MCP server.
* The default is true.
*/
var enableBaseTools = true;
/**
* When this is false, the tools for managing the Google Classroom API are not included in the tools of the MCP server.
* The default is true.
*/
var enableClassroomTools = true;
/**
* When this is false, the tools for managing the Google People API are not included in the tools of the MCP server.
* The default is true.
*/
var enablePeopleTools = true;
/**
* When this is false, the tools for managing the Google Analytics Admin and Data APIs are not included in the tools of the MCP server.
* The default is true.
*/
var enableAnalyticsTools = true;
/**
* This is an access token and key for using StackExchange API.
* When the `get_questions_on_stackoverflow` tool is used, these values are required to be used.
*/
var accessToken_stackoverflow = "";
var key_stackoverflow = "";
/**
* This function returns tool objects as an array.
* @private
*/
function functions_() {
const descriptions = [];
/**
* Add base tools.
*/
if (enableBaseTools) {
descriptions.push(
descriptions_management_apis,
descriptions_management_calendar,
descriptions_management_docs,
descriptions_management_drive,
descriptions_management_forms,
descriptions_management_gmail,
descriptions_management_sheets,
descriptions_management_slides,
descriptions_use_gemini,
descriptions_management_people,
// descriptions_dev,
);
}
/**
* Add tools for managing Google Classroom.
*/
if (enableClassroomTools === true) {
descriptions.push(descriptions_management_classroom);
}
/**
* Add tools for managing Google People.
*/
if (enablePeopleTools === true) {
descriptions.push(descriptions_management_people);
}
/**
* Add tools for managing Google Analytics.
*/
if (enableAnalyticsTools === true) {
descriptions.push(descriptions_management_analytics);
}
const descriptionObj = descriptions.reduce((o, e) => (o = { ...o, ...e }, o), {});
/**
* By the way, the format of "functions" is the same with GeminiWithFiles.
* So, you can test the functions by directly using it with GeminiWithFiles.
* ref: https://github.com/tanaikech/GeminiWithFiles?tab=readme-ov-file#use-function-calling
*/
const functions = Object.entries(descriptionObj).reduce((o, [k, v]) => {
o.params_[k] = v;
o[k] = this[k];
return o;
}, { params_: {} });
const tools = Object.keys(functions.params_).sort((a, b) => a > b ? 1 : -1);
functions.params_ = tools.reduce((o, f) => {
if (functions.params_[f]) {
o[f] = functions.params_[f];
}
return o;
}, {});
return functions;
}
/**
* This function filteres tools.
* @private
*/
function filterTools_(object) {
const { enables = [], disables = [] } = object;
let functions = functions_();
if (enables.length > 0) {
functions.params_ = enables.reduce((o, f) => {
if (functions.params_[f]) {
o[f] = functions.params_[f];
}
return o;
}, {});
} else if (disables.length > 0) {
disables.forEach(f => {
if (functions.params_[f]) {
delete functions.params_[f];
}
});
}
return functions;
}
/**
* Main method.
* @return {Array} Tools for MCP server.
*/
function getTools(object = {}) {
const functions = filterTools_(object);
// for MCP
const itemsForMCP = [
{
"type": "initialize",
"value": {
"protocolVersion": ProtocolVersion,
"capabilities": {
"tools": { "listChanged": false }, "prompts": { listChanged: false }, "resources": { "subscribe": false, "listChanged": false }
},
"serverInfo": { "name": ServerName, "version": ToolsForMCPServerVersion }
}
},
{
"type": "prompts/list",
"value": {
"prompts": [
{
name: "search_files_on_google_drive",
description: "Search files on Google Drive.",
arguments: [
{ name: "filename", description: "Filename of the search file.", required: true },
],
},
{
name: "get_weather",
description: "Search the current weather.",
arguments: [
{ name: "location", description: "Location of the weather.", required: true },
],
},
{
name: "generate_roadmap",
description: "Generate a roadmap in Google Sheets.",
arguments: [
{ name: "goal", description: "Goal of the roadmap.", required: true },
],
},
]
}
},
{
"type": "prompts/get",
"value": {
"search_files_on_google_drive": {
description: "Search files on Google Drive.",
messages: [
{
role: "user",
content: {
type: "text",
text: "Return file metadata of '{{filename}}' on Google Drive.",
},
},
],
},
"get_weather": {
description: "Search the current weather.",
messages: [
{
role: "user",
content: {
type: "text",
text: "What is the current weather in {{location}}?",
},
},
],
},
"generate_roadmap": {
description: "Generate a roadmap in Google Sheets.",
messages: [
{
role: "user",
content: {
type: "text",
text: [
`Create a roadmap for a new Google Spreadsheet with the goal of "{{goal}}".`,
`1. Create a new Google Spreadsheet.`,
`2. Generate a detailed roadmap in the spreadsheet to reach the goal.`,
`3. Return the spreadsheet's URL.`,
].join("\n"),
},
},
],
},
}
},
{
"type": "resources/list",
"value": { "resources": [] }
},
...Object.keys(functions.params_).map(f => {
const tempValue = {
name: f,
title: functions.params_[f].title || f,
description: functions.params_[f].description,
inputSchema: functions.params_[f].parameters,
};
if (functions.params_[f].outputSchema) {
tempValue.outputSchema = functions.params_[f].outputSchema;
}
return {
"type": "tools/list",
"function": functions[f],
"value": tempValue
}
})
];
return itemsForMCP;
}
/**
* This function returns all tools as an object.
* The key and value are the tool name and the description of the tool, respectively.
* @return {Object}
*/
function getToolList() {
const functions = functions_();
return Object.keys(functions.params_).sort((a, b) => a > b ? 1 : -1).reduce((o, f) => (o[f] = functions.params_[f].description, o), {});
}
/**
* The method name was changed from "getServer" to "getTools".
* This is an old method name. But, this can also be used.
* The same result with getTools will be returned.
* @return {Array} Tools for MCP server.
*/
function getServer(object = {}) {
return getTools(object);
}
/**
* ### Description
* Check whether Drive API is enabled at Advanced Google services, and return it as true or false and the version.
* ref: https://medium.com/google-cloud/checking-api-enabled-with-advanced-google-services-using-google-apps-script-572bcdeb39a8
*
* @param {String} apiName API name you want to check.
* @returns {Object} Object including "api" and "version" properties.
*/
function isAPIAtAdvancedGoogleServices_(apiName) {
if (!apiName || apiName == "" || typeof apiName != "string") {
throw new Error("Please set a valid API name.");
} else if (!/^[A-Z]+$/g.test(apiName[0])) {
const [t, ...b] = apiName;
apiName = [t.toUpperCase(), ...b].join("");
}
const obj = { apiName, api: "disable" };
if (typeof this[apiName] !== "undefined") {
obj.api = "enable";
obj.version = this[apiName].getVersion();
}
return obj;
}
/**
* Check API.
* @param {String} apiName API name you want to check.
* @returns {Object}
*/
function checkAPI_(apiName) {
if (isAPIAtAdvancedGoogleServices_(apiName).api == "disable") {
result = { content: [{ type: "text", text: `${apiName} API is disabled. Please enable ${apiName} API in the Advanced Google services.` }], isError: true };
return { jsonrpc: "2.0", result };
}
return {};
}
// Common functions.
var for_google_apis = {
list: ({ func, args, jsonSchema, itemName, count = null }) => {
const queryParameters = args.pop();
let result;
try {
const ar = [];
let pageToken;
do {
const a = [...args, queryParameters];
const obj = func(...a);
if (obj[itemName]) {
ar.push(...obj[itemName]);
}
pageToken = obj.nextPageToken;
queryParameters.pageToken = pageToken;
} while (pageToken);
const itemJsonSchema = {
"$schema": "http://json-schema.org/draft-07/schema#",
"description": "JSON schema for the response value.",
"type": "object",
"properties": {
[itemName]: {
"description": `${itemName} that match the list request.`,
"type": "array",
"items": jsonSchema
}
},
"required": [itemName],
};
const arr = count ? ar.splice(0, count) : ar;
const text = [
`Retrieved values are as follows. ${arr.length} items were retrieved.`,
`<Values>${arr.length == 0 ? "No values." : JSON.stringify({ [itemName]: arr })}</Values>`,
`JSON schema of "Values" is as follows.`,
`<JSONSchema>${JSON.stringify(itemJsonSchema)}</JSONSchema>`,
].join("\n");
result = { content: [{ type: "text", text }], isError: false };
} catch ({ stack }) {
result = { content: [{ type: "text", text: stack }], isError: true };
}
console.log(result); // Check response.
return { jsonrpc: "2.0", result };
},
create: ({ func, args, jsonSchema }) => {
let result;
try {
const obj = func(...args);
const text = [
`Response value is as follows.`,
`<Value>${JSON.stringify(obj)}</Value>`,
`JSON schema of the response value is as follows.`,
`<JSONSchema>${JSON.stringify(jsonSchema)}</JSONSchema>`,
].join("\n");
result = { content: [{ type: "text", text }], isError: false };
} catch ({ stack }) {
result = { content: [{ type: "text", text: stack }], isError: true };
}
console.log(result); // Check response.
return { jsonrpc: "2.0", result };
},
update: ({ func, args }) => {
let result;
try {
const obj = func(...args);
result = { content: [{ type: "text", text: JSON.stringify(obj) }], isError: false };
} catch ({ stack }) {
result = { content: [{ type: "text", text: stack }], isError: true };
}
console.log(result); // Check response.
return { jsonrpc: "2.0", result };
},
remove: ({ func, args }) => {
let result;
try {
const obj = func(...args);
result = { content: [{ type: "text", text: JSON.stringify(obj) }], isError: false };
} catch ({ stack }) {
result = { content: [{ type: "text", text: stack }], isError: true };
}
console.log(result); // Check response.
return { jsonrpc: "2.0", result };
},
get: ({ func, args, jsonSchema }) => {
let result;
try {
const obj = func(...args);
const text = [
`Response value is as follows.`,
`<Value>${JSON.stringify(obj)}</Value>`,
`JSON schema of the response value is as follows.`,
`<JSONSchema>${JSON.stringify(jsonSchema)}</JSONSchema>`,
].join("\n");
result = { content: [{ type: "text", text }], isError: false };
} catch ({ stack }) {
result = { content: [{ type: "text", text: stack }], isError: true };
}
console.log(result); // Check response.
return { jsonrpc: "2.0", result };
}
};