( tools: Tools, getToolPermissionContext: () => Promise<ToolPermissionContext>, agentInfo: AgentDefinitionsResult | null, model: string, messages?: Message[], )
| 614 | } |
| 615 | |
| 616 | export async function countMcpToolTokens( |
| 617 | tools: Tools, |
| 618 | getToolPermissionContext: () => Promise<ToolPermissionContext>, |
| 619 | agentInfo: AgentDefinitionsResult | null, |
| 620 | model: string, |
| 621 | messages?: Message[], |
| 622 | ): Promise<{ |
| 623 | mcpToolTokens: number |
| 624 | mcpToolDetails: McpTool[] |
| 625 | deferredToolTokens: number |
| 626 | loadedMcpToolNames: Set<string> |
| 627 | }> { |
| 628 | const mcpTools = tools.filter(tool => tool.isMcp) |
| 629 | const mcpToolDetails: McpTool[] = [] |
| 630 | // Single bulk API call for all MCP tools (instead of N individual calls) |
| 631 | const totalTokensRaw = await countToolDefinitionTokens( |
| 632 | mcpTools, |
| 633 | getToolPermissionContext, |
| 634 | agentInfo, |
| 635 | model, |
| 636 | ) |
| 637 | // Subtract the single overhead since we made one bulk call |
| 638 | const totalTokens = Math.max( |
| 639 | 0, |
| 640 | (totalTokensRaw || 0) - TOOL_TOKEN_COUNT_OVERHEAD, |
| 641 | ) |
| 642 | |
| 643 | // Estimate per-tool proportions for display using local estimation. |
| 644 | // Include name + description + input schema to match what toolToAPISchema |
| 645 | // sends — otherwise tools with similar schemas but different descriptions |
| 646 | // get identical counts (MCP tools share the same base Zod inputSchema). |
| 647 | const estimates = await Promise.all( |
| 648 | mcpTools.map(async t => |
| 649 | roughTokenCountEstimation( |
| 650 | jsonStringify({ |
| 651 | name: t.name, |
| 652 | description: await t.prompt({ |
| 653 | getToolPermissionContext, |
| 654 | tools, |
| 655 | agents: agentInfo?.activeAgents ?? [], |
| 656 | }), |
| 657 | input_schema: t.inputJSONSchema ?? {}, |
| 658 | }), |
| 659 | ), |
| 660 | ), |
| 661 | ) |
| 662 | const estimateTotal = estimates.reduce((s, e) => s + e, 0) || 1 |
| 663 | const mcpToolTokensByTool = estimates.map(e => |
| 664 | Math.round((e / estimateTotal) * totalTokens), |
| 665 | ) |
| 666 | |
| 667 | // Check if tool search is enabled - if so, MCP tools are deferred |
| 668 | // isToolSearchEnabled handles threshold calculation internally for TstAuto mode |
| 669 | const { isToolSearchEnabled } = await import('./toolSearch.js') |
| 670 | const { isDeferredTool } = await import('../tools/ToolSearchTool/prompt.js') |
| 671 | |
| 672 | const isDeferred = await isToolSearchEnabled( |
| 673 | model, |
no test coverage detected