|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +module RobotLab |
| 4 | + module MCP |
| 5 | + # Selects relevant MCP servers for a given user query using TF cosine |
| 6 | + # similarity between the query and each server's topic text |
| 7 | + # (name + description). |
| 8 | + # |
| 9 | + # This is used as a fallback mechanism when a robot has many MCP servers |
| 10 | + # configured but only some are relevant to a particular user message. |
| 11 | + # Instead of connecting to all servers upfront, the robot can enable |
| 12 | + # discovery so only the semantically matching servers are connected. |
| 13 | + # |
| 14 | + # == Usage |
| 15 | + # |
| 16 | + # robot = RobotLab.build( |
| 17 | + # name: "assistant", |
| 18 | + # mcp_discovery: true, |
| 19 | + # mcp: [ |
| 20 | + # { |
| 21 | + # name: "filesystem", |
| 22 | + # description: "Read, write, and search local files and directories", |
| 23 | + # transport: { type: "stdio", command: "mcp-server-fs" } |
| 24 | + # }, |
| 25 | + # { |
| 26 | + # name: "github", |
| 27 | + # description: "GitHub repos, issues, pull requests, code search", |
| 28 | + # transport: { type: "stdio", command: "mcp-server-github" } |
| 29 | + # }, |
| 30 | + # { |
| 31 | + # name: "brew", |
| 32 | + # description: "Install, update, and manage macOS packages via Homebrew", |
| 33 | + # transport: { type: "stdio", command: "mcp-server-brew" } |
| 34 | + # } |
| 35 | + # ] |
| 36 | + # ) |
| 37 | + # |
| 38 | + # # Discovery connects only the :brew server for this query: |
| 39 | + # robot.run("install imagemagick") |
| 40 | + # |
| 41 | + # == Fallback Behaviour |
| 42 | + # |
| 43 | + # The full server list is returned unchanged when: |
| 44 | + # - No server has a +:description+ field |
| 45 | + # - The 'classifier' gem is unavailable |
| 46 | + # - The query is blank |
| 47 | + # - No server scores at or above +threshold+ (minimum relevance) |
| 48 | + # |
| 49 | + # @api private |
| 50 | + module ServerDiscovery |
| 51 | + # Minimum cosine similarity score for a server to be considered relevant. |
| 52 | + # Low by design — server descriptions are short, so scores are naturally |
| 53 | + # low even for on-topic queries. |
| 54 | + DEFAULT_THRESHOLD = 0.05 |
| 55 | + |
| 56 | + # Select MCP servers relevant to the given query. |
| 57 | + # |
| 58 | + # @param query [String] user message or intent |
| 59 | + # @param from [Array<Hash, MCP::Server>] candidate server configs |
| 60 | + # @param threshold [Float] minimum cosine score (default 0.05) |
| 61 | + # @return [Array<Hash, MCP::Server>] matching servers, or +from+ as |
| 62 | + # fallback when no match is found |
| 63 | + def self.select(query, from:, threshold: DEFAULT_THRESHOLD) |
| 64 | + return from if from.empty? |
| 65 | + return from if query.to_s.strip.empty? |
| 66 | + return from unless any_descriptions?(from) |
| 67 | + |
| 68 | + TextAnalysis.require_classifier! |
| 69 | + |
| 70 | + scored = from.map { |server| [server, score(query, server)] } |
| 71 | + matches = scored.select { |_, s| s >= threshold }.map(&:first) |
| 72 | + |
| 73 | + matches.empty? ? from : matches |
| 74 | + rescue DependencyError |
| 75 | + # Classifier gem not available — connect to all servers |
| 76 | + from |
| 77 | + end |
| 78 | + |
| 79 | + private |
| 80 | + |
| 81 | + # @param servers [Array<Hash, MCP::Server>] |
| 82 | + def self.any_descriptions?(servers) |
| 83 | + servers.any? { |s| !description_for(s).empty? } |
| 84 | + end |
| 85 | + |
| 86 | + # Build the topic string used for similarity scoring: name + description. |
| 87 | + # |
| 88 | + # @param server [Hash, MCP::Server] |
| 89 | + # @return [String] |
| 90 | + def self.topic_text(server) |
| 91 | + name = server.is_a?(Hash) ? server[:name].to_s : server.name.to_s |
| 92 | + "#{name} #{description_for(server)}".strip |
| 93 | + end |
| 94 | + |
| 95 | + # @param server [Hash, MCP::Server] |
| 96 | + # @return [String] description or empty string |
| 97 | + def self.description_for(server) |
| 98 | + desc = server.is_a?(Hash) ? server[:description] : server.respond_to?(:description) && server.description |
| 99 | + desc.to_s.strip |
| 100 | + end |
| 101 | + |
| 102 | + # @param query [String] |
| 103 | + # @param server [Hash, MCP::Server] |
| 104 | + # @return [Float] cosine similarity in [0.0, 1.0] |
| 105 | + def self.score(query, server) |
| 106 | + TextAnalysis.tf_cosine_similarity(query, topic_text(server)) |
| 107 | + end |
| 108 | + end |
| 109 | + end |
| 110 | +end |
0 commit comments