-
Notifications
You must be signed in to change notification settings - Fork 0
Resource discovery DX: did-you-mean, relationship targets, session-no… #3
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,75 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| begin | ||
| require "did_you_mean" | ||
| rescue LoadError | ||
| nil | ||
| end | ||
|
|
||
| # Builds the McpToolkit::Registry::UnknownResource message from a bad name and the | ||
| # registered resource names: it states the bad name, then (a) suggests the nearest | ||
| # registered name(s) for a near-miss — via Ruby's stdlib DidYouMean spell checker | ||
| # when available, else a dependency-free prefix/substring + edit-distance fallback — | ||
| # and (b), when the catalog is short, lists them all, so a caller (typically an MCP | ||
| # agent that guessed a name) can self-correct without another round-trip to the | ||
| # `resources` tool. | ||
| class McpToolkit::UnknownResourceMessage | ||
| FULL_LIST_MAX = 10 | ||
| MAX_SUGGESTIONS = 3 | ||
|
|
||
| def initialize(name, resource_names) | ||
| @name = name | ||
| @resource_names = resource_names | ||
| end | ||
|
|
||
| def build | ||
| message = "unknown resource: #{@name.inspect}" | ||
| return message if @resource_names.empty? | ||
|
|
||
| suggestions = suggestions_for(@name.to_s, @resource_names) | ||
| message += ". Did you mean #{quote_join(suggestions)}?" if suggestions.any? | ||
| message += " Registered resources: #{quote_join(@resource_names.sort)}." if @resource_names.size <= FULL_LIST_MAX | ||
| message | ||
| end | ||
|
|
||
| private | ||
|
|
||
| def suggestions_for(name, names) | ||
| if defined?(DidYouMean::SpellChecker) | ||
| Array(DidYouMean::SpellChecker.new(dictionary: names).correct(name)).first(MAX_SUGGESTIONS) | ||
| else | ||
| fallback_suggestions(name, names) | ||
| end | ||
| end | ||
|
|
||
| def fallback_suggestions(name, names) | ||
| target = name.downcase | ||
| matches = names.select { |candidate| near_miss?(candidate.downcase, target) } | ||
| matches.sort_by { |candidate| levenshtein(candidate.downcase, target) }.first(MAX_SUGGESTIONS) | ||
| end | ||
|
|
||
| def near_miss?(candidate, target) | ||
| candidate.start_with?(target) || target.start_with?(candidate) || | ||
| candidate.include?(target) || levenshtein(candidate, target) <= 2 | ||
| end | ||
|
|
||
| def levenshtein(source, target) | ||
| return target.length if source.empty? | ||
| return source.length if target.empty? | ||
|
|
||
| previous = (0..target.length).to_a | ||
| source.each_char.with_index do |source_char, row| | ||
| current = [row + 1] | ||
| target.each_char.with_index do |target_char, col| | ||
| cost = source_char == target_char ? 0 : 1 | ||
| current << [previous[col + 1] + 1, current[col] + 1, previous[col] + cost].min | ||
| end | ||
| previous = current | ||
| end | ||
| previous.last | ||
| end | ||
|
|
||
| def quote_join(names) | ||
| names.map { |name| "\"#{name}\"" }.join(", ") | ||
| end | ||
| end | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,5 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| module McpToolkit | ||
| VERSION = "0.2.0" | ||
| VERSION = "0.3.0" | ||
| end |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
some basic test coverage would be useful