MCPcopy Create free account
hub / github.com/echoVic/blade-code / customToolExample

Function customToolExample

examples/tools-example.ts:202–299  ·  view source on GitHub ↗

* 自定义工具示例

()

Source from the content-addressed store, hash-verified

200 * 自定义工具示例
201 */
202async function customToolExample() {
203 console.log('=== 自定义工具示例 ===\n');
204
205 const toolManager = await createToolManager();
206
207 // 定义自定义工具
208 const calculatorTool: ToolDefinition = {
209 name: 'calculator',
210 description: '简单计算器',
211 version: '1.0.0',
212 category: 'math',
213 tags: ['math', 'calculator'],
214 parameters: {
215 operation: {
216 type: 'string',
217 description: '数学运算',
218 enum: ['add', 'subtract', 'multiply', 'divide'],
219 required: true,
220 },
221 a: {
222 type: 'number',
223 description: '第一个数字',
224 required: true,
225 },
226 b: {
227 type: 'number',
228 description: '第二个数字',
229 required: true,
230 },
231 },
232 required: ['operation', 'a', 'b'],
233 async execute(params) {
234 const { operation, a, b } = params;
235
236 try {
237 let result: number;
238
239 switch (operation) {
240 case 'add':
241 result = a + b;
242 break;
243 case 'subtract':
244 result = a - b;
245 break;
246 case 'multiply':
247 result = a * b;
248 break;
249 case 'divide':
250 if (b === 0) {
251 return {
252 success: false,
253 error: '除数不能为零',
254 };
255 }
256 result = a / b;
257 break;
258 default:
259 return {

Callers 1

mainFunction · 0.85

Calls 4

createToolManagerFunction · 0.85
logMethod · 0.45
registerToolMethod · 0.45
callToolMethod · 0.45

Tested by

no test coverage detected