spamming the house with try catches

This commit is contained in:
lda
2026-04-30 00:36:34 +07:00 Verified
parent 67a3e6a64c
commit 449e61b0d5
6 changed files with 167 additions and 47 deletions
+10 -2
View File
@@ -4,7 +4,7 @@ import json
import os
from dataclasses import asdict
from pathlib import Path
from typing import Any, Literal, cast
from typing import Any, Literal
from mcp.server.fastmcp import FastMCP
@@ -57,7 +57,15 @@ def create_broker_server(service: WfMcpService) -> FastMCP:
@server.tool()
async def refresh_connection_catalog(connection_id: str) -> dict[str, Any]:
await service.refresh_connection_catalog(connection_id)
try:
await service.refresh_connection_catalog(connection_id)
except Exception as exc:
return {
"connection_id": connection_id,
"refreshed": False,
"error_type": type(exc).__name__,
"error": str(exc),
}
snapshot = service.get_connection_snapshot(connection_id)
if snapshot is None:
return {"connection_id": connection_id, "refreshed": False}
+43 -6
View File
@@ -52,6 +52,40 @@ def _json_dump(data: Any) -> None:
print(json.dumps(data, indent=2))
async def _refresh_all(service, connection_id: str | None) -> list[dict[str, Any]]:
target_ids = (
[connection_id]
if connection_id is not None
else [connection.id for connection in service.connections.list_enabled()]
)
results: list[dict[str, Any]] = []
for target_id in target_ids:
try:
await service.refresh_connection_catalog(target_id)
snapshot = service.get_connection_snapshot(target_id)
results.append(
{
"connection_id": target_id,
"refreshed": snapshot is not None,
"node_count": 0 if snapshot is None else len(snapshot.nodes),
"resource_count": 0
if snapshot is None
else len(snapshot.resources),
"prompt_count": 0 if snapshot is None else len(snapshot.prompts),
}
)
except Exception as exc:
results.append(
{
"connection_id": target_id,
"refreshed": False,
"error_type": type(exc).__name__,
"error": str(exc),
}
)
return results
def main(argv: list[str] | None = None) -> int:
parser = build_parser()
args = parser.parse_args(argv)
@@ -82,12 +116,15 @@ def main(argv: list[str] | None = None) -> int:
return 0
if args.command == "refresh":
if args.connection_id:
asyncio.run(service.refresh_connection_catalog(args.connection_id))
else:
for connection in service.connections.list_enabled():
asyncio.run(service.refresh_connection_catalog(connection.id))
_json_dump(service.get_catalog().as_payload())
results = asyncio.run(_refresh_all(service, args.connection_id))
_json_dump(
{
"results": results,
"catalog": service.get_catalog().as_payload(),
}
)
if any(not result["refreshed"] for result in results):
return 1
return 0
parser.error(f"unknown command {args.command!r}")
+50 -37
View File
@@ -292,44 +292,57 @@ class WfMcpService:
payload={"server": connection.server},
)
)
capabilities = await discover_connection_capabilities(
connection=connection,
auth=auth,
adapter=adapter,
)
specs = specs_from_discovered_tools(
connection=connection,
auth=auth,
adapter=adapter,
tools=capabilities.tools,
emit_event=self._record_event,
)
self.register_specs(
connection_id,
*specs,
max_age_seconds=max_age_seconds,
)
snapshot = snapshot_from_specs(
connection_id,
specs=self.specs_by_connection.get(connection_id, {}),
resources=capabilities.resources,
prompts=capabilities.prompts,
metadata=capabilities.metadata,
fetched_at_epoch_ms=int(time.time() * 1000),
max_age_seconds=max_age_seconds or self.default_catalog_max_age_seconds,
)
self.store.save_catalog(snapshot)
self._record_event(
make_event(
"catalog_refresh_completed",
connection_id=connection_id,
payload={
"node_count": len(snapshot.nodes),
"resource_count": len(snapshot.resources),
"prompt_count": len(snapshot.prompts),
},
try:
capabilities = await discover_connection_capabilities(
connection=connection,
auth=auth,
adapter=adapter,
)
)
specs = specs_from_discovered_tools(
connection=connection,
auth=auth,
adapter=adapter,
tools=capabilities.tools,
emit_event=self._record_event,
)
self.register_specs(
connection_id,
*specs,
max_age_seconds=max_age_seconds,
)
snapshot = snapshot_from_specs(
connection_id,
specs=self.specs_by_connection.get(connection_id, {}),
resources=capabilities.resources,
prompts=capabilities.prompts,
metadata=capabilities.metadata,
fetched_at_epoch_ms=int(time.time() * 1000),
max_age_seconds=max_age_seconds or self.default_catalog_max_age_seconds,
)
self.store.save_catalog(snapshot)
self._record_event(
make_event(
"catalog_refresh_completed",
connection_id=connection_id,
payload={
"node_count": len(snapshot.nodes),
"resource_count": len(snapshot.resources),
"prompt_count": len(snapshot.prompts),
},
)
)
except Exception as exc:
self._record_event(
make_event(
"catalog_refresh_failed",
connection_id=connection_id,
payload={
"error_type": type(exc).__name__,
"error": str(exc),
},
)
)
raise
def compile_plan(self, plan: RawWorkflowPlan) -> Workflow:
node_defs: dict[str, Any] = {}