11import { z , type ZodRawShape , type ZodNever } from "zod" ;
22import type { McpServer , ToolCallback } from "@modelcontextprotocol/sdk/server/mcp.js" ;
3- import type { CallToolResult } from "@modelcontextprotocol/sdk/types.js" ;
3+ import type { CallToolResult , Tool } from "@modelcontextprotocol/sdk/types.js" ;
44import { Session } from "../session.js" ;
55import logger from "../logger.js" ;
66import { mongoLogId } from "mongodb-log-writer" ;
@@ -38,24 +38,25 @@ export abstract class ToolBase {
3838 * @param result - Whether the command succeeded or failed
3939 * @param error - Optional error if the command failed
4040 */
41- private async emitToolEvent ( startTime : number , result : TelemetryResult , error ?: Error ) : Promise < void > {
41+ private async emitToolEvent ( startTime : number , result : CallToolResult ) : Promise < void > {
4242 const duration = Date . now ( ) - startTime ;
43+ logger . info (
44+ mongoLogId ( 1_000_007 ) ,
45+ "tool" ,
46+ `Tool ${ this . name } executed in ${ duration } ms with result: ${ result } `
47+ ) ;
4348 const event : ToolEvent = {
4449 timestamp : new Date ( ) . toISOString ( ) ,
4550 source : "mdbmcp" ,
4651 properties : {
4752 ...this . telemetry . getCommonProperties ( ) ,
4853 command : this . name ,
4954 category : this . category ,
55+ component : "tool" ,
5056 duration_ms : duration ,
51- result,
52- ...( error && {
53- error_type : error . name ,
54- error_code : error . message ,
55- } ) ,
57+ result : result . isError ? "failure" : "success" ,
5658 } ,
5759 } ;
58-
5960 await this . telemetry . emitEvents ( [ event ] ) ;
6061 }
6162
@@ -74,17 +75,21 @@ export abstract class ToolBase {
7475 ) ;
7576
7677 const result = await this . execute ( ...args ) ;
77- await this . emitToolEvent ( startTime , "success" ) ;
78+ await this . emitToolEvent ( startTime , result ) ;
7879 return result ;
7980 } catch ( error : unknown ) {
8081 logger . error ( mongoLogId ( 1_000_000 ) , "tool" , `Error executing ${ this . name } : ${ error as string } ` ) ;
8182
82- await this . emitToolEvent (
83- startTime ,
84- "failure" ,
85- error instanceof Error ? error : new Error ( String ( error ) )
86- ) ;
87-
83+ const toolResult : CallToolResult = {
84+ isError : true ,
85+ content : [
86+ {
87+ type : "text" ,
88+ text : `Error running ${ this . name } : ${ error instanceof Error ? error . message : String ( error ) } ` ,
89+ } ,
90+ ] ,
91+ } ;
92+ await this . emitToolEvent ( startTime , toolResult ) ;
8893 return await this . handleError ( error , args [ 0 ] as ToolArgs < typeof this . argsShape > ) ;
8994 }
9095 } ;
0 commit comments