( content: MCPToolResult, )
| 149 | } |
| 150 | |
| 151 | export 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 | |
| 180 | export async function truncateMcpContent( |
| 181 | content: MCPToolResult, |
no test coverage detected