1
1
import { z } from "zod";
2
2
import { zodToJsonSchema } from 'zod-to-json-schema';
3
3
import { Tool } from "@agentico/mcp-server";
4
- <% if (instrumentation) { % > import opentelemetry from ' @opentelemetry/api' ;< % } %>
5
4
6
5
// @todo Adapt the schema and input type to match the tool's input you want to create
7
6
const <%= toolPascalCase %> Schema = z.object({
@@ -22,34 +21,31 @@ export class <%= toolPascalCase %>Tool extends Tool {
22
21
// @todo - Implement any initialization logic here
23
22
// console.error("<%= toolPascalCase %> Tool initialized");
24
23
}
25
- execute(input: <%= toolPascalCase %> Input): Promise<any > {
24
+ async execute(input: <%= toolPascalCase %> Input): Promise<any > {
26
25
// @todo - Implement the tool logic here - this is where the tool does its work
27
26
<% if (instrumentation) { % >
28
27
// OpenTelemetry instrumentation example - @todo - adapt to your needs
29
- const tracer = opentelemetry .trace .getTracer (' Agentico-tracer' );
30
- const message = input .message .length > 20 ? ` ${ input .message .substring (0 , 20 )} ...` : input .message ;
31
- const rootSpan = tracer .startSpan (` <%= toolPascalCase %>Tool Execution - ${ message} ` );
28
+ this .otel .startSpan (` Tool Execution - ${ input .message } ` );
32
29
33
30
try {
34
31
// Start another span. In this example, the main method already started a
35
- // span, so that'll be the parent span, and this will be a child span.
36
- const ctx = opentelemetry .trace .setSpan (opentelemetry .context .active (), rootSpan);
37
32
// simulate 3 steps
38
- const step1 = tracer . startSpan (' <%= toolPascalCase %> Execution Step 1' , undefined , ctx );
39
- rootSpan . addEvent (' <%= toolPascalCase %> Execution Started - Step 1' );
33
+ const step1 = this . otel . startSpan (' Execution Step 1' );
34
+ this . otel . addEvent (' Execution Started - Step 1' , step1 );
40
35
await new Promise ((r ) => setTimeout (r, 700 ));
41
36
step1 .end ();
42
- rootSpan .addEvent (' <%= toolPascalCase %> Execution Ended - Step 1' );
43
- const step2 = tracer .startSpan (' <%= toolPascalCase %> Execution Step 2' , undefined , ctx);
37
+ // unporpose, no span for this event - see the result in the trace ;)
38
+ this .otel .addEvent (' Execution Ended - Step 1' );
39
+ const step2 = this .otel .startSpan (' Execution Step 2' );
44
40
await new Promise ((r ) => setTimeout (r, 1000 ));
45
41
step2 .end ();
46
- const step3 = tracer . startSpan (' <%= toolPascalCase %> Execution Step 3' , undefined , ctx );
42
+ const step3 = this . otel . startSpan (' Execution Step 3' );
47
43
await new Promise ((r ) => setTimeout (r, 800 ));
48
44
step3 .end ();
49
45
} catch (error) {
50
- console . error ( ' Automation failed: ' , error );
46
+ this . otel . addErrorEvent (error as Error );
51
47
} finally {
52
- rootSpan . end ();
48
+ this . otel . endSpan ();
53
49
}
54
50
< % } %>
55
51
// echoing, just return the input message
0 commit comments