MCPcopy Create free account
hub / github.com/UsefulSoftwareCo/executor / scoreToolMatch

Function scoreToolMatch

packages/core/execution/src/tool-invoker.ts:588–655  ·  view source on GitHub ↗
(tool: SearchableTool, query: string)

Source from the content-addressed store, hash-verified

586};
587
588const scoreToolMatch = (tool: SearchableTool, query: string): ToolDiscoveryResult | null => {
589 const normalizedQuery = normalizeSearchText(query);
590 const queryTokens = tokenizeSearchText(query);
591
592 if (normalizedQuery.length === 0 || queryTokens.length === 0) {
593 return null;
594 }
595
596 const path = prepareField(tool.path);
597 const integration = prepareField(tool.integration);
598 const name = prepareField(tool.name);
599 const description = prepareField(tool.description);
600
601 const fieldScores = [
602 scorePreparedField(normalizedQuery, queryTokens, path, SEARCH_FIELD_WEIGHTS.path),
603 scorePreparedField(normalizedQuery, queryTokens, integration, SEARCH_FIELD_WEIGHTS.integration),
604 scorePreparedField(normalizedQuery, queryTokens, name, SEARCH_FIELD_WEIGHTS.name),
605 scorePreparedField(normalizedQuery, queryTokens, description, SEARCH_FIELD_WEIGHTS.description),
606 ];
607
608 const matchedTokens = new Set<string>();
609 let score = 0;
610 let exactPhraseMatch = false;
611
612 for (const fieldScore of fieldScores) {
613 score += fieldScore.score;
614 exactPhraseMatch ||= fieldScore.exactPhraseMatch;
615 for (const token of fieldScore.matchedTokens) {
616 matchedTokens.add(token);
617 }
618 }
619
620 if (matchedTokens.size === 0) {
621 return null;
622 }
623
624 const coverage = matchedTokens.size / queryTokens.length;
625 const minimumCoverage = queryTokens.length <= 2 ? 1 : 0.6;
626
627 if (coverage < minimumCoverage && !exactPhraseMatch) {
628 return null;
629 }
630
631 if (coverage === 1) {
632 score += 25;
633 } else {
634 score += Math.round(coverage * 10);
635 }
636
637 if (path.tokens[0] === queryTokens[0] || name.tokens[0] === queryTokens[0]) {
638 score += 8;
639 }
640
641 if (
642 normalizeSearchText(tool.path) === normalizedQuery ||
643 normalizeSearchText(tool.name) === normalizedQuery
644 ) {
645 score += 20;

Callers 1

tool-invoker.tsFile · 0.85

Calls 4

normalizeSearchTextFunction · 0.85
tokenizeSearchTextFunction · 0.85
prepareFieldFunction · 0.85
scorePreparedFieldFunction · 0.85

Tested by

no test coverage detected