MCPcopy Create free account
hub / github.com/Noumena-Network/code / calculateDeferredToolDescriptionChars

Function calculateDeferredToolDescriptionChars

src/utils/toolSearch.ts:342–367  ·  view source on GitHub ↗

* Calculate total deferred tool description size in characters. * Includes name, description text, and input schema to match what's actually sent to the API.

(
  tools: Tools,
  getToolPermissionContext: () => Promise<ToolPermissionContext>,
  agents: AgentDefinition[],
)

Source from the content-addressed store, hash-verified

340 * Includes name, description text, and input schema to match what's actually sent to the API.
341 */
342async function calculateDeferredToolDescriptionChars(
343 tools: Tools,
344 getToolPermissionContext: () => Promise<ToolPermissionContext>,
345 agents: AgentDefinition[],
346): Promise<number> {
347 const deferredTools = tools.filter(t => isDeferredTool(t))
348 if (deferredTools.length === 0) return 0
349
350 const sizes = await Promise.all(
351 deferredTools.map(async tool => {
352 const description = await tool.prompt({
353 getToolPermissionContext,
354 tools,
355 agents,
356 })
357 const inputSchema = tool.inputJSONSchema
358 ? jsonStringify(tool.inputJSONSchema)
359 : tool.inputSchema
360 ? jsonStringify(zodToJsonSchema(tool.inputSchema))
361 : ''
362 return tool.name.length + description.length + inputSchema.length
363 }),
364 )
365
366 return sizes.reduce((total, size) => total + size, 0)
367}
368
369/**
370 * Check if tool search (MCP tool deferral with tool_reference) is enabled for a specific request.

Callers 1

checkAutoThresholdFunction · 0.85

Calls 3

isDeferredToolFunction · 0.85
jsonStringifyFunction · 0.85
zodToJsonSchemaFunction · 0.85

Tested by

no test coverage detected