Skip to content

@W-21474595 - add MCP tool-level response token usage (responseCharCount)#400

Merged
iowillhoit merged 5 commits intosalesforcecli:mainfrom
smuduganti-sf:W-20995739-add-responseCharCount
Mar 17, 2026
Merged

@W-21474595 - add MCP tool-level response token usage (responseCharCount)#400
iowillhoit merged 5 commits intosalesforcecli:mainfrom
smuduganti-sf:W-20995739-add-responseCharCount

Conversation

@smuduganti-sf
Copy link
Copy Markdown
Collaborator

@smuduganti-sf smuduganti-sf commented Mar 4, 2026

  • Added calculateResponseCharCount() to compute total character count from tool result content.
  • On each tool call, the response character count is computed and sent in the TOOL_CALLED telemetry event as responseCharCount.

What does this PR do?

@W-21474595@

What issues does this PR fix or reference?

@smuduganti-sf smuduganti-sf requested a review from a team as a code owner March 4, 2026 06:14
@smuduganti-sf smuduganti-sf changed the title @W-20995739 - add MCP tool-level response token usage (responseCharCount) @W-21474595 - add MCP tool-level response token usage (responseCharCount) Mar 6, 2026
@smuduganti-sf smuduganti-sf force-pushed the W-20995739-add-responseCharCount branch from 32cdc84 to 3b8b1ca Compare March 6, 2026 04:01
Comment thread packages/mcp/src/sf-mcp-server.ts Outdated
if (result.isError) this.logger.debug(`Tool ${name} errored`);

// Calculate response character count for token usage
const responseCharCount = this.calculateResponseCharCount(result);
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This method only considers plain text from tool responses but tools can also have structured output, see:

Image

https://modelcontextprotocol.io/specification/2025-11-25/server/tools#output-schema

I think the safe way would be to account for both since a tool seems to be able to return plain text + structured output

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think i would feel better if this entire call was wrapped in a try/catch.

I never want this to cause a tool call to fail. Example:

let responseCharCount = 0;
try {
  responseCharCount = this.calculateResponseCharCount(result);
} catch {
  // never let telemetry instrumentation fail a tool call
}

@smuduganti-sf smuduganti-sf force-pushed the W-20995739-add-responseCharCount branch from 5b62886 to c75c061 Compare March 11, 2026 05:19
@smuduganti-sf smuduganti-sf force-pushed the W-20995739-add-responseCharCount branch from 3913594 to dd36886 Compare March 17, 2026 05:57
@smuduganti-sf smuduganti-sf force-pushed the W-20995739-add-responseCharCount branch from 61fddbc to dcb1c68 Compare March 17, 2026 06:14
@iowillhoit iowillhoit merged commit 217e7ea into salesforcecli:main Mar 17, 2026
16 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants