-
Notifications
You must be signed in to change notification settings - Fork 54
Expand file tree
/
Copy pathsimple_amap_example.py
More file actions
77 lines (58 loc) · 2.17 KB
/
Copy pathsimple_amap_example.py
File metadata and controls
77 lines (58 loc) · 2.17 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
#!/usr/bin/env python3
"""
ANPCrawler简单示例 - AMAP服务
这是一个简化的示例,展示如何快速使用ANPCrawler获取和调用AMAP服务。
"""
import asyncio
import json
import sys
from pathlib import Path
# 添加项目路径
project_root = Path(__file__).parent.parent.parent.parent
sys.path.insert(0, str(project_root))
from anp.anp_crawler.anp_crawler import ANPCrawler
async def simple_amap_example():
"""简单的AMAP服务示例"""
# 1. 初始化爬虫
crawler = ANPCrawler(
did_document_path=str(project_root / "docs" / "did_public" / "public-did-doc.json"),
private_key_path=str(project_root / "docs" / "did_public" / "public-private-key.pem")
)
# 2. 目标URL
url = "https://agent-connect.ai/mcp/agents/amap/ad.json"
print("步骤1: 获取AMAP代理描述文档")
print("-" * 40)
# 3. 获取代理描述
content_json, interfaces_list = await crawler.fetch_text(url)
# 4. 打印ad.json内容
print("代理描述文档内容:")
try:
parsed_content = json.loads(content_json["content"])
print(json.dumps(parsed_content, indent=2, ensure_ascii=False))
except:
print(content_json["content"])
print(f"\n步骤2: 发现的接口数量: {len(interfaces_list)}")
print("-" * 40)
# 5. 显示可用的工具
tools = crawler.list_available_tools()
print(f"可用工具: {tools}")
# 6. 如果有工具可用,尝试调用
if tools:
tool_name = tools[0]
print(f"\n步骤3: 尝试调用工具 '{tool_name}'")
print("-" * 40)
# 示例参数 (需要根据实际接口调整)
test_arguments = {
"query": "天安门广场"
}
try:
result = await crawler.execute_tool_call(tool_name, test_arguments)
print("调用结果:")
print(json.dumps(result, indent=2, ensure_ascii=False))
except Exception as e:
print(f"调用失败: {e}")
print("\n会话信息:")
print(f"- 访问的URL: {crawler.get_visited_urls()}")
print(f"- 缓存条目数: {crawler.get_cache_size()}")
if __name__ == "__main__":
asyncio.run(simple_amap_example())