MCPcopy
hub / github.com/codeaashu/claude-code / mcpContentNeedsTruncation

Function mcpContentNeedsTruncation

src/utils/mcpValidation.ts:151–178  ·  view source on GitHub ↗
(
  content: MCPToolResult,
)

Source from the content-addressed store, hash-verified

149}
150
151export async function mcpContentNeedsTruncation(
152 content: MCPToolResult,
153): Promise<boolean> {
154 if (!content) return false
155
156 // Use size check as a heuristic to avoid unnecessary token counting API calls
157 const contentSizeEstimate = getContentSizeEstimate(content)
158 if (
159 contentSizeEstimate <=
160 getMaxMcpOutputTokens() * MCP_TOKEN_COUNT_THRESHOLD_FACTOR
161 ) {
162 return false
163 }
164
165 try {
166 const messages =
167 typeof content === 'string'
168 ? [{ role: 'user' as const, content }]
169 : [{ role: 'user' as const, content }]
170
171 const tokenCount = await countMessagesTokensWithAPI(messages, [])
172 return !!(tokenCount && tokenCount > getMaxMcpOutputTokens())
173 } catch (error) {
174 logError(error)
175 // Assume no truncation needed on error
176 return false
177 }
178}
179
180export async function truncateMcpContent(
181 content: MCPToolResult,

Callers 2

processMCPResultFunction · 0.85

Calls 4

getContentSizeEstimateFunction · 0.85
getMaxMcpOutputTokensFunction · 0.85
logErrorFunction · 0.70

Tested by

no test coverage detected