-
Notifications
You must be signed in to change notification settings - Fork 2
feat: allow filtering exposed tools #4
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
2 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,127 @@ | ||
| /** | ||
| * Tests for tool filtering utilities. | ||
| */ | ||
|
|
||
| import { assertEquals } from "@std/assert"; | ||
| import { shouldIncludeTool, type ToolFilters } from "./tool-filter.ts"; | ||
|
|
||
| Deno.test("shouldIncludeTool should include all tools when no filters", () => { | ||
| const filters: ToolFilters = {}; | ||
|
|
||
| assertEquals(shouldIncludeTool("any_tool", filters), true); | ||
| assertEquals(shouldIncludeTool("another_tool", filters), true); | ||
| assertEquals(shouldIncludeTool("delete_something", filters), true); | ||
| }); | ||
|
|
||
| Deno.test("shouldIncludeTool should match exact tool names in includeTools", () => { | ||
| const filters: ToolFilters = { | ||
| includeTools: new Set(["create_issue", "list_repos"]), | ||
| }; | ||
|
|
||
| assertEquals(shouldIncludeTool("create_issue", filters), true); | ||
| assertEquals(shouldIncludeTool("list_repos", filters), true); | ||
| assertEquals(shouldIncludeTool("delete_repo", filters), false); | ||
| assertEquals(shouldIncludeTool("get_issue", filters), false); | ||
| }); | ||
|
|
||
| Deno.test("shouldIncludeTool should match multiple exact tool names in includeTools", () => { | ||
| const filters: ToolFilters = { | ||
| includeTools: new Set(["get_issue", "get_repo", "list_repos"]), | ||
| }; | ||
|
|
||
| assertEquals(shouldIncludeTool("get_issue", filters), true); | ||
| assertEquals(shouldIncludeTool("get_repo", filters), true); | ||
| assertEquals(shouldIncludeTool("list_repos", filters), true); | ||
| assertEquals(shouldIncludeTool("create_issue", filters), false); | ||
| assertEquals(shouldIncludeTool("delete_repo", filters), false); | ||
| }); | ||
|
|
||
| Deno.test("shouldIncludeTool should exclude exact tool names in excludeTools", () => { | ||
| const filters: ToolFilters = { | ||
| excludeTools: new Set(["delete_repo", "remove_file"]), | ||
| }; | ||
|
|
||
| assertEquals(shouldIncludeTool("get_issue", filters), true); | ||
| assertEquals(shouldIncludeTool("create_issue", filters), true); | ||
| assertEquals(shouldIncludeTool("delete_repo", filters), false); | ||
| assertEquals(shouldIncludeTool("remove_file", filters), false); | ||
| }); | ||
|
|
||
| Deno.test("shouldIncludeTool should exclude multiple exact tool names in excludeTools", () => { | ||
| const filters: ToolFilters = { | ||
| excludeTools: new Set(["delete_repo", "delete_issue", "remove_file"]), | ||
| }; | ||
|
|
||
| assertEquals(shouldIncludeTool("get_issue", filters), true); | ||
| assertEquals(shouldIncludeTool("create_issue", filters), true); | ||
| assertEquals(shouldIncludeTool("delete_repo", filters), false); | ||
| assertEquals(shouldIncludeTool("delete_issue", filters), false); | ||
| assertEquals(shouldIncludeTool("remove_file", filters), false); | ||
| }); | ||
|
|
||
| Deno.test("shouldIncludeTool should apply both include and exclude filters", () => { | ||
| const filters: ToolFilters = { | ||
| includeTools: new Set(["get_issue", "create_issue", "create_repo", "delete_issue"]), | ||
| excludeTools: new Set(["delete_issue"]), | ||
| }; | ||
|
|
||
| // In include list, not excluded | ||
| assertEquals(shouldIncludeTool("get_issue", filters), true); | ||
| assertEquals(shouldIncludeTool("create_issue", filters), true); | ||
| assertEquals(shouldIncludeTool("create_repo", filters), true); | ||
|
|
||
| // In include list but also excluded (exclude wins) | ||
| assertEquals(shouldIncludeTool("delete_issue", filters), false); | ||
|
|
||
| // Not in include list | ||
| assertEquals(shouldIncludeTool("list_users", filters), false); | ||
| assertEquals(shouldIncludeTool("get_user", filters), false); | ||
| }); | ||
|
|
||
| Deno.test("shouldIncludeTool should work with include filters", () => { | ||
| const filters: ToolFilters = { | ||
| includeTools: new Set(["get_user", "get_public_data"]), | ||
| excludeTools: new Set(["get_private_key"]), | ||
| }; | ||
|
|
||
| assertEquals(shouldIncludeTool("get_user", filters), true); | ||
| assertEquals(shouldIncludeTool("get_public_data", filters), true); | ||
| assertEquals(shouldIncludeTool("get_private_key", filters), false); | ||
| assertEquals(shouldIncludeTool("create_user", filters), false); | ||
| }); | ||
|
|
||
| Deno.test("shouldIncludeTool should work with exclude filters", () => { | ||
| const filters: ToolFilters = { | ||
| excludeTools: new Set(["delete_dangerous", "get_internal"]), | ||
| }; | ||
|
|
||
| assertEquals(shouldIncludeTool("get_user", filters), true); | ||
| assertEquals(shouldIncludeTool("create_user", filters), true); | ||
| assertEquals(shouldIncludeTool("delete_dangerous", filters), false); | ||
| assertEquals(shouldIncludeTool("get_internal", filters), false); | ||
| }); | ||
|
|
||
| Deno.test("shouldIncludeTool should handle empty includeTools Set", () => { | ||
| const filters: ToolFilters = { | ||
| includeTools: new Set([]), | ||
| }; | ||
|
|
||
| // Empty includeTools means include all (same as not specifying it) | ||
| assertEquals(shouldIncludeTool("any_tool", filters), true); | ||
| }); | ||
|
|
||
| Deno.test("shouldIncludeTool should handle empty excludeTools Set", () => { | ||
| const filters: ToolFilters = { | ||
| excludeTools: new Set([]), | ||
| }; | ||
|
|
||
| // Empty excludeTools means exclude none (same as not specifying it) | ||
| assertEquals(shouldIncludeTool("any_tool", filters), true); | ||
| }); | ||
|
|
||
| Deno.test("shouldIncludeTool should work with empty ToolFilters", () => { | ||
| const filters: ToolFilters = {}; | ||
|
|
||
| assertEquals(shouldIncludeTool("any_tool", filters), true); | ||
| assertEquals(shouldIncludeTool("another_tool", filters), true); | ||
| }); |
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,47 @@ | ||
| /** | ||
| * Tool filtering utilities for MCP server configurations. | ||
| */ | ||
|
|
||
| /** | ||
| * Tool filter configuration | ||
| */ | ||
| export interface ToolFilters { | ||
| includeTools?: Set<string>; | ||
| excludeTools?: Set<string>; | ||
| } | ||
|
|
||
| /** | ||
| * Check if a tool should be included based on includeTools and excludeTools filters. | ||
| * | ||
| * @param toolName - The name of the tool to check | ||
| * @param filters - The filter configuration | ||
| * @returns true if the tool should be included, false otherwise | ||
| * | ||
| * Filter logic: | ||
| * 1. If includeTools is specified, tool must be in the list | ||
| * 2. If excludeTools is specified, tool must not be in the list | ||
| * 3. Exclude filters are applied after include filters | ||
| * 4. If no filters are specified, all tools are included | ||
| */ | ||
| export function shouldIncludeTool( | ||
| toolName: string, | ||
| filters: ToolFilters, | ||
| ): boolean { | ||
| const { includeTools, excludeTools } = filters; | ||
|
|
||
| // If includeTools is specified, tool must be in the list | ||
| if (includeTools && includeTools.size > 0) { | ||
| if (!includeTools.has(toolName)) { | ||
| return false; | ||
| } | ||
| } | ||
|
|
||
| // If excludeTools is specified, tool must not be in the list | ||
| if (excludeTools && excludeTools.size > 0) { | ||
| if (excludeTools.has(toolName)) { | ||
| return false; | ||
| } | ||
| } | ||
|
|
||
| return true; | ||
| } | ||
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.
Uh oh!
There was an error while loading. Please reload this page.