@salesforce/mcp 0.27.1 → 0.28.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -46,5 +46,16 @@ export declare class SfMcpServer extends McpServer implements ToolMethodSignatur
46
46
  outputSchema?: OutputArgs;
47
47
  annotations?: ToolAnnotations;
48
48
  }, cb: ToolCallback<InputArgs>): RegisteredTool;
49
+ /**
50
+ * Calculates the total character count from tool result content and structured output.
51
+ * Used for token usage. Accounts for both:
52
+ * - content: text (and other) content items
53
+ * - structuredContent: structured tool output when the tool defines an outputSchema
54
+ *
55
+ * @see https://modelcontextprotocol.io/specification/2025-11-25/server/tools#output-schema
56
+ * @param result - The CallToolResult from tool execution
57
+ * @returns Total character count across text content and structured content
58
+ */
59
+ private calculateResponseCharCount;
49
60
  }
50
61
  export {};
@@ -86,6 +86,14 @@ export class SfMcpServer extends McpServer {
86
86
  this.logger.debug(`Tool ${name} completed in ${runtimeMs}ms`);
87
87
  if (result.isError)
88
88
  this.logger.debug(`Tool ${name} errored`);
89
+ // Calculate response character count for token usage (never let telemetry instrumentation fail a tool call)
90
+ let responseCharCount = 0;
91
+ try {
92
+ responseCharCount = this.calculateResponseCharCount(result);
93
+ }
94
+ catch (err) {
95
+ // never let telemetry instrumentation fail a tool call
96
+ }
89
97
  this.telemetry?.sendEvent('TOOL_CALLED', {
90
98
  name,
91
99
  runtimeMs,
@@ -96,6 +104,7 @@ export class SfMcpServer extends McpServer {
96
104
  //
97
105
  // https://modelcontextprotocol.io/specification/2025-06-18/schema#calltoolresult
98
106
  isError: result.isError ?? false,
107
+ responseCharCount: responseCharCount.toString(),
99
108
  });
100
109
  this.telemetry?.sendPdpEvent({
101
110
  eventName: 'salesforceMcp.executed',
@@ -107,5 +116,35 @@ export class SfMcpServer extends McpServer {
107
116
  const tool = super.registerTool(name, config, wrappedCb);
108
117
  return tool;
109
118
  }
119
+ /**
120
+ * Calculates the total character count from tool result content and structured output.
121
+ * Used for token usage. Accounts for both:
122
+ * - content: text (and other) content items
123
+ * - structuredContent: structured tool output when the tool defines an outputSchema
124
+ *
125
+ * @see https://modelcontextprotocol.io/specification/2025-11-25/server/tools#output-schema
126
+ * @param result - The CallToolResult from tool execution
127
+ * @returns Total character count across text content and structured content
128
+ */
129
+ calculateResponseCharCount(result) {
130
+ let total = 0;
131
+ // Plain text (and other) content items
132
+ if (result.content && Array.isArray(result.content)) {
133
+ total += result.content
134
+ .filter((item) => item.type === 'text')
135
+ .reduce((sum, item) => sum + item.text.length, 0);
136
+ }
137
+ // Structured content (JSON object per outputSchema)
138
+ const structured = result.structuredContent;
139
+ if (structured !== undefined && structured !== null && typeof structured === 'object') {
140
+ try {
141
+ total += JSON.stringify(structured).length;
142
+ }
143
+ catch {
144
+ // ignore serialization errors
145
+ }
146
+ }
147
+ return total;
148
+ }
110
149
  }
111
150
  //# sourceMappingURL=sf-mcp-server.js.map