Skip to content

Commit fd7a448

Browse files
committed
merge
2 parents 85865c5 + 9eed233 commit fd7a448

3 files changed

Lines changed: 19 additions & 9 deletions

File tree

mcp2anp/core/handlers.py

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -70,11 +70,13 @@ async def handle_fetch_doc(self, arguments: dict[str, Any]) -> dict[str, Any]:
7070
except json.JSONDecodeError:
7171
pass # 不是 JSON 内容,跳过
7272

73-
logger.info("Document fetched successfully", url=request.url, links_count=len(links))
73+
logger.info("Document fetched successfully",
74+
url=request.url, links_count=len(links))
7475
return result
7576

7677
except Exception as e:
77-
logger.error("Failed to fetch document", url=arguments.get("url"), error=str(e))
78+
logger.error("Failed to fetch document",
79+
url=arguments.get("url"), error=str(e))
7880
return {
7981
"ok": False,
8082
"error": {
@@ -98,7 +100,8 @@ async def handle_invoke_openrpc(self, arguments: dict[str, Any]) -> dict[str, An
98100

99101
# 检查 ANPCrawler 是否已初始化
100102
if self.anp_crawler is None:
101-
logger.error("ANPCrawler not initialized. Please check DID credentials.")
103+
logger.error(
104+
"ANPCrawler not initialized. Please check DID credentials.")
102105
return {
103106
"ok": False,
104107
"error": {
@@ -123,10 +126,13 @@ async def handle_invoke_openrpc(self, arguments: dict[str, Any]) -> dict[str, An
123126

124127
# 因为execute_json_rpc在调用的时候,会有一定的概率失败。主要是Endpoint错误,所以这里暂时用execute_tool_call来调用。
125128
# execute_json_rpc的通用性最好,后面在根据模型能力调整
126-
# result = await self.anp_crawler.execute_json_rpc(request.endpoint, request.method, tool_params)
127-
result = await self.anp_crawler.execute_tool_call(tool_name, tool_params)
129+
result = await self.anp_crawler.execute_json_rpc(request.endpoint, request.method, tool_params)
130+
# result = await self.anp_crawler.execute_tool_call(tool_name, tool_params)
128131

129-
logger.info("OpenRPC method invoked successfully", method=request.method)
132+
logger.info("OpenRPC method invoked successfully",
133+
method=request.method)
134+
logger.info(
135+
f"result:{json.dumps(result, indent=2, ensure_ascii=False)}")
130136
return {
131137
"ok": True,
132138
"result": result,
@@ -164,8 +170,10 @@ def initialize_anp_crawler() -> ANPCrawler | None:
164170
# 如果环境变量未设置,使用默认的公共 DID 凭证
165171
if not did_document_path or not did_private_key_path:
166172
project_root = Path(__file__).parent.parent.parent
167-
did_document_path = str(project_root / "docs" / "did_public" / "public-did-doc.json")
168-
did_private_key_path = str(project_root / "docs" / "did_public" / "public-private-key.pem")
173+
did_document_path = str(
174+
project_root / "docs" / "did_public" / "public-did-doc.json")
175+
did_private_key_path = str(
176+
project_root / "docs" / "did_public" / "public-private-key.pem")
169177
logger.info(
170178
"Using default DID credentials",
171179
did_doc=did_document_path,

mcp2anp/server.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ async def list_tools() -> list[Tool]:
4848

4949
"ANP生态的入口URL:https://agent-navigation.com/ad.json"
5050
"你可以从这个URL开始,逐步跟进并调用工具,访问ANP生态的资源和能力。"
51+
"当遇到 interfaces.url 是一个以 .json 结尾的 URL 时,你应该使用 anp.fetchDoc 继续拉取该json文档,才能获得正确的 endPoint。"
5152
),
5253
inputSchema={
5354
"type": "object",
@@ -109,6 +110,7 @@ async def call_tool(name: str, arguments: dict[str, Any]) -> Sequence[TextConten
109110
raise ValueError(f"Unknown tool: {name}")
110111

111112
# 将结果转换为字符串格式返回
113+
logger.info(f"result length: {len(json.dumps(result, indent=2, ensure_ascii=False))}")
112114
return [TextContent(type="text", text=json.dumps(result, indent=2, ensure_ascii=False))]
113115

114116
except Exception as e:

mcp2anp/server_remote.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -234,7 +234,7 @@ async def anp_fetchDoc(ctx: Context, url: str) -> str:
234234
235235
ANP生态的入口URL:https://agent-navigation.com/ad.json
236236
你可以从这个URL开始,逐步跟进并调用工具,访问ANP生态的资源和能力。
237-
237+
当遇到 interfaces.url 是一个以 .json 结尾的 URL 时,你应该使用 anp.fetchDoc 继续拉取该json文档,才能获得正确的 endPoint。
238238
239239
Args:
240240
ctx: FastMCP 上下文对象

0 commit comments

Comments
 (0)