Skip to content

Commit acf25ef

Browse files
committed
handle errors in telemetry middleware
1 parent dcd2f42 commit acf25ef

File tree

3 files changed

+34
-23
lines changed

3 files changed

+34
-23
lines changed

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ readme = "README.md"
66
requires-python = ">=3.10"
77
dependencies = [
88
"acryl-datahub==1.1.0.5rc11",
9-
"fastmcp==2.10.4",
9+
"fastmcp==2.10.5",
1010
"jmespath~=1.0.1",
1111
]
1212
license = "Apache-2.0"

src/mcp_server_datahub/__main__.py

Lines changed: 29 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import logging
2+
from typing import Any
23

34
import click
45
import mcp.types as mt
@@ -29,30 +30,39 @@ async def on_call_tool(
2930
context: MiddlewareContext[mt.CallToolRequestParams],
3031
call_next: CallNext[mt.CallToolRequestParams, mt.CallToolResult],
3132
) -> mt.CallToolResult:
33+
telemetry_data: dict[str, Any] = {}
3234
with PerfTimer() as timer:
33-
result = await call_next(context)
34-
35-
try:
36-
telemetry.telemetry_instance.ping(
37-
"mcp-server-tool-call",
38-
{
39-
"tool": context.message.name,
40-
"source": context.source,
41-
"type": context.type,
42-
"method": context.method,
43-
"duration_seconds": timer.elapsed_seconds(),
44-
"tool_result_is_error": result.isError,
45-
"tool_result_length": sum(
35+
telemetry_data = {
36+
"tool": context.message.name,
37+
"source": context.source,
38+
"type": context.type,
39+
"method": context.method,
40+
}
41+
try:
42+
result = await call_next(context)
43+
44+
# BUG: The FastMCP type annotations seem to be incorrect.
45+
# This method typically returns fastmcp.tools.tool.ToolResult.
46+
if isinstance(result, mt.CallToolResult):
47+
telemetry_data["tool_result_is_error"] = result.isError
48+
telemetry_data["tool_result_length"] = (
49+
sum(
4650
len(block.text)
4751
for block in result.content
4852
if isinstance(block, mt.TextContent)
4953
),
50-
},
51-
)
52-
except Exception:
53-
logger.info("Error generating telemetry", exc_info=True)
54+
)
55+
56+
return result
5457

55-
return result
58+
except Exception as e:
59+
telemetry_data["tool_call_error"] = e.__class__.__name__
60+
raise
61+
finally:
62+
telemetry_data["duration_seconds"] = timer.elapsed_seconds()
63+
telemetry.telemetry_instance.ping(
64+
"mcp-server-tool-call", telemetry_data
65+
)
5666

5767

5868
@click.command()
@@ -77,6 +87,7 @@ def main(transport: Literal["stdio", "sse", "http"], debug: bool) -> None:
7787
)
7888

7989
if debug:
90+
# logging.getLogger("datahub").setLevel(logging.DEBUG)
8091
mcp.add_middleware(LoggingMiddleware(include_payloads=True))
8192
mcp.add_middleware(TelemetryMiddleware())
8293

uv.lock

Lines changed: 4 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)