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

Function scoreToolMatch

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

Source from the content-addressed store, hash-verified

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