-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtools.ts
More file actions
480 lines (419 loc) · 14.9 KB
/
tools.ts
File metadata and controls
480 lines (419 loc) · 14.9 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
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
/**
* Example Tools for Agent Framework
*
* This module demonstrates how to create custom tools using the BaseTool framework.
* Includes WeatherTool for getting weather data and SubTool for basic math operations.
*
* COMPATIBILITY NOTE:
* ✅ Compatible with MiniAgent v0.1.7+ and new MCP SDK integration
* ✅ Works with StandardAgent and built-in MCP support
* ✅ Uses modern BaseTool implementation with DefaultToolResult
* ✅ No MCP-specific dependencies - these are pure native tools
*
* These tools can be used both as native tools and alongside MCP tools
* in the same agent instance thanks to the unified tool interface.
*/
import { BaseTool, Type, Schema } from '../src/index.js';
import { DefaultToolResult } from '../src/interfaces.js';
// ============================================================================
// WEATHER TOOL
// ============================================================================
/**
* Weather Tool - Get current weather for specified coordinates
*
* This tool fetches weather data from the Open-Meteo API for any given
* latitude and longitude coordinates.
*/
/**
* Weather result interface for better type safety and structure
*/
export interface WeatherResult {
success: boolean;
latitude: number;
longitude: number;
temperature?: number;
unit?: string;
message: string;
}
export class WeatherTool extends BaseTool<{ latitude: number; longitude: number }, WeatherResult> {
constructor() {
super(
'get_weather',
'Weather Tool',
'Get current weather temperature (Celsius) for specified coordinates',
{
type: Type.OBJECT,
properties: {
latitude: {
type: Type.NUMBER,
description: 'Latitude coordinate (range: -90 to 90)'
},
longitude: {
type: Type.NUMBER,
description: 'Longitude coordinate (range: -180 to 180)'
}
},
required: ['latitude', 'longitude']
},
false, // isOutputMarkdown
true // canUpdateOutput
);
}
override validateToolParams(params: { latitude: number; longitude: number }): string | null {
const requiredError = this.validateRequiredParams(params, ['latitude', 'longitude']);
if (requiredError) return requiredError;
const typeError = this.validateParameterTypes(params, {
latitude: 'number',
longitude: 'number'
});
if (typeError) return typeError;
// Validate coordinate ranges
if (params.latitude < -90 || params.latitude > 90) {
return 'Latitude must be between -90 and 90';
}
if (params.longitude < -180 || params.longitude > 180) {
return 'Longitude must be between -180 and 180';
}
return null;
}
override getDescription(params: { latitude: number; longitude: number }): string {
return `Get weather for coordinates (${params.latitude}, ${params.longitude})`;
}
/**
* Core execution logic for weather fetching
* @param params Weather parameters
* @returns Weather result data
*/
protected async executeCore(params: { latitude: number; longitude: number }): Promise<WeatherResult> {
const { latitude, longitude } = params;
try {
const temperature = await this.fetchWeatherData(latitude, longitude);
return {
success: true,
latitude,
longitude,
temperature,
unit: '°C',
message: `Weather: ${temperature}°C at coordinates (${latitude}, ${longitude})`
};
} catch (error) {
return {
success: false,
latitude,
longitude,
message: error instanceof Error ? error.message : String(error)
};
}
}
/**
* Enhanced execute method with progress reporting
* @param params Tool parameters
* @param abortSignal Abort signal for cancellation
* @param outputUpdateHandler Optional output update handler
* @returns DefaultToolResult containing weather data
*/
async execute(
params: { latitude: number; longitude: number },
abortSignal: AbortSignal,
outputUpdateHandler?: (output: string) => void
): Promise<DefaultToolResult<WeatherResult>> {
const { latitude, longitude } = params;
if (outputUpdateHandler) {
outputUpdateHandler(this.formatProgress('Fetching weather', `${latitude}, ${longitude}`, '🌤️'));
}
try {
// Check for cancellation
this.checkAbortSignal(abortSignal, 'Weather fetch');
if (outputUpdateHandler) {
outputUpdateHandler(this.formatProgress('Contacting API', 'open-meteo.com', '🌐'));
}
const result = await this.executeCore(params);
// Check for cancellation after API call
this.checkAbortSignal(abortSignal, 'Weather fetch');
return new DefaultToolResult(result);
} catch (error) {
const errorResult: WeatherResult = {
success: false,
latitude,
longitude,
message: error instanceof Error ? error.message : String(error)
};
return new DefaultToolResult(errorResult);
}
}
/**
* Fetch weather data from Open-Meteo API
*/
private async fetchWeatherData(latitude: number, longitude: number): Promise<number> {
const url = `https://api.open-meteo.com/v1/forecast?latitude=${latitude}&longitude=${longitude}¤t=temperature_2m,wind_speed_10m&hourly=temperature_2m,relative_humidity_2m,wind_speed_10m`;
const response = await fetch(url);
if (!response.ok) {
throw new Error(`Weather API error: ${response.status} ${response.statusText}`);
}
const data = await response.json() as any;
if (!data.current || typeof data.current.temperature_2m !== 'number') {
throw new Error('Invalid weather data received from API');
}
return data.current.temperature_2m;
}
}
// ============================================================================
// SUBTRACTION TOOL
// ============================================================================
/**
* Subtraction Tool - Perform basic subtraction operations
*
* This tool performs subtraction between two numbers and provides
* detailed calculation information.
*/
/**
* Subtraction result interface for better type safety and structure
*/
export interface SubtractionResult {
success: boolean;
operation: string;
result: number;
minuend: number;
subtrahend: number;
isNegative: boolean;
message: string;
}
export class SubTool extends BaseTool<{ minuend: number; subtrahend: number }, SubtractionResult> {
constructor() {
super(
'subtract',
'Subtraction Tool',
'Perform subtraction operation between two numbers',
{
type: Type.OBJECT,
properties: {
minuend: {
type: Type.NUMBER,
description: 'The number to subtract from (first number)'
},
subtrahend: {
type: Type.NUMBER,
description: 'The number to subtract (second number)'
}
},
required: ['minuend', 'subtrahend']
},
false, // isOutputMarkdown
true // canUpdateOutput
);
}
override validateToolParams(params: { minuend: number; subtrahend: number }): string | null {
const requiredError = this.validateRequiredParams(params, ['minuend', 'subtrahend']);
if (requiredError) return requiredError;
const typeError = this.validateParameterTypes(params, {
minuend: 'number',
subtrahend: 'number'
});
if (typeError) return typeError;
// Check for invalid numbers
if (!isFinite(params.minuend) || !isFinite(params.subtrahend)) {
return 'Numbers must be finite values';
}
return null;
}
override getDescription(params: { minuend: number; subtrahend: number }): string {
return `Subtract ${params.subtrahend} from ${params.minuend}`;
}
/**
* Core execution logic for subtraction
* @param params Subtraction parameters
* @returns Subtraction result data
*/
protected async executeCore(params: { minuend: number; subtrahend: number }): Promise<SubtractionResult> {
const { minuend, subtrahend } = params;
// Simulate brief calculation delay
await new Promise(resolve => setTimeout(resolve, 100));
const result = minuend - subtrahend;
const operation = `${minuend} - ${subtrahend} = ${result}`;
const isNegative = result < 0;
const info = isNegative ? 'negative result' : 'positive result';
return {
success: true,
operation,
result,
minuend,
subtrahend,
isNegative,
message: `${operation} (${info})`
};
}
/**
* Enhanced execute method with progress reporting
* @param params Tool parameters
* @param abortSignal Abort signal for cancellation
* @param outputUpdateHandler Optional output update handler
* @returns DefaultToolResult containing subtraction data
*/
async execute(
params: { minuend: number; subtrahend: number },
abortSignal: AbortSignal,
outputUpdateHandler?: (output: string) => void
): Promise<DefaultToolResult<SubtractionResult>> {
const { minuend, subtrahend } = params;
if (outputUpdateHandler) {
outputUpdateHandler(this.formatProgress('Calculating', `${minuend} - ${subtrahend}`, '➖'));
}
try {
// Check for cancellation
this.checkAbortSignal(abortSignal, 'Subtraction calculation');
const result = await this.executeCore(params);
// Check for cancellation after calculation
this.checkAbortSignal(abortSignal, 'Subtraction calculation');
return new DefaultToolResult(result);
} catch (error) {
const errorResult: SubtractionResult = {
success: false,
operation: `${minuend} - ${subtrahend}`,
result: 0,
minuend,
subtrahend,
isNegative: false,
message: error instanceof Error ? error.message : String(error)
};
return new DefaultToolResult(errorResult);
}
}
}
// ============================================================================
// UTILITY FUNCTIONS AND CONSTANTS
// ============================================================================
/**
* Convenience function to create a WeatherTool instance
*/
export function createWeatherTool(): WeatherTool {
return new WeatherTool();
}
/**
* Convenience function to create a SubTool instance
*/
export function createSubTool(): SubTool {
return new SubTool();
}
/**
* Common city coordinates for weather queries
*/
export const CITY_COORDINATES = {
// 中国主要城市
'北京': { latitude: 39.9042, longitude: 116.4074 },
'上海': { latitude: 31.2304, longitude: 121.4737 },
'广州': { latitude: 23.1291, longitude: 113.2644 },
'深圳': { latitude: 22.5431, longitude: 114.0579 },
'成都': { latitude: 30.5728, longitude: 104.0668 },
'杭州': { latitude: 30.2741, longitude: 120.1551 },
'西安': { latitude: 34.3416, longitude: 108.9398 },
'武汉': { latitude: 30.5928, longitude: 114.3055 },
'南京': { latitude: 32.0603, longitude: 118.7969 },
'重庆': { latitude: 29.4316, longitude: 106.9123 },
'天津': { latitude: 39.3434, longitude: 117.3616 },
'苏州': { latitude: 31.2989, longitude: 120.5853 },
'青岛': { latitude: 36.0986, longitude: 120.3719 },
'大连': { latitude: 38.9140, longitude: 121.6147 },
'厦门': { latitude: 24.4798, longitude: 118.0819 },
// 国际主要城市
'东京': { latitude: 35.6762, longitude: 139.6503 },
'纽约': { latitude: 40.7128, longitude: -74.0060 },
'伦敦': { latitude: 51.5074, longitude: -0.1278 },
'巴黎': { latitude: 48.8566, longitude: 2.3522 },
'洛杉矶': { latitude: 34.0522, longitude: -118.2437 },
'悉尼': { latitude: -33.8688, longitude: 151.2093 },
'新加坡': { latitude: 1.3521, longitude: 103.8198 },
'首尔': { latitude: 37.5665, longitude: 126.9780 },
'曼谷': { latitude: 13.7563, longitude: 100.5018 },
'迪拜': { latitude: 25.2048, longitude: 55.2708 },
'多伦多': { latitude: 43.6532, longitude: -79.3832 },
'柏林': { latitude: 52.5200, longitude: 13.4050 },
'罗马': { latitude: 41.9028, longitude: 12.4964 },
'马德里': { latitude: 40.4168, longitude: -3.7038 },
'莫斯科': { latitude: 55.7558, longitude: 37.6173 }
} as const;
/**
* Get coordinates for a city name
*/
export function getCityCoordinates(cityName: string): { latitude: number; longitude: number } | null {
const coordinates = CITY_COORDINATES[cityName as keyof typeof CITY_COORDINATES];
return coordinates ? { ...coordinates } : null;
}
/**
* Get weather for a city by name
*/
export async function getWeatherForCity(cityName: string): Promise<{ city: string; temperature: number; coordinates: { latitude: number; longitude: number } } | null> {
const coordinates = getCityCoordinates(cityName);
if (!coordinates) {
return null;
}
const weatherTool = createWeatherTool();
const abortController = new AbortController();
try {
const result = await weatherTool.execute(coordinates, abortController.signal);
const weatherData = result.data;
// Check if the result was successful
if (!weatherData.success || weatherData.temperature === undefined) {
return null;
}
return {
city: cityName,
temperature: weatherData.temperature,
coordinates
};
} catch (error) {
console.error(`Failed to get weather for ${cityName}:`, error);
return null;
}
}
/**
* List all available cities
*/
export function getAvailableCities(): string[] {
return Object.keys(CITY_COORDINATES);
}
/**
* Find cities by partial name match
*/
export function findCitiesByName(partialName: string): string[] {
const searchTerm = partialName.toLowerCase();
return Object.keys(CITY_COORDINATES).filter(city =>
city.toLowerCase().includes(searchTerm)
);
}
// ============================================================================
// USAGE WITH MCP INTEGRATION
// ============================================================================
/**
* Example: Using these native tools alongside MCP tools in StandardAgent
*
* ```typescript
* import { StandardAgent, GeminiChat } from '../src/index.js';
* import { WeatherTool, SubTool } from './tools.js';
*
* const agent = new StandardAgent({
* chat: new GeminiChat({ apiKey: 'your-key' }),
* tools: [
* new WeatherTool(),
* new SubTool()
* ],
* // MCP servers are automatically integrated via StandardAgent's built-in MCP support
* mcpServers: [
* {
* name: 'filesystem',
* transport: 'stdio',
* command: 'npx',
* args: ['-y', '@modelcontextprotocol/server-filesystem', '/tmp']
* }
* ]
* });
*
* // The agent now has access to both native tools (WeatherTool, SubTool)
* // and MCP tools (filesystem operations) in a unified interface
* ```
*
* Benefits of this approach:
* - Native tools have zero latency (no IPC overhead)
* - MCP tools provide access to external capabilities
* - Both types work identically from the LLM's perspective
* - Easy to migrate between native and MCP implementations
*/