MCPcopy Create free account
hub / github.com/Louisym/MiniCC / main

Function main

tutorials/03_tool_system.py:321–418  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

319# ============================================================
320
321def main():
322 print("=" * 60)
323 print("Tutorial 03: Tool System 工具系统演示")
324 print("=" * 60)
325
326 # --- 1. 创建工具执行器并注册工具 ---
327 # 注意这里的"链式调用":.register().register().register()
328 # 这就是"Builder 模式"的一种简单形式 —— 一行代码完成多步配置
329 executor = (
330 ToolExecutor()
331 .register(BASH_SPEC, execute_bash)
332 .register(READ_FILE_SPEC, execute_read_file)
333 .register(WRITE_FILE_SPEC, execute_write_file)
334 .register(GREP_SPEC, execute_grep)
335 )
336
337 print(f"\n已注册 {len(executor.get_specs())} 个工具:")
338 for spec in executor.get_specs():
339 print(f" - {spec.name}: {spec.description} [权限: {spec.required_permission}]")
340
341 # --- 2. 通过统一接口执行工具 ---
342 print("\n--- 执行工具演示 ---")
343
344 # 执行 bash 工具
345 print("\n[1] 执行 bash: echo hello")
346 output, is_error = executor.execute("bash", '{"command": "echo hello"}')
347 print(f" 结果: {output}")
348 print(f" 出错: {is_error}")
349
350 # 执行 read_file 工具(读取当前教程文件的前 3 行)
351 print("\n[2] 执行 read_file: 读取本文件前 3 行")
352 this_file = os.path.abspath(__file__)
353 output, is_error = executor.execute(
354 "read_file",
355 json.dumps({"path": this_file, "limit": 3}),
356 )
357 print(f" 结果:\n{output}")
358
359 # 执行 write_file 工具
360 print("\n[3] 执行 write_file: 写一个临时文件")
361 tmp = os.path.join(tempfile.gettempdir(), "tutorial_test.txt")
362 output, is_error = executor.execute(
363 "write_file",
364 json.dumps({"path": tmp, "content": "Hello from Tutorial 03!\n"}),
365 )
366 print(f" 结果: {output}")
367
368 # 执行一个不存在的工具
369 print("\n[4] 执行未知工具: unknown_tool")
370 output, is_error = executor.execute("unknown_tool", '{}')
371 print(f" 结果: {output}")
372 print(f" 出错: {is_error}")
373
374 # 清理
375 if os.path.exists(tmp):
376 os.remove(tmp)
377
378 # --- 3. 查看工具的 input_schema ---

Callers 1

03_tool_system.pyFile · 0.70

Calls 5

get_specsMethod · 0.80
get_specMethod · 0.80
ToolExecutorClass · 0.70
registerMethod · 0.45
executeMethod · 0.45

Tested by

no test coverage detected